-
Notifications
You must be signed in to change notification settings - Fork 2
/
Encoder.py
91 lines (70 loc) · 3.44 KB
/
Encoder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import logging
import os
from . import bulletin
from . import xmlConfig as des
class Encoder(object):
def __init__(self):
"""Superclass for invoking MDL decoders and encoders for a code form."""
self.geoLocationsDB = None
self._Logger = logging.getLogger(__name__)
#
# Always work in GMT
os.environ['TZ'] = 'GMT0'
def encode(self, text, receiptTime=None, **attrs):
"""Parses text to extract the WMO AHL line and one or more TAC forms.
text = character string containing entire TAC message (required)
receiptTime = date/time stamp the TAC message was received (optional, see xmlConfig.py)
returns Bulletin object."""
#
collection = bulletin.Bulletin()
#
# Get the WMO AHL line and the TAC form(s)
try:
AHL = self.re_AHL.search(text)
attrs.update(AHL.groupdict(''))
attrs['tt'] = self.T1T2
collection.set_bulletinIdentifier(**attrs)
translatedBulletinID = AHL.group(0).replace(' ', '')
for tac in self.re_TAC.findall(text):
decodedTAC = self.decoder(tac)
if decodedTAC['bbb'] == '':
decodedTAC['bbb'] = attrs['bbb']
if des.TRANSLATOR:
decodedTAC['translatedBulletinID'] = translatedBulletinID
if receiptTime is not None:
decodedTAC['translatedBulletinReceptionTime'] = receiptTime
else:
decodedTAC['translatedBulletinReceptionTime'] = decodedTAC['translationTime']
elif 'err_msg' in decodedTAC:
if self.T1T2 == 'L':
try:
self._Logger.warning('Will not create IWXXM document for %s' % decodedTAC['ident']['str'])
except KeyError:
self._Logger.warning('Bad observation or TAF: Could not determine ICAO ID: %s' % tac)
else:
self._Logger.warning('Will not create IWXXM advisory because of a decoding error.')
continue
if self.geoLocationsDB is not None:
try:
metaData = self.geoLocationsDB.get(decodedTAC['ident']['str'], '|||0.0 0.0 0')
except KeyError:
self._Logger.warning('Bad observation, could not determine icaoID: %s' % tac)
continue
fullname, iataID, alternateID, position = metaData.split('|')
if len(fullname) > 0:
decodedTAC['ident']['name'] = fullname
if len(alternateID) > 0:
decodedTAC['ident']['alternate'] = alternateID
if len(iataID) > 0:
decodedTAC['ident']['iataID'] = iataID
decodedTAC['ident']['position'] = position
if position == '0.0 0.0 0':
self._Logger.warning('"%s" not found in geoLocationsDB. Location missing.' %
decodedTAC['ident']['str'])
try:
collection.append(self.encoder(decodedTAC, tac))
except SyntaxError as msg:
self._Logger.warning(msg)
except AttributeError:
pass
return collection