From c3aa3b241f8c32c2b6d8f5aec30b8b2432ca0b83 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 23 Apr 2015 08:22:05 +0530 Subject: [PATCH] When sorting filenames in comics treat the file extension as a secondary sort key. --- src/calibre/libunzip.py | 8 ++++++-- src/calibre/utils/unrar.py | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/calibre/libunzip.py b/src/calibre/libunzip.py index 172d1ce512..bad66f35bb 100644 --- a/src/calibre/libunzip.py +++ b/src/calibre/libunzip.py @@ -5,6 +5,7 @@ __docformat__ = 'restructuredtext en' import re from calibre.utils import zipfile +from calibre.utils.icu import numeric_sort_key def update(pathtozip, patterns, filepaths, names, compression=zipfile.ZIP_DEFLATED, verbose=True): ''' @@ -43,12 +44,15 @@ def extract(filename, dir): zf = zipfile.ZipFile(filename) zf.extractall(dir) +def sort_key(filename): + bn, ext = filename.rpartition('.')[::2] + return (numeric_sort_key(bn), numeric_sort_key(ext)) + def extract_member(filename, match=re.compile(r'\.(jpg|jpeg|gif|png)\s*$', re.I), sort_alphabetically=False): zf = zipfile.ZipFile(filename) names = list(zf.namelist()) if sort_alphabetically: - from calibre.utils.icu import numeric_sort_key - names.sort(key=numeric_sort_key) + names.sort(key=sort_key) for name in names: if match.search(name): return name, zf.read(name) diff --git a/src/calibre/utils/unrar.py b/src/calibre/utils/unrar.py index 96f0d2820b..c3940ad68f 100644 --- a/src/calibre/utils/unrar.py +++ b/src/calibre/utils/unrar.py @@ -185,9 +185,9 @@ def extract_member(stream, match=re.compile(r'\.(jpg|jpeg|gif|png)\s*$', re.I), return h['filename'], et.getvalue() def extract_first_alphabetically(stream): - from calibre.utils.icu import numeric_sort_key + from calibre.libunzip import sort_key names_ = sorted([x for x in names(stream) if os.path.splitext(x)[1][1:].lower() in - {'png', 'jpg', 'jpeg', 'gif'}], key=numeric_sort_key) + {'png', 'jpg', 'jpeg', 'gif', 'webp'}], key=sort_key) return extract_member(stream, name=names_[0], match=None) # Test normal RAR file {{{