Extended new encoding functionality also to batch commands.
This commit is contained in:
parent
7080de4022
commit
12acb34ce7
1 changed files with 43 additions and 33 deletions
|
|
@ -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.
|
for file_encoding in ENCODINGS:
|
||||||
fobj = codecs.open(abspath, 'r', encoding=file_encoding)
|
# try different encodings, in order
|
||||||
except IOError:
|
err = None
|
||||||
# try again without the appended file ending
|
|
||||||
abspath2 = utils.pypath_to_realpath(pythonpath, None)
|
|
||||||
try:
|
try:
|
||||||
|
# we read the file directly into unicode.
|
||||||
fobj = codecs.open(abspath, 'r', encoding=file_encoding)
|
fobj = codecs.open(abspath, 'r', encoding=file_encoding)
|
||||||
except IOError:
|
except IOError:
|
||||||
string = "Could not open batchfile '%s', nor '%s'."
|
# try again without the appended file ending
|
||||||
logger.log_errmsg(string % (abspath2, abspath))
|
abspath2 = utils.pypath_to_realpath(pythonpath, None)
|
||||||
return 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
|
# 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:
|
|
||||||
lines = fobj.readlines()
|
|
||||||
except UnicodeDecodeError:
|
|
||||||
# give the line of failure
|
|
||||||
fobj.seek(0)
|
|
||||||
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
|
||||||
|
|
||||||
#------------------------------------------------------------
|
#------------------------------------------------------------
|
||||||
#
|
#
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue