GEPS008: Move fix_encoding function

svn: r19922
This commit is contained in:
Nick Hall 2012-06-25 20:19:34 +00:00
parent a072c989b2
commit 6f04233439
5 changed files with 33 additions and 35 deletions

View File

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

View File

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

View File

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

View File

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

View File

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