1
|
1 |
|
from plugin.core.environment import Environment |
2
|
|
|
|
3
|
1 |
|
from ConfigParser import NoOptionError, NoSectionError, ParsingError, SafeConfigParser |
4
|
1 |
|
import logging |
5
|
1 |
|
import os |
6
|
|
|
|
7
|
1 |
|
log = logging.getLogger(__name__) |
8
|
|
|
|
9
|
1 |
|
CONFIGURATION_FILES = [ |
10
|
|
|
'advanced' |
11
|
|
|
] |
12
|
|
|
|
13
|
|
|
|
14
|
1 |
|
class ConfigurationFile(object): |
15
|
1 |
|
def __init__(self, path): |
16
|
1 |
|
self._path = path |
17
|
1 |
|
self._relpath = os.path.relpath(self._path, Environment.path.plugin_support) |
18
|
|
|
|
19
|
1 |
|
self._parser = None |
20
|
1 |
|
self._error = False |
21
|
|
|
|
22
|
1 |
|
def __getitem__(self, section): |
23
|
|
|
# Ensure file is loaded |
24
|
1 |
|
self.load() |
25
|
|
|
|
26
|
|
|
# Construct section |
27
|
1 |
|
return ConfigurationSection(self._parser, section) |
28
|
|
|
|
29
|
1 |
|
def load(self): |
30
|
1 |
|
if self._parser or self._error: |
31
|
1 |
|
return |
32
|
|
|
|
33
|
1 |
|
log.debug('Parsing configuration file: %r', self._relpath) |
34
|
|
|
|
35
|
1 |
|
try: |
36
|
1 |
|
self._parser = SafeConfigParser() |
37
|
1 |
|
self._parser.read(self._path) |
38
|
|
|
except ParsingError, ex: |
39
|
|
|
log.info(ex.message) |
40
|
|
|
|
41
|
|
|
self._parser = None |
42
|
|
|
self._error = True |
43
|
|
|
except Exception, ex: |
44
|
|
|
log.warn('Unable to parse configuration file: %r - %s', self._relpath, ex, exc_info=True) |
45
|
|
|
|
46
|
|
|
self._parser = None |
47
|
|
|
self._error = True |
48
|
|
|
|
49
|
|
|
|
50
|
1 |
|
class ConfigurationSection(object): |
51
|
1 |
|
def __init__(self, parser, name): |
52
|
1 |
|
self._parser = parser |
53
|
1 |
|
self._name = name |
54
|
|
|
|
55
|
1 |
|
def _get(self, func, key, default=None): |
56
|
1 |
|
if not self._parser: |
57
|
|
|
return default |
58
|
|
|
|
59
|
1 |
|
try: |
60
|
1 |
|
return getattr(self._parser, func)(self._name, key) |
61
|
1 |
|
except (NoSectionError, NoOptionError): |
62
|
1 |
|
return default |
63
|
|
|
|
64
|
1 |
|
def get(self, key, default=None): |
65
|
1 |
|
return self._get('get', key, default) |
66
|
|
|
|
67
|
1 |
|
def get_int(self, key, default=None): |
68
|
|
|
return self._get('getint', key, default) |
69
|
|
|
|
70
|
1 |
|
def get_float(self, key, default=None): |
71
|
|
|
return self._get('getfloat', key, default) |
72
|
|
|
|
73
|
1 |
|
def get_boolean(self, key, default=None): |
74
|
1 |
|
return self._get('getboolean', key, default) |
75
|
|
|
|
76
|
1 |
|
def __getitem__(self, key): |
77
|
|
|
if not self._parser: |
78
|
|
|
return None |
79
|
|
|
|
80
|
|
|
return self._parser.get(self._name, key) |
81
|
|
|
|
82
|
1 |
|
def __setitem__(self, key, value): |
83
|
|
|
if not self._parser: |
84
|
|
|
return |
85
|
|
|
|
86
|
|
|
self._parser.set(self._name, key, value) |
87
|
|
|
|
88
|
|
|
|
89
|
1 |
|
class ConfigurationMeta(type): |
90
|
1 |
|
def __new__(cls, name, parents, dct): |
91
|
|
|
# Load configuration files |
92
|
1 |
|
for name in CONFIGURATION_FILES: |
93
|
|
|
# Build path |
94
|
1 |
|
path = os.path.join(Environment.path.plugin_data, '%s.ini' % name) |
95
|
|
|
|
96
|
|
|
# Parse configuration file |
97
|
1 |
|
dct[name] = ConfigurationFile(path) |
98
|
|
|
|
99
|
|
|
# Construct object |
100
|
1 |
|
return super(ConfigurationMeta, cls).__new__(cls, name, parents, dct) |
101
|
|
|
|
102
|
|
|
|
103
|
1 |
|
class Configuration(object): |
104
|
1 |
|
__metaclass__ = ConfigurationMeta |
105
|
|
|
|
106
|
|
|
advanced = None |
107
|
|
|
|