From 2733e4558f07ca6b66b9e7b34223b877cd222c1b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 13 Mar 2016 09:26:27 +0530 Subject: [PATCH] Fix #1556322 [automatic adding not sorted to last modified time stamp](https://bugs.launchpad.net/calibre/+bug/1556322) --- src/calibre/gui2/auto_add.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/calibre/gui2/auto_add.py b/src/calibre/gui2/auto_add.py index 69f81e12e4..ec5f95c7e4 100644 --- a/src/calibre/gui2/auto_add.py +++ b/src/calibre/gui2/auto_add.py @@ -96,11 +96,17 @@ class Worker(Thread): # Must be a known ebook file type self.is_filename_allowed(x) ] - data = {} + data = [] # Give any in progress copies time to complete time.sleep(2) - for fname in files: + def safe_mtime(x): + try: + return os.path.getmtime(os.path.join(self.path, x)) + except EnvironmentError: + return time.time() + + for fname in sorted(files, key=safe_mtime): f = os.path.join(self.path, fname) # Try opening the file for reading, if the OS prevents us, then at @@ -141,7 +147,7 @@ class Worker(Thread): with open(opfpath, 'wb') as f: f.write(metadata_to_opf(mi)) self.staging.add(fname) - data[fname] = tdir + data.append((fname, tdir)) if data: self.callback(data) @@ -208,7 +214,7 @@ class AutoAdder(QObject): duplicates = [] added_ids = set() - for fname, tdir in data.iteritems(): + for fname, tdir in data: paths = [os.path.join(self.worker.path, fname)] sz = os.path.join(tdir, 'size.txt') try: @@ -277,7 +283,7 @@ class AutoAdder(QObject): num = len(ids) count += num - for tdir in data.itervalues(): + for fname, tdir in data: try: shutil.rmtree(tdir) except: