Failed Conditions
Pull Request — master (#2076)
by Abdeali
02:11
created

coalib/parsing/ConfParser.py (36 issues)

1
import os
2
from collections import OrderedDict
3
4
from coalib.misc import Constants
5
from coalib.parsing.LineParser import LineParser
6
from coalib.settings.Section import Section
7
from coalib.settings.Setting import Setting
8
9
10
class ConfParser:
11
12
    def __init__(self,
13
                 key_value_delimiters=('=',),
14
                 comment_seperators=('#',),
15
                 key_delimiters=(',', ' '),
16
                 section_name_surroundings=None,
17
                 remove_empty_iter_elements=True):
18
        section_name_surroundings = section_name_surroundings or {"[": "]"}
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable section_name_surroundings does not seem to be defined.
Loading history...
19
20
        self.line_parser = LineParser(key_value_delimiters,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable key_value_delimiters does not seem to be defined.
Loading history...
21
                                      comment_seperators,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable comment_seperators does not seem to be defined.
Loading history...
22
                                      key_delimiters,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable key_delimiters does not seem to be defined.
Loading history...
23
                                      section_name_surroundings)
24
25
        self.__remove_empty_iter_elements = remove_empty_iter_elements
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable remove_empty_iter_elements does not seem to be defined.
Loading history...
26
27
        # Declare it
28
        self.sections = None
29
        self.__rand_helper = None
30
        self.__init_sections()
31
32
    def parse(self, input_data, overwrite=False):
33
        """
34
        Parses the input and adds the new data to the existing.
35
36
        :param input_data: The filename to parse from.
37
        :param overwrite:  If True, wipes all existing Settings inside this
38
                           instance and adds only the newly parsed ones. If
39
                           False, adds the newly parsed data to the existing
40
                           one (and overwrites already existing keys with the
41
                           newly parsed values).
42
        :return:           A dictionary with (lowercase) section names as keys
43
                           and their Setting objects as values.
44
        """
45
        if os.path.isdir(input_data):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable input_data does not seem to be defined.
Loading history...
46
            input_data = os.path.join(input_data, Constants.default_coafile)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable Constants does not seem to be defined.
Loading history...
47
48
        with open(input_data, "r", encoding='utf-8') as _file:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable _file does not seem to be defined.
Loading history...
49
            lines = _file.readlines()
50
51
        if overwrite:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable overwrite does not seem to be defined.
Loading history...
52
            self.__init_sections()
53
54
        self.__parse_lines(lines, input_data)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable lines does not seem to be defined.
Loading history...
55
56
        return self.sections
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
57
58
    def get_section(self, name, create_if_not_exists=False):
59
        key = self.__refine_key(name)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable name does not seem to be defined.
Loading history...
60
        sec = self.sections.get(key, None)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable key does not seem to be defined.
Loading history...
61
        if sec is not None:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable sec does not seem to be defined.
Loading history...
62
            return sec
63
64
        if not create_if_not_exists:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable create_if_not_exists does not seem to be defined.
Loading history...
65
            raise IndexError
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable IndexError does not seem to be defined.
Loading history...
66
67
        retval = self.sections[key] = Section(str(name),
68
                                              self.sections["default"])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
69
        return retval
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable retval does not seem to be defined.
Loading history...
70
71
    @staticmethod
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable staticmethod does not seem to be defined.
Loading history...
72
    def __refine_key(key):
73
        return str(key).lower().strip()
74
75
    def __add_comment(self, section, comment, origin):
76
        key = "comment" + str(self.__rand_helper)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
77
        self.__rand_helper += 1
78
        section.append(Setting(
79
            key,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable key does not seem to be defined.
Loading history...
80
            comment,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable comment does not seem to be defined.
Loading history...
81
            origin,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable origin does not seem to be defined.
Loading history...
82
            remove_empty_iter_elements=self.__remove_empty_iter_elements))
83
84
    def __parse_lines(self, lines, origin):
85
        current_section_name = "default"
86
        current_section = self.get_section(current_section_name)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable current_section_name does not seem to be defined.
Loading history...
87
        current_keys = []
88
89
        for line in lines:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable lines does not seem to be defined.
Loading history...
90
            section_name, keys, value, comment = self.line_parser.parse(line)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable line does not seem to be defined.
Loading history...
91
92
            if comment != "":
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable comment does not seem to be defined.
Loading history...
93
                self.__add_comment(current_section, comment, origin)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable current_section does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable origin does not seem to be defined.
Loading history...
94
95
            if section_name != "":
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable section_name does not seem to be defined.
Loading history...
96
                current_section_name = section_name
97
                current_section = self.get_section(current_section_name, True)
98
                current_keys = []
99
                continue
100
101
            if comment == "" and keys == [] and value == "":
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable value does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable keys does not seem to be defined.
Loading history...
102
                self.__add_comment(current_section, "", origin)
103
                continue
104
105
            if keys != []:
106
                current_keys = keys
107
108
            for section_override, key in current_keys:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable current_keys does not seem to be defined.
Loading history...
109
                if key == "":
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable key does not seem to be defined.
Loading history...
110
                    continue
111
112
                if section_override == "":
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable section_override does not seem to be defined.
Loading history...
113
                    current_section.add_or_create_setting(
114
                        Setting(key,
115
                                value,
116
                                origin,
117
                                # Ignore PEP8Bear, it fails to format that
118
                                remove_empty_iter_elements=
119
                                self.__remove_empty_iter_elements),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
120
                        allow_appending=(keys == []))
121
                else:
122
                    self.get_section(
123
                        section_override,
124
                        True).add_or_create_setting(
125
                            Setting(key,
126
                                    value,
127
                                    origin,
128
                                    # Ignore PEP8Bear, it fails to format that
129
                                    remove_empty_iter_elements=
130
                                    self.__remove_empty_iter_elements),
131
                            allow_appending=(keys == []))
132
133
    def __init_sections(self):
134
        self.sections = OrderedDict()
135
        self.sections["default"] = Section("Default")
136
        self.__rand_helper = 0
137