From 90ab946498953a0d40bc53b53fcc27c190a9f494 Mon Sep 17 00:00:00 2001 From: Jan Date: Thu, 10 Oct 2013 13:24:31 +1100 Subject: [PATCH] Adding folders now follows Windows ordering As per issue #11. Was causing problems adding folders numbered 1...999 rather than 001...999. --- mangle/book.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/mangle/book.py b/mangle/book.py index 15dc4ea..633de50 100644 --- a/mangle/book.py +++ b/mangle/book.py @@ -24,6 +24,15 @@ from options import DialogOptions from convert import DialogConvert +import re +digits = re.compile(r'(\d+)') +def tokenize(filename): + return tuple(int(token) if match else token + for token, match in + ((fragment, digits.search(fragment)) + for fragment in digits.split(filename))) + + class Book(object): DefaultDevice = 'Kindle 4' DefaultOutputFormat = 'Images & CBZ' @@ -369,12 +378,15 @@ class MainWindowBook(QtGui.QMainWindow): filenames = [] for directory in directories: - for root, subdirs, subfiles in os.walk(unicode(directory)): - for filename in subfiles: - path = os.path.join(root, filename) - if self.isImageFile(path): - filenames.append(path) - + directory = unicode(directory) + for item in sorted(os.listdir(directory), key=tokenize): + item = unicode(item) + path = os.path.join(directory, item) + if self.isImageFile(path): + filenames.append(path) + elif os.path.isdir: + self.addImageDirs([path]) + self.addImageFiles(filenames)