Completed
Push — master ( 025da3...410210 )
by Oleksandr
01:36
created

il2fb.parsers.mission.sections.NStationaryParser   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 100%
Metric Value
dl 0
loc 116
ccs 57
cts 57
cp 1
rs 10
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __parse_aircraft() 0 20 3
B parse_line() 0 31 3
A __parse_artillery() 0 20 3
A __parse_ship() 0 9 1
A _get_code() 0 3 1
A clean() 0 2 1
A _get_type_name() 0 7 2
A check_section_name() 0 2 1
1
# -*- coding: utf-8 -*-
2
3 1
from il2fb.commons.spatial import Point2D
4 1
from il2fb.commons.structures import BaseStructure
5
6 1
from ..constants import NULL, IS_STATIONARY_AIRCRAFT_RESTORABLE
7 1
from ..converters import (
8
    to_bool, to_skill, to_air_force, to_unit_type, to_belligerent,
9
)
10 1
from . import CollectingParser
11
12
13 1
class StationaryObject(BaseStructure):
14 1
    __slots__ = [
15
        'id', 'belligerent', 'code', 'pos', 'rotation_angle', 'type',
16
    ]
17
18 1
    def __init__(self, id, belligerent, code, pos, rotation_angle, type):
19 1
        self.id = id
20 1
        self.belligerent = belligerent
21 1
        self.code = code
22 1
        self.pos = pos
23 1
        self.rotation_angle = rotation_angle
24 1
        self.type = type
25
26 1
    def __repr__(self):
27 1
        return "<{0} '{1}'>".format(self.__class__.__name__, self.id)
28
29
30 1
class StationaryArtillery(StationaryObject):
31 1
    __slots__ = StationaryObject.__slots__ + [
32
        'awakening_time', 'range', 'skill', 'use_spotter',
33
    ]
34
35 1
    def __init__(self, id, belligerent, code, pos, rotation_angle, type,
36
                 awakening_time, range, skill, use_spotter):
37 1
        super(StationaryArtillery, self).__init__(
38
            id, belligerent, code, pos, rotation_angle, type)
39 1
        self.awakening_time = awakening_time
40 1
        self.range = range
41 1
        self.skill = skill
42 1
        self.use_spotter = use_spotter
43
44
45 1
class StationaryAircraft(StationaryObject):
46 1
    __slots__ = StationaryObject.__slots__ + [
47
        'air_force', 'allows_spawning', 'is_restorable', 'skin',
48
        'show_markings',
49
    ]
50
51 1
    def __init__(self, id, belligerent, code, pos, rotation_angle, type,
52
                 air_force, allows_spawning, is_restorable, skin,
53
                 show_markings):
54 1
        super(StationaryAircraft, self).__init__(
55
            id, belligerent, code, pos, rotation_angle, type)
56 1
        self.air_force = air_force
57 1
        self.allows_spawning = allows_spawning
58 1
        self.is_restorable = is_restorable
59 1
        self.skin = skin
60 1
        self.show_markings = show_markings
61
62
63 1
class StationaryShip(StationaryObject):
64 1
    __slots__ = StationaryObject.__slots__ + [
65
        'awakening_time', 'recharge_time', 'skill',
66
    ]
67
68 1
    def __init__(self, id, belligerent, code, pos, rotation_angle, type,
69
                 awakening_time, recharge_time, skill):
70 1
        super(StationaryShip, self).__init__(
71
            id, belligerent, code, pos, rotation_angle, type)
72 1
        self.awakening_time = awakening_time
73 1
        self.recharge_time = recharge_time
74 1
        self.skill = skill
75
76
77 1
class NStationaryParser(CollectingParser):
78
    """
79
    Parses ``NStationary`` section.
80
    View :ref:`detailed description <nstationary-section>`.
81
    """
82
83 1
    def __parse_artillery(params):
84
        """
85
        Parse additional options for ``artillery`` type
86
        """
87 1
        try:
88 1
            awakening_time, the_range, skill, use_spotter = params
89 1
            skill = to_skill(skill)
90 1
            use_spotter = to_bool(use_spotter)
91 1
        except ValueError:
92 1
            try:
93 1
                awakening_time, the_range = params
94 1
            except ValueError:
95 1
                awakening_time, the_range = params[0], 0
96 1
            skill, use_spotter = None, False
97
98 1
        return {
99
            'awakening_time': float(awakening_time),
100
            'range': int(the_range),
101
            'skill': skill,
102
            'use_spotter': use_spotter,
103
        }
104
105 1
    def __parse_aircraft(params):
106
        """
107
        Parse additional options for ``planes`` type
108
        """
109 1
        try:
110 1
            air_force, allows_spawning__restorable = params[1:3]
111 1
            skin, show_markings = params[4:]
112 1
        except ValueError:
113 1
            air_force, allows_spawning__restorable = None, 0
114 1
            skin, show_markings = params[1:]
115
116 1
        is_restorable = allows_spawning__restorable == IS_STATIONARY_AIRCRAFT_RESTORABLE
117 1
        skin = None if skin == NULL else skin
118
119 1
        return {
120
            'air_force': to_air_force(air_force),
121
            'allows_spawning': to_bool(allows_spawning__restorable),
122
            'is_restorable': is_restorable,
123
            'skin': skin,
124
            'show_markings': to_bool(show_markings),
125
        }
126
127 1
    def __parse_ship(params):
128
        """
129
        Parse additional options for ``ships`` type
130
        """
131 1
        awakening_time, skill, recharge_time = params[1:]
132 1
        return {
133
            'awakening_time': float(awakening_time),
134
            'recharge_time': float(recharge_time),
135
            'skill': to_skill(skill),
136
        }
137
138 1
    subparsers = {
139
        'artillery': (StationaryArtillery, __parse_artillery),
140
        'planes': (StationaryAircraft, __parse_aircraft),
141
        'ships': (StationaryShip, __parse_ship),
142
    }
143
144 1
    def check_section_name(self, section_name):
145 1
        return section_name == "NStationary"
146
147 1
    def parse_line(self, line):
148 1
        params = line.split()
149
150 1
        oid, object_name, belligerent = params[0], params[1], params[2]
151 1
        pos = params[3:5]
152 1
        rotation_angle = params[5]
153 1
        params = params[6:]
154
155 1
        type_name = self._get_type_name(object_name)
156 1
        try:
157 1
            object_type = to_unit_type(type_name)
158 1
        except Exception:
159
            # Use original string as object's type
160 1
            object_type = type_name
161
162 1
        the_object = {
163
            'id': oid,
164
            'belligerent': to_belligerent(belligerent),
165
            'code': self._get_code(object_name),
166
            'pos': Point2D(*pos),
167
            'rotation_angle': float(rotation_angle),
168
            'type': object_type,
169
        }
170
171 1
        if type_name in self.subparsers:
172 1
            structure, subparser = self.subparsers.get(type_name)
173 1
            the_object.update(subparser(params))
174
        else:
175 1
            structure = StationaryObject
176
177 1
        self.data.append(structure(**the_object))
178
179 1
    def _get_type_name(self, object_name):
180 1
        if object_name.startswith('ships'):
181 1
            return "ships"
182
        else:
183 1
            start = object_name.index('.') + 1
184 1
            stop = object_name.rindex('.')
185 1
            return object_name[start:stop]
186
187 1
    def _get_code(self, code):
188 1
        start = code.index('$') + 1
189 1
        return code[start:]
190
191 1
    def clean(self):
192
        return {'stationary': self.data, }
193