|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
1 |
|
from ..constants import WEAPONS_CONTINUATION_MARK |
|
4
|
1 |
|
from . import CollectingParser |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
1 |
|
class BornPlaceAircraftsParser(CollectingParser): |
|
8
|
|
|
""" |
|
9
|
|
|
Parses ``BornPlaceN`` section. |
|
10
|
|
|
View :ref:`detailed description <bornplace-aircrafts-section>`. |
|
11
|
|
|
""" |
|
12
|
1 |
|
input_prefix = 'BornPlace' |
|
13
|
1 |
|
output_prefix = 'home_base_aircrafts_' |
|
14
|
|
|
|
|
15
|
1 |
|
def check_section_name(self, section_name): |
|
16
|
1 |
|
if not section_name.startswith(self.input_prefix): |
|
17
|
1 |
|
return False |
|
18
|
1 |
|
try: |
|
19
|
1 |
|
self._extract_section_number(section_name) |
|
20
|
1 |
|
except ValueError: |
|
21
|
1 |
|
return False |
|
22
|
|
|
else: |
|
23
|
1 |
|
return True |
|
24
|
|
|
|
|
25
|
1 |
|
def init_parser(self, section_name): |
|
26
|
1 |
|
super(BornPlaceAircraftsParser, self).init_parser(section_name) |
|
27
|
1 |
|
self.output_key = ( |
|
28
|
|
|
"{}{}".format(self.output_prefix, |
|
29
|
|
|
self._extract_section_number(section_name))) |
|
30
|
1 |
|
self.aircraft = None |
|
31
|
|
|
|
|
32
|
1 |
|
def _extract_section_number(self, section_name): |
|
33
|
1 |
|
start = len(self.input_prefix) |
|
34
|
1 |
|
return int(section_name[start:]) |
|
35
|
|
|
|
|
36
|
1 |
|
def parse_line(self, line): |
|
37
|
1 |
|
parts = line.split() |
|
38
|
|
|
|
|
39
|
1 |
|
if parts[0] == WEAPONS_CONTINUATION_MARK: |
|
40
|
1 |
|
self.aircraft['weapon_limitations'].extend(parts[1:]) |
|
41
|
|
|
else: |
|
42
|
1 |
|
if self.aircraft: |
|
43
|
|
|
# Finalize previous aircraft |
|
44
|
1 |
|
self.data.append(self.aircraft) |
|
45
|
1 |
|
self.aircraft = BornPlaceAircraftsParser._parse_new_item(parts) |
|
46
|
|
|
|
|
47
|
1 |
|
@staticmethod |
|
48
|
|
|
def _parse_new_item(parts): |
|
49
|
1 |
|
code = parts.pop(0) |
|
50
|
1 |
|
limit = BornPlaceAircraftsParser._extract_limit(parts) |
|
51
|
1 |
|
return { |
|
52
|
|
|
'code': code, |
|
53
|
|
|
'limit': limit, |
|
54
|
|
|
'weapon_limitations': parts, |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
@staticmethod |
|
58
|
|
|
def _extract_limit(parts): |
|
59
|
1 |
|
if parts: |
|
60
|
1 |
|
limit = int(parts.pop(0)) |
|
61
|
1 |
|
limit = limit if limit >= 0 else None |
|
62
|
|
|
else: |
|
63
|
1 |
|
limit = None |
|
64
|
1 |
|
return limit |
|
65
|
|
|
|
|
66
|
1 |
|
def clean(self): |
|
67
|
1 |
|
if self.aircraft: |
|
68
|
1 |
|
aircraft, self.aircraft = self.aircraft, None |
|
69
|
1 |
|
self.data.append(aircraft) |
|
70
|
|
|
|
|
71
|
|
|
return {self.output_key: self.data, } |
|
72
|
|
|
|