Fixed batchprocessor readin.

This commit is contained in:
Griatch 2015-02-09 22:02:44 +01:00
parent 91608d7787
commit 4045e0ec16
2 changed files with 36 additions and 34 deletions

View file

@ -199,35 +199,30 @@ def read_batchfile(pythonpath, file_ending='.py'):
""" """
# open the file # open the file
if pythonpath and not (pythonpath.startswith('evennia.') or pythonpath.startswith('game.')
or pythonpath.startswith('contrib.')):
abspaths = [] abspaths = []
for basepath in settings.BASE_BATCHPROCESS_PATHS: for basepath in settings.BASE_BATCHPROCESS_PATHS:
abspaths.append(utils.pypath_to_realpath("%s.%s" % (basepath, pythonpath), file_ending)) # note that pypath_to_realpath has already checked the file for existence
else: if basepath.startswith("evennia"):
abspaths = [utils.pypath_to_realpath(pythonpath, file_ending)] basepath = basepath.split("evennia", 1)[-1]
text, fobj = None, None abspaths.extend(utils.pypath_to_realpath("%s.%s" % (basepath, pythonpath), file_ending))
fileerr, decoderr = [], [] if not abspaths:
raise IOError
text = None
decoderr = []
for abspath in abspaths: for abspath in abspaths:
# try different paths, until we get a match # try different paths, until we get a match
# we read the file directly into unicode. # we read the file directly into unicode.
for file_encoding in ENCODINGS: for file_encoding in ENCODINGS:
# try different encodings, in order # try different encodings, in order
try: try:
fobj = codecs.open(abspath, 'r', encoding=file_encoding) with codecs.open(abspath, 'r', encoding=file_encoding) as fobj:
text = fobj.read() text = fobj.read()
except IOError, e:
# could not find the file
fileerr.append(str(e))
break
except (ValueError, UnicodeDecodeError), e: except (ValueError, UnicodeDecodeError), e:
# this means an encoding error; try another encoding # this means an encoding error; try another encoding
decoderr.append(str(e)) decoderr.append(str(e))
continue continue
break break
if not fobj: if not text and decoderr:
raise IOError("\n".join(fileerr))
if not text:
raise UnicodeDecodeError("\n".join(decoderr)) raise UnicodeDecodeError("\n".join(decoderr))
return text return text

View file

@ -312,26 +312,33 @@ def get_evennia_version():
def pypath_to_realpath(python_path, file_ending='.py'): def pypath_to_realpath(python_path, file_ending='.py'):
""" """
Converts a path on dot python form (e.g. 'evennia.objects.models') to Converts a dotted Python path to an absolute path under the
a system path ($ROOT_DIR/evennia/objects/models.py). Calculates all Evennia library directory or under the current game directory.
paths as absoulte paths starting from the evennia main directory.
Args:
python_path (str): a dot-python path
file_ending (str): a file ending, including the period.
Returns:
abspaths (list of str): The two absolute paths created by prepending
EVENNIA_DIR and GAME_DIR respectively. These are checked for
existence before being returned, so this may be an empty list.
Since it seems to be a common mistake to include the file ending
when entering filename for things like batchprocess, we handle the
case of erroneously adding the file ending too.
""" """
print "settings.EVENNIA_DIR:", settings.EVENNIA_DIR
print "settings.GAME_DIR:", settings.GAME_DIR
print python_path
pathsplit = python_path.strip().split('.') pathsplit = python_path.strip().split('.')
if python_path.endswith(file_ending): paths = [os.path.join(settings.EVENNIA_DIR, *pathsplit),
# this is actually a malformed path ... os.path.join(settings.GAME_DIR, *pathsplit)]
pathsplit = pathsplit[:-1]
if not pathsplit:
return python_path
path = settings.EVENNIA_DIR
for directory in pathsplit:
path = os.path.join(path, directory)
if file_ending: if file_ending:
return "%s%s" % (path, file_ending) # attach file ending to the paths if not already set (a common mistake)
return path file_ending = ".%s" % file_ending if not file_ending.startswith(".") else file_ending
paths = ["%s%s" % (p, file_ending) if not p.endswith(file_ending) else p
for p in paths]
# check so the paths actually exists before returning
print "py to path:", paths
return [p for p in paths if os.path.isfile(p)]
def dbref(dbref, reqhash=True): def dbref(dbref, reqhash=True):