refactor suggestions code
This commit is contained in:
parent
2e983ffed2
commit
af9464c966
2 changed files with 61 additions and 56 deletions
|
@ -5,8 +5,6 @@ Templates are self-contained in this directory
|
|||
'''
|
||||
from __future__ import print_function
|
||||
|
||||
import os
|
||||
|
||||
from flask import current_app, Blueprint, render_template, jsonify, abort, \
|
||||
request, redirect, url_for
|
||||
|
||||
|
@ -14,8 +12,7 @@ from larigira.entrypoints_utils import get_avail_entrypoints
|
|||
from larigira.audiogen import get_audiogenerator
|
||||
from larigira.timegen import get_timegenerator
|
||||
from larigira import forms
|
||||
from larigira.config import get_conf
|
||||
from larigira.fsutils import scan_dir_audio
|
||||
from .suggestions import get_suggestions
|
||||
db = Blueprint('db', __name__, url_prefix='/db', template_folder='templates')
|
||||
|
||||
|
||||
|
@ -23,58 +20,6 @@ def get_model():
|
|||
return current_app.larigira.controller.monitor.model
|
||||
|
||||
|
||||
def get_suggested_files():
|
||||
if not get_conf()['FILE_PATH_SUGGESTION']:
|
||||
return []
|
||||
if current_app.cache.has('dbadmin.get_suggested_files'):
|
||||
return current_app.cache.get('dbadmin.get_suggested_files')
|
||||
current_app.logger.debug('get_suggested_files MISS in cache')
|
||||
files = []
|
||||
for path in get_conf()['FILE_PATH_SUGGESTION']:
|
||||
if not os.path.isdir(path):
|
||||
current_app.logger.warn('Invalid suggestion path: %s' % path)
|
||||
continue
|
||||
pathfiles = scan_dir_audio(path)
|
||||
files.extend(pathfiles)
|
||||
current_app.logger.debug('Suggested files: %s' % ', '.join(files))
|
||||
|
||||
current_app.cache.set('dbadmin.get_suggested_files', files,
|
||||
timeout=600) # ten minutes
|
||||
return files
|
||||
|
||||
|
||||
def get_suggested_dirs():
|
||||
dirset = set()
|
||||
for f in get_suggested_files():
|
||||
dirpath = os.path.dirname(f)
|
||||
while dirpath:
|
||||
if dirpath in dirset:
|
||||
break
|
||||
dirset.add(dirpath)
|
||||
dirpath = os.path.dirname(dirpath)
|
||||
|
||||
return list(dirset)
|
||||
|
||||
|
||||
def get_suggested_scripts():
|
||||
base = get_conf()['SCRIPTS_PATH']
|
||||
fnames = [f for f in os.listdir(base)
|
||||
if os.access(os.path.join(base, f), os.R_OK | os.X_OK)]
|
||||
return fnames
|
||||
|
||||
|
||||
def get_suggestions():
|
||||
files = get_suggested_files()
|
||||
if len(files) > 200:
|
||||
current_app.logger.warn("Too many suggested files, cropping")
|
||||
files = files[:200]
|
||||
return dict(
|
||||
files=files,
|
||||
dirs=get_suggested_dirs(),
|
||||
scripts=get_suggested_scripts(),
|
||||
)
|
||||
|
||||
|
||||
@db.route('/')
|
||||
def home():
|
||||
return render_template('dbadmin_base.html')
|
||||
|
|
60
larigira/dbadmin/suggestions.py
Normal file
60
larigira/dbadmin/suggestions.py
Normal file
|
@ -0,0 +1,60 @@
|
|||
import os
|
||||
|
||||
from flask import current_app
|
||||
|
||||
from larigira.config import get_conf
|
||||
from larigira.fsutils import scan_dir_audio
|
||||
|
||||
|
||||
def get_suggested_files():
|
||||
if not get_conf()['FILE_PATH_SUGGESTION']:
|
||||
return []
|
||||
if current_app.cache.has('dbadmin.get_suggested_files'):
|
||||
return current_app.cache.get('dbadmin.get_suggested_files')
|
||||
current_app.logger.debug('get_suggested_files MISS in cache')
|
||||
files = []
|
||||
for path in get_conf()['FILE_PATH_SUGGESTION']:
|
||||
if not os.path.isdir(path):
|
||||
current_app.logger.warn('Invalid suggestion path: %s' % path)
|
||||
continue
|
||||
pathfiles = scan_dir_audio(path)
|
||||
files.extend(pathfiles)
|
||||
current_app.logger.debug('Suggested files: %s' % ', '.join(files))
|
||||
|
||||
current_app.cache.set('dbadmin.get_suggested_files', files,
|
||||
timeout=600) # ten minutes
|
||||
return files
|
||||
|
||||
|
||||
def get_suggested_dirs():
|
||||
dirset = set()
|
||||
for f in get_suggested_files():
|
||||
dirpath = os.path.dirname(f)
|
||||
while dirpath:
|
||||
if dirpath in dirset:
|
||||
break
|
||||
dirset.add(dirpath)
|
||||
dirpath = os.path.dirname(dirpath)
|
||||
|
||||
return list(dirset)
|
||||
|
||||
|
||||
def get_suggested_scripts():
|
||||
base = get_conf()['SCRIPTS_PATH']
|
||||
fnames = [f for f in os.listdir(base)
|
||||
if os.access(os.path.join(base, f), os.R_OK | os.X_OK)]
|
||||
return fnames
|
||||
|
||||
|
||||
def get_suggestions():
|
||||
files = get_suggested_files()
|
||||
if len(files) > 200:
|
||||
current_app.logger.warn("Too many suggested files, cropping")
|
||||
files = files[:200]
|
||||
return dict(
|
||||
files=files,
|
||||
dirs=get_suggested_dirs(),
|
||||
scripts=get_suggested_scripts(),
|
||||
)
|
||||
|
||||
|
Loading…
Reference in a new issue