remove duplicate folder scanning code

This commit is contained in:
j 2016-02-14 14:25:54 +05:30
parent 2959f44e24
commit 59765b074e
1 changed files with 30 additions and 36 deletions

View File

@ -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)