Passed
Push — develop ( 1eb88a...c50d7f )
by Dean
02:41
created

ConfigurationSection   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 55.17%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
dl 0
loc 40
ccs 16
cts 29
cp 0.5517
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 3 1
A get_boolean() 0 2 1
A get() 0 2 1
A get_float() 0 2 1
A __getitem__() 0 5 2
A _get() 0 11 4
A __setitem__() 0 5 2
A get_int() 0 2 1
1 1
from plugin.core.environment import Environment
2
3 1
from ConfigParser import NoOptionError, NoSectionError, ParsingError, SafeConfigParser
0 ignored issues
show
Configuration introduced by
The import ConfigParser could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
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 as ex:
39
            log.info(ex.message)
40
41
            self._parser = None
42
            self._error = True
43
        except Exception as ex:
0 ignored issues
show
Best Practice introduced by
Catching very general exceptions such as Exception is usually not recommended.

Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.

So, unless you specifically plan to handle any error, consider adding a more specific exception.

Loading history...
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
        if not self._parser.has_option(self._name, key):
60 1
            return default
61
62
        try:
63
            return getattr(self._parser, func)(self._name, key)
64
        except (NoSectionError, NoOptionError):
65
            return default
66
67 1
    def get(self, key, default=None):
68 1
        return self._get('get', key, default)
69
70 1
    def get_int(self, key, default=None):
71
        return self._get('getint', key, default)
72
73 1
    def get_float(self, key, default=None):
74
        return self._get('getfloat', key, default)
75
76 1
    def get_boolean(self, key, default=None):
77 1
        return self._get('getboolean', key, default)
78
79 1
    def __getitem__(self, key):
80
        if not self._parser:
81
            return None
82
83
        return self._parser.get(self._name, key)
84
85 1
    def __setitem__(self, key, value):
86
        if not self._parser:
87
            return
88
89
        self._parser.set(self._name, key, value)
90
91
92 1
class ConfigurationMeta(type):
93 1
    def __new__(cls, name, parents, dct):
0 ignored issues
show
Coding Style Best Practice introduced by
The first argument of the __new__ class method of metaclasses should be named mcs by convention.
Loading history...
94
        # Load configuration files
95 1
        for name in CONFIGURATION_FILES:
96
            # Build path
97 1
            path = os.path.join(Environment.path.plugin_data, '%s.ini' % name)
98
99
            # Parse configuration file
100 1
            dct[name] = ConfigurationFile(path)
101
102
        # Construct object
103 1
        return super(ConfigurationMeta, cls).__new__(cls, name, parents, dct)
104
105
106 1
class Configuration(object):
107 1
    __metaclass__ = ConfigurationMeta
108
109
    advanced = None
110