DeviceLink   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 4
c 2
b 0
f 1
dl 0
loc 22
ccs 0
cts 0
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A default() 0 4 1
A from_ini() 0 4 1
A to_ini() 0 3 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
7 1
from schematics.types.compound import ListType, ModelType
8
9 1
from .constants import FORBID_DEVICE_LINK_CONNECTIONS_FLAG
10 1
from .interfaces import INISerializable, DefaultProvider
11
from .helpers import field_from_ini, field_to_ini
12
13 1
14 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...
15 1
@zope.interface.implementer(DefaultProvider)
16 1
class Connection(Model):
17
    host = StringType(
18
        default="",
19
        required=True,
20 1
    )
21
    port = IntType(
22
        min_value=1000,
23
        max_value=65000,
24
        default=None,
25
        required=False,
26 1
    )
27
    allowed_hosts = ListType(
28
        field=StringType,
29
        min_size=0,
30
        required=True,
31
        min_length=1,
32
    )
33 1
34
    @classmethod
35
    def from_ini(cls, ini):
36
        port = field_from_ini(
37
            cls.port, ini,
38
            'DeviceLink', 'port',
39
        )
40
        port = (
41
            None
42
            if port == FORBID_DEVICE_LINK_CONNECTIONS_FLAG
43
            else port
44
        )
45
46
        return cls({
47
            'host': field_from_ini(
48
                cls.host, ini,
49
                'DeviceLink', 'host',
50
            ),
51
            'port': port,
52
            'allowed_hosts': (
53 1
                field_from_ini(
54
                    cls.allowed_hosts, ini,
55
                    'DeviceLink', 'IPS', default="",
56
                )
57
                .split()
58
            ),
59
        })
60
61
    def to_ini(self, ini):
62 1
        port = (
63 1
            FORBID_DEVICE_LINK_CONNECTIONS_FLAG
64 1
            if self.port is None
65 1
            else self.port
66
        )
67
        allowed_hosts = ' '.join(self.allowed_hosts)
68
69
        field_to_ini(port, ini, 'DeviceLink', 'port')
70 1
        field_to_ini(self.host, ini, 'DeviceLink', 'host')
71
        field_to_ini(allowed_hosts, ini, 'DeviceLink', 'IPS')
72
73
    @classmethod
74
    def default(cls):
75
        return cls({
76 1
            'host': cls.host.default,
77
            'port': cls.port.default,
78
            'allowed_hosts': [],
79
        })
80
81
82
@zope.interface.implementer(INISerializable)
83
@zope.interface.implementer(DefaultProvider)
84
class DeviceLink(Model):
85
    connection = ModelType(
86
        model_spec=Connection,
87
        required=True,
88
    )
89
90
    @classmethod
91
    def from_ini(cls, ini):
92
        return cls({
93
            'connection': Connection.from_ini(ini),
94
        })
95
96
    def to_ini(self, ini):
97
        for field_name in self.iter():
98
            self[field_name].to_ini(ini)
99
100
    @classmethod
101
    def default(cls):
102
        return cls({
103
            'connection': Connection.default(),
104
        })
105