Skip to content

Commit

Permalink
added tests for parsing.common #15
Browse files Browse the repository at this point in the history
  • Loading branch information
rossengeorgiev committed Jan 27, 2016
1 parent 10ce450 commit e9044fe
Show file tree
Hide file tree
Showing 2 changed files with 333 additions and 40 deletions.
89 changes: 53 additions & 36 deletions aprslib/parsing/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
'parse_header',
'parse_timestamp',
'parse_comment',
'parse_data_extentions',
'parse_comment_altitude',
'parse_dao',
]

def validate_callsign(callsign, prefix=""):
Expand Down Expand Up @@ -44,7 +47,7 @@ def parse_header(head):

path = path.split(',')

if len(path) < 1 or len(path[0]) == 0:
if len(path[0]) == 0:
raise ParseError("no tocallsign in header")

tocall = path[0]
Expand Down Expand Up @@ -99,83 +102,97 @@ def parse_timestamp(body, packet_type=''):

timestamp = utc.strptime(timestamp, "%Y%m%d%H%M%S")
timestamp = time.mktime(timestamp.timetuple())

parsed.update({'raw_timestamp': rawts})
except Exception as exp:
timestamp = 0
logger.debug(exp)

parsed.update({'timestamp': int(timestamp)})
parsed.update({
'raw_timestamp': rawts,
'timestamp': int(timestamp),
})

return (body, parsed)


def parse_comment(body, parsed):
match = re.findall(r"^([0-9]{3})/([0-9]{3})", body)
body, result = parse_data_extentions(body)
parsed.update(result)

body, result = parse_comment_altitude(body)
parsed.update(result)

body, result = parse_comment_telemetry(body)
parsed.update(result)

body = parse_dao(body, parsed)

if len(body) > 0 and body[0] == "/":
body = body[1:]

parsed.update({'comment': body.strip(' ')})


def parse_data_extentions(body):
parsed = {}
match = re.findall(r"^([0-9 \.]{3})/([0-9 \.]{3})", body)

if match:
cse, spd = match[0]
body = body[7:]
parsed.update({
'course': int(cse),
'speed': int(spd)*1.852 # knots to kms
'course': int(cse) if cse.strip(' .') != '' else 0,
'speed': int(spd)*1.852 if spd.strip(' .') != '' else 0,
})

# try BRG/NRQ/
match = re.findall(r"^([0-9]{3})/([0-9]{3})", body)
match = re.findall(r"^/([0-9]{3})/([0-9]{3})", body)
if match:
brg, nrq = match[0]
body = body[7:]
body = body[8:]
parsed.update({'bearing': int(brg), 'nrq': int(nrq)})
else:
match = re.findall(r"^(PHG(\d[\x30-\x7e]\d\d[0-9A-Z]?))\/", body)
match = re.findall(r"^(PHG(\d[\x30-\x7e]\d\d[0-9A-Z]?))", body)
if match:
ext, phg = match[0]
body = body[len(ext):]
parsed.update({'phg': phg})
else:
match = re.findall(r"^(RNG(\d{4}))\/", body)
match = re.findall(r"^RNG(\d{4})", body)
if match:
ext, rng = match[0]
body = body[len(ext):]
rng = match[0]
body = body[7:]
parsed.update({'rng': int(rng) * 1.609344}) # miles to km

# try find altitude in comment /A=dddddd
match = re.findall(r"^(.*?)/A=(\-\d{5}|\d{6})(.*)$", body)
return body, parsed

def parse_comment_altitude(body):
parsed = {}
match = re.findall(r"^(.*?)/A=(\-\d{5}|\d{6})(.*)$", body)
if match:
body, altitude, rest = match[0]
body += rest

parsed.update({'altitude': int(altitude)*0.3048})

body, telemetry = parse_comment_telemetry(body)
parsed.update(telemetry)

# parse DAO extention
body = parse_dao(body, parsed)

if len(body) > 0 and body[0] == "/":
body = body[1:]

parsed.update({'comment': body.strip(' ')})
return body, parsed


def parse_dao(body, parsed):
match = re.findall("^(.*)\!([\x21-\x7b][\x20-\x7b]{2})\!(.*?)$", body)
match = re.findall("^(.*)\!([\x21-\x7b])([\x20-\x7b]{2})\!(.*?)$", body)
if match:
body, dao, rest = match[0]
body, daobyte, dao, rest = match[0]
body += rest

parsed.update({'daodatumbyte': dao[0].upper()})
parsed.update({
'daodatumbyte': daobyte.upper(),
})

lat_offset = lon_offset = 0

if re.match("^[A-Z]", dao):
lat_offset = int(dao[1]) * 0.001 / 60
lon_offset = int(dao[2]) * 0.001 / 60
elif re.match("^[a-z]", dao):
lat_offset = base91.to_decimal(dao[1]) / 91.0 * 0.01 / 60
lon_offset = base91.to_decimal(dao[2]) / 91.0 * 0.01 / 60
if daobyte == 'W' and dao.isdigit():
lat_offset = int(dao[0]) * 0.001 / 60
lon_offset = int(dao[1]) * 0.001 / 60
elif daobyte == 'w' and ' ' not in dao:
lat_offset = (base91.to_decimal(dao[0]) / 91.0) * 0.01 / 60
lon_offset = (base91.to_decimal(dao[1]) / 91.0) * 0.01 / 60

parsed['latitude'] += lat_offset if parsed['latitude'] >= 0 else -lat_offset
parsed['longitude'] += lon_offset if parsed['longitude'] >= 0 else -lon_offset
Expand Down
Loading

0 comments on commit e9044fe

Please sign in to comment.