Update pypath_to_abspath to make batchfile file lookups much more reliable. This is done as part of the batchprocess refactor #939.
This commit is contained in:
parent
faa3dca2d0
commit
6c34cb40ed
3 changed files with 48 additions and 28 deletions
|
|
@ -246,8 +246,8 @@ class CmdBatchCommands(COMMAND_DEFAULT_CLASS):
|
||||||
caller.msg(_UTF8_ERROR % (python_path, err))
|
caller.msg(_UTF8_ERROR % (python_path, err))
|
||||||
return
|
return
|
||||||
except IOError as err:
|
except IOError as err:
|
||||||
string = "'%s' not found.\nYou have to supply the python path "
|
string = "'%s' not found.\nYou have to supply the python path\n" \
|
||||||
string += "of the file relative to \none of your batch-file directories (%s)."
|
"using one of the defined batch-file directories\n (%s)."
|
||||||
caller.msg(string % (python_path, ", ".join(settings.BASE_BATCHPROCESS_PATHS)))
|
caller.msg(string % (python_path, ", ".join(settings.BASE_BATCHPROCESS_PATHS)))
|
||||||
return
|
return
|
||||||
if not commands:
|
if not commands:
|
||||||
|
|
@ -353,8 +353,8 @@ class CmdBatchCode(COMMAND_DEFAULT_CLASS):
|
||||||
caller.msg(_UTF8_ERROR % (python_path, err))
|
caller.msg(_UTF8_ERROR % (python_path, err))
|
||||||
return
|
return
|
||||||
except IOError:
|
except IOError:
|
||||||
string = "'%s' not found.\nYou have to supply the python path "
|
string = "'%s' not found.\nYou have to supply the python path\n" \
|
||||||
string += "of the file relative to \nyour batch-file directories (%s)."
|
"from one of the defined batch-file directories\n (%s)."
|
||||||
caller.msg(string % (python_path, ", ".join(settings.BASE_BATCHPROCESS_PATHS)))
|
caller.msg(string % (python_path, ", ".join(settings.BASE_BATCHPROCESS_PATHS)))
|
||||||
return
|
return
|
||||||
if not codes:
|
if not codes:
|
||||||
|
|
|
||||||
|
|
@ -209,13 +209,9 @@ def read_batchfile(pythonpath, file_ending='.py'):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# open the file
|
# find all possible absolute paths
|
||||||
abspaths = []
|
abspaths = utils.pypath_to_realpath(pythonpath,
|
||||||
for basepath in settings.BASE_BATCHPROCESS_PATHS:
|
file_ending, settings.BASE_BATCHPROCESS_PATHS)
|
||||||
# note that pypath_to_realpath has already checked the file for existence
|
|
||||||
if basepath.startswith("evennia"):
|
|
||||||
basepath = basepath.split("evennia", 1)[-1]
|
|
||||||
abspaths.extend(utils.pypath_to_realpath("%s.%s" % (basepath, pythonpath), file_ending))
|
|
||||||
if not abspaths:
|
if not abspaths:
|
||||||
raise IOError
|
raise IOError
|
||||||
text = None
|
text = None
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ import math
|
||||||
import re
|
import re
|
||||||
import textwrap
|
import textwrap
|
||||||
import random
|
import random
|
||||||
|
from os.path import join as osjoin
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
from inspect import ismodule, trace, getmembers, getmodule
|
from inspect import ismodule, trace, getmembers, getmodule
|
||||||
from collections import defaultdict, OrderedDict
|
from collections import defaultdict, OrderedDict
|
||||||
|
|
@ -28,6 +29,8 @@ from django.utils.translation import ugettext as _
|
||||||
from evennia.utils import logger
|
from evennia.utils import logger
|
||||||
|
|
||||||
_MULTIMATCH_SEPARATOR = settings.SEARCH_MULTIMATCH_SEPARATOR
|
_MULTIMATCH_SEPARATOR = settings.SEARCH_MULTIMATCH_SEPARATOR
|
||||||
|
_EVENNIA_DIR = settings.EVENNIA_DIR
|
||||||
|
_GAME_DIR = settings.GAME_DIR
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import cPickle as pickle
|
import cPickle as pickle
|
||||||
|
|
@ -409,31 +412,52 @@ def get_evennia_version():
|
||||||
return evennia.__version__
|
return evennia.__version__
|
||||||
|
|
||||||
|
|
||||||
def pypath_to_realpath(python_path, file_ending='.py'):
|
def pypath_to_realpath(python_path, file_ending='.py', pypath_prefixes=None):
|
||||||
"""
|
"""
|
||||||
Converts a dotted Python path to an absolute path under the
|
Converts a dotted Python path to an absolute path under the
|
||||||
Evennia library directory or under the current game directory.
|
Evennia library directory or under the current game directory.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
python_path (str): a dot-python path
|
python_path (str): A dot-python path
|
||||||
file_ending (str): a file ending, including the period.
|
file_ending (str): A file ending, including the period.
|
||||||
|
pypath_prefixes (list): A list of paths to test for existence. These
|
||||||
|
should be on python.path form. EVENNIA_DIR and GAME_DIR are automatically
|
||||||
|
checked, they need not be added to this list.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
abspaths (list of str): The two absolute paths created by prepending
|
abspaths (list): All existing, absolute paths created by
|
||||||
`EVENNIA_DIR` and `GAME_DIR` respectively. These are checked for
|
converting `python_path` to an absolute paths and/or
|
||||||
existence before being returned, so this may be an empty list.
|
prepending `python_path` by `settings.EVENNIA_DIR`,
|
||||||
|
`settings.GAME_DIR` and by`pypath_prefixes` respectively.
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
This will also try a few combinations of paths to allow cases
|
||||||
|
where pypath is given including the "evennia." or "mygame."
|
||||||
|
prefixes.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
pathsplit = python_path.strip().split('.')
|
path = python_path.strip().split('.')
|
||||||
paths = [os.path.join(settings.EVENNIA_DIR, *pathsplit),
|
plong = osjoin(*path) + file_ending
|
||||||
os.path.join(settings.GAME_DIR, *pathsplit)]
|
pshort = osjoin(*path[1:]) + file_ending if len(path) > 1 else plong # in case we had evennia. or mygame.
|
||||||
if file_ending:
|
prefixlong = [osjoin(*ppath.strip().split('.'))
|
||||||
# attach file ending to the paths if not already set (a common mistake)
|
for ppath in make_iter(pypath_prefixes)] \
|
||||||
file_ending = ".%s" % file_ending if not file_ending.startswith(".") else file_ending
|
if pypath_prefixes else []
|
||||||
paths = ["%s%s" % (p, file_ending) if not p.endswith(file_ending) else p
|
prefixshort = [osjoin(*ppath.strip().split('.')[1:])
|
||||||
for p in paths]
|
for ppath in make_iter(pypath_prefixes) if len(ppath.strip().split('.')) > 1] \
|
||||||
# check so the paths actually exists before returning
|
if pypath_prefixes else []
|
||||||
return [p for p in paths if os.path.isfile(p)]
|
paths = [plong] + \
|
||||||
|
[osjoin(_EVENNIA_DIR, prefix, plong) for prefix in prefixlong] + \
|
||||||
|
[osjoin(_GAME_DIR, prefix, plong) for prefix in prefixlong] + \
|
||||||
|
[osjoin(_EVENNIA_DIR, prefix, plong) for prefix in prefixshort] + \
|
||||||
|
[osjoin(_GAME_DIR, prefix, plong) for prefix in prefixshort] + \
|
||||||
|
[osjoin(_EVENNIA_DIR, plong), osjoin(_GAME_DIR, plong)] + \
|
||||||
|
[osjoin(_EVENNIA_DIR, prefix, pshort) for prefix in prefixshort] + \
|
||||||
|
[osjoin(_GAME_DIR, prefix, pshort) for prefix in prefixshort] + \
|
||||||
|
[osjoin(_EVENNIA_DIR, prefix, pshort) for prefix in prefixlong] + \
|
||||||
|
[osjoin(_GAME_DIR, prefix, pshort) for prefix in prefixlong] + \
|
||||||
|
[osjoin(_EVENNIA_DIR, pshort), osjoin(_GAME_DIR, pshort)]
|
||||||
|
# filter out non-existing paths
|
||||||
|
return list(set(p for p in paths if os.path.isfile(p)))
|
||||||
|
|
||||||
|
|
||||||
def dbref(dbref, reqhash=True):
|
def dbref(dbref, reqhash=True):
|
||||||
|
|
@ -441,7 +465,7 @@ def dbref(dbref, reqhash=True):
|
||||||
Converts/checks if input is a valid dbref.
|
Converts/checks if input is a valid dbref.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
dbref (int or str): A datbase ref on the form N or #N.
|
dbref (int or str): A database ref on the form N or #N.
|
||||||
reqhash (bool, optional): Require the #N form to accept
|
reqhash (bool, optional): Require the #N form to accept
|
||||||
input as a valid dbref.
|
input as a valid dbref.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue