Delete a bunch of superflous re-encodings of path names

This commit is contained in:
John Ralls 2014-04-03 16:52:43 -07:00
parent d69400b513
commit 28505a5d8d
10 changed files with 15 additions and 36 deletions

View File

@ -466,7 +466,7 @@ def find_next_db_name(name_list):
while True:
title = "%s %d" % (DEFAULT_TITLE, i)
if title not in name_list:
return title
return conv_to_unicode(title)
i += 1
def find_next_db_dir():

View File

@ -55,10 +55,10 @@ WINDOWS = ["Windows", "win32"]
#python 2 and 3 support, use correct conversion to unicode
if sys.version_info[0] < 3:
def conv_to_unicode(x, y):
def conv_to_unicode(x, y=None):
if isinstance(x, unicode):
return x
return unicode(x, y)
return unicode(x, y) if y else unicode(x)
conv_to_unicode_direct = unicode
STRTYPE = basestring

View File

@ -31,6 +31,7 @@ from xml.sax import make_parser, SAXParseException
import os
import sys
import collections
import io
#-------------------------------------------------------------------------
#
@ -108,7 +109,7 @@ class FilterList(object):
if os.path.isfile(self.file):
parser = make_parser()
parser.setContentHandler(FilterParser(self))
the_file = open(self.file)
the_file = io.open(self.file, 'r', encoding='utf8')
parser.parse(the_file)
the_file.close()
except (IOError, OSError):
@ -125,7 +126,7 @@ class FilterList(object):
return l.replace('"', '&quot;')
def save(self):
f = open(self.file.encode(glocale.getfilesystemencoding()), 'w')
f = io.open(self.file, 'w', encoding='utf8')
f.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
f.write('<filters>\n')
for namespace in self.filter_namespaces:

View File

@ -443,6 +443,7 @@ class DbManager(CLIDbManager):
#path is a string, convert to TreePath first
path = Gtk.TreePath(path=path)
if len(new_text) > 0:
new_text = conv_to_unicode(new_text, 'utf8')
node = self.model.get_iter(path)
old_text = self.model.get_value(node, NAME_COL)
if not old_text.strip() == new_text.strip():

View File

@ -526,19 +526,16 @@ class GalleryTab(ButtonTab, DbGUIElement):
d = fix_encoding(file.replace('\0',' ').strip())
protocol, site, mfile, j, k, l = urlparse(d)
if protocol == "file":
name = fix_encoding(mfile)
name = cuni(url2pathname(
name.encode(glocale.getfilesystemencoding())))
mime = get_type(name)
mime = get_type(mfile)
if not is_valid_type(mime):
return
photo = MediaObject()
self.uistate.set_busy_cursor(True)
photo.set_checksum(create_checksum(name))
photo.set_checksum(create_checksum(mfile))
self.uistate.set_busy_cursor(False)
base_dir = cuni(media_path(self.dbstate.db))
if os.path.exists(base_dir):
name = relative_path(name, base_dir)
name = relative_path(mfile, base_dir)
photo.set_path(name)
photo.set_mime_type(mime)
basename = os.path.basename(name)

View File

@ -1302,13 +1302,10 @@ class ViewManager(CLIManager):
basefile = file_entry.get_text()
basefile = basefile.replace("/", r"-")
filename = os.path.join(path_entry.get_text(), basefile)
if sys.version_info[0] < 3:
filename = filename.encode(glocale.getfilesystemencoding())
if os.path.exists(filename):
sfilename = get_unicode_path_from_env_var(filename)
question = QuestionDialog2(
_("Backup file already exists! Overwrite?"),
_("The file '%s' exists.") % sfilename,
_("The file '%s' exists.") % filename,
_("Proceed and overwrite"),
_("Cancel the backup"))
yes_no = question.run()

View File

@ -96,8 +96,6 @@ class CairoDocgen(libcairodoc.CairoDoc):
# create cairo context and pango layout
filename = self._backend.filename
if sys.version_info[0] < 3:
filename = self._backend.filename.encode(glocale.getfilesystemencoding())
try:
surface = self.create_cairo_surface(filename, paper_width, paper_height)
except IOError as msg:

View File

@ -154,16 +154,10 @@ class GeoGraphyView(OsmGps, NavigationView):
self.geo_mainmap = None
path = os.path.join(IMAGE_DIR, "48x48",
('gramps-geo-mainmap' + '.png' ))
pathu = path
if sys.version_info[0] < 3:
pathu = path.encode(glocale.getfilesystemencoding())
self.geo_mainmap = cairo.ImageSurface.create_from_png(pathu)
self.geo_mainmap = cairo.ImageSurface.create_from_png(path)
path = os.path.join(IMAGE_DIR, "48x48",
('gramps-geo-altmap' + '.png' ))
pathu = path
if sys.version_info[0] < 3:
pathu = path.encode(glocale.getfilesystemencoding())
self.geo_altmap = cairo.ImageSurface.create_from_png(pathu)
self.geo_altmap = cairo.ImageSurface.create_from_png(path)
if ( config.get('geography.map_service') in
( constants.OPENSTREETMAP,
constants.MAPS_FOR_FREE,
@ -179,10 +173,7 @@ class GeoGraphyView(OsmGps, NavigationView):
EventType.MARRIAGE ):
path = os.path.join(IMAGE_DIR, "48x48",
(constants.ICONS.get(int(ident), default_image) + '.png' ))
pathu = path
if sys.version_info[0] < 3:
pathu = path.encode(glocale.getfilesystemencoding())
self.geo_othermap[ident] = cairo.ImageSurface.create_from_png(pathu)
self.geo_othermap[ident] = cairo.ImageSurface.create_from_png(path)
def add_bookmark(self, menu, handle):
if handle:

View File

@ -190,11 +190,7 @@ class MediaView(ListView):
file.replace('\0',' ').replace("\r", " ").strip())
protocol, site, mfile, j, k, l = urlparse(clean_string)
if protocol == "file":
if sys.version_info[0] < 3:
name = cuni(url2pathname(
mfile.encode(glocale.getfilesystemencoding())))
else:
name = cuni(url2pathname(mfile))
name = mfile
mime = get_type(name)
if not is_valid_type(mime):
return

View File

@ -208,8 +208,6 @@ class PersonBoxWidgetCairo(_PersonWidgetBase):
self.img_surf = None
if image:
image_path = self.get_image(dbstate, person)
if sys.version_info[0] < 3 and isinstance(image_path, STRTYPE):
image_path = image_path.encode(glocale.getfilesystemencoding())
if image_path and os.path.exists(image_path):
self.img_surf = cairo.ImageSurface.create_from_png(image_path)