Completed
Push — master ( fffb7d...c4aefa )
by Oleksandr
06:25
created

BornPlaceAircraftsSectionParser._extract_limit()   A

Complexity

Conditions 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3
Metric Value
cc 3
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 3
rs 9.4286
1
# -*- coding: utf-8 -*-
2
3 1
from il2fb.commons.spatial import Point2D
4
5 1
from ..constants import WEAPONS_CONTINUATION_MARK
6 1
from ..converters import to_bool, to_belligerent, to_air_force
7 1
from . import CollectingParser
8
9
10 1
class BornPlaceSectionParser(CollectingParser):
11
    """
12
    Parses ``BornPlace`` section.
13
    View :ref:`detailed description <bornplace-section>`.
14
    """
15
16 1
    def check_section_name(self, section_name):
17 1
        return section_name == "BornPlace"
18
19 1
    def parse_line(self, line):
20 1
        (
21
            belligerent, the_range, pos_x, pos_y, has_parachutes,
22
            air_spawn_height, air_spawn_speed, air_spawn_heading, max_pilots,
23
            radar_min_height, radar_max_height, radar_range, air_spawn_always,
24
            enable_aircraft_limits, aircraft_limits_consider_lost,
25
            disable_spawning, friction_enabled, friction_value,
26
            aircraft_limits_consider_stationary, show_default_icon,
27
            air_spawn_if_deck_is_full, spawn_in_stationary,
28
            return_to_start_position
29
        ) = line.split()
30
31 1
        self.data.append({
32
            'range': int(the_range),
33
            'belligerent': to_belligerent(belligerent),
34
            'show_default_icon': to_bool(show_default_icon),
35
            'friction': {
36
                'enabled': to_bool(friction_enabled),
37
                'value': float(friction_value),
38
            },
39
            'spawning': {
40
                'enabled': not to_bool(disable_spawning),
41
                'with_parachutes': to_bool(has_parachutes),
42
                'max_pilots': int(max_pilots),
43
                'in_stationary': {
44
                    'enabled': to_bool(spawn_in_stationary),
45
                    'return_to_start_position': to_bool(return_to_start_position),
46
                },
47
                'in_air': {
48
                    'height': int(air_spawn_height),
49
                    'speed': int(air_spawn_speed),
50
                    'heading': int(air_spawn_heading),
51
                    'conditions': {
52
                        'always': to_bool(air_spawn_always),
53
                        'if_deck_is_full': to_bool(air_spawn_if_deck_is_full),
54
                    },
55
                },
56
                'aircraft_limitations': {
57
                    'enabled': to_bool(enable_aircraft_limits),
58
                    'consider_lost': to_bool(aircraft_limits_consider_lost),
59
                    'consider_stationary': to_bool(aircraft_limits_consider_stationary),
60
                },
61
            },
62
            'radar': {
63
                'range': int(radar_range),
64
                'min_height': int(radar_min_height),
65
                'max_height': int(radar_max_height),
66
            },
67
            'pos': Point2D(pos_x, pos_y),
68
        })
69
70 1
    def clean(self):
71 1
        return {'home_bases': self.data, }
72
73
74 1
class BornPlaceAirForcesSectionParser(CollectingParser):
75
    """
76
    Parses ``BornPlaceCountriesN`` section.
77
    View :ref:`detailed description <bornplace-air-forces-section>`.
78
    """
79 1
    input_prefix = 'BornPlaceCountries'
80 1
    output_prefix = 'home_base_air_forces_'
81
82 1
    def check_section_name(self, section_name):
83 1
        if not section_name.startswith(self.input_prefix):
84 1
            return False
85 1
        try:
86 1
            self._extract_section_number(section_name)
87 1
        except ValueError:
88 1
            return False
89
        else:
90 1
            return True
91
92 1
    def init_parser(self, section_name):
93 1
        super(BornPlaceAirForcesSectionParser, self).init_parser(section_name)
94 1
        self.output_key = (
95
            "{0}{1}".format(self.output_prefix,
96
                            self._extract_section_number(section_name)))
97 1
        self.countries = {}
98
99 1
    def _extract_section_number(self, section_name):
100 1
        start = len(self.input_prefix)
101 1
        return int(section_name[start:])
102
103 1
    def parse_line(self, line):
104 1
        air_force = to_air_force(line.strip())
105 1
        self.data.append(air_force)
106
107 1
    def clean(self):
108 1
        return {self.output_key: self.data, }
109
110
111 1
class BornPlaceAircraftsSectionParser(CollectingParser):
112
    """
113
    Parses ``BornPlaceN`` section.
114
    View :ref:`detailed description <bornplace-aircrafts-section>`.
115
    """
116 1
    input_prefix = 'BornPlace'
117 1
    output_prefix = 'home_base_aircrafts_'
118
119 1
    def check_section_name(self, section_name):
120 1
        if not section_name.startswith(self.input_prefix):
121 1
            return False
122 1
        try:
123 1
            self._extract_section_number(section_name)
124 1
        except ValueError:
125 1
            return False
126
        else:
127 1
            return True
128
129 1
    def init_parser(self, section_name):
130 1
        super(BornPlaceAircraftsSectionParser, self).init_parser(section_name)
131 1
        self.output_key = (
132
            "{}{}".format(self.output_prefix,
133
                          self._extract_section_number(section_name)))
134 1
        self.aircraft = None
135
136 1
    def _extract_section_number(self, section_name):
137 1
        start = len(self.input_prefix)
138 1
        return int(section_name[start:])
139
140 1
    def parse_line(self, line):
141 1
        parts = line.split()
142
143 1
        if parts[0] == WEAPONS_CONTINUATION_MARK:
144 1
            self.aircraft['weapon_limitations'].extend(parts[1:])
145
        else:
146 1
            if self.aircraft:
147
                # Finalize previous aircraft
148 1
                self.data.append(self.aircraft)
149 1
            self.aircraft = self._parse_new_item(parts)
150
151 1
    @classmethod
152
    def _parse_new_item(cls, parts):
153 1
        code = parts.pop(0)
154 1
        limit = cls._extract_limit(parts)
155 1
        return {
156
            'code': code,
157
            'limit': limit,
158
            'weapon_limitations': parts,
159
        }
160
161 1
    @staticmethod
162
    def _extract_limit(parts):
163 1
        if parts:
164 1
            limit = int(parts.pop(0))
165 1
            limit = limit if limit >= 0 else None
166
        else:
167 1
            limit = None
168 1
        return limit
169
170 1
    def clean(self):
171 1
        if self.aircraft:
172 1
            aircraft, self.aircraft = self.aircraft, None
173 1
            self.data.append(aircraft)
174
175
        return {self.output_key: self.data, }
176