From a4779e8c5038400031687bb95dc86ffaf243cfef Mon Sep 17 00:00:00 2001 From: John Ralls Date: Sun, 20 Apr 2014 17:04:42 -0700 Subject: [PATCH] Remove gramps.gen.utils.file.fix_encoding() Yet another overly complicated way to spell conv_to_unicode(). Note that unicode() and str.decode() are alternate spellings of the same function in Py2, and unicode(); note as well that this was used on strings coming from the GUI or the database, not the shell, so that using glocale.encoding was also incorrect. (cherry picked from commit f0ffd6e9b9dee138e5c8eca8d418d96f34438b88) --- gramps/gen/utils/file.py | 18 ------------------ gramps/gui/editors/displaytabs/gallerytab.py | 12 +++++------- gramps/plugins/tool/check.py | 6 +++--- gramps/plugins/view/mediaview.py | 8 ++++---- gramps/webapp/libdjango.py | 4 ++-- 5 files changed, 14 insertions(+), 34 deletions(-) diff --git a/gramps/gen/utils/file.py b/gramps/gen/utils/file.py index 437c36301..9b9dd7b2c 100644 --- a/gramps/gen/utils/file.py +++ b/gramps/gen/utils/file.py @@ -185,21 +185,3 @@ def search_for(name): if os.access(fname, os.X_OK) and not os.path.isdir(fname): return 1 return 0 - -def fix_encoding(value, errors='strict'): - # The errors argument specifies the response when the input string can't be - # converted according to the encoding's rules. Legal values for this - # argument are 'strict' (raise a UnicodeDecodeError exception), 'replace' - # (add U+FFFD, 'REPLACEMENT CHARACTER'), or 'ignore' (just leave the - # character out of the Unicode result). - if not isinstance(value, UNITYPE): - try: - return cuni(value) - except: - codeset = glocale.encoding - if sys.version_info[0] < 3: - return unicode(value, codeset, errors) - else: - return value.decode(encoding=codeset, errors=errors) - else: - return value diff --git a/gramps/gui/editors/displaytabs/gallerytab.py b/gramps/gui/editors/displaytabs/gallerytab.py index 8bf2506e2..19e8269a4 100644 --- a/gramps/gui/editors/displaytabs/gallerytab.py +++ b/gramps/gui/editors/displaytabs/gallerytab.py @@ -26,8 +26,6 @@ # Python classes # #------------------------------------------------------------------------- -from gramps.gen.const import GRAMPS_LOCALE as glocale -_ = glocale.translation.gettext import os import sys if sys.version_info[0] < 3: @@ -60,18 +58,18 @@ from gi.repository import GObject from ...utils import is_right_click, open_file_with_default_application from ...dbguielement import DbGUIElement from ...selectors import SelectorFactory -from gramps.gen.constfunc import cuni +from gramps.gen.constfunc import cuni, win, conv_to_unicode from gramps.gen.lib import MediaObject, MediaRef from gramps.gen.db import DbTxn -from gramps.gen.utils.file import (media_path_full, media_path, relative_path, - fix_encoding) +from gramps.gen.utils.file import (media_path_full, media_path, relative_path) from ...thumbnails import get_thumbnail_image from gramps.gen.errors import WindowActiveError from gramps.gen.mime import get_type, is_valid_type from ...ddtargets import DdTargets from .buttontab import ButtonTab -from gramps.gen.constfunc import win from gramps.gen.const import THUMBSCALE +from gramps.gen.const import GRAMPS_LOCALE as glocale +_ = glocale.translation.gettext #------------------------------------------------------------------------- # # @@ -522,7 +520,7 @@ class GalleryTab(ButtonTab, DbGUIElement): else: files = sel_data.get_uris() for file in files: - d = fix_encoding(file.replace('\0',' ').strip()) + d = conv_to_unicode((file.replace('\0',' ').strip()), None) protocol, site, mfile, j, k, l = urlparse(d) if protocol == "file": mime = get_type(mfile) diff --git a/gramps/plugins/tool/check.py b/gramps/plugins/tool/check.py index 696fc114f..ab532e00d 100644 --- a/gramps/plugins/tool/check.py +++ b/gramps/plugins/tool/check.py @@ -68,7 +68,7 @@ from gramps.gen.config import config from gramps.gen.utils.id import create_id from gramps.gen.utils.db import family_name from gramps.gen.utils.unknown import make_unknown -from gramps.gen.utils.file import (media_path_full, find_file, fix_encoding) +from gramps.gen.utils.file import (media_path_full, find_file) from gramps.gui.managedwindow import ManagedWindow from gramps.gui.plug import tool @@ -351,8 +351,8 @@ class CheckIntegrity(object): data = self.db.media_map[bhandle] if not isinstance(data[2], UNITYPE) or not isinstance(data[4], UNITYPE): obj = self.db.get_object_from_handle(handle) - obj.path = fix_encoding( obj.path, errors='ignore') - obj.desc = fix_encoding( obj.desc, errors='ignore') + obj.path = conv_to_unicode(obj.path, None) + obj.desc = conv_to_unicode(obj.desc, None) self.db.commit_media_object(obj, self.trans) if not isinstance(data[2], UNITYPE): logging.warning(' FAIL: encoding error on media object ' diff --git a/gramps/plugins/view/mediaview.py b/gramps/plugins/view/mediaview.py index a819eea40..b52e9a008 100644 --- a/gramps/plugins/view/mediaview.py +++ b/gramps/plugins/view/mediaview.py @@ -62,8 +62,7 @@ from gramps.gui.views.listview import ListView, TEXT, MARKUP, ICON from gramps.gui.views.treemodels import MediaModel from gramps.gen.constfunc import win, cuni from gramps.gen.config import config -from gramps.gen.utils.file import (media_path, relative_path, media_path_full, - fix_encoding) +from gramps.gen.utils.file import (media_path, relative_path, media_path_full) from gramps.gen.utils.db import get_media_referents from gramps.gui.views.bookmarks import MediaBookmarks from gramps.gen.mime import get_type, is_valid_type @@ -187,8 +186,9 @@ class MediaView(ListView): #modern file managers provide URI_LIST. For Windows split sel_data.data files = sel_data.get_uris() for file in files: - clean_string = fix_encoding( - file.replace('\0',' ').replace("\r", " ").strip()) + clean_string = conv_to_unicode( + file.replace('\0',' ').replace("\r", " ").strip(), + None) protocol, site, mfile, j, k, l = urlparse(clean_string) if protocol == "file": name = mfile diff --git a/gramps/webapp/libdjango.py b/gramps/webapp/libdjango.py index 546e3d74f..3113786a0 100644 --- a/gramps/webapp/libdjango.py +++ b/gramps/webapp/libdjango.py @@ -53,7 +53,7 @@ from django.db import transaction import gramps.webapp.grampsdb.models as models from gramps.gen.lib import Name from gramps.gen.utils.id import create_id -from gramps.gen.utils.file import fix_encoding +gramps.gen.utils.constfunc import conv_to_unicode # To get a django person from a django database: # djperson = dji.Person.get(handle='djhgsdh324hjg234hj24') @@ -433,7 +433,7 @@ class DjangoInterface(object): date = self.get_date(media) return (str(media.handle), media.gramps_id, - fix_encoding(media.path), + conv_to_unicode(media.path, None), str(media.mime), fix_encoding(media.desc), attribute_list,