mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Make find_pages useable with things other than directories
This commit is contained in:
parent
7c81951153
commit
5725b1cd2c
@ -39,32 +39,41 @@ def extract_comic(path_to_comic_file):
|
|||||||
return tdir
|
return tdir
|
||||||
|
|
||||||
|
|
||||||
def find_pages(dir, sort_on_mtime=False, verbose=False):
|
def generate_entries_from_dir(path):
|
||||||
|
from functools import partial
|
||||||
|
from calibre import walk
|
||||||
|
ans = {}
|
||||||
|
for x in walk(path):
|
||||||
|
x = os.path.abspath(x)
|
||||||
|
ans[x] = partial(os.path.getmtime, x)
|
||||||
|
return ans
|
||||||
|
|
||||||
|
|
||||||
|
def find_pages(dir_or_items, sort_on_mtime=False, verbose=False):
|
||||||
'''
|
'''
|
||||||
Find valid comic pages in a previously un-archived comic.
|
Find valid comic pages in a previously un-archived comic.
|
||||||
|
|
||||||
:param dir: Directory in which extracted comic lives
|
:param dir_or_items: Directory in which extracted comic lives or a dict of paths to function getting mtime
|
||||||
:param sort_on_mtime: If True sort pages based on their last modified time.
|
:param sort_on_mtime: If True sort pages based on their last modified time.
|
||||||
Otherwise, sort alphabetically.
|
Otherwise, sort alphabetically.
|
||||||
'''
|
'''
|
||||||
extensions = {'jpeg', 'jpg', 'gif', 'png', 'webp'}
|
extensions = {'jpeg', 'jpg', 'gif', 'png', 'webp'}
|
||||||
|
items = generate_entries_from_dir(dir_or_items) if isinstance(dir_or_items, str) else dir_or_items
|
||||||
|
sep_counts = set()
|
||||||
pages = []
|
pages = []
|
||||||
for datum in os.walk(dir):
|
for path in items:
|
||||||
for name in datum[-1]:
|
if '__MACOSX' in path:
|
||||||
path = os.path.abspath(os.path.join(datum[0], name))
|
continue
|
||||||
if '__MACOSX' in path:
|
ext = path.rpartition('.')[2].lower()
|
||||||
continue
|
if ext in extensions:
|
||||||
for ext in extensions:
|
sep_counts.add(path.replace(os.sep, '/').count('/'))
|
||||||
if path.lower().endswith('.'+ext):
|
pages.append(path)
|
||||||
pages.append(path)
|
|
||||||
break
|
|
||||||
sep_counts = {x.replace(os.sep, '/').count('/') for x in pages}
|
|
||||||
# Use the full path to sort unless the files are in folders of different
|
# Use the full path to sort unless the files are in folders of different
|
||||||
# levels, in which case simply use the filenames.
|
# levels, in which case simply use the filenames.
|
||||||
basename = os.path.basename if len(sep_counts) > 1 else lambda x: x
|
basename = os.path.basename if len(sep_counts) > 1 else lambda x: x
|
||||||
if sort_on_mtime:
|
if sort_on_mtime:
|
||||||
def key(x):
|
def key(x):
|
||||||
return os.stat(x).st_mtime
|
return items[x]()
|
||||||
else:
|
else:
|
||||||
def key(x):
|
def key(x):
|
||||||
return numeric_sort_key(basename(x))
|
return numeric_sort_key(basename(x))
|
||||||
@ -72,7 +81,12 @@ def find_pages(dir, sort_on_mtime=False, verbose=False):
|
|||||||
pages.sort(key=key)
|
pages.sort(key=key)
|
||||||
if verbose:
|
if verbose:
|
||||||
prints('Found comic pages...')
|
prints('Found comic pages...')
|
||||||
prints('\t'+'\n\t'.join([os.path.relpath(p, dir) for p in pages]))
|
try:
|
||||||
|
base = os.path.commonpath(pages)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
prints('\t'+'\n\t'.join([os.path.relpath(p, base) for p in pages]))
|
||||||
return pages
|
return pages
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,10 +6,13 @@ __docformat__ = 'restructuredtext en'
|
|||||||
Based on ideas from comiclrf created by FangornUK.
|
Based on ideas from comiclrf created by FangornUK.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import shutil, textwrap, codecs, os
|
import codecs
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import textwrap
|
||||||
|
|
||||||
from calibre.customize.conversion import InputFormatPlugin, OptionRecommendation
|
|
||||||
from calibre import CurrentDir
|
from calibre import CurrentDir
|
||||||
|
from calibre.customize.conversion import InputFormatPlugin, OptionRecommendation
|
||||||
from calibre.ptempfile import PersistentTemporaryDirectory
|
from calibre.ptempfile import PersistentTemporaryDirectory
|
||||||
|
|
||||||
|
|
||||||
@ -126,8 +129,7 @@ class ComicInput(InputFormatPlugin):
|
|||||||
return comics
|
return comics
|
||||||
|
|
||||||
def get_pages(self, comic, tdir2):
|
def get_pages(self, comic, tdir2):
|
||||||
from calibre.ebooks.comic.input import (extract_comic, process_pages,
|
from calibre.ebooks.comic.input import extract_comic, find_pages, process_pages
|
||||||
find_pages)
|
|
||||||
tdir = extract_comic(comic)
|
tdir = extract_comic(comic)
|
||||||
new_pages = find_pages(tdir, sort_on_mtime=self.opts.no_sort,
|
new_pages = find_pages(tdir, sort_on_mtime=self.opts.no_sort,
|
||||||
verbose=self.opts.verbose)
|
verbose=self.opts.verbose)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user