From 59765b074eb3302df33dea19b3cec69fa713a303 Mon Sep 17 00:00:00 2001 From: j Date: Sun, 14 Feb 2016 14:25:54 +0530 Subject: [PATCH] remove duplicate folder scanning code --- oml/item/scan.py | 66 ++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 36 deletions(-) diff --git a/oml/item/scan.py b/oml/item/scan.py index 390ad81..395e6d1 100644 --- a/oml/item/scan.py +++ b/oml/item/scan.py @@ -27,10 +27,10 @@ extensions = ['epub', 'pdf', 'txt', 'cbr', 'cbz'] def remove_missing(books=None): dirty = False logger.debug('remove missing') + prefix = get_prefix() if books is None: - books = collect_books() + books = collect_books(prefix) with db.session(): - prefix = get_prefix() if os.path.exists(prefix): logger.debug('scan for removed files') db_paths = [] @@ -97,14 +97,14 @@ def get_prefix(): assert isinstance(prefix, str) return prefix -def collect_books(): +def collect_books(prefix, status=None): logger.debug('collect books') books = [] - prefix = get_prefix() + count = 0 for root, folders, files in os.walk(prefix): for f in files: if state.shutdown: - return + return [] if f.startswith('.'): continue f = os.path.join(root, f) @@ -113,21 +113,22 @@ def collect_books(): ext = 'epub' if ext in extensions: books.append(f) + count += 1 + if status and not status(count): + return None logger.debug('found %s books', len(books)) return books def run_scan(): logger.debug('run_scan') prefix = get_prefix() - books = collect_books() + books = collect_books(prefix) remove_missing(books) - position = 0 added = 0 for f in ox.sorted_strings(books): if state.shutdown: return - position += 1 if os.path.exists(f): id = media.get_id(f) with db.session(): @@ -135,8 +136,9 @@ def run_scan(): if not file: file = add_file(id, f, prefix, f) added += 1 - trigger_event('change', {}) - logger.debug('imported unknown books') + if added: + trigger_event('change', {}) + logger.debug('imported %s unknown books', added) def change_path(old, new): new_books = os.path.join(new, 'Books') @@ -186,30 +188,22 @@ def run_import(options=None): listitems = [] assert isinstance(prefix, str) books = [] - count = 0 - for root, folders, files in os.walk(prefix): - for f in files: - if state.shutdown: - return - #if f.startswith('._') or f == '.DS_Store': - if f.startswith('.'): - continue - f = os.path.join(root, f) - ext = f.split('.')[-1].lower() - if ext in extensions: - books.append(f) - count += 1 - if state.activity.get('cancel'): - logger.debug('active import canceled') - state.activity = {} - return - if count % 100 == 0: - state.activity = { - 'activity': 'import', - 'path': prefix, - 'progress': [0, count], - } - trigger_event('activity', state.activity) + def activity(count): + if count % 100 == 0: + state.activity = { + 'activity': 'import', + 'path': prefix, + 'progress': [0, count], + } + trigger_event('activity', state.activity) + if state.activity.get('cancel'): + logger.debug('active import canceled') + state.activity = {} + return False + return True + books = collect_books(prefix, status=activity) + if books is None: + return state.activity = { 'activity': 'import', 'path': prefix, @@ -259,8 +253,8 @@ def run_import(options=None): } trigger_event('activity', state.activity) - with db.session(): - if listname and listitems: + if listname and listitems: + with db.session(): l = List.get(settings.USER_ID, listname) if l: l.add_items(listitems)