Completed
Push — master ( caabd8...c07396 )
by Oleksandr
01:11
created

check_section_name()   A

Complexity

Conditions 3

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3
Metric Value
cc 3
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 3
rs 9.4286
1
# -*- coding: utf-8 -*-
2
3 1
from il2fb.commons.organization import AirForces, Regiments
4
5 1
from ..converters import to_skill
6 1
from . import ValuesParser
7
8
9 1
class FlightInfoSectionParser(ValuesParser):
10
    """
11
    Parses settings for a moving flight group.
12
    View :ref:`detailed description <flight-info-section>`.
13
    """
14
15 1
    def check_section_name(self, section_name):
16 1
        try:
17 1
            self._decompose_section_name(section_name)
18 1
        except Exception:
19 1
            return False
20
        else:
21 1
            return True
22
23 1
    def init_parser(self, section_name):
24 1
        super(FlightInfoSectionParser, self).init_parser(section_name)
25 1
        self.output_key = section_name
26 1
        self.flight_info = self._decompose_section_name(section_name)
27
28 1
    def _decompose_section_name(self, section_name):
29 1
        prefix = section_name[:-2]
30 1
        squadron, flight = section_name[-2:]
31
32 1
        try:
33 1
            regiment = None
34 1
            air_force = AirForces.get_by_flight_prefix(prefix)
35 1
        except ValueError:
36 1
            regiment = Regiments.get_by_code_name(prefix)
37 1
            air_force = regiment.air_force
38
39 1
        return {
40
            'id': section_name,
41
            'air_force': air_force,
42
            'regiment': regiment,
43
            'squadron_index': int(squadron),
44
            'flight_index': int(flight),
45
        }
46
47 1
    def clean(self):
48 1
        count = int(self.data['Planes'])
49 1
        code = self.data['Class'].split('.', 1)[1]
50 1
        aircrafts = []
51
52 1
        def _add_if_present(target, key, value):
53 1
            if value:
54 1
                target[key] = value
55
56 1
        for i in range(count):
57 1
            aircraft = {
58
                'index': i,
59
                'has_markings': self._has_markings(i),
60
                'skill': self._get_skill(i),
61
            }
62 1
            _add_if_present(
63
                aircraft, 'aircraft_skin', self._get_skin('skin', i))
64 1
            _add_if_present(
65
                aircraft, 'nose_art', self._get_skin('nose_art', i))
66 1
            _add_if_present(
67
                aircraft, 'pilot_skin', self._get_skin('pilot', i))
68 1
            _add_if_present(
69
                aircraft, 'spawn_object', self._get_spawn_object_id(i))
70 1
            aircrafts.append(aircraft)
71
72 1
        self.flight_info.update({
73
            'ai_only': 'OnlyAI' in self.data,
74
            'aircrafts': aircrafts,
75
            'code': code,
76
            'fuel': int(self.data['Fuel']),
77
            'with_parachutes': 'Parachute' not in self.data,
78
            'count': count,
79
            'weapons': self.data['weapons'],
80
        })
81
82 1
        return {self.output_key: self.flight_info}
83
84 1
    def _get_skill(self, aircraft_id):
85 1
        if 'Skill' in self.data:
86 1
            return to_skill(self.data['Skill'])
87
        else:
88 1
            return to_skill(self.data['Skill{:}'.format(aircraft_id)])
89
90 1
    def _has_markings(self, aircraft_id):
91 1
        return 'numberOn{:}'.format(aircraft_id) not in self.data
92
93 1
    def _get_skin(self, prefix, aircraft_id):
94 1
        return self.data.get('{:}{:}'.format(prefix, aircraft_id))
95
96 1
    def _get_spawn_object_id(self, aircraft_id):
97
        return self.data.get('spawn{:}'.format(aircraft_id))
98