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

MaxTime   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 57.14%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 5
c 3
b 0
f 1
dl 0
loc 45
ccs 8
cts 14
cp 0.5714
rs 10

3 Methods

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