From f0ffd6e9b9dee138e5c8eca8d418d96f34438b88 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. --- gramps/gen/utils/file.py | 18 ------------------ gramps/gui/editors/displaytabs/gallerytab.py | 11 +++++------ gramps/plugins/tool/check.py | 6 +++--- gramps/plugins/view/mediaview.py | 9 +++++---- gramps/webapp/libdjango.py | 4 ++-- 5 files changed, 15 insertions(+), 33 deletions(-) diff --git a/gramps/gen/utils/file.py b/gramps/gen/utils/file.py index 36114d397..404e26f7e 100644 --- a/gramps/gen/utils/file.py +++ b/gramps/gen/utils/file.py @@ -186,24 +186,6 @@ def search_for(name): 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 - def create_checksum(full_path): """ Create a md5 hash for the given file. diff --git a/gramps/gui/editors/displaytabs/gallerytab.py b/gramps/gui/editors/displaytabs/gallerytab.py index a2cd8ed9b..8e0882890 100644 --- a/gramps/gui/editors/displaytabs/gallerytab.py +++ b/gramps/gui/editors/displaytabs/gallerytab.py @@ -24,8 +24,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: @@ -59,18 +57,19 @@ from gi.repository import GLib 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, create_checksum) + create_checksum) 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 #------------------------------------------------------------------------- # # @@ -521,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 ab251c8a1..ca8a71997 100644 --- a/gramps/plugins/tool/check.py +++ b/gramps/plugins/tool/check.py @@ -69,7 +69,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.gen.utils.file import create_checksum 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 7ff58d06d..f821476f5 100644 --- a/gramps/plugins/view/mediaview.py +++ b/gramps/plugins/view/mediaview.py @@ -60,8 +60,8 @@ 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, create_checksum) +from gramps.gen.utils.file import (media_path, relative_path, media_path_full, + create_checksum) 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 @@ -184,8 +184,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 0e9cb2100..8aa4764ee 100644 --- a/gramps/webapp/libdjango.py +++ b/gramps/webapp/libdjango.py @@ -51,7 +51,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') @@ -457,7 +457,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), media.checksum,