remove duplicate folder scanning code

This commit is contained in:
j 2016-02-14 14:25:54 +05:30
parent 2959f44e24
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')
prefix = get_prefix()
if books is None: if books is None:
books = collect_books() books = collect_books(prefix)
with db.session(): with db.session():
prefix = get_prefix()
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
trigger_event('change', {}) if added:
logger.debug('imported unknown books') trigger_event('change', {})
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,30 +188,22 @@ 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): if count % 100 == 0:
for f in files: state.activity = {
if state.shutdown: 'activity': 'import',
return 'path': prefix,
#if f.startswith('._') or f == '.DS_Store': 'progress': [0, count],
if f.startswith('.'): }
continue trigger_event('activity', state.activity)
f = os.path.join(root, f) if state.activity.get('cancel'):
ext = f.split('.')[-1].lower() logger.debug('active import canceled')
if ext in extensions: state.activity = {}
books.append(f) return False
count += 1 return True
if state.activity.get('cancel'): books = collect_books(prefix, status=activity)
logger.debug('active import canceled') if books is None:
state.activity = {} return
return
if count % 100 == 0:
state.activity = {
'activity': 'import',
'path': prefix,
'progress': [0, count],
}
trigger_event('activity', state.activity)
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)