Passed
Push — master ( c71ac2...00d8ab )
by Oleksandr
03:39
created

AIR_FORCES.get_by_flight_prefix()   A

Complexity

Conditions 3

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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