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,6 +169,10 @@ 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)
for file_encoding in ENCODINGS:
# try different encodings, in order
err = None
try: try:
# we read the file directly into unicode. # we read the file directly into unicode.
fobj = codecs.open(abspath, 'r', encoding=file_encoding) fobj = codecs.open(abspath, 'r', encoding=file_encoding)
@ -183,7 +188,6 @@ def read_batchfile(pythonpath, file_ending='.py', file_encoding='utf-8'):
# We have successfully found and opened the file. Now actually # We have successfully found and opened the file. Now actually
# try to decode it using the given protocol. # try to decode it using the given protocol.
try: try:
lines = fobj.readlines() lines = fobj.readlines()
except UnicodeDecodeError: except UnicodeDecodeError:
@ -199,8 +203,14 @@ def read_batchfile(pythonpath, file_ending='.py', file_encoding='utf-8'):
# so we add another 1 (thus +2) to get # so we add another 1 (thus +2) to get
# the actual line number seen in an editor. # the actual line number seen in an editor.
err.linenum = lnum + 2 err.linenum = lnum + 2
raise err
fobj.close() fobj.close()
# possibly try another encoding
continue
# if we get here, the encoding worked. Stop iteration.
break
if err:
return err
else:
return lines return lines
#------------------------------------------------------------ #------------------------------------------------------------