|
1 | 1 | import re |
2 | 2 |
|
3 | 3 | from metar_taf_parser.commons import converter |
| 4 | +from metar_taf_parser.commons.exception import ParseError |
4 | 5 | from metar_taf_parser.model.enum import DepositType, DepositCoverage |
5 | 6 | from metar_taf_parser.model.model import RunwayInfo, Metar |
6 | 7 | from metar_taf_parser.commons.i18n import _ |
@@ -41,6 +42,22 @@ def execute(self, metar: Metar, input: str): |
41 | 42 | metar.altimeter = int(converter.convert_inches_mercury_to_pascal(mercury)) |
42 | 43 |
|
43 | 44 |
|
| 45 | +def _parse_runway(matches, metar, runway): |
| 46 | + runway.name = matches[0][0] |
| 47 | + runway.indicator = matches[0][1] |
| 48 | + runway.min_range = int(matches[0][2]) |
| 49 | + runway.trend = matches[0][3] |
| 50 | + metar.add_runway_info(runway) |
| 51 | + |
| 52 | + |
| 53 | +def _parse_runway_max_range(matches, metar, runway): |
| 54 | + runway.name = matches[0][0] |
| 55 | + runway.min_range = int(matches[0][1]) |
| 56 | + runway.max_range = int(matches[0][2]) |
| 57 | + runway.trend = matches[0][3] |
| 58 | + metar.add_runway_info(runway) |
| 59 | + |
| 60 | + |
44 | 61 | class RunwayCommand: |
45 | 62 | generic_regex = r'^(R\d{2}\w?/)' |
46 | 63 | runway_max_range_regex = r'^R(\d{2}\w?)/(\d{4})V(\d{3,4})([UDN])?(FT)?' |
@@ -79,31 +96,29 @@ def can_parse(self, input: str): |
79 | 96 | def execute(self, metar: Metar, input: str): |
80 | 97 | matches = self._runway_deposit_pattern.findall(input) |
81 | 98 | runway = RunwayInfo() |
82 | | - if matches: |
83 | | - runway.name = matches[0][0] |
84 | | - runway.deposit_type = DepositType(matches[0][1]) |
85 | | - runway.coverage = DepositCoverage(matches[0][2]) |
86 | | - runway.thickness = self.__parse_deposit_thickness(matches[0][3]) |
87 | | - runway.braking_capacity = self.__parse_deposit_braking_capacity(matches[0][4]) |
88 | | - metar.add_runway_info(runway) |
89 | | - return |
90 | | - |
91 | | - matches = self._runway_pattern.findall(input) |
92 | | - if matches: |
93 | | - runway.name = matches[0][0] |
94 | | - runway.indicator = matches[0][1] |
95 | | - runway.min_range = int(matches[0][2]) |
96 | | - runway.trend = matches[0][3] |
97 | | - metar.add_runway_info(runway) |
98 | | - return |
99 | | - |
100 | | - matches = self._max_range_pattern.findall(input) |
101 | | - if matches: |
102 | | - runway.name = matches[0][0] |
103 | | - runway.min_range = int(matches[0][1]) |
104 | | - runway.max_range = int(matches[0][2]) |
105 | | - runway.trend = matches[0][3] |
106 | | - metar.add_runway_info(runway) |
| 99 | + try: |
| 100 | + if matches: |
| 101 | + self.__parse_runway_deposit(matches, metar, runway) |
| 102 | + return |
| 103 | + |
| 104 | + matches = self._runway_pattern.findall(input) |
| 105 | + if matches: |
| 106 | + _parse_runway(matches, metar, runway) |
| 107 | + return |
| 108 | + |
| 109 | + matches = self._max_range_pattern.findall(input) |
| 110 | + if matches: |
| 111 | + _parse_runway_max_range(matches, metar, runway) |
| 112 | + except ValueError: |
| 113 | + raise ParseError(_("ErrorCode.IncompleteRunwayInformation")) |
| 114 | + |
| 115 | + def __parse_runway_deposit(self, matches, metar, runway): |
| 116 | + runway.name = matches[0][0] |
| 117 | + runway.deposit_type = DepositType(matches[0][1]) |
| 118 | + runway.coverage = DepositCoverage(matches[0][2]) |
| 119 | + runway.thickness = self.__parse_deposit_thickness(matches[0][3]) |
| 120 | + runway.braking_capacity = self.__parse_deposit_braking_capacity(matches[0][4]) |
| 121 | + metar.add_runway_info(runway) |
107 | 122 |
|
108 | 123 | def __parse_deposit_thickness(self, input): |
109 | 124 | thickness = self._deposit_thickness.get(input, 'DepositThickness.default') |
|
0 commit comments