]> git.phdru.name Git - m_librarian.git/commitdiff
Feat(config): Warp config to provide default value
authorOleg Broytman <phd@phdru.name>
Sun, 10 Jun 2018 21:31:41 +0000 (00:31 +0300)
committerOleg Broytman <phd@phdru.name>
Sun, 10 Jun 2018 23:31:08 +0000 (02:31 +0300)
docs-ru/news.rst
docs/news.rst
m_librarian/config.py
m_librarian/db.py
m_librarian/download.py
m_librarian/search.py
m_librarian/web/app.py
m_librarian/web/views/search_books_form.py
m_librarian/web/views/search_books_form.tmpl
sample/m_librarian.conf
scripts/ml-search.py

index bdd599909b8137ace5707064d56d482f13f0a19e..3269fa19534d65bd4ad1acf23339afbab1a5b6ce 100644 (file)
@@ -8,6 +8,8 @@ Version 0.1.4 (2018-05-??)
 
 * Показывать список найденных книг с разбивкой по авторам и сериям.
 
+* Файл конфигурации, все секции и все ключи сделаны необязательными.
+
 Version 0.1.3 (2018-05-25)
 --------------------------
 
index d07b99e0e22b86f2aaa0c8f9c38c9ca0c3fdde77..6c12e087f8016d77942b5982504b2379c5423e6b 100644 (file)
@@ -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)
 --------------------------
 
index 4abed14ebc699d430f02cd0940abd5754c358c96..cbf76b7aab2c8e168ae734d6b09f8055519c181d 100755 (executable)
@@ -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
 
 
index fc3325f516386dfd299758dfab8668d1ae6d9028..0c41d4b7c9e70eb675bce95661576509688eab83 100755 (executable)
@@ -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, '/')
index e10e9fe0e49fad48421354bce8ebd04915710ce6..7e27561be25f413e8b054d34239c36c2740962bd 100755 (executable)
@@ -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
index 1405e09dac6fe582a7c1ce3f0fd9efbe15cb0021..e4e2884c2c7d5a61642fb7324e7f55cd4db30fdd 100644 (file)
@@ -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 = []
index d0110207a1fad5cf7ef31220acef2fae4875885e..9991bb714f6fc9ea0653152f60a71929c4b2c0cd 100644 (file)
@@ -70,7 +70,7 @@ def search_authors_post():
 @route('/books-by-author/<id:int>/', 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))
index 45d907550eb83d19162224a59d8677355c4b8998..487aaaf4a1052a093923ed70b8ab6b80c286029a 100644 (file)
@@ -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''')
         
         ########################################
index e8b5e553289dfd256c6c5ce3377f46815e85ef39..d5e0c0ea8a019062ddfb204f427f23b417743b50 100644 (file)
@@ -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
index bca577d1049e3410dd33c90bf5bdbcbf5a845042..cc8f3f5705b88e8463a53c43a227d63b6564a1ba 100644 (file)
@@ -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
index 7cc32e187d9b913d46010e0b06b9cc275c4bb383..5cc0bc4a1b1a24aab281389f77f018be3cd67b59 100755 (executable)
@@ -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)