From ce6b81ab732d36552b8780f9bb79ecf7bce39bc5 Mon Sep 17 00:00:00 2001 From: "beville@gmail.com" Date: Wed, 13 Feb 2013 21:50:47 +0000 Subject: [PATCH] Reworked the encoding in the recursive filelist creation Changed the "fix encoding" fuction to only load once, and set the local from env git-svn-id: http://comictagger.googlecode.com/svn/trunk@488 6c5673fe-1810-88d6-992b-cd32ca31540c --- comictaggerlib/utils.py | 43 ++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/comictaggerlib/utils.py b/comictaggerlib/utils.py index 2eaabf1..0323115 100644 --- a/comictaggerlib/utils.py +++ b/comictaggerlib/utils.py @@ -25,36 +25,53 @@ import re import platform import locale import codecs + +class UtilsVars: + already_fixed_encoding = False def fix_output_encoding( ): + if not UtilsVars.already_fixed_encoding: + # this reads the environment and inits the right locale + locale.setlocale(locale.LC_ALL, "") - # try to make stdout/stderr encodings happy for unicode printing - if platform.system() == "Darwin": - preferred_encoding = "utf-8" - else: + # try to make stdout/stderr encodings happy for unicode printing preferred_encoding = locale.getpreferredencoding() - sys.stdout = codecs.getwriter(preferred_encoding)(sys.stdout) - sys.stderr = codecs.getwriter(preferred_encoding)(sys.stderr) + if getattr(sys, 'frozen', None) and platform.system() == "Darwin": + preferred_encoding = "utf-8" + sys.stdout = codecs.getwriter(preferred_encoding)(sys.stdout) + sys.stderr = codecs.getwriter(preferred_encoding)(sys.stderr) + elif platform.system() == "Windows": + sys.stdout = codecs.getwriter(preferred_encoding)(sys.stdout) + sys.stderr = codecs.getwriter(preferred_encoding)(sys.stderr) + UtilsVars.already_fixed_encoding = True def get_recursive_filelist( pathlist ): """ Get a recursive list of of all files under all path items in the list """ - + filename_encoding = sys.getfilesystemencoding() filelist = [] for p in pathlist: # if path is a folder, walk it recursivly, and all files underneath if type(p) == str: #make sure string is unicode - filename_encoding = sys.getfilesystemencoding() - p = p.decode(filename_encoding, 'replace') + p = p.decode(filename_encoding) #, 'replace') + elif type(p) != unicode: + #it's probably a QString + p = unicode(p) - if os.path.isdir( unicode(p)): - for root,dirs,files in os.walk( unicode(p) ): + if os.path.isdir( p ): + for root,dirs,files in os.walk( p ): for f in files: - filelist.append(os.path.join(root,unicode(f))) + if type(f) == str: + #make sure string is unicode + f = f.decode(filename_encoding, 'replace') + elif type(f) != unicode: + #it's probably a QString + f = unicode(f) + filelist.append(os.path.join(root,f)) else: - filelist.append(unicode(p)) + filelist.append(p) return filelist