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 . import CollectingParser |
7
|
|
|
|
8
|
|
|
|
9
|
1 |
|
class GroundRoutePoint(BaseStructure): |
10
|
1 |
|
__slots__ = ['pos', 'is_checkpoint', 'delay', 'section_length', 'speed', ] |
11
|
|
|
|
12
|
1 |
|
def __init__(self, pos, is_checkpoint, delay=None, section_length=None, |
13
|
|
|
speed=None): |
14
|
1 |
|
self.pos = pos |
15
|
1 |
|
self.is_checkpoint = is_checkpoint |
16
|
1 |
|
self.delay = delay |
17
|
1 |
|
self.section_length = section_length |
18
|
1 |
|
self.speed = speed |
19
|
|
|
|
20
|
1 |
|
def __repr__(self): |
21
|
1 |
|
return "<GroundRoutePoint '{0};{1}'>".format(self.pos.x, self.pos.y) |
22
|
|
|
|
23
|
|
|
|
24
|
1 |
|
class ChiefRoadParser(CollectingParser): |
25
|
|
|
""" |
26
|
|
|
Parses ``N_Chief_Road`` section. |
27
|
|
|
View :ref:`detailed description <chief-road-section>`. |
28
|
|
|
""" |
29
|
1 |
|
id_suffix = "_Chief" |
30
|
1 |
|
section_suffix = "_Road" |
31
|
1 |
|
input_suffix = id_suffix + section_suffix |
32
|
1 |
|
output_prefix = 'route_' |
33
|
|
|
|
34
|
1 |
|
def check_section_name(self, section_name): |
35
|
1 |
|
if not section_name.endswith(self.input_suffix): |
36
|
1 |
|
return False |
37
|
1 |
|
unit_id = self._extract_unit_id(section_name) |
38
|
1 |
|
stop = unit_id.index(self.id_suffix) |
39
|
1 |
|
return unit_id[:stop].isdigit() |
40
|
|
|
|
41
|
1 |
|
def init_parser(self, section_name): |
42
|
1 |
|
super(ChiefRoadParser, self).init_parser(section_name) |
43
|
1 |
|
unit_id = self._extract_unit_id(section_name) |
44
|
1 |
|
self.output_key = "{}{}".format(self.output_prefix, unit_id) |
45
|
|
|
|
46
|
1 |
|
def _extract_unit_id(self, section_name): |
47
|
1 |
|
stop = section_name.index(self.section_suffix) |
48
|
1 |
|
return section_name[:stop] |
49
|
|
|
|
50
|
1 |
|
def parse_line(self, line): |
51
|
1 |
|
params = line.split() |
52
|
1 |
|
pos, params = params[0:2], params[3:] |
53
|
|
|
|
54
|
1 |
|
args = { |
55
|
|
|
'pos': Point2D(*pos), |
56
|
|
|
} |
57
|
1 |
|
is_checkpoint = bool(params) |
58
|
1 |
|
args['is_checkpoint'] = is_checkpoint |
59
|
1 |
|
if is_checkpoint: |
60
|
1 |
|
args['delay'] = int(params[0]) |
61
|
1 |
|
args['section_length'] = int(params[1]) |
62
|
1 |
|
args['speed'] = float(params[2]) |
63
|
|
|
|
64
|
1 |
|
point = GroundRoutePoint(**args) |
65
|
1 |
|
self.data.append(point) |
66
|
|
|
|
67
|
1 |
|
def clean(self): |
68
|
|
|
return {self.output_key: self.data} |
69
|
|
|
|