Passed
Push — master ( 0fb205...8fdecd )
by Oleksandr
56s
created

AirForceConstant.merge_into_group()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
from candv import Values
2
from candv import VerboseValueConstant
3
from candv import with_constant_class
4
5
from .countries import COUNTRY
6
from .exceptions import IL2FBLookupError
7
8
from ._translations import gettext_lazy as _
9
from ._utils import export
10
11
12
@export
13
class AirForceConstant(VerboseValueConstant):
14
15
  def __init__(
16
    self,
17
    country,
18
    default_flight_prefix,
19
    value,
20
    verbose_name=None,
21
    help_text=None,
22
  ):
23
    super().__init__(value, verbose_name=verbose_name, help_text=help_text)
24
    self.country = country
25
26
    if default_flight_prefix is not None:
27
      self.default_flight_prefix = str(default_flight_prefix)
28
    else:
29
      self.default_flight_prefix = None
30
31
  def merge_into_group(self, group):
32
    super().merge_into_group(group)
33
    group.country = self.country
34
    group.default_flight_prefix = self.default_flight_prefix
35
36
  def to_primitive(self, context=None):
37
    primitive = super().to_primitive(context)
38
    country = self.country and self.country.to_primitive(context)
39
    primitive.update({
40
      'country': country,
41
      'default_flight_prefix': self.default_flight_prefix,
42
    })
43
    return primitive
44
45
46
@export
47
class AIR_FORCE(with_constant_class(AirForceConstant), Values):
48
  ALA = AirForceConstant(
49
    country=COUNTRY.FR,
50
    default_flight_prefix="fr01",
51
    value="fr",
52
    verbose_name=_("ALA"),
53
    help_text=_("Army of the Air"),
54
  )
55
  FAF = AirForceConstant(
56
    country=COUNTRY.FI,
57
    default_flight_prefix="f01",
58
    value="fi",
59
    verbose_name=_("FAF"),
60
    help_text=_("Finnish Air Force"),
61
  )
62
  FAR = AirForceConstant(
63
    country=COUNTRY.RO,
64
    default_flight_prefix="ro01",
65
    value="ro",
66
    verbose_name=_("FAR"),
67
    help_text=_("Romanian Air Force"),
68
  )
69
  HAF = AirForceConstant(
70
    country=COUNTRY.HU,
71
    default_flight_prefix="h01",
72
    value="hu",
73
    verbose_name=_("HAF"),
74
    help_text=_("Hungarian Air Force"),
75
  )
76
  LUFTWAFFE = AirForceConstant(
77
    country=COUNTRY.DE,
78
    default_flight_prefix="g01",
79
    value="de",
80
    verbose_name=_("Luftwaffe"),
81
    help_text=_("German Air Force"),
82
  )
83
  IJA = AirForceConstant(
84
    country=COUNTRY.JP,
85
    default_flight_prefix="ja01",
86
    value="ja",
87
    verbose_name=_("IJA"),
88
    help_text=_("Imperial Japanese Army"),
89
  )
90
  IJN = AirForceConstant(
91
    country=COUNTRY.JP,
92
    default_flight_prefix="IN_NN",
93
    value="in",
94
    verbose_name=_("IJN"),
95
    help_text=_("Imperial Japanese Navy"),
96
  )
97
  PAF = AirForceConstant(
98
    country=COUNTRY.PL,
99
    default_flight_prefix="pl01",
100
    value="pl",
101
    verbose_name=_("PAF"),
102
    help_text=_("Polish Air Force"),
103
  )
104
  RAI = AirForceConstant(
105
    country=COUNTRY.IT,
106
    default_flight_prefix="i01",
107
    value="it",
108
    verbose_name=_("RAI"),
109
    help_text=_("Regia Aeronautica Italiana"),
110
  )
111
  RAAF = AirForceConstant(
112
    country=COUNTRY.AU,
113
    default_flight_prefix="RA_NN",
114
    value="ra",
115
    verbose_name=_("RAAF"),
116
    help_text=_("Royal Australian Air Force"),
117
  )
118
  RAF = AirForceConstant(
119
    country=COUNTRY.UK,
120
    default_flight_prefix="gb01",
121
    value="gb",
122
    verbose_name=_("RAF"),
123
    help_text=_("Royal Air Force"),
124
  )
125
  RN = AirForceConstant(
126
    country=COUNTRY.UK,
127
    default_flight_prefix="RN_NN",
128
    value="rn",
129
    verbose_name=_("RN"),
130
    help_text=_("Royal Navy"),
131
  )
132
  RNLAF = AirForceConstant(
133
    country=COUNTRY.NL,
134
    default_flight_prefix="DU_NN",
135
    value="du",
136
    verbose_name=_("RNLAF"),
137
    help_text=_("Royal Netherlands Air Force"),
138
  )
139
  RNZAF = AirForceConstant(
140
    country=COUNTRY.NZ,
141
    default_flight_prefix="RZ_NN",
142
    value="rz",
143
    verbose_name=_("RNZAF"),
144
    help_text=_("Royal New Zealand Air Force"),
145
  )
146
  SAF = AirForceConstant(
147
    country=COUNTRY.SK,
148
    default_flight_prefix="sk01",
149
    value="sk",
150
    verbose_name=_("SAF"),
151
    help_text=_("Slovak Air Force"),
152
  )
153
  USAAF = AirForceConstant(
154
    country=COUNTRY.US,
155
    default_flight_prefix="usa01",
156
    value="us",
157
    verbose_name=_("USAAF"),
158
    help_text=_("United States Army Air Forces"),
159
  )
160
  USMC = AirForceConstant(
161
    country=COUNTRY.US,
162
    default_flight_prefix="UM_NN",
163
    value="um",
164
    verbose_name=_("USMC"),
165
    help_text=_("United States Marine Corps"),
166
  )
167
  USN = AirForceConstant(
168
    country=COUNTRY.US,
169
    default_flight_prefix="UN_NN",
170
    value="un",
171
    verbose_name=_("USN"),
172
    help_text=_("United States Navy"),
173
  )
174
  VVS_RKKA = AirForceConstant(
175
    country=COUNTRY.SU,
176
    default_flight_prefix="r01",
177
    value="ru",
178
    verbose_name=_("VVS RKKA"),
179
    help_text=_("Workers-Peasants Red Army Air Forces"),
180
  )
181
  NONE = AirForceConstant(
182
    country=None,
183
    default_flight_prefix=None,
184
    value="nn",
185
    verbose_name=_("None"),
186
    help_text=_("No Air Force"),
187
  )
188
189
  @classmethod
190
  def get_flight_prefixes(cls):
191
    return [x.default_flight_prefix for x in cls.iterconstants()]
192
193
  @classmethod
194
  def get_by_flight_prefix(cls, prefix):
195
    for constant in cls.iterconstants():
196
      if constant.default_flight_prefix == prefix:
197
        return constant
198
199
    raise IL2FBLookupError(
200
      f"air force with prefix '{prefix}' is not present in '{cls.__name__}'"
201
    )
202
203
  @classmethod
204
  def filter_by_country(cls, country):
205
    return filter(lambda x: x.country == country, cls.constants())
206