From 1356749036af0fc07253aac4f534d84c99e8c741 Mon Sep 17 00:00:00 2001 From: Dylan Garrett Date: Wed, 9 Jun 2021 22:08:05 -0700 Subject: Cleanup config and allow passing as json on terminal --- lib/books.py | 7 +------ roka.py | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/lib/books.py b/lib/books.py index 6f3510b..95df374 100644 --- a/lib/books.py +++ b/lib/books.py @@ -13,11 +13,6 @@ ABS_PATH = os.path.dirname(os.path.abspath(__file__)) CACHE_PATH = os.path.join(ABS_PATH, '../', 'cache') JSON_PATH = os.path.join(CACHE_PATH, 'audiobooks.json') -# use Flask's config parser, configparser would be hacky -APP = Flask(__name__) -if os.path.exists(os.path.join(ABS_PATH, '../', 'app.cfg')): - APP.config.from_pyfile(os.path.join(ABS_PATH, '../', 'app.cfg')) - class Books: def __init__(self): ''' @@ -95,7 +90,7 @@ class Books: (existing content is not re-hashed) ''' ex = self._get_path_hash_dict() - dirs = self._get_dirs(audiobook_path or APP.config['ROOT_PATH']) + dirs = self._get_dirs(audiobook_path) books = dict() for path in dirs: diff --git a/roka.py b/roka.py index 8d81641..cfcd7e5 100755 --- a/roka.py +++ b/roka.py @@ -3,14 +3,13 @@ import argparse import os import shutil +import json from flask import Flask, request, Response, render_template, send_file, templating from lib.books import Books from lib.util import check_auth, escape, generate_rss, read_cache abs_path = os.path.dirname(os.path.abspath(__file__)) app = Flask(__name__) -if os.path.exists(os.path.join(abs_path, 'app.cfg')): - app.config.from_pyfile(os.path.join(abs_path, 'app.cfg')) cache_path = os.path.join(abs_path, 'cache') json_path = os.path.join(cache_path, 'audiobooks.json') @@ -78,7 +77,6 @@ def generate(static_path, base_url, audiobook_dirs): books = Books() books.scan_books(audiobook_dirs) - # TODO avoid writing and reading cache books.write_cache() books = read_cache(json_path) index = my_render_template(app, 'index.html', books=books, static=True) @@ -102,7 +100,8 @@ def generate(static_path, base_url, audiobook_dirs): for f_key, file in book['files'].items(): f_path = file['path'] copy_path = os.path.join(book_dir, f_key + '.mp3') - shutil.copyfile(f_path, copy_path) + if not os.path.exists(copy_path): + shutil.copyfile(f_path, copy_path) if __name__ == '__main__': desc = 'roka: listen to audiobooks with podcast apps via RSS' @@ -116,19 +115,30 @@ if __name__ == '__main__': parser.add_argument('--base_url', dest='base_url', type=str, action='store', help='Base URL to use in static files', required=False) - parser.add_argument('--scan_dir', dest='scan_dir', type=str, action='store', - help='Directory to scan for audiobooks', + parser.add_argument('--config', dest='config', type=str, action='store', + help='Json configuration instead of app.cfg', required=False) args = parser.parse_args() if args.static_path and not args.base_url or args.base_url and not args.static_path: parser.error('--generate and --base_url must be included together') + if args.config: + class objectview(object): + def __init__(self, d): + self.__dict__ = d + config = objectview(json.loads(args.config)) + app.config.from_object(config) + elif os.path.exists(os.path.join(abs_path, 'app.cfg')): + app.config.from_pyfile(os.path.join(abs_path, 'app.cfg')) + + root_path = os.path.expanduser(app.config['ROOT_PATH']) + if args.scan: books = Books() - books.scan_books(args.scan_dir) + books.scan_books(root_path) books.write_cache() elif args.static_path: - generate(args.static_path, args.base_url, args.scan_dir) + generate(args.static_path, args.base_url, root_path) else: app.run(host='127.0.0.1', port='8085', threaded=True) -- cgit v1.2.3-54-g00ecf