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
if pythonpath and not (pythonpath.startswith('evennia.') or pythonpath.startswith('game.')
or pythonpath.startswith('contrib.')):
abspaths = []
for basepath in settings.BASE_BATCHPROCESS_PATHS:
abspaths.append(utils.pypath_to_realpath("%s.%s" % (basepath, pythonpath), file_ending))
else:
abspaths = [utils.pypath_to_realpath(pythonpath, file_ending)]
text, fobj = None, None
fileerr, decoderr = [], []
abspaths = []
for basepath in 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:
raise IOError
text = None
decoderr = []
for abspath in abspaths:
# try different paths, until we get a match
# we read the file directly into unicode.
for file_encoding in ENCODINGS:
# try different encodings, in order
try:
fobj = codecs.open(abspath, 'r', encoding=file_encoding)
text = fobj.read()
except IOError, e:
# could not find the file
fileerr.append(str(e))
break
with codecs.open(abspath, 'r', encoding=file_encoding) as fobj:
text = fobj.read()
except (ValueError, UnicodeDecodeError), e:
# this means an encoding error; try another encoding
decoderr.append(str(e))
continue
break
if not fobj:
raise IOError("\n".join(fileerr))
if not text:
if not text and decoderr:
raise UnicodeDecodeError("\n".join(decoderr))
return text

View file

@ -312,26 +312,33 @@ def get_evennia_version():
def pypath_to_realpath(python_path, file_ending='.py'):
"""
Converts a path on dot python form (e.g. 'evennia.objects.models') to
a system path ($ROOT_DIR/evennia/objects/models.py). Calculates all
paths as absoulte paths starting from the evennia main directory.
Converts a dotted Python path to an absolute path under the
Evennia library directory or under the current game 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('.')
if python_path.endswith(file_ending):
# this is actually a malformed path ...
pathsplit = pathsplit[:-1]
if not pathsplit:
return python_path
path = settings.EVENNIA_DIR
for directory in pathsplit:
path = os.path.join(path, directory)
paths = [os.path.join(settings.EVENNIA_DIR, *pathsplit),
os.path.join(settings.GAME_DIR, *pathsplit)]
if file_ending:
return "%s%s" % (path, file_ending)
return path
# attach file ending to the paths if not already set (a common mistake)
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):