Extended new encoding functionality also to batch commands.

This commit is contained in:
Griatch 2010-10-03 21:02:52 +00:00
parent 7080de4022
commit 12acb34ce7

View file

@ -146,12 +146,13 @@ from src.utils import logger
from src.utils import utils from src.utils import utils
from game import settings as settings_module from game import settings as settings_module
ENCODINGS = settings.ENCODINGS
#------------------------------------------------------------ #------------------------------------------------------------
# Helper function # Helper function
#------------------------------------------------------------ #------------------------------------------------------------
def read_batchfile(pythonpath, file_ending='.py', file_encoding='utf-8'): def read_batchfile(pythonpath, file_ending='.py'):
""" """
This reads the contents of a batch-file. This reads the contents of a batch-file.
Filename is considered to be the name of the batch file Filename is considered to be the name of the batch file
@ -168,40 +169,49 @@ def read_batchfile(pythonpath, file_ending='.py', file_encoding='utf-8'):
pythonpath = "%s.%s" % (settings.BASE_BATCHPROCESS_PATH, pythonpath = "%s.%s" % (settings.BASE_BATCHPROCESS_PATH,
pythonpath) pythonpath)
abspath = utils.pypath_to_realpath(pythonpath, file_ending) abspath = utils.pypath_to_realpath(pythonpath, file_ending)
try:
# we read the file directly into unicode.
fobj = codecs.open(abspath, 'r', encoding=file_encoding)
except IOError:
# try again without the appended file ending
abspath2 = utils.pypath_to_realpath(pythonpath, None)
try:
fobj = codecs.open(abspath, 'r', encoding=file_encoding)
except IOError:
string = "Could not open batchfile '%s', nor '%s'."
logger.log_errmsg(string % (abspath2, abspath))
return None
# We have successfully found and opened the file. Now actually
# try to decode it using the given protocol.
try: for file_encoding in ENCODINGS:
lines = fobj.readlines() # try different encodings, in order
except UnicodeDecodeError: err = None
# give the line of failure try:
fobj.seek(0) # we read the file directly into unicode.
fobj = codecs.open(abspath, 'r', encoding=file_encoding)
except IOError:
# try again without the appended file ending
abspath2 = utils.pypath_to_realpath(pythonpath, None)
try:
fobj = codecs.open(abspath, 'r', encoding=file_encoding)
except IOError:
string = "Could not open batchfile '%s', nor '%s'."
logger.log_errmsg(string % (abspath2, abspath))
return None
# We have successfully found and opened the file. Now actually
# try to decode it using the given protocol.
try: try:
lnum = 0 lines = fobj.readlines()
for lnum, line in enumerate(fobj): except UnicodeDecodeError:
pass # give the line of failure
except UnicodeDecodeError, err: fobj.seek(0)
# lnum starts from 0, so we add +1 line, try:
# besides the faulty line is never read lnum = 0
# so we add another 1 (thus +2) to get for lnum, line in enumerate(fobj):
# the actual line number seen in an editor. pass
err.linenum = lnum + 2 except UnicodeDecodeError, err:
raise err # lnum starts from 0, so we add +1 line,
fobj.close() # besides the faulty line is never read
return lines # so we add another 1 (thus +2) to get
# the actual line number seen in an editor.
err.linenum = lnum + 2
fobj.close()
# possibly try another encoding
continue
# if we get here, the encoding worked. Stop iteration.
break
if err:
return err
else:
return lines
#------------------------------------------------------------ #------------------------------------------------------------
# #