From 63bb5df8cf2595bbe106031911666fb7061a03aa Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Mon, 11 Jun 2018 00:51:29 +0300 Subject: [PATCH] Feat(config): Environment variables are expanded in paths --- docs-ru/news.rst | 3 +++ docs/news.rst | 2 ++ m_librarian/config.py | 4 ++++ m_librarian/download.py | 2 +- m_librarian/web/app.py | 2 +- scripts/ml-search.py | 3 +-- 6 files changed, 12 insertions(+), 4 deletions(-) diff --git a/docs-ru/news.rst b/docs-ru/news.rst index 3269fa1..558bc5c 100644 --- a/docs-ru/news.rst +++ b/docs-ru/news.rst @@ -10,6 +10,9 @@ Version 0.1.4 (2018-05-??) * Файл конфигурации, все секции и все ключи сделаны необязательными. +* В файле конфигурации подставляются переменные окружения в ключах, + указывающих пути. + Version 0.1.3 (2018-05-25) -------------------------- diff --git a/docs/news.rst b/docs/news.rst index 6c12e08..8fd1e7b 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -10,6 +10,8 @@ Version 0.1.4 (2018-06-??) * Config file, all sections and all key are now completely optional. +* Environment variables are expanded in paths in config file. + Version 0.1.3 (2018-05-25) -------------------------- diff --git a/m_librarian/config.py b/m_librarian/config.py index cbf76b7..9fff407 100755 --- a/m_librarian/config.py +++ b/m_librarian/config.py @@ -63,6 +63,10 @@ class ConfigWrapper(object): return default # Do not catch ValueError here, it must be propagated + def getpath(self, section, option, default=os.path.curdir): + path = self.get(section, option, default=default) + return os.path.expanduser(os.path.expandvars(path)) + def get_config(config_path=None): global _ml_config diff --git a/m_librarian/download.py b/m_librarian/download.py index 7e27561..6304658 100755 --- a/m_librarian/download.py +++ b/m_librarian/download.py @@ -67,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') or '.' + _library_path = get_config().getpath('library', 'path') lib_path = _library_path global format, compile_format, compiled_format diff --git a/m_librarian/web/app.py b/m_librarian/web/app.py index 9991bb7..42c5c5a 100644 --- a/m_librarian/web/app.py +++ b/m_librarian/web/app.py @@ -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') or '.' + download_path = get_config().getpath('download', 'path') if books_ids: for id in books_ids: book = Book.get(int(id)) diff --git a/scripts/ml-search.py b/scripts/ml-search.py index 5cc0bc4..24db982 100755 --- a/scripts/ml-search.py +++ b/scripts/ml-search.py @@ -203,8 +203,7 @@ 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: - download_to = get_config().get('download', 'path') \ - or os.path.curdir + download_to = get_config().getpath('download', 'path') download(book, download_to, args.path, args.format) count += 1 print_count(count) -- 2.39.2