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
This commit is contained in:
John Ralls 2013-01-17 19:47:59 +00:00
parent 08d8d4c775
commit 9a515bfde6
11 changed files with 44 additions and 95 deletions

View File

@ -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:

View File

@ -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') % \

View File

@ -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

View File

@ -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")

View File

@ -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):
"""

View File

@ -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)

View File

@ -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 = ""

View File

@ -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()

View File

@ -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):

View File

@ -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

View File

@ -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):