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

coalib/output/ConfWriter.py (28 issues)

1
from itertools import chain
2
3
from pyprint.ClosableObject import ClosableObject
4
5
from coalib.parsing.StringProcessing import escape
6
from coalib.settings.Section import Section
7
8
9
class ConfWriter(ClosableObject):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ClosableObject does not seem to be defined.
Loading history...
10
11
    def __init__(self,
12
                 file_name,
13
                 key_value_delimiters=('=',),
14
                 comment_seperators=('#',),
15
                 key_delimiters=(',', ' '),
16
                 section_name_surroundings=None,
17
                 section_override_delimiters=(".",),
18
                 unsavable_keys=("save",)):
19
        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...
20
        ClosableObject.__init__(self)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
21
        self.__file_name = file_name
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable file_name does not seem to be defined.
Loading history...
22
        self.__file = open(self.__file_name, "w")
23
        self.__key_value_delimiters = 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...
24
        self.__comment_seperators = comment_seperators
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable comment_seperators does not seem to be defined.
Loading history...
25
        self.__key_delimiters = key_delimiters
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable key_delimiters does not seem to be defined.
Loading history...
26
        self.__section_name_surroundings = section_name_surroundings
27
        self.__section_override_delimiters = section_override_delimiters
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable section_override_delimiters does not seem to be defined.
Loading history...
28
        self.__unsavable_keys = unsavable_keys
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable unsavable_keys does not seem to be defined.
Loading history...
29
        self.__wrote_newline = True
30
        self.__closed = False
31
32
        self.__key_delimiter = self.__key_delimiters[0]
33
        self.__key_value_delimiter = self.__key_value_delimiters[0]
34
        (self.__section_name_surrounding_beg,
35
         self.__section_name_surrounding_end) = (
36
            tuple(self.__section_name_surroundings.items())[0])
37
38
    def _close(self):
39
        self.__file.close()
40
41
    def write_sections(self, sections):
42
        assert not self.__closed
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
43
44
        self.__wrote_newline = True
45
        for section in sections:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable sections does not seem to be defined.
Loading history...
46
            self.write_section(sections[section])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable section does not seem to be defined.
Loading history...
47
48
    def write_section(self, section):
49
        assert not self.__closed
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
50
51
        if not isinstance(section, Section):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable Section does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable section does not seem to be defined.
Loading history...
52
            raise TypeError
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable TypeError does not seem to be defined.
Loading history...
53
54
        self.__write_section_name(section.name)
55
56
        keys = []
57
        val = None
58
        section_iter = section.__iter__(ignore_defaults=True)
59
        try:
60
            while True:
61
                setting = section[next(section_iter)]
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable section_iter does not seem to be defined.
Loading history...
62
                if (str(setting) == val and
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable setting does not seem to be defined.
Loading history...
63
                    not self.is_comment(setting.key) and
64
                    (
65
                        (setting.key not in self.__unsavable_keys) or
66
                        (not setting.from_cli))):
67
                    keys.append(setting.key)
68
                elif ((setting.key not in self.__unsavable_keys) or
69
                      (not setting.from_cli)):
70
                    self.__write_key_val(keys, val)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable val does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable keys does not seem to be defined.
Loading history...
71
                    keys = [setting.key]
72
                    val = str(setting)
73
        except StopIteration:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable StopIteration does not seem to be defined.
Loading history...
74
            self.__write_key_val(keys, val)
75
76
    def __write_section_name(self, name):
77
        assert not self.__closed
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
78
79
        if not self.__wrote_newline:
80
            self.__file.write("\n")
81
82
        self.__file.write(self.__section_name_surrounding_beg + name +
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable name does not seem to be defined.
Loading history...
83
                          self.__section_name_surrounding_end + '\n')
84
        self.__wrote_newline = False
85
86
    def __write_key_val(self, keys, val):
87
        assert not self.__closed
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
88
89
        if keys == []:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable keys does not seem to be defined.
Loading history...
90
            return
91
92
        if all(self.is_comment(key) for key in keys):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable key does not seem to be defined.
Loading history...
93
            self.__file.write(val + "\n")
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable val does not seem to be defined.
Loading history...
94
            self.__wrote_newline = val == ""
95
            return
96
97
        # Add escape characters as appropriate
98
        keys = [escape(key, chain(['\\'],
99
                                  self.__key_value_delimiters,
100
                                  self.__comment_seperators,
101
                                  self.__key_delimiters,
102
                                  self.__section_override_delimiters))
103
                for key in keys]
104
        val = escape(val, chain(['\\'], self.__comment_seperators))
105
106
        self.__file.write((self.__key_delimiter + " ").join(keys) + " " +
107
                          self.__key_value_delimiter + " " + val + "\n")
108
        self.__wrote_newline = False
109
110
    @staticmethod
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable staticmethod does not seem to be defined.
Loading history...
111
    def is_comment(key):
112
        return key.lower().startswith("comment")
113