Completed
Push — master ( 9d3bc0...b059cb )
by Oleksandr
01:07
created

Console.default()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 6
Ratio 100 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 6
loc 6
ccs 1
cts 2
cp 0.5
rs 9.4285
cc 1
crap 1.125
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 .interfaces import INISerializable, DefaultProvider
10 1
from .helpers import field_from_ini
11
12
13 1 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...
14 1
@zope.interface.implementer(DefaultProvider)
15 1
class Connection(Model):
16 1
    port = IntType(
17
        min_value=0,
18
        max_value=65000,
19
        default=0,
20
        required=True,
21
    )
22 1
    allowed_hosts = ListType(
23
        field=StringType,
24
        min_size=0,
25
        required=True,
26
        min_length=1,
27
    )
28
29 1
    @classmethod
30
    def from_ini(cls, ini):
31
        return cls({
32
            'port': field_from_ini(
33
                cls.port, ini,
34
                'Console', 'IP',
35
            ),
36
            'allowed_hosts': (
37
                field_from_ini(
38
                    cls.allowed_hosts, ini,
39
                    'Console', 'IPS', default="",
40
                )
41
                .split()
42
            ),
43
        })
44
45 1
    @classmethod
46
    def default(cls):
47
        return cls({
48
            'port': cls.port.default,
49
            'allowed_hosts': [],
50
        })
51
52
53 1
@zope.interface.implementer(INISerializable)
54 1
@zope.interface.implementer(DefaultProvider)
55 1
class Logging(Model):
56 1
    is_enabled = BooleanType(
57
        default=False,
58
        required=True,
59
    )
60 1
    file_name = StringType(
61
        default="log.lst",
62
        min_length=1,
63
        required=True,
64
    )
65 1
    keep = BooleanType(
66
        default=True,
67
        required=True,
68
    )
69 1
    log_time = BooleanType(
70
        default=False,
71
        required=True,
72
    )
73
74 1
    @classmethod
75
    def from_ini(cls, ini):
76
        return cls({
77
            'is_enabled': field_from_ini(
78
                cls.is_enabled, ini,
79
                'Console', 'LOG',
80
            ),
81
            'file_name': field_from_ini(
82
                cls.file_name, ini,
83
                'Console', 'LOGFILE',
84
            ),
85
            'log_time': field_from_ini(
86
                cls.log_time, ini,
87
                'Console', 'LOGTIME',
88
            ),
89
            'keep': field_from_ini(
90
                cls.keep, ini,
91
                'Console', 'LOGKEEP',
92
            ),
93
        })
94
95 1
    @classmethod
96
    def default(cls):
97
        return cls({
98
            field_name: field.default
99
            for field_name, field in cls.fields.items()
100
        })
101
102
103 1 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...
104 1
@zope.interface.implementer(DefaultProvider)
105 1
class HistorySize(Model):
106 1
    commands = IntType(
107
        min_value=0,
108
        max_value=10000,
109
        default=128,
110
        required=True,
111
    )
112 1
    records = IntType(
113
        min_value=0,
114
        max_value=10000,
115
        default=128,
116
        required=True,
117
    )
118
119 1
    @classmethod
120
    def from_ini(cls, ini):
121
        return cls({
122
            'commands': field_from_ini(
123
                cls.commands, ini,
124
                'Console', 'HISTORYCMD',
125
            ),
126
            'records': field_from_ini(
127
                cls.records, ini,
128
                'Console', 'HISTORY',
129
            ),
130
        })
131
132 1
    @classmethod
133
    def default(cls):
134
        return cls({
135
            field_name: field.default
136
            for field_name, field in cls.fields.items()
137
        })
138
139
140 1 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...
141 1
@zope.interface.implementer(DefaultProvider)
142 1
class Console(Model):
143 1
    connection = ModelType(
144
        model_spec=Connection,
145
        required=True,
146
    )
147 1
    logging = ModelType(
148
        model_spec=Logging,
149
        required=True,
150
    )
151 1
    history_size = ModelType(
152
        model_spec=HistorySize,
153
        required=True,
154
    )
155
156 1
    @classmethod
157
    def from_ini(cls, ini):
158
        return cls({
159
            'connection': Connection.from_ini(ini),
160
            'logging': Logging.from_ini(ini),
161
            'history_size': HistorySize.from_ini(ini),
162
        })
163
164 1
    @classmethod
165
    def default(cls):
166
        return cls({
167
            'connection': Connection.default(),
168
            'logging': Logging.default(),
169
            'history_size': HistorySize.default(),
170
        })
171