From fea6a98cb0076d68a429feeedbee6ed172c270f5 Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Wed, 10 Jul 2013 00:35:09 +0200 Subject: [PATCH] Filenames and dirnames should always be unicode (ref #16). --- sigal/gallery.py | 15 ++++++++++++--- sigal/image.py | 4 ++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/sigal/gallery.py b/sigal/gallery.py index 47fe03b..ce749f6 100644 --- a/sigal/gallery.py +++ b/sigal/gallery.py @@ -23,6 +23,7 @@ from __future__ import absolute_import import codecs +import locale import logging import markdown import os @@ -49,10 +50,18 @@ class PathsDb(object): """ def __init__(self, path, ext_list): - self.basepath = path self.ext_list = ext_list self.logger = logging.getLogger(__name__) + # basepath must to be a unicode string so that os.walk will return + # unicode dirnames and filenames. If basepath is a str, we must + # convert it to unicode. + if isinstance(path, str): + enc = locale.getpreferredencoding() + self.basepath = path.decode(enc) + else: + self.basepath = path + def get_subdirs(self, path): """Return the list of all sub-directories of path.""" @@ -78,8 +87,8 @@ class PathsDb(object): relpath = os.path.relpath(path, self.basepath) # sort images and sub-albums by name - filenames.sort(key=str.lower) - dirnames.sort(key=str.lower) + filenames.sort(cmp=locale.strcoll) + dirnames.sort(cmp=locale.strcoll) self.db['paths_list'].append(relpath) self.db[relpath] = { diff --git a/sigal/image.py b/sigal/image.py index dfbf385..94a98f4 100644 --- a/sigal/image.py +++ b/sigal/image.py @@ -64,7 +64,7 @@ def generate_image(source, outname, size, format, options=None, add_copyright(img, copyright_text) format = format or img.format or original_format or 'JPEG' - logger.debug('Save resized image to {0} ({1})'.format(outname, format)) + logger.debug(u'Save resized image to {0} ({1})'.format(outname, format)) save_image(img, outname, format, options=options, autoconvert=autoconvert) @@ -81,7 +81,7 @@ def generate_thumbnail(source, outname, box, format, fit=True, options=None): img.thumbnail(box, PILImage.ANTIALIAS) format = format or img.format or original_format or 'JPEG' - logger.debug('Save thumnail image to {0} ({1})'.format(outname, format)) + logger.debug(u'Save thumnail image to {0} ({1})'.format(outname, format)) save_image(img, outname, format, options=options, autoconvert=True)