From af9464c9663d7775a358801871fdc6dcb5af6321 Mon Sep 17 00:00:00 2001 From: boyska Date: Tue, 3 Jan 2017 20:46:09 +0100 Subject: [PATCH] refactor suggestions code --- larigira/dbadmin/__init__.py | 57 +------------------------------ larigira/dbadmin/suggestions.py | 60 +++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 56 deletions(-) create mode 100644 larigira/dbadmin/suggestions.py diff --git a/larigira/dbadmin/__init__.py b/larigira/dbadmin/__init__.py index 60833d7..54fb763 100644 --- a/larigira/dbadmin/__init__.py +++ b/larigira/dbadmin/__init__.py @@ -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') diff --git a/larigira/dbadmin/suggestions.py b/larigira/dbadmin/suggestions.py new file mode 100644 index 0000000..f659442 --- /dev/null +++ b/larigira/dbadmin/suggestions.py @@ -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(), + ) + +