From: Oleg Broytman Date: Sun, 10 Jun 2018 21:31:41 +0000 (+0300) Subject: Feat(config): Warp config to provide default value X-Git-Tag: 0.1.4~13 X-Git-Url: https://git.phdru.name/?p=m_librarian.git;a=commitdiff_plain;h=8fdf6aab492482fcacad9ce2ec764bdc5bf76590 Feat(config): Warp config to provide default value --- diff --git a/docs-ru/news.rst b/docs-ru/news.rst index bdd5999..3269fa1 100644 --- a/docs-ru/news.rst +++ b/docs-ru/news.rst @@ -8,6 +8,8 @@ Version 0.1.4 (2018-05-??) * Показывать список найденных книг с разбивкой по авторам и сериям. +* Файл конфигурации, все секции и все ключи сделаны необязательными. + Version 0.1.3 (2018-05-25) -------------------------- diff --git a/docs/news.rst b/docs/news.rst index d07b99e..6c12e08 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -8,6 +8,8 @@ Version 0.1.4 (2018-06-??) * Use filters from config. +* Config file, all sections and all key are now completely optional. + Version 0.1.3 (2018-05-25) -------------------------- diff --git a/m_librarian/config.py b/m_librarian/config.py index 4abed14..cbf76b7 100755 --- a/m_librarian/config.py +++ b/m_librarian/config.py @@ -3,9 +3,10 @@ from __future__ import print_function import os try: - from ConfigParser import RawConfigParser + from ConfigParser import RawConfigParser, NoSectionError, NoOptionError except ImportError: # py3 - from configparser import RawConfigParser + from configparser import RawConfigParser, NoSectionError, NoOptionError + __all__ = ['get_config'] @@ -25,7 +26,7 @@ def _find_config_dirs_posix(): def find_config_dirs(): if os.name == 'posix': return _find_config_dirs_posix() - raise OSError("Unknow OS") + return None def find_config_file(config_dirs=None): @@ -36,19 +37,42 @@ def find_config_file(config_dirs=None): if os.path.exists(ml_conf_file): return ml_conf_file else: - raise IOError("Cannot find m_librarian.conf in %s" % config_dirs) + return None _ml_config = None +class ConfigWrapper(object): + def __init__(self, config): + self.config = config + + def get(self, section, option, default=None): + try: + return self.config.get(section, option) + except (NoSectionError, NoOptionError): + return default + + def set(self, section, option, value): + self.config.set(section, option, value) + + def getint(self, section, option, default=0): + try: + return self.config.getint(section, option) + except (NoSectionError, NoOptionError): + return default + # Do not catch ValueError here, it must be propagated + + def get_config(config_path=None): global _ml_config if _ml_config is None: + _ml_config = RawConfigParser() if config_path is None: config_path = find_config_file() - _ml_config = RawConfigParser() - _ml_config.read(config_path) + if config_path is not None: + _ml_config.read(config_path) + _ml_config = ConfigWrapper(_ml_config) return _ml_config diff --git a/m_librarian/db.py b/m_librarian/db.py index fc3325f..0c41d4b 100755 --- a/m_librarian/db.py +++ b/m_librarian/db.py @@ -145,10 +145,7 @@ def find_sqlite_dburi(db_dirs=None): def open_db(db_uri=None): if db_uri is None: - try: - db_uri = get_config().get('database', 'URI') - except Exception: - db_uri = find_sqlite_dburi() + db_uri = get_config().get('database', 'URI') or find_sqlite_dburi() if '://' not in db_uri: db_uri = 'sqlite://' + os.path.abspath(db_uri).replace(os.sep, '/') diff --git a/m_librarian/download.py b/m_librarian/download.py index e10e9fe..7e27561 100755 --- a/m_librarian/download.py +++ b/m_librarian/download.py @@ -19,9 +19,8 @@ def _compile_format(): global format, compile_format, compiled_format if compile_format: compile_format = False - try: - format = get_config().get('download', 'format') - except Exception: + format = get_config().get('download', 'format') + if not format: return got_percent = False compiled = [] @@ -68,7 +67,7 @@ def download(book, dest_path=None, lib_path=None, a_format=None): if lib_path is None: global _library_path if _library_path is None: - _library_path = get_config().get('library', 'path') + _library_path = get_config().get('library', 'path') or '.' lib_path = _library_path global format, compile_format, compiled_format diff --git a/m_librarian/search.py b/m_librarian/search.py index 1405e09..e4e2884 100644 --- a/m_librarian/search.py +++ b/m_librarian/search.py @@ -1,8 +1,3 @@ -try: - from configparser import NoSectionError, NoOptionError -except ImportError: # Python 2 - from ConfigParser import NoSectionError, NoOptionError - from sqlobject.sqlbuilder import AND, OR, func from .config import get_config from .db import Author, Book, Extension, Genre, Language @@ -80,14 +75,8 @@ def search_books(search_type, case_sensitive, values, join_expressions=None, orderBy=None, use_filters=False): if use_filters: config = get_config() - try: - lang_filter = config.get('filters', 'lang') - except (NoSectionError, NoOptionError): - lang_filter = None - try: - deleted_filter = config.getint('filters', 'deleted') - except (NoSectionError, NoOptionError): - deleted_filter = None + lang_filter = config.get('filters', 'lang') + deleted_filter = config.getint('filters', 'deleted') if lang_filter: if join_expressions is None: join_expressions = [] diff --git a/m_librarian/web/app.py b/m_librarian/web/app.py index d011020..9991bb7 100644 --- a/m_librarian/web/app.py +++ b/m_librarian/web/app.py @@ -70,7 +70,7 @@ def search_authors_post(): @route('/books-by-author//', method='GET') @cheetah_view('books_by_author.tmpl') def books_by_author(id): - use_filters = get_config().getint('filters', 'use_in_books_list') + use_filters = get_config().getint('filters', 'use_in_books_list', 1) if use_filters: join_expressions = [] join_expressions.append(Book.j.authors) @@ -106,7 +106,7 @@ def send_static(filename): @cheetah_view('download.tmpl') def download_books(): books_ids = request.forms.getall('books') - download_path = get_config().get('download', 'path') + download_path = get_config().get('download', 'path') or '.' if books_ids: for id in books_ids: book = Book.get(int(id)) diff --git a/m_librarian/web/views/search_books_form.py b/m_librarian/web/views/search_books_form.py index 45d9075..487aaaf 100644 --- a/m_librarian/web/views/search_books_form.py +++ b/m_librarian/web/views/search_books_form.py @@ -35,10 +35,10 @@ VFN=valueForName currentTime=time.time __CHEETAH_version__ = '3.1.0' __CHEETAH_versionTuple__ = (3, 1, 0, 'final', 1) -__CHEETAH_genTime__ = 1528138831.71884 -__CHEETAH_genTimestamp__ = 'Mon Jun 4 22:00:31 2018' +__CHEETAH_genTime__ = 1528667817.826233 +__CHEETAH_genTimestamp__ = 'Mon Jun 11 00:56:57 2018' __CHEETAH_src__ = 'search_books_form.tmpl' -__CHEETAH_srcLastModified__ = 'Mon Jun 4 22:00:29 2018' +__CHEETAH_srcLastModified__ = 'Mon Jun 11 00:30:36 2018' __CHEETAH_docstring__ = 'Autogenerated by Cheetah: The Python-Powered Template Engine' if __CHEETAH_versionTuple__ < RequiredCheetahVersionTuple: @@ -142,7 +142,7 @@ class search_books_form(Template): ######################################## ## START - generated method body - if VFFSL(SL,"getVar",False)('use_filters', VFN(VFFSL(SL,"get_config",False)(),"getint",False)('filters', 'use_in_search_forms')): # generated from line 14, col 1 + if VFFSL(SL,"getVar",False)('use_filters', VFN(VFFSL(SL,"get_config",False)(),"getint",False)('filters', 'use_in_search_forms', 1)): # generated from line 14, col 1 write(u'''checked''') ######################################## diff --git a/m_librarian/web/views/search_books_form.tmpl b/m_librarian/web/views/search_books_form.tmpl index e8b5e55..d5e0c0e 100644 --- a/m_librarian/web/views/search_books_form.tmpl +++ b/m_librarian/web/views/search_books_form.tmpl @@ -11,7 +11,7 @@ checked#slurp #end if #end def #def $use_filters_check -#if $getVar('use_filters', $get_config().getint('filters', 'use_in_search_forms')) +#if $getVar('use_filters', $get_config().getint('filters', 'use_in_search_forms', 1)) checked#slurp #end if #end def diff --git a/sample/m_librarian.conf b/sample/m_librarian.conf index bca577d..cc8f3f5 100644 --- a/sample/m_librarian.conf +++ b/sample/m_librarian.conf @@ -17,7 +17,7 @@ path = /var/lib/LRE_Flibusta lang = ru en # Show/hide deleted books; 0 - hide, 1 - show -deleted = 1 +deleted = 0 # Default value to use filters in search forms use_in_search_forms = 1 diff --git a/scripts/ml-search.py b/scripts/ml-search.py index 7cc32e1..5cc0bc4 100755 --- a/scripts/ml-search.py +++ b/scripts/ml-search.py @@ -2,10 +2,6 @@ from __future__ import print_function import argparse -try: - from configparser import NoSectionError, NoOptionError -except ImportError: # Python 2 - from ConfigParser import NoSectionError, NoOptionError import os import sys from sqlobject.sqlbuilder import CONCAT @@ -207,10 +203,8 @@ def _search_books(case_sensitive, search_type, args): if args.get or args.get_many: download_to = args.download_to if download_to is None: - try: - download_to = get_config().get('download', 'path') - except (NoSectionError, NoOptionError): - download_to = os.path.curdir + download_to = get_config().get('download', 'path') \ + or os.path.curdir download(book, download_to, args.path, args.format) count += 1 print_count(count)