[r21148]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: r21396
This commit is contained in:
parent
3f408af0fa
commit
fccfdecd03
@ -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("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
|
||||
f.write('<filters>\n')
|
||||
for namespace in self.filter_namespaces:
|
||||
|
@ -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') % \
|
||||
|
@ -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("<?xml version=\"1.0\" encoding=\"utf-8\"?>\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
|
||||
|
@ -42,6 +42,7 @@ else:
|
||||
import errno
|
||||
import copy
|
||||
import logging
|
||||
import io
|
||||
|
||||
from ..constfunc import STRTYPE
|
||||
from ..const import GRAMPS_LOCALE as glocale
|
||||
@ -339,10 +340,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")
|
||||
|
@ -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):
|
||||
"""
|
||||
|
@ -89,7 +89,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)
|
||||
|
@ -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 = ""
|
||||
|
@ -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()
|
||||
|
@ -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):
|
||||
|
||||
|
@ -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
|
||||
|
@ -79,7 +79,7 @@ except ImportError:
|
||||
from md5 import md5
|
||||
import time, datetime
|
||||
import shutil
|
||||
import codecs
|
||||
import io
|
||||
import tarfile
|
||||
import tempfile
|
||||
if sys.version_info[0] < 3:
|
||||
@ -7827,14 +7827,9 @@ class NavWebReport(Report):
|
||||
else:
|
||||
self.cur_fname = fname + ext
|
||||
if self.archive:
|
||||
if sys.version_info[0] < 3:
|
||||
string_io = StringIO()
|
||||
of = codecs.EncodedFile(string_io, 'utf-8', self.encoding,
|
||||
'xmlcharrefreplace')
|
||||
else:
|
||||
string_io = BytesIO()
|
||||
of = TextIOWrapper(string_io, encoding=self.encoding,
|
||||
errors='xmlcharrefreplace')
|
||||
string_io = StringIO()
|
||||
of = io.open(fname, "w", encoding = self.encoding,
|
||||
errors = 'xmlcharrefreplace')
|
||||
else:
|
||||
string_io = None
|
||||
if subdir:
|
||||
@ -7842,22 +7837,9 @@ 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:
|
||||
# In python 2.x, the data written by of.write() is genarally of
|
||||
# type 'str' (i.e. 8-bit strings), except for cases where (at
|
||||
# least) one of the objects being converted by a '%' operator is
|
||||
# unicode (e.g. the "Generated by" line or the _META3 line), in
|
||||
# which case the data being written is of type 'unicode' (See
|
||||
# http://docs.python.org/2/library/stdtypes.html#string-
|
||||
# formatting). The data written to the file is encoded according
|
||||
# to self.encoding
|
||||
of = codecs.EncodedFile(open(fname, 'w'), 'utf-8',
|
||||
self.encoding, 'xmlcharrefreplace')
|
||||
else:
|
||||
# In python 3, the data that is written by of.write() is always
|
||||
# of type 'str' (i.e. unicode text).
|
||||
of = open(fname, 'w', encoding=self.encoding,
|
||||
errors='xmlcharrefreplace')
|
||||
of = io.open(fname, "w", encoding = self.encoding,
|
||||
errors = 'xmlcharrefreplace')
|
||||
|
||||
return (of, string_io)
|
||||
|
||||
def close_file(self, of, string_io):
|
||||
|
Loading…
x
Reference in New Issue
Block a user