Completed
Push — master ( 51f7d3...6868d8 )
by Oleksandr
02:10
created

Regiment   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 95.12%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 17
dl 0
loc 69
ccs 39
cts 41
cp 0.9512
c 6
b 0
f 0
rs 10
1
# coding: utf-8
2
3 1
import six
4
5 1
from candv import (
6
    Values, VerboseConstant, VerboseValueConstant, with_constant_class,
7
)
8
9 1
from .utils import translations
10
11
12 1
_ = translations.ugettext_lazy
13
14
15 1
class Belligerents(Values):
16 1
    none = VerboseValueConstant(0, _("neutral"))
17 1
    red = VerboseValueConstant(1, _("allies"))
18 1
    blue = VerboseValueConstant(2, _("axis"))
19 1
    green = VerboseValueConstant(3, _("green"))
20 1
    gold = VerboseValueConstant(4, _("gold"))
21 1
    purple = VerboseValueConstant(5, _("purple"))
22 1
    aqua = VerboseValueConstant(6, _("aqua"))
23 1
    maroon = VerboseValueConstant(7, _("maroon"))
24 1
    navy = VerboseValueConstant(8, _("navy"))
25 1
    emerald = VerboseValueConstant(9, _("emerald"))
26 1
    olive = VerboseValueConstant(10, _("olive"))
27 1
    magenta = VerboseValueConstant(11, _("magenta"))
28 1
    teal = VerboseValueConstant(12, _("teal"))
29 1
    orange = VerboseValueConstant(13, _("orange"))
30 1
    turquoise = VerboseValueConstant(14, _("turquoise"))
31 1
    brown = VerboseValueConstant(15, _("brown"))
32 1
    salad = VerboseValueConstant(16, _("salad"))
33
34
35 1
class Country(VerboseConstant):
36
37 1
    def __init__(self, belligerent, verbose_name=None, help_text=None):
38 1
        super(Country, self).__init__(verbose_name=verbose_name,
39
                                      help_text=help_text)
40 1
        self.belligerent = belligerent
41
42 1
    def merge_into_group(self, group):
43 1
        super(Country, self).merge_into_group(group)
44 1
        group.belligerent = self.belligerent
45
46 1
    def to_primitive(self, context=None):
47 1
        primitive = super(Country, self).to_primitive(context)
48 1
        primitive['belligerent'] = self.belligerent.to_primitive(context)
49 1
        return primitive
50
51
52 1
class Countries(with_constant_class(Country), Values):
53 1
    au = Country(Belligerents.red, _("Australia"))
54 1
    fi = Country(Belligerents.blue, _("Finland"))
55 1
    fr = Country(Belligerents.red, _("France"))
56 1
    de = Country(Belligerents.blue, _("Germany"))
57 1
    hu = Country(Belligerents.blue, _("Hungary"))
58 1
    jp = Country(Belligerents.blue, _("Japan"))
59 1
    it = Country(Belligerents.blue, _("Italy"))
60 1
    nl = Country(Belligerents.red, _("Netherlands"))
61 1
    nz = Country(Belligerents.red, _("New Zealand"))
62 1
    pl = Country(Belligerents.red, _("Poland"))
63 1
    ro = Country(Belligerents.blue, _("Romania"))
64 1
    sk = Country(Belligerents.blue, _("Slovakia"))
65 1
    su = Country(Belligerents.red, _("Soviet Union"))
66 1
    uk = Country(Belligerents.red, _("United Kingdom"))
67 1
    us = Country(Belligerents.red, _("United States"))
68
69 1
    @classmethod
70
    def filter_by_belligerent(cls, belligerent):
71 1
        return filter(lambda x: x.belligerent == belligerent, cls.constants())
72
73
74 1
class AirForce(VerboseValueConstant):
75
76 1
    def __init__(self, country, default_flight_prefix, value,
77
                 verbose_name=None, help_text=None):
78 1
        super(AirForce, self).__init__(value,
79
                                       verbose_name=verbose_name,
80
                                       help_text=help_text)
81 1
        self.country = country
82
83 1
        if default_flight_prefix is not None:
84 1
            self.default_flight_prefix = str(default_flight_prefix)
85
        else:
86 1
            self.default_flight_prefix = None
87
88 1
    def merge_into_group(self, group):
89 1
        super(AirForce, self).merge_into_group(group)
90 1
        group.country = self.country
91 1
        group.default_flight_prefix = self.default_flight_prefix
92
93 1
    def to_primitive(self, context=None):
94 1
        primitive = super(AirForce, self).to_primitive(context)
95 1
        country = self.country and self.country.to_primitive(context)
96 1
        primitive.update({
97
            'country': country,
98
            'default_flight_prefix': self.default_flight_prefix,
99
        })
100 1
        return primitive
101
102
103 1
class AirForces(with_constant_class(AirForce), Values):
104 1
    ala = AirForce(
105
        country=Countries.fr,
106
        default_flight_prefix='fr01',
107
        value='fr',
108
        verbose_name=_("ALA"),
109
        help_text=_("Army of the Air"),
110
    )
111 1
    faf = AirForce(
112
        country=Countries.fi,
113
        default_flight_prefix='f01',
114
        value='fi',
115
        verbose_name=_("FAF"),
116
        help_text=_("Finnish Air Force"),
117
    )
118 1
    far = AirForce(
119
        country=Countries.ro,
120
        default_flight_prefix='ro01',
121
        value='ro',
122
        verbose_name=_("FAR"),
123
        help_text=_("Romanian Air Force"),
124
    )
125 1
    haf = AirForce(
126
        country=Countries.hu,
127
        default_flight_prefix='h01',
128
        value='hu',
129
        verbose_name=_("HAF"),
130
        help_text=_("Hungarian Air Force"),
131
    )
132 1
    luftwaffe = AirForce(
133
        country=Countries.de,
134
        default_flight_prefix='g01',
135
        value='de',
136
        verbose_name=_("Luftwaffe"),
137
        help_text=_("German Air Force"),
138
    )
139 1
    ija = AirForce(
140
        country=Countries.jp,
141
        default_flight_prefix='ja01',
142
        value='ja',
143
        verbose_name=_("IJA"),
144
        help_text=_("Imperial Japanese Army"),
145
    )
146 1
    ijn = AirForce(
147
        country=Countries.jp,
148
        default_flight_prefix='IN_NN',
149
        value='in',
150
        verbose_name=_("IJN"),
151
        help_text=_("Imperial Japanese Navy"),
152
    )
153 1
    paf = AirForce(
154
        country=Countries.pl,
155
        default_flight_prefix='pl01',
156
        value='pl',
157
        verbose_name=_("PAF"),
158
        help_text=_("Polish Air Force"),
159
    )
160 1
    rai = AirForce(
161
        country=Countries.it,
162
        default_flight_prefix='i01',
163
        value='it',
164
        verbose_name=_("RAI"),
165
        help_text=_("Regia Aeronautica Italiana"),
166
    )
167 1
    raaf = AirForce(
168
        country=Countries.au,
169
        default_flight_prefix='RA_NN',
170
        value='ra',
171
        verbose_name=_("RAAF"),
172
        help_text=_("Royal Australian Air Force"),
173
    )
174 1
    raf = AirForce(
175
        country=Countries.uk,
176
        default_flight_prefix='gb01',
177
        value='gb',
178
        verbose_name=_("RAF"),
179
        help_text=_("Royal Air Force"),
180
    )
181 1
    rn = AirForce(
182
        country=Countries.uk,
183
        default_flight_prefix='RN_NN',
184
        value='rn',
185
        verbose_name=_("RN"),
186
        help_text=_("Royal Navy"),
187
    )
188 1
    rnlaf = AirForce(
189
        country=Countries.nl,
190
        default_flight_prefix='DU_NN',
191
        value='du',
192
        verbose_name=_("RNLAF"),
193
        help_text=_("Royal Netherlands Air Force"),
194
    )
195 1
    rnzaf = AirForce(
196
        country=Countries.nz,
197
        default_flight_prefix='RZ_NN',
198
        value='rz',
199
        verbose_name=_("RNZAF"),
200
        help_text=_("Royal New Zealand Air Force"),
201
    )
202 1
    saf = AirForce(
203
        country=Countries.sk,
204
        default_flight_prefix='sk01',
205
        value='sk',
206
        verbose_name=_("SAF"),
207
        help_text=_("Slovak Air Force"),
208
    )
209 1
    usaaf = AirForce(
210
        country=Countries.us,
211
        default_flight_prefix='usa01',
212
        value='us',
213
        verbose_name=_("USAAF"),
214
        help_text=_("United States Army Air Forces"),
215
    )
216 1
    usmc = AirForce(
217
        country=Countries.us,
218
        default_flight_prefix='UM_NN',
219
        value='um',
220
        verbose_name=_("USMC"),
221
        help_text=_("United States Marine Corps"),
222
    )
223 1
    usn = AirForce(
224
        country=Countries.us,
225
        default_flight_prefix='UN_NN',
226
        value='un',
227
        verbose_name=_("USN"),
228
        help_text=_("United States Navy"),
229
    )
230 1
    vvs_rkka = AirForce(
231
        country=Countries.su,
232
        default_flight_prefix='r01',
233
        value='ru',
234
        verbose_name=_("VVS RKKA"),
235
        help_text=_("Workers-Peasants Red Army Air Forces"),
236
    )
237 1
    none = AirForce(
238
        country=None,
239
        default_flight_prefix=None,
240
        value='nn',
241
        verbose_name=_("None"),
242
        help_text=_("No Air Force"),
243
    )
244
245 1
    @classmethod
246
    def get_flight_prefixes(cls):
247 1
        result = map(lambda x: x.default_flight_prefix, cls.iterconstants())
248 1
        if six.PY3:
249
            result = list(result)
250 1
        return result
251
252 1
    @classmethod
253
    def get_by_flight_prefix(cls, prefix):
254 1
        for constant in cls.iterconstants():
255 1
            if constant.default_flight_prefix == prefix:
256 1
                return constant
257 1
        raise ValueError(
258
            "Air force with prefix '{0}' is not present in '{1}'"
259
            .format(prefix, cls.__name__)
260
        )
261
262 1
    @classmethod
263
    def filter_by_country(cls, country):
264 1
        return filter(lambda x: x.country == country, cls.constants())
265
266 1
    @classmethod
267
    def filter_by_belligerent(cls, belligerent):
268 1
        return filter(
269
            lambda x: x.country.belligerent == belligerent if x.country else False,
270
            cls.constants()
271
        )
272