From: Oleg Broytman Date: Tue, 19 Dec 2023 12:48:17 +0000 (+0300) Subject: Feat(wx): Find session config X-Git-Tag: 0.3.0~9^2~39 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=786c61174105cd361c81d3214e93e3881f5507e9;p=m_librarian.git Feat(wx): Find session config [skip ci] --- diff --git a/m_librarian/config.py b/m_librarian/config.py index b4a7b60..a6b88f0 100755 --- a/m_librarian/config.py +++ b/m_librarian/config.py @@ -29,11 +29,11 @@ def find_config_dirs(): return None -def find_config_file(config_dirs=None): +def find_config_file(config_dirs=None, config_filename='m_librarian.conf'): if config_dirs is None: config_dirs = find_config_dirs() for d in config_dirs: - ml_conf_file = os.path.join(d, 'm_librarian.conf') + ml_conf_file = os.path.join(d, config_filename) if os.path.exists(ml_conf_file): return ml_conf_file else: diff --git a/m_librarian/wx/session_config.py b/m_librarian/wx/session_config.py new file mode 100755 index 0000000..315c107 --- /dev/null +++ b/m_librarian/wx/session_config.py @@ -0,0 +1,53 @@ +#! /usr/bin/env python + +from __future__ import print_function +import os + +from ..config import RawConfigParser, ConfigWrapper, find_config_file + + +__all__ = ['get_session_config'] + + +def _find_config_dirs_posix(): + config_dirs = [] + if 'XDG_CACHE_HOME' in os.environ: + config_dirs.append(os.environ['XDG_CACHE_HOME']) + home_cache = os.path.expanduser('~/.cache') + if home_cache not in config_dirs: + config_dirs.append(home_cache) + return config_dirs + + +def _find_config_dirs(): + if os.name == 'posix': + return _find_config_dirs_posix() + return None + + +_ml_session_config = None + + +def get_session_config(config_path=None): + global _ml_session_config + if _ml_session_config is None: + _ml_session_config = RawConfigParser() + if config_path is None: + config_dirs = _find_config_dirs() + config_path = \ + find_config_file(config_dirs, 'm_librarian_session.conf') + if config_path is not None: + _ml_session_config.read(config_path) + _ml_session_config = ConfigWrapper(_ml_session_config) + return _ml_session_config + + +def _test(): + config_dirs = _find_config_dirs() + print("Config dirs:", config_dirs) + print("Config file:", + find_config_file(config_dirs, 'm_librarian_session.conf')) + + +if __name__ == '__main__': + _test()