Proxy   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 100 %

Test Coverage

Coverage 92.31%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 4
c 3
b 0
f 1
dl 54
loc 54
ccs 12
cts 13
cp 0.9231
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A from_ini() 18 18 1
A default() 5 5 2
A to_ini() 5 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 ModelType
8
9 1
from .interfaces import INISerializable, DefaultProvider
10 1
from .helpers import field_from_ini, field_to_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 Proxy(Model):
16 1
    host = StringType(
17
        default="",
18
        required=True,
19
    )
20 1
    port = IntType(
21
        min_value=1000,
22
        max_value=65000,
23
        default=1080,
24 1
        required=True,
25
    )
26
    user = StringType(
27
        default="",
28
        required=True,
29
    )
30
    password = StringType(
31
        default="",
32
        required=True,
33 1
    )
34
35
    @classmethod
36
    def from_ini(cls, ini):
37
        return cls({
38
            'host': field_from_ini(
39
                cls.host, ini,
40
                'NET', 'socksHost',
41
            ),
42
            'port': field_from_ini(
43
                cls.port, ini,
44
                'NET', 'socksPort',
45
            ),
46
            'user': field_from_ini(
47
                cls.user, ini,
48
                'NET', 'socksUser',
49
            ),
50
            'password': field_from_ini(
51
                cls.password, ini,
52
                'NET', 'socksPwd',
53
            ),
54 1
        })
55
56
    def to_ini(self, ini):
57
        field_to_ini(self.host, ini, 'NET', 'socksHost')
58
        field_to_ini(self.port, ini, 'NET', 'socksPort')
59
        field_to_ini(self.user, ini, 'NET', 'socksUser')
60
        field_to_ini(self.password, ini, 'NET', 'socksPwd')
61
62 1
    @classmethod
63 1
    def default(cls):
64 1
        return cls({
65 1
            field_name: field.default
66
            for field_name, field in cls.fields.items()
67
        })
68
69 1
70
@zope.interface.implementer(INISerializable)
71
@zope.interface.implementer(DefaultProvider)
72
class Connection(Model):
73
    host = StringType(
74
        default="",
75 1
        required=True,
76
    )
77
    port = IntType(
78
        min_value=1000,
79
        max_value=65000,
80
        default=21000,
81 1
        required=True,
82
    )
83
    max_clients = IntType(
84
        min_value=1,
85
        max_value=128,
86
        default=8,
87 1
        required=True,
88
    )
89
    bandwidth = IntType(
90
        min_value=300,
91
        max_value=1000000,
92 1
        default=5000,
93
        required=True,
94
    )
95
    proxy = ModelType(
96
        model_spec=Proxy,
97
        required=True,
98
    )
99
100
    @classmethod
101
    def from_ini(cls, ini):
102
        return cls({
103
            'host': field_from_ini(
104
                cls.host, ini,
105
                'NET', 'localHost',
106
            ),
107
            'port': field_from_ini(
108
                cls.port, ini,
109
                'NET', 'localPort',
110
            ),
111
            'max_clients': field_from_ini(
112
                cls.max_clients, ini,
113
                'NET', 'serverChannels',
114 1
            ),
115
            'bandwidth': field_from_ini(
116
                cls.bandwidth, ini,
117
                'NET', 'speed',
118
            ),
119
            'proxy': Proxy.from_ini(ini),
120
        })
121
122
    def to_ini(self, ini):
123
        field_to_ini(self.host, ini, 'NET', 'localHost')
124
        field_to_ini(self.port, ini, 'NET', 'localPort')
125
        field_to_ini(self.max_clients, ini, 'NET', 'serverChannels')
126
        field_to_ini(self.bandwidth, ini, 'NET', 'speed')
127
        self.proxy.to_ini(ini)
128
129
    @classmethod
130
    def default(cls):
131
        return cls({
132
            'host': cls.host.default,
133
            'port': cls.port.default,
134
            'max_clients': cls.max_clients.default,
135
            'bandwidth': cls.bandwidth.default,
136
            'proxy': Proxy.default(),
137
        })
138