From 2c5fb72281b81814274d436de4b1e6496f27c8b7 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Wed, 19 May 2010 17:24:44 +0100 Subject: [PATCH 1/2] Add filesystem_encoding to unicode calls --- src/calibre/devices/usbms/driver.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/calibre/devices/usbms/driver.py b/src/calibre/devices/usbms/driver.py index c75fc9f6a1..4a020a24d4 100644 --- a/src/calibre/devices/usbms/driver.py +++ b/src/calibre/devices/usbms/driver.py @@ -16,6 +16,7 @@ import json from itertools import cycle from calibre import prints +from calibre.constants import filesystem_encoding from calibre.devices.usbms.cli import CLI from calibre.devices.usbms.device import Device from calibre.devices.usbms.books import BookList, Book @@ -87,7 +88,6 @@ class USBMS(CLI, Device): if idx is not None: bl_cache[lpath] = None if self.update_metadata_item(bl[idx]): - #print 'update_metadata_item returned true' changed = True else: if bl.add_book(self.book_from_path(prefix, lpath), @@ -112,8 +112,8 @@ class USBMS(CLI, Device): for path, dirs, files in os.walk(ebook_dir): for filename in files: if filename != self.METADATA_CACHE: - flist.append({'filename':unicode(filename), - 'path':unicode(path)}) + flist.append({'filename':self.path_to_unicode(filename), + 'path':self.path_to_unicode(path)}) for i, f in enumerate(flist): self.report_progress(i/float(len(flist)), _('Getting list of books on device...')) changed = update_booklist(f['filename'], f['path'], prefix) @@ -123,7 +123,8 @@ class USBMS(CLI, Device): paths = os.listdir(ebook_dir) for i, filename in enumerate(paths): self.report_progress((i+1) / float(len(paths)), _('Getting list of books on device...')) - changed = update_booklist(unicode(filename), ebook_dir, prefix) + changed = update_booklist(self.path_to_unicode(filename), + ebook_dir, prefix) if changed: need_sync = True @@ -267,16 +268,22 @@ class USBMS(CLI, Device): self.report_progress(1.0, _('Sending metadata to device...')) + @classmethod + def path_to_unicode(cls, path): + if isinstance(path, str): ## bytes is synonym for str as of python 2.6 + print 'p2u: isString', path + return unicode(path, filesystem_encoding) + return path + @classmethod def normalize_path(cls, path): 'Return path with platform native path separators' if path is None: return None if os.sep == '\\': - path = path.replace('/', '\\') + return cls.path_to_unicode(path.replace('/', '\\')) else: - path = path.replace('\\', '/') - return unicode(path) + return cls.path_to_unicode(path.replace('\\', '/')) @classmethod def parse_metadata_cache(cls, bl, prefix, name): From 057743f17707a207f76f90e58371304e018410ee Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Wed, 19 May 2010 18:26:24 +0100 Subject: [PATCH 2/2] Add path_to_unicode. Ensure os path walk results are converted. --- src/calibre/devices/usbms/driver.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/calibre/devices/usbms/driver.py b/src/calibre/devices/usbms/driver.py index cd6bddfc28..7b3531abf6 100644 --- a/src/calibre/devices/usbms/driver.py +++ b/src/calibre/devices/usbms/driver.py @@ -102,8 +102,7 @@ class USBMS(CLI, Device): if isinstance(ebook_dirs, basestring): ebook_dirs = [ebook_dirs] for ebook_dir in ebook_dirs: - if isbytestring(ebook_dir): - ebook_dir = ebook_dir.decode(filesystem_encoding) + ebook_dir = self.path_to_unicode(filesystem_encoding) ebook_dir = self.normalize_path( \ os.path.join(prefix, *(ebook_dir.split('/'))) \ if ebook_dir else prefix) @@ -115,8 +114,8 @@ class USBMS(CLI, Device): for path, dirs, files in os.walk(ebook_dir): for filename in files: if filename != self.METADATA_CACHE: - flist.append({'filename':filename, - 'path':path}) + flist.append({'filename': self.path_to_unicode(filename), + 'path':self.path_to_unicode(path)}) for i, f in enumerate(flist): self.report_progress(i/float(len(flist)), _('Getting list of books on device...')) changed = update_booklist(f['filename'], f['path'], prefix) @@ -126,7 +125,7 @@ class USBMS(CLI, Device): paths = os.listdir(ebook_dir) for i, filename in enumerate(paths): self.report_progress((i+1) / float(len(paths)), _('Getting list of books on device...')) - changed = update_booklist(filename, ebook_dir, prefix) + changed = update_booklist(self.path_to_unicode(filename), ebook_dir, prefix) if changed: need_sync = True @@ -270,6 +269,12 @@ class USBMS(CLI, Device): self.report_progress(1.0, _('Sending metadata to device...')) + @classmethod + def path_to_unicode(cls, path): + if isbytestring(path): + path = path.decode(filesystem_encoding) + return path + @classmethod def normalize_path(cls, path): 'Return path with platform native path separators' @@ -279,9 +284,7 @@ class USBMS(CLI, Device): path = path.replace('/', '\\') else: path = path.replace('\\', '/') - if isbytestring(path): - path = path.decode(filesystem_encoding) - return path + return cls.path_to_unicode(path) @classmethod def parse_metadata_cache(cls, bl, prefix, name):