From 86fa385a9bb6249ec6f07ca9ffb903e0bbb9c59e Mon Sep 17 00:00:00 2001 From: Martin Hawlisch Date: Fri, 18 Nov 2005 13:56:37 +0000 Subject: [PATCH] * src/Utils.py (find_file): new method that tries to check the existance of a file by trying out multiple encoding variants for the filename. * src/ImgManip.py (get_thumbnail_image): Use Utils.find_file to fix encoding problems, additionally catch OSError * src/ImageSelect.py (on_name_changed): Use Utils.find_file * src/AddMedia.py (on_name_changed): Use Utils.find_file * src/plugins/Check.py (encoding) dont encode a utf-8 string as again, (cleanup_missing_photos) Use Utils.find_file to fix encoding problems svn: r5405 --- gramps2/ChangeLog | 10 ++++++++++ gramps2/src/AddMedia.py | 5 +++-- gramps2/src/ImageSelect.py | 3 ++- gramps2/src/ImgManip.py | 3 ++- gramps2/src/Utils.py | 24 ++++++++++++++++++++++++ gramps2/src/plugins/Check.py | 13 +++---------- 6 files changed, 44 insertions(+), 14 deletions(-) diff --git a/gramps2/ChangeLog b/gramps2/ChangeLog index 202c369b6..9b22d1297 100644 --- a/gramps2/ChangeLog +++ b/gramps2/ChangeLog @@ -1,3 +1,13 @@ +2005-11-18 Martin Hawlisch + * src/Utils.py (find_file): new method that tries to check the existance + of a file by trying out multiple encoding variants for the filename. + * src/ImgManip.py (get_thumbnail_image): Use Utils.find_file to fix + encoding problems, additionally catch OSError + * src/ImageSelect.py (on_name_changed): Use Utils.find_file + * src/AddMedia.py (on_name_changed): Use Utils.find_file + * src/plugins/Check.py (encoding) dont encode a utf-8 string as again, + (cleanup_missing_photos) Use Utils.find_file to fix encoding problems + 2005-11-17 Martin Hawlisch * src/gramps.glade, src/TipOfDay.py: Add window title diff --git a/gramps2/src/AddMedia.py b/gramps2/src/AddMedia.py index c2477d792..bca7fb1d7 100644 --- a/gramps2/src/AddMedia.py +++ b/gramps2/src/AddMedia.py @@ -155,8 +155,9 @@ class AddMediaObject: if old_title == '' or old_title == self.temp_name: self.description.set_text(root) self.temp_name = root - - if os.path.isfile(filename): + + filename = Utils.find_file( filename) + if filename: mtype = GrampsMime.get_type(filename) if mtype and mtype.startswith("image"): image = RelImage.scale_image(filename,const.thumbScale) diff --git a/gramps2/src/ImageSelect.py b/gramps2/src/ImageSelect.py index 0a4c7ef7b..02d67b19d 100644 --- a/gramps2/src/ImageSelect.py +++ b/gramps2/src/ImageSelect.py @@ -166,7 +166,8 @@ class ImageSelect: self.description.set_text(root) self.temp_name = root - if os.path.isfile(filename): + filename = Utils.find_file( filename) + if filename: mtype = GrampsMime.get_type(filename) if mtype and mtype.startswith("image"): image = RelImage.scale_image(filename,const.thumbScale) diff --git a/gramps2/src/ImgManip.py b/gramps2/src/ImgManip.py index 9730039eb..21fd3a0ab 100644 --- a/gramps2/src/ImgManip.py +++ b/gramps2/src/ImgManip.py @@ -144,12 +144,13 @@ def get_thumbnail_image(path,mtype=None): filename = _build_thumb_path(path) try: + path = Utils.find_file( path) if not os.path.isfile(filename): set_thumbnail_image(path,mtype) elif os.path.getmtime(path) > os.path.getmtime(filename): set_thumbnail_image(path,mtype) return gtk.gdk.pixbuf_new_from_file(filename) - except gobject.GError: + except (gobject.GError, OSError): if mtype: return Utils.find_mime_type_pixbuf(mtype) else: diff --git a/gramps2/src/Utils.py b/gramps2/src/Utils.py index 36b63d3cd..544fa62a8 100644 --- a/gramps2/src/Utils.py +++ b/gramps2/src/Utils.py @@ -26,6 +26,7 @@ # #------------------------------------------------------------------------- import os +import sys import locale import sets from gettext import gettext as _ @@ -262,6 +263,29 @@ def get_mime_description(mime_type): except: return '' +def find_file( filename): + # try the filename we got + try: + fname = filename + if os.path.isfile( filename): + return( filename) + except: + pass + + # Build list of elternate encodings + encodings = [sys.getfilesystemencoding(), locale.getpreferredencoding(), 'UTF-8', 'ISO-8859-1'] + encodings = list(sets.Set(encodings)) + for enc in encodings: + try: + fname = filename.encode(enc) + if os.path.isfile( fname): + return fname + except: + pass + + # not found + return '' + #------------------------------------------------------------------------- # # diff --git a/gramps2/src/plugins/Check.py b/gramps2/src/plugins/Check.py index f5b36376c..f6514bc6c 100644 --- a/gramps2/src/plugins/Check.py +++ b/gramps2/src/plugins/Check.py @@ -157,11 +157,6 @@ class CheckIntegrity: def fix_encoding(self): - import locale - codeset = locale.nl_langinfo(locale.CODESET) - if codeset == 'UTF-8': - codeset = 'latin1' - self.progress.set_pass(_('Looking for character encoding errors'), self.db.get_number_of_media_objects()) @@ -171,10 +166,8 @@ class CheckIntegrity: (handle,data) = value if type(data[2]) != unicode or type(data[4]) != unicode: obj = self.db.get_object_from_handle(handle) - if type(obj.path) != unicode: - obj.path = unicode(obj.path,codeset) - if type(obj.desc) != unicode: - obj.desc = unicode(obj.desc,codeset) + obj.path = Utils.fix_encoding( obj.path) + obj.desc = Utils.fix_encoding( obj.desc) self.db.commit_media_object(obj,self.trans) self.progress.step() value = cursor.next() @@ -346,7 +339,7 @@ class CheckIntegrity: for ObjectId in self.db.get_media_object_handles(): obj = self.db.get_object_from_handle(ObjectId) photo_name = obj.get_path() - if photo_name is not None and photo_name != "" and not os.path.isfile(photo_name): + if photo_name is not None and photo_name != "" and not Utils.find_file(photo_name): if cl: print "Warning: media file %s was not found." \ % os.path.basename(photo_name)