Skip to content

Commit

Permalink
Merge pull request #65 from mivek/feature/runway_error_parse
Browse files Browse the repository at this point in the history
Feature/runway error parse
  • Loading branch information
mivek authored May 20, 2024
2 parents d99973b + 42b51bc commit 7bc2580
Show file tree
Hide file tree
Showing 23 changed files with 641 additions and 175 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## [1.9.0] - 2024-05-19

### Added

- When information about a runway is incomplete in a METAR, a `ParseError` is raised instead of `ValueError`.

## [1.8.2] - 2024-01-14

### Fixed
Expand Down
65 changes: 40 additions & 25 deletions metar_taf_parser/command/metar.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re

from metar_taf_parser.commons import converter
from metar_taf_parser.commons.exception import ParseError
from metar_taf_parser.model.enum import DepositType, DepositCoverage
from metar_taf_parser.model.model import RunwayInfo, Metar
from metar_taf_parser.commons.i18n import _
Expand Down Expand Up @@ -41,6 +42,22 @@ def execute(self, metar: Metar, input: str):
metar.altimeter = int(converter.convert_inches_mercury_to_pascal(mercury))


def _parse_runway(matches, metar, runway):
runway.name = matches[0][0]
runway.indicator = matches[0][1]
runway.min_range = int(matches[0][2])
runway.trend = matches[0][3]
metar.add_runway_info(runway)


def _parse_runway_max_range(matches, metar, runway):
runway.name = matches[0][0]
runway.min_range = int(matches[0][1])
runway.max_range = int(matches[0][2])
runway.trend = matches[0][3]
metar.add_runway_info(runway)


class RunwayCommand:
generic_regex = r'^(R\d{2}\w?/)'
runway_max_range_regex = r'^R(\d{2}\w?)/(\d{4})V(\d{3,4})([UDN])?(FT)?'
Expand Down Expand Up @@ -79,31 +96,29 @@ def can_parse(self, input: str):
def execute(self, metar: Metar, input: str):
matches = self._runway_deposit_pattern.findall(input)
runway = RunwayInfo()
if matches:
runway.name = matches[0][0]
runway.deposit_type = DepositType(matches[0][1])
runway.coverage = DepositCoverage(matches[0][2])
runway.thickness = self.__parse_deposit_thickness(matches[0][3])
runway.braking_capacity = self.__parse_deposit_braking_capacity(matches[0][4])
metar.add_runway_info(runway)
return

matches = self._runway_pattern.findall(input)
if matches:
runway.name = matches[0][0]
runway.indicator = matches[0][1]
runway.min_range = int(matches[0][2])
runway.trend = matches[0][3]
metar.add_runway_info(runway)
return

matches = self._max_range_pattern.findall(input)
if matches:
runway.name = matches[0][0]
runway.min_range = int(matches[0][1])
runway.max_range = int(matches[0][2])
runway.trend = matches[0][3]
metar.add_runway_info(runway)
try:
if matches:
self.__parse_runway_deposit(matches, metar, runway)
return

matches = self._runway_pattern.findall(input)
if matches:
_parse_runway(matches, metar, runway)
return

matches = self._max_range_pattern.findall(input)
if matches:
_parse_runway_max_range(matches, metar, runway)
except ValueError:
raise ParseError(_("ErrorCode.IncompleteRunwayInformation"))

def __parse_runway_deposit(self, matches, metar, runway):
runway.name = matches[0][0]
runway.deposit_type = DepositType(matches[0][1])
runway.coverage = DepositCoverage(matches[0][2])
runway.thickness = self.__parse_deposit_thickness(matches[0][3])
runway.braking_capacity = self.__parse_deposit_braking_capacity(matches[0][4])
metar.add_runway_info(runway)

def __parse_deposit_thickness(self, input):
thickness = self._deposit_thickness.get(input, 'DepositThickness.default')
Expand Down
8 changes: 8 additions & 0 deletions metar_taf_parser/commons/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ class TranslationError(Exception):
def __init__(self, translation: str, message: str):
self.message = message
self.translation = translation


class ParseError(Exception):
def __init__(self, message: str):
self.__message = message

def message(self):
return self.__message
Binary file modified metar_taf_parser/locale/de/LC_MESSAGES/messages.mo
Binary file not shown.
Binary file modified metar_taf_parser/locale/en/LC_MESSAGES/messages.mo
Binary file not shown.
4 changes: 4 additions & 0 deletions metar_taf_parser/locale/en/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ msgstr "The airport was not found for this message."
msgid "ErrorCode.InvalidMessage"
msgstr "The entered message is invalid."

#:
msgid "ErrorCode.IncompleteRunwayInformation"
msgstr "The runway information is incomplete and cannot be parsed."

#:
msgid "Flag.AMD"
msgstr "amended TAF"
Expand Down
Binary file modified metar_taf_parser/locale/es/LC_MESSAGES/messages.mo
Binary file not shown.
Loading

0 comments on commit 7bc2580

Please sign in to comment.