From 6f042334393e73e513145807581c2e695cd62545 Mon Sep 17 00:00:00 2001 From: Nick Hall Date: Mon, 25 Jun 2012 20:19:34 +0000 Subject: [PATCH] GEPS008: Move fix_encoding function svn: r19922 --- src/Utils.py | 21 --------------------- src/gen/utils/file.py | 23 ++++++++++++++++++++++- src/gui/editors/displaytabs/gallerytab.py | 8 ++++---- src/plugins/tool/check.py | 10 ++++------ src/plugins/view/mediaview.py | 6 +++--- 5 files changed, 33 insertions(+), 35 deletions(-) diff --git a/src/Utils.py b/src/Utils.py index 3dfc1e8bd..bf8a2fabc 100644 --- a/src/Utils.py +++ b/src/Utils.py @@ -56,27 +56,6 @@ def history_broken(): global _history_brokenFlag _history_brokenFlag = 1 -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, unicode): - try: - return unicode(value) - except: - try: - if mac(): - codeset = locale.getlocale()[1] - else: - codeset = locale.getpreferredencoding() - except: - codeset = "UTF-8" - return unicode(value, codeset, errors) - else: - return value - def xml_lang(): loc = locale.getlocale() if loc[0] is None: diff --git a/src/gen/utils/file.py b/src/gen/utils/file.py index e4b2904fa..747419c96 100644 --- a/src/gen/utils/file.py +++ b/src/gen/utils/file.py @@ -43,7 +43,7 @@ LOG = logging.getLogger(".gen.utils.file") # Gramps modules # #------------------------------------------------------------------------- -from gen.constfunc import win +from gen.constfunc import win, mac from const import TEMP_DIR, USER_HOME #------------------------------------------------------------------------- @@ -279,3 +279,24 @@ 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, unicode): + try: + return unicode(value) + except: + try: + if mac(): + codeset = locale.getlocale()[1] + else: + codeset = locale.getpreferredencoding() + except: + codeset = "UTF-8" + return unicode(value, codeset, errors) + else: + return value diff --git a/src/gui/editors/displaytabs/gallerytab.py b/src/gui/editors/displaytabs/gallerytab.py index c620ff2b6..e3432d2a4 100644 --- a/src/gui/editors/displaytabs/gallerytab.py +++ b/src/gui/editors/displaytabs/gallerytab.py @@ -52,8 +52,8 @@ from gui.dbguielement import DbGUIElement from gui.selectors import SelectorFactory import gen.lib from gen.db import DbTxn -import Utils -from gen.utils.file import media_path_full, media_path, relative_path +from gen.utils.file import (media_path_full, media_path, relative_path, + fix_encoding) from gui.thumbnails import get_thumbnail_image from gen.errors import WindowActiveError import gen.mime @@ -493,10 +493,10 @@ class GalleryTab(ButtonTab, DbGUIElement): else: files = sel_data.get_uris() for file in files: - d = Utils.fix_encoding(file.replace('\0',' ').strip()) + d = fix_encoding(file.replace('\0',' ').strip()) protocol, site, mfile, j, k, l = urlparse.urlparse(d) if protocol == "file": - name = Utils.fix_encoding(mfile) + name = fix_encoding(mfile) name = unicode(urllib.url2pathname( name.encode(sys.getfilesystemencoding()))) mime = gen.mime.get_type(name) diff --git a/src/plugins/tool/check.py b/src/plugins/tool/check.py index 7bb9cebc9..1861f2f74 100644 --- a/src/plugins/tool/check.py +++ b/src/plugins/tool/check.py @@ -65,13 +65,11 @@ import gtk import gen.lib from gen.db import DbTxn from gen.config import config -import Utils from gen.utils.id import create_id from gen.utils.name import family_name from gen.utils.unknown import make_unknown -from gen.utils.file import (get_unicode_path_from_file_chooser, - media_path_full, - find_file) +from gen.utils.file import (media_path_full, find_file, fix_encoding, + get_unicode_path_from_file_chooser) from gui.utils import ProgressMeter from gui.managedwindow import ManagedWindow @@ -338,8 +336,8 @@ class CheckIntegrity(object): data = self.db.media_map[handle] if not isinstance(data[2], unicode) or not isinstance(data[4], unicode): obj = self.db.get_object_from_handle(handle) - obj.path = Utils.fix_encoding( obj.path, errors='ignore') - obj.desc = Utils.fix_encoding( obj.desc, errors='ignore') + obj.path = fix_encoding( obj.path, errors='ignore') + obj.desc = fix_encoding( obj.desc, errors='ignore') self.db.commit_media_object(obj, self.trans) if not isinstance(data[2], unicode): LOG(' FAIL: encoding error on media object "%s"' diff --git a/src/plugins/view/mediaview.py b/src/plugins/view/mediaview.py index 058309cb3..58b68b484 100644 --- a/src/plugins/view/mediaview.py +++ b/src/plugins/view/mediaview.py @@ -55,8 +55,8 @@ from gui.views.treemodels import MediaModel import const from gen.constfunc import win from gen.config import config -import Utils -from gen.utils.file import media_path, relative_path, media_path_full +from gen.utils.file import (media_path, relative_path, media_path_full, + fix_encoding) from gen.utils.referent import get_media_referents from gui.views.bookmarks import MediaBookmarks import gen.mime @@ -181,7 +181,7 @@ class MediaView(ListView): else: files = sel_data.get_uris() for file in files: - clean_string = Utils.fix_encoding( + clean_string = fix_encoding( file.replace('\0',' ').replace("\r", " ").strip()) protocol, site, mfile, j, k, l = urlparse.urlparse(clean_string) if protocol == "file":