]> git.phdru.name Git - m_librarian.git/blob - m_librarian/config.py
Feat(config): getlist
[m_librarian.git] / m_librarian / config.py
1 #! /usr/bin/env python
2
3 from __future__ import print_function
4 import os
5 try:
6     from ConfigParser import RawConfigParser, NoSectionError, NoOptionError
7 except ImportError:  # py3
8     from configparser import RawConfigParser, NoSectionError, NoOptionError
9
10
11 __all__ = ['get_config']
12
13
14 def _find_config_dirs_posix():
15     config_dirs = []
16     if 'XDG_CONFIG_HOME' in os.environ:
17         config_dirs.append(os.environ['XDG_CONFIG_HOME'])
18     if 'XDG_CONFIG_DIRS' in os.environ:
19         config_dirs.extend(os.environ['XDG_CONFIG_DIRS'].split(':'))
20     home_config = os.path.expanduser('~/.config')
21     if home_config not in config_dirs:
22         config_dirs.append(home_config)
23     return config_dirs
24
25
26 def find_config_dirs():
27     if os.name == 'posix':
28         return _find_config_dirs_posix()
29     return None
30
31
32 def find_config_file(config_dirs=None):
33     if config_dirs is None:
34         config_dirs = find_config_dirs()
35     for d in config_dirs:
36         ml_conf_file = os.path.join(d, 'm_librarian.conf')
37         if os.path.exists(ml_conf_file):
38             return ml_conf_file
39     else:
40         return None
41
42
43 _ml_config = None
44
45
46 class ConfigWrapper(object):
47     def __init__(self, config):
48         self.config = config
49
50     def get(self, section, option, default=None):
51         try:
52             return self.config.get(section, option)
53         except (NoSectionError, NoOptionError):
54             return default
55
56     def set(self, section, option, value):
57         self.config.set(section, option, value)
58
59     def getint(self, section, option, default=0):
60         try:
61             return self.config.getint(section, option)
62         except (NoSectionError, NoOptionError):
63             return default
64         # Do not catch ValueError here, it must be propagated
65
66     def getlist(self, section, option, default=None, sep=None):
67         value = self.get(section, option)
68         if not value:
69             if default is None:
70                 return []
71             return default
72         return value.split(sep)
73
74     def getpath(self, section, option, default=os.path.curdir):
75         path = self.get(section, option, default=default)
76         return os.path.expanduser(os.path.expandvars(path))
77
78
79 def get_config(config_path=None):
80     global _ml_config
81     if _ml_config is None:
82         _ml_config = RawConfigParser()
83         if config_path is None:
84             config_path = find_config_file()
85         if config_path is not None:
86             _ml_config.read(config_path)
87         _ml_config = ConfigWrapper(_ml_config)
88     return _ml_config
89
90
91 def test():
92     config_dirs = find_config_dirs()
93     print("Config dirs:", config_dirs)
94     print("Config file:", find_config_file(config_dirs))
95
96
97 if __name__ == '__main__':
98     test()