Test Failed
Push — develop ( 20bb73...3c901c )
by Dean
02:47
created

ConfigurationMeta   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 12
Duplicated Lines 0 %
Metric Value
dl 0
loc 12
rs 10
wmc 2

1 Method

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