Completed
Pull Request — master (#127)
by Jasper
01:07
created

niprov.Configuration.__init__()   C

Complexity

Conditions 13

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 13
dl 0
loc 19
rs 5.2936

How to fix   Complexity   

Complexity

Complex classes like niprov.Configuration.__init__() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import os, ConfigParser
2
3
4
class Configuration(object):
5
    """General settings for niprov.
6
7
    Individual settings are documented as follows;
8
9
    **setting** *= default_value*
10
        *type* - Explanation.
11
12
    The settings can be changed in the configuration file, or in code.
13
14
    All settings:
15
    """
16
    database_type = 'file'
17
    """str: Type of backend in which to store provenance. Currently only 'file' 
18
    or 'MongoDB'
19
    """
20
21
    database_url = '~/provenance.json'
22
    """str: URL of the database. If ``database-type`` is ``file``, this is the 
23
    path to the file."""
24
25
    dryrun = False
26
    """bool: Do not execute commands or make lasting changes to the 
27
    provenance database."""
28
29
    verbosity = 'info'
30
    """string: Level of information to report. One of 'debug','info','warning',
31
    'error'. Any level includes higher levels, i.e. 'info' will log messages of 
32
    that are deemed 'info', 'warning' or 'error'. """
33
34
    discover_file_extensions = ['.PAR','.dcm','.fif','.cnt']
35
    """list: Discover uses this to determine which files to include. 
36
    Not strictly extensions, can be any string that appears in the file name. 
37
    Use comma's to separate items."""
38
39
    attach = False
40
    """bool: Attach provenance to image files. For nifti files for instance,
41
    this means inserting a header extension with serialized provenance. See 
42
    'attach_format' to configure which data format is used."""
43
44
    attach_format = 'json'
45
    """string: Format in which to attach provenance to the file. One of 'json',
46
    or 'xml'.
47
    For example, if set to 'json' and the 'attach' option is True, this will 
48
    add a header extension to nifti files created with the relevant provenance 
49
    data in json format."""
50
51
    def __init__(self, configFilePath='~/niprov.cfg'):
52
        configFilePath = os.path.expanduser(configFilePath)
53
        if os.path.isfile(configFilePath):
54
            keys = [k for k in dir(self) if k[0] != '_']
55
            defaults = {k:getattr(self, k) for k in keys}
56
            types = {k:type(defaults[k]) for k in keys}
57
            parser = ConfigParser.SafeConfigParser()
58
            parser.read(configFilePath)
59
            for key in keys:
60
                if not parser.has_option('main', key):
61
                    val = defaults[key]
62
                elif types[key] is str:
63
                    val = parser.get('main', key)
64
                elif types[key] is bool:
65
                    val = parser.getboolean('main', key)
66
                elif types[key] is list:
67
                    items = parser.get('main', key).split(',')
68
                    val = [i.strip() for i in items if i is not '']
69
                setattr(self, key, val)
70