ConfigManager: Ensure key files are loaded and saved as utf-8

svn: r21447
This commit is contained in:
John Ralls 2013-02-26 00:15:55 +00:00
parent 3178850a29
commit 07b52e2a11

View File

@ -257,10 +257,13 @@ class ConfigManager(object):
if filename and os.path.exists(filename): if filename and os.path.exists(filename):
parser = configparser.RawConfigParser() parser = configparser.RawConfigParser()
try: # see bugs 5356, 5490, 5591, 5651, 5718, etc. try: # see bugs 5356, 5490, 5591, 5651, 5718, etc.
parser.read(filename) if sys.version_info[0] >= 3 :
except: parser.read(filename, encoding='utf8')
msg1 = _("WARNING: could not parse file, recreating it:\n%s") else:
print(msg1 % filename, file=sys.stderr) parser.readfp(io.open(filename, encoding="utf-8"))
except Exception as err:
msg1 = _("WARNING: could not parse file %s because %s, recreating it:\n")
logging.warn(msg1 % (filename, str(err)))
return return
for sec in parser.sections(): for sec in parser.sections():
name = sec.lower() name = sec.lower()
@ -336,7 +339,13 @@ class ConfigManager(object):
except OSError as exp: except OSError as exp:
if exp.errno != errno.EEXIST: if exp.errno != errno.EEXIST:
raise raise
try:
key_file = io.open(filename, "w", encoding="utf-8") key_file = io.open(filename, "w", encoding="utf-8")
except IOError as err:
logging.warn("Failed to open %s because %s",
filename, str(err))
return;
key_file.write(";; Gramps key file\n") key_file.write(";; Gramps key file\n")
key_file.write((";; Automatically created at %s" % key_file.write((";; Automatically created at %s" %
time.strftime("%Y/%m/%d %H:%M:%S")) + "\n\n") time.strftime("%Y/%m/%d %H:%M:%S")) + "\n\n")