Console.default()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 6
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 6
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
crap 2
1
# coding: utf-8
2
3 1
import zope.interface
4
5 1
from schematics.models import Model
6 1
from schematics.types import StringType, IntType, BooleanType
7 1
from schematics.types.compound import ListType, ModelType
8
9 1
from .constants import FORBID_CONSOLE_CONNECTIONS_FLAG
10 1
from .interfaces import INISerializable, DefaultProvider
11
from .helpers import field_from_ini, field_to_ini
12
13 1
14 1
@zope.interface.implementer(INISerializable)
15 1
@zope.interface.implementer(DefaultProvider)
16 1
class Connection(Model):
17
    port = IntType(
18
        min_value=1000,
19
        max_value=65000,
20
        default=None,
21
        required=False,
22 1
    )
23
    allowed_hosts = ListType(
24
        field=StringType,
25
        min_size=0,
26
        required=True,
27
        min_length=1,
28
    )
29 1
30
    @classmethod
31
    def from_ini(cls, ini):
32
        port = field_from_ini(
33
            cls.port, ini,
34
            'Console', 'IP',
35
        )
36
        port = (
37
            None
38
            if port == FORBID_CONSOLE_CONNECTIONS_FLAG
39
            else port
40
        )
41
42
        return cls({
43
            'port': port,
44
            'allowed_hosts': (
45 1
                field_from_ini(
46
                    cls.allowed_hosts, ini,
47
                    'Console', 'IPS', default="",
48
                )
49
                .split()
50
            ),
51
        })
52
53 1
    def to_ini(self, ini):
54 1
        port = (
55 1
            FORBID_CONSOLE_CONNECTIONS_FLAG
56 1
            if self.port is None
57
            else self.port
58
        )
59
        allowed_hosts = ' '.join(self.allowed_hosts)
60 1
61
        field_to_ini(port, ini, 'Console', 'IP')
62
        field_to_ini(allowed_hosts, ini, 'Console', 'IPS')
63
64
    @classmethod
65 1
    def default(cls):
66
        return cls({
67
            'port': cls.port.default,
68
            'allowed_hosts': [],
69 1
        })
70
71
72 View Code Duplication
@zope.interface.implementer(INISerializable)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
73
@zope.interface.implementer(DefaultProvider)
74 1
class Logging(Model):
75
    enabled = BooleanType(
76
        default=False,
77
        required=True,
78
    )
79
    file_name = StringType(
80
        default="log.lst",
81
        min_length=1,
82
        required=True,
83
    )
84
    keep_file = BooleanType(
85
        default=True,
86
        required=True,
87
    )
88
    log_time = BooleanType(
89
        default=False,
90
        required=True,
91
    )
92
93
    @classmethod
94
    def from_ini(cls, ini):
95 1
        return cls({
96
            'enabled': field_from_ini(
97
                cls.enabled, ini,
98
                'Console', 'LOG',
99
            ),
100
            'file_name': field_from_ini(
101
                cls.file_name, ini,
102
                'Console', 'LOGFILE',
103 1
            ),
104 1
            'log_time': field_from_ini(
105 1
                cls.log_time, ini,
106 1
                'Console', 'LOGTIME',
107
            ),
108
            'keep_file': field_from_ini(
109
                cls.keep_file, ini,
110
                'Console', 'LOGKEEP',
111
            ),
112 1
        })
113
114
    def to_ini(self, ini):
115
        field_to_ini(self.enabled, ini, 'Console', 'LOG')
116
        field_to_ini(self.file_name, ini, 'Console', 'LOGFILE')
117
        field_to_ini(self.log_time, ini, 'Console', 'LOGTIME')
118
        field_to_ini(self.keep_file, ini, 'Console', 'LOGKEEP')
119 1
120
    @classmethod
121
    def default(cls):
122
        return cls({
123
            field_name: field.default
124
            for field_name, field in cls.fields.items()
125
        })
126
127
128
@zope.interface.implementer(INISerializable)
129
@zope.interface.implementer(DefaultProvider)
130
class History(Model):
131
    max_commands = IntType(
132 1
        min_value=0,
133
        max_value=10000,
134
        default=128,
135
        required=True,
136
    )
137
    max_records = IntType(
138
        min_value=0,
139
        max_value=10000,
140 1
        default=128,
141 1
        required=True,
142 1
    )
143 1
144
    @classmethod
145
    def from_ini(cls, ini):
146
        return cls({
147 1
            'max_commands': field_from_ini(
148
                cls.max_commands, ini,
149
                'Console', 'HISTORYCMD',
150
            ),
151 1
            'max_records': field_from_ini(
152
                cls.max_records, ini,
153
                'Console', 'HISTORY',
154
            ),
155
        })
156 1
157
    def to_ini(self, ini):
158
        field_to_ini(self.max_commands, ini, 'Console', 'HISTORYCMD')
159
        field_to_ini(self.max_records, ini, 'Console', 'HISTORY')
160
161
    @classmethod
162
    def default(cls):
163
        return cls({
164 1
            field_name: field.default
165
            for field_name, field in cls.fields.items()
166
        })
167
168
169 View Code Duplication
@zope.interface.implementer(INISerializable)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
170
@zope.interface.implementer(DefaultProvider)
171
class Console(Model):
172
    connection = ModelType(
173
        model_spec=Connection,
174
        required=True,
175
    )
176
    logging = ModelType(
177
        model_spec=Logging,
178
        required=True,
179
    )
180
    history = ModelType(
181
        model_spec=History,
182
        required=True,
183
    )
184
185
    @classmethod
186
    def from_ini(cls, ini):
187
        return cls({
188
            'connection': Connection.from_ini(ini),
189
            'logging': Logging.from_ini(ini),
190
            'history': History.from_ini(ini),
191
        })
192
193
    def to_ini(self, ini):
194
        for field_name in self.iter():
195
            self[field_name].to_ini(ini)
196
197
    @classmethod
198
    def default(cls):
199
        return cls({
200
            'connection': Connection.default(),
201
            'logging': Logging.default(),
202
            'history': History.default(),
203
        })
204