From 09a875db11789befcb752efe2db46c3498d97244 Mon Sep 17 00:00:00 2001 From: John Ralls Date: Thu, 17 Jan 2013 19:47:59 +0000 Subject: [PATCH] GrampsLocale: Simplify the file-finding functions Making use of the fact that GrampsLocale now knows what encoding to use, and noting that filesystems don't use more than one encoding to write filenames in directories. Also specify the encoding on some more files svn: r21148 --- gramps/gen/filters/_filterlist.py | 2 +- gramps/gen/plug/_pluginreg.py | 18 ++----- gramps/gen/recentfiles.py | 9 ++-- gramps/gen/utils/configmanager.py | 6 +-- gramps/gen/utils/file.py | 61 ++++++------------------ gramps/grampsapp.py | 2 +- gramps/plugins/docgen/asciidoc.py | 8 ++-- gramps/plugins/export/exportgedcom.py | 7 +-- gramps/plugins/export/exportvcalendar.py | 3 +- gramps/plugins/export/exportvcard.py | 5 +- gramps/plugins/webreport/narrativeweb.py | 18 ++----- 11 files changed, 44 insertions(+), 95 deletions(-) diff --git a/gramps/gen/filters/_filterlist.py b/gramps/gen/filters/_filterlist.py index 0641015ee..5d333e7b3 100644 --- a/gramps/gen/filters/_filterlist.py +++ b/gramps/gen/filters/_filterlist.py @@ -124,7 +124,7 @@ class FilterList(object): return l.replace('"', '"') def save(self): - f = open(self.file.encode(sys.getfilesystemencoding()), 'w') + f = open(self.file.encode(glocale.getfilesystemencoding()), 'w') f.write("\n") f.write('\n') for namespace in self.filter_namespaces: diff --git a/gramps/gen/plug/_pluginreg.py b/gramps/gen/plug/_pluginreg.py index 05b1f6591..e6154384e 100644 --- a/gramps/gen/plug/_pluginreg.py +++ b/gramps/gen/plug/_pluginreg.py @@ -36,6 +36,7 @@ import os import sys import re import traceback +import codecs #------------------------------------------------------------------------- # @@ -1093,22 +1094,11 @@ class PluginRegister(object): if sys.version_info[0] < 3: full_filename = full_filename.encode(glocale.getfilesystemencoding()) local_gettext = glocale.get_addon_translator(full_filename).gettext - try: - stream = open(full_filename).read() - except UnicodeError as err: - if sys.version_info[0] < 3: - print(_("ERROR: Failed to read file %s, %s") % (full_filename, str(err))) - continue - else: - try: - stream = open(full_filename, encoding = 'utf-8').read() - except ValueError as err: - print(_("ERROR: Failed to read file %s, %s") % (full_filename, str(err))) - continue try: #execfile(full_filename, - exec(compile(stream, full_filename, 'exec'), - make_environment(_=local_gettext), + exec(compile(codecs.open(full_filename, 'r', 'utf-8').read(), + full_filename, 'exec'), + make_environment(_=local_gettext), {}) except ValueError as msg: print(_('ERROR: Failed reading plugin registration %(filename)s') % \ diff --git a/gramps/gen/recentfiles.py b/gramps/gen/recentfiles.py index 19f1d5f25..e26752424 100644 --- a/gramps/gen/recentfiles.py +++ b/gramps/gen/recentfiles.py @@ -29,6 +29,7 @@ #------------------------------------------------------------------------- import os import time +import io from xml.parsers.expat import ParserCreate try: @@ -181,7 +182,7 @@ class RecentFiles(object): """ Saves the current GRAMPS RecentFiles collection to the associated file. """ - xml_file = file(os.path.expanduser(GRAMPS_FILENAME),'w') + xml_file = io.open(os.path.expanduser(GRAMPS_FILENAME),'w', encoding="utf-8") if use_lock: fcntl.lockf(xml_file,fcntl.LOCK_EX) xml_file.write("\n") @@ -215,7 +216,8 @@ class RecentParser(object): self.recent_files = [] try: - xml_file = open(os.path.expanduser(GRAMPS_FILENAME)) + xml_file = io.open(os.path.expanduser(GRAMPS_FILENAME), "r", + encoding = 'utf-8') if use_lock: fcntl.lockf(xml_file,fcntl.LOCK_SH) @@ -229,7 +231,8 @@ class RecentParser(object): fcntl.lockf(xml_file,fcntl.LOCK_UN) xml_file.close() except: - pass + if xml_file: + xml_file.close() def get(self): return self.recent_files diff --git a/gramps/gen/utils/configmanager.py b/gramps/gen/utils/configmanager.py index c766852bf..3d8f12edf 100644 --- a/gramps/gen/utils/configmanager.py +++ b/gramps/gen/utils/configmanager.py @@ -42,6 +42,7 @@ else: import errno import copy import logging +import io from ..constfunc import STRTYPE from ..const import GRAMPS_LOCALE as glocale @@ -335,10 +336,7 @@ class ConfigManager(object): except OSError as exp: if exp.errno != errno.EEXIST: raise - if sys.version_info[0] < 3: - key_file = open(filename, "w") - else: - key_file = open(filename, "w", encoding="utf-8") + key_file = io.open(filename, "w", encoding = "utf-8") key_file.write(";; Gramps key file\n") key_file.write((";; Automatically created at %s" % time.strftime("%Y/%m/%d %H:%M:%S")) + "\n\n") diff --git a/gramps/gen/utils/file.py b/gramps/gen/utils/file.py index ba2c711c1..07039ea61 100644 --- a/gramps/gen/utils/file.py +++ b/gramps/gen/utils/file.py @@ -62,62 +62,29 @@ def find_file( filename): # try the filename we got try: fname = filename - if os.path.isfile( filename): - return( filename) - except: - pass - - # Build list of alternate encodings - encodings = set() - #Darwin returns "mac roman" for preferredencoding, but since it - #returns "UTF-8" for filesystemencoding, and that's first, this - #works. - for enc in [sys.getfilesystemencoding, locale.getpreferredencoding]: + if os.path.isfile(filename): + return(filename) + except UnicodeError: try: - encodings.add(enc) - except: - pass - encodings.add('UTF-8') - encodings.add('ISO-8859-1') - - for enc in encodings: - try: - fname = filename.encode(enc) - if os.path.isfile( fname): + fname = filename.encode(glocale.getfilesystemencoding()) + if os.path.isfile(fname): return fname - except: - pass - - # not found - return '' + except UnicodeError: + return '' def find_folder( filename): # try the filename we got try: fname = filename - if os.path.isdir( filename): - return( filename) - except: - pass - - # Build list of alternate encodings - try: - encodings = [sys.getfilesystemencoding(), - locale.getpreferredencoding(), - 'UTF-8', 'ISO-8859-1'] - except: - encodings = [sys.getfilesystemencoding(), 'UTF-8', 'ISO-8859-1'] - encodings = list(set(encodings)) - for enc in encodings: + if os.path.isdir(filename): + return(filename) + except UnicodeError: try: - fname = filename.encode(enc) - if os.path.isdir( fname): + fname = filename.encode(glocale.getfilesystemencoding()) + if os.path.isdir(fname): return fname - except: - pass - - # not found - return '' + except UnicodeError: + return '' def get_unicode_path_from_file_chooser(path): """ diff --git a/gramps/grampsapp.py b/gramps/grampsapp.py index 31ee13fca..f5f6fbbb4 100644 --- a/gramps/grampsapp.py +++ b/gramps/grampsapp.py @@ -87,7 +87,7 @@ if win(): pass # ok elif not os.path.isdir(HOME_DIR): os.makedirs(HOME_DIR) - sys.stdout = sys.stderr = open(logfile, "w") + sys.stdout = sys.stderr = io.open(logfile, "w", encoding='utf-8') stderrh = logging.StreamHandler(sys.stderr) stderrh.setFormatter(form) stderrh.setLevel(logging.DEBUG) diff --git a/gramps/plugins/docgen/asciidoc.py b/gramps/plugins/docgen/asciidoc.py index e1be61018..b74964afa 100644 --- a/gramps/plugins/docgen/asciidoc.py +++ b/gramps/plugins/docgen/asciidoc.py @@ -157,12 +157,12 @@ class AsciiDoc(BaseDoc,TextDoc): self.filename = filename try: - self.f = open(self.filename,"w") - except IOError as msg: + self.f = io.open(self.filename,"w", + encoding='ascii', + errors = 'backslashreplace') + except Exception as msg: errmsg = "%s\n%s" % (_("Could not create %s") % self.filename, msg) raise ReportError(errmsg) - except: - raise ReportError(_("Could not create %s") % self.filename) self.in_cell = 0 self.text = "" diff --git a/gramps/plugins/export/exportgedcom.py b/gramps/plugins/export/exportgedcom.py index 9ff276dfc..5ecba9c19 100644 --- a/gramps/plugins/export/exportgedcom.py +++ b/gramps/plugins/export/exportgedcom.py @@ -33,16 +33,17 @@ # Standard Python Modules # #------------------------------------------------------------------------- -from gramps.gen.const import GRAMPS_LOCALE as glocale -_ = glocale.get_translation().gettext import os import time +import io #------------------------------------------------------------------------- # # GRAMPS modules # #------------------------------------------------------------------------- +from gramps.gen.const import GRAMPS_LOCALE as glocale +_ = glocale.get_translation().gettext from gramps.gen.lib import AttributeType, ChildRefType, Citation, Date, EventRoleType, EventType, LdsOrd, NameType, NoteType, Person, UrlType from gramps.gen.const import VERSION import gramps.plugins.lib.libgedcom as libgedcom @@ -235,7 +236,7 @@ class GedcomWriter(UpdateCallback): """ self.dirname = os.path.dirname (filename) - self.gedcom_file = open(filename, "w") + self.gedcom_file = io.open(filename, "w", encoding='utf-8') self._header(filename) self._submitter() self._individuals() diff --git a/gramps/plugins/export/exportvcalendar.py b/gramps/plugins/export/exportvcalendar.py index 428e15c34..881da3576 100644 --- a/gramps/plugins/export/exportvcalendar.py +++ b/gramps/plugins/export/exportvcalendar.py @@ -90,8 +90,7 @@ class CalendarWriter(object): self.oldval = newval def writeln(self, text): - #self.g.write('%s\n' % (text.encode('iso-8859-1'))) - self.g.write('%s\n' % (text.encode(sys.getfilesystemencoding()))) + self.g.write('%s\n' % text.encode('ascii', 'backslashreplace')) def export_data(self, filename): diff --git a/gramps/plugins/export/exportvcard.py b/gramps/plugins/export/exportvcard.py index 83303d541..03cf93718 100644 --- a/gramps/plugins/export/exportvcard.py +++ b/gramps/plugins/export/exportvcard.py @@ -145,12 +145,11 @@ class VCardWriter(object): Can't cope with nested VCards, section 2.4.2 of RFC 2426. """ - sysencoding = sys.getfilesystemencoding() self.filehandle.write('%s\r\n' % '\r\n'.join( - [line.encode(sysencoding) for line in self.txtwrp.wrap(text)])) + [line.encode('utf-8') for line in self.txtwrp.wrap(text)])) def export_data(self): - """Open the file and loop over everyone two write their VCards.""" + """Open the file and loop over everyone too write their VCards.""" with OpenFileOrStdout(self.filename) as self.filehandle: if self.filehandle: self.count = 0 diff --git a/gramps/plugins/webreport/narrativeweb.py b/gramps/plugins/webreport/narrativeweb.py index eaa484db0..bb31dddc3 100644 --- a/gramps/plugins/webreport/narrativeweb.py +++ b/gramps/plugins/webreport/narrativeweb.py @@ -81,7 +81,7 @@ except ImportError: import time, datetime import locale import shutil -import codecs +import io import tarfile import tempfile if sys.version_info[0] < 3: @@ -7829,12 +7829,8 @@ class NavWebReport(Report): self.cur_fname = fname + ext if self.archive: string_io = StringIO() - if sys.version_info[0] < 3: - of = open(fname, "w") - else: - of = open(fname, "w", encoding='utf-8') -# of = codecs.EncodedFile(string_io, 'utf-8', -# self.encoding, 'xmlcharrefreplace') + of = io.open(fname, "w", encoding = self.encoding, + errors = 'xmlcharrefreplace') else: string_io = None if subdir: @@ -7842,12 +7838,8 @@ class NavWebReport(Report): if not os.path.isdir(subdir): os.makedirs(subdir) fname = os.path.join(self.html_dir, self.cur_fname) - if sys.version_info[0] < 3: - of = open(fname, "w") - else: - of = open(fname, "w", encoding='utf-8') -# of = codecs.EncodedFile(string_io, 'utf-8', -# self.encoding, 'xmlcharrefreplace') + of = io.open(fname, "w", encoding = self.encoding, + errors = 'xmlcharrefreplace') return (of, string_io) def close_file(self, of, string_io):