Lags   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 100 %

Test Coverage

Coverage 0%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A from_ini() 5 5 1
A to_ini() 3 3 2
A default() 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.exceptions import ValidationError
6 1
from schematics.models import Model
7 1
from schematics.types import IntType, FloatType
8 1
from schematics.types.compound import ModelType
9
10 1
from ..constants import NO_CHEATER_WARNINGS_LIMIT_FLAG
11 1
from ..helpers import field_from_ini, field_to_ini
12
from ..interfaces import INISerializable, DefaultProvider
13
14 1
15 1
@zope.interface.implementer(INISerializable)
16 1
@zope.interface.implementer(DefaultProvider)
17 1
class MaxTime(Model):
18
    near = FloatType(
19
        min_value=0.1,
20
        max_value=30.0,
21
        default=2.0,
22
        required=True,
23 1
    )
24
    far = FloatType(
25
        min_value=0.1,
26
        max_value=30.0,
27
        default=10.0,
28
        required=True,
29
    )
30 1
31
    def validate_far(self, data, value):
32
        near = data['near']
33
34
        if value < near:
35
            raise ValidationError(
36
                "'far' value ({far}) cannot be less than 'near' value ({near})."
37
                .format(far=value, near=near)
38
            )
39
40
        return value
41 1
42
    @classmethod
43
    def from_ini(cls, ini):
44
        return cls({
45
            'near': field_from_ini(
46
                cls.near, ini,
47
                'MaxLag', 'nearMaxLagTime',
48
            ),
49
            'far': field_from_ini(
50
                cls.far, ini,
51
                'MaxLag', 'farMaxLagTime',
52
            ),
53
        })
54 1
55
    def to_ini(self, ini):
56
        field_to_ini(self.near, ini, 'MaxLag', 'nearMaxLagTime')
57
        field_to_ini(self.far, ini, 'MaxLag', 'farMaxLagTime')
58
59
    @classmethod
60
    def default(cls):
61
        return cls({
62 1
            field_name: field.default
63 1
            for field_name, field in cls.fields.items()
64 1
        })
65 1
66
67
@zope.interface.implementer(INISerializable)
68
@zope.interface.implementer(DefaultProvider)
69
class Warnings(Model):
70
    delay = FloatType(
71 1
        min_value=1.0,
72
        max_value=30.0,
73
        default=10.0,
74
        required=True,
75
    )
76 1
    limit = IntType(
77
        default=3,
78
        min_value=1,
79
        required=False,
80
    )
81
82
    @classmethod
83
    def from_ini(cls, ini):
84
        limit = field_from_ini(
85
            cls.limit, ini,
86
            'MaxLag', 'cheaterWarningNum',
87
        )
88
        limit = (
89 1
            None
90
            if limit == NO_CHEATER_WARNINGS_LIMIT_FLAG
91
            else limit
92
        )
93
94
        return cls({
95
            'delay': field_from_ini(
96
                cls.delay, ini,
97 1
                'MaxLag', 'cheaterWarningDelay',
98 1
            ),
99 1
            'limit': limit,
100 1
        })
101
102
    def to_ini(self, ini):
103
        limit = (
104 1
            NO_CHEATER_WARNINGS_LIMIT_FLAG
105
            if self.limit is None
106
            else self.limit
107
        )
108
        field_to_ini(limit, ini, 'MaxLag', 'cheaterWarningNum')
109 1
        field_to_ini(self.delay, ini, 'MaxLag', 'cheaterWarningDelay')
110
111
    @classmethod
112
    def default(cls):
113
        return cls({
114
            field_name: field.default
115
            for field_name, field in cls.fields.items()
116 1
        })
117
118
119 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...
120
@zope.interface.implementer(DefaultProvider)
121
class Lags(Model):
122
    max_time = ModelType(
123
        model_spec=MaxTime,
124
        required=True,
125
    )
126
    warnings = ModelType(
127
        model_spec=Warnings,
128
        required=True,
129
    )
130
131
    @classmethod
132
    def from_ini(cls, ini):
133
        return cls({
134
            'max_time': MaxTime.from_ini(ini),
135
            'warnings': Warnings.from_ini(ini),
136
        })
137
138
    def to_ini(self, ini):
139
        for field_name in self.iter():
140
            self[field_name].to_ini(ini)
141
142
    @classmethod
143
    def default(cls):
144
        return cls({
145
            'max_time': MaxTime.default(),
146
            'warnings': Warnings.default(),
147
        })
148