From 071f22093f133ede991d6125905e07a25f4c7ec6 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sun, 20 Dec 2015 01:01:02 +0300 Subject: [PATCH] Add configuration file parser and sample --- m_librarian/config.py | 28 ++++++++++++++++++++++++++++ sample/m_librarian.conf | 10 ++++++++++ 2 files changed, 38 insertions(+) create mode 100755 m_librarian/config.py create mode 100644 sample/m_librarian.conf diff --git a/m_librarian/config.py b/m_librarian/config.py new file mode 100755 index 0000000..f1dfdb7 --- /dev/null +++ b/m_librarian/config.py @@ -0,0 +1,28 @@ +#! /usr/bin/env python + +__all__ = ['ml_conf'] + +import os +from ConfigParser import SafeConfigParser + +config_dirs = [] +if 'XDG_CONFIG_HOME' in os.environ: + config_dirs.append(os.environ['XDG_CONFIG_HOME']) +if 'XDG_CONFIG_DIRS' in os.environ: + config_dirs.extend(os.environ['XDG_CONFIG_DIRS'].split(':')) +home_config = os.path.expanduser('~/.config') +if home_config not in config_dirs: + config_dirs.append(home_config) + +for d in config_dirs: + ml_conf_file = os.path.join(d, 'm_librarian.conf') + if os.path.exists(ml_conf_file): + ml_conf = SafeConfigParser() + ml_conf.read(ml_conf_file) + break +else: + ml_conf = ml_conf_file = None + +if __name__ == '__main__': + print "Config dirs:", config_dirs + print "Config file:", ml_conf_file diff --git a/sample/m_librarian.conf b/sample/m_librarian.conf new file mode 100644 index 0000000..17fc310 --- /dev/null +++ b/sample/m_librarian.conf @@ -0,0 +1,10 @@ +[database] +; DB URI syntax examples: + +; URI = mysql://host/database?debug=1 +; URI = mysql://user:password@host/database +; URI = postgres:///full/path/to/socket/database +; URI = postgres://host:5432/database +; URI = postgres://user@host/database?debug=&cache= +; URI = sqlite:///full/path/to/database +; URI = sqlite:/C:/full/path/to/database -- 2.39.2