remove duplicate folder scanning code

This commit is contained in:
j 2016-02-14 14:25:54 +05:30
commit 59765b074e

View file

@ -27,10 +27,10 @@ extensions = ['epub', 'pdf', 'txt', 'cbr', 'cbz']
def remove_missing(books=None): def remove_missing(books=None):
dirty = False dirty = False
logger.debug('remove missing') logger.debug('remove missing')
if books is None:
books = collect_books()
with db.session():
prefix = get_prefix() prefix = get_prefix()
if books is None:
books = collect_books(prefix)
with db.session():
if os.path.exists(prefix): if os.path.exists(prefix):
logger.debug('scan for removed files') logger.debug('scan for removed files')
db_paths = [] db_paths = []
@ -97,14 +97,14 @@ def get_prefix():
assert isinstance(prefix, str) assert isinstance(prefix, str)
return prefix return prefix
def collect_books(): def collect_books(prefix, status=None):
logger.debug('collect books') logger.debug('collect books')
books = [] books = []
prefix = get_prefix() count = 0
for root, folders, files in os.walk(prefix): for root, folders, files in os.walk(prefix):
for f in files: for f in files:
if state.shutdown: if state.shutdown:
return return []
if f.startswith('.'): if f.startswith('.'):
continue continue
f = os.path.join(root, f) f = os.path.join(root, f)
@ -113,21 +113,22 @@ def collect_books():
ext = 'epub' ext = 'epub'
if ext in extensions: if ext in extensions:
books.append(f) books.append(f)
count += 1
if status and not status(count):
return None
logger.debug('found %s books', len(books)) logger.debug('found %s books', len(books))
return books return books
def run_scan(): def run_scan():
logger.debug('run_scan') logger.debug('run_scan')
prefix = get_prefix() prefix = get_prefix()
books = collect_books() books = collect_books(prefix)
remove_missing(books) remove_missing(books)
position = 0
added = 0 added = 0
for f in ox.sorted_strings(books): for f in ox.sorted_strings(books):
if state.shutdown: if state.shutdown:
return return
position += 1
if os.path.exists(f): if os.path.exists(f):
id = media.get_id(f) id = media.get_id(f)
with db.session(): with db.session():
@ -135,8 +136,9 @@ def run_scan():
if not file: if not file:
file = add_file(id, f, prefix, f) file = add_file(id, f, prefix, f)
added += 1 added += 1
if added:
trigger_event('change', {}) trigger_event('change', {})
logger.debug('imported unknown books') logger.debug('imported %s unknown books', added)
def change_path(old, new): def change_path(old, new):
new_books = os.path.join(new, 'Books') new_books = os.path.join(new, 'Books')
@ -186,23 +188,7 @@ def run_import(options=None):
listitems = [] listitems = []
assert isinstance(prefix, str) assert isinstance(prefix, str)
books = [] books = []
count = 0 def activity(count):
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: if count % 100 == 0:
state.activity = { state.activity = {
'activity': 'import', 'activity': 'import',
@ -210,6 +196,14 @@ def run_import(options=None):
'progress': [0, count], 'progress': [0, count],
} }
trigger_event('activity', state.activity) 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 = { state.activity = {
'activity': 'import', 'activity': 'import',
'path': prefix, 'path': prefix,
@ -259,8 +253,8 @@ def run_import(options=None):
} }
trigger_event('activity', state.activity) 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) l = List.get(settings.USER_ID, listname)
if l: if l:
l.add_items(listitems) l.add_items(listitems)