-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparse-restrictions
82 lines (73 loc) · 3.83 KB
/
parse-restrictions
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
#!/usr/bin/env python
import json
from split.parse import unpack, cnv_full_date, loop, fare_file
# No HCs
lookups = {
# Dates when C/F rows are valid
#'RD': ( ('start', 8, cnv_full_date), ('end', 8, cnv_full_date) ),
# Description of restriction code
'RH': ( ('code', 2), ('desc', 30), ('desc_out', 50), ('desc_rtn', 50), ('type_out', 1), ('type_rtn', 1), ('change_ind', 1) ),
# Dates/days when code is in operation
'HD': ( ('code', 2), ('date_from', 4), ('date_to', 4), ('days', 7) ),
# Stations code is in operation
#"HL": ( ('code', 2), ('crs_code', 3) ), # restriction applies to from/to/changing here
#"HC": ( ('code', 2), ('crs_code', 3) ), # allowed to change if restriction applies
# Times/location code is in operation
"TR": ( ('code', 2), ('seq_no', 4), ('out_ret', 1), ('time_from', 4), ('time_to', 4), ('arr_dep_via', 1), ('location', 3), ('type', 1), ('train_type', 1), ('min_fare', 1) ), # type always T
# Dates/days particular time restricions of this code are in operation
"TD": ( ('code', 2), ('seq_no', 4), ('out_ret', 1), ('date_from', 4), ('date_to', 4), ('days', 7) ),
# TOCs particular time restrictions of this code apply to
"TT": ( ('code', 2), ('seq_no', 4), ('out_ret', 1), ('toc_code', 2) ),
# Particular trains this code applies to
"SR": ( ('code', 2), ('id', 6), ('dir', 1), ('quota', 1), ('sleeping_ind', 1) ),
#"SD": ( ('code', 2), ('train_id', 6), ('out_ret', 1), ('date_from', 4), ('date_to', 4), ('days', 7) ),
#"SQ": ( ('code', 2), ('train_id', 6), ('out_ret', 1), ('location', 3), ('quota_restriction', 1), ('arr_dep', 1) ),
# Particular railcard restrictions
#"RR": ( ('railcard_code', 3), ('seq_no', 4), ('ticket', 3), ('route', 5), ('location', 3), ('code', 2), ('total_ban', 1) ),
#'EC': ( ('exception_code', 1), ('exception_desc', 50) ),
# Ticket Calendar record
#'CA': ( ('ticket_code', 3), ('cal_type', 1), ('route_code', 5), ('country_code', 1), ('date_from', 4), ('date_to', 4), ('days', 7) ),
}
data = {}
for row in loop(fare_file('RST')):
record_type, cf_mkr = row[0:2], row[2]
if cf_mkr == 'F': continue
if record_type in ('TP', 'TE', 'SP', 'SE'): continue
if record_type in ('RD',): continue
if record_type in ('CA', 'EC', 'RR'): continue
if record_type in ('SQ', 'SD'): continue
if record_type in ('HD', 'TD'): continue
row = unpack(row[3:], *lookups[record_type])
code = row['code']
del row['code']
data.setdefault(code, {})
if record_type == 'RH':
del row['change_ind']
data[code]['info'] = row
elif record_type == 'HD':
data[code].setdefault('days', []).append(row)
elif record_type == 'TR':
del row['type'], row['min_fare'], row['train_type']
key = row['seq_no'] + row['out_ret']
data[code].setdefault('times', {}).setdefault(key, {}).update({ 'f': row['time_from'], 't': row['time_to'], 'adv': row['arr_dep_via'], 'l': row['location'] })
elif record_type == 'TT':
key = row['seq_no'] + row['out_ret']
data[code].setdefault('times', {}).setdefault(key, {}).setdefault('tocs', []).append(row['toc_code'])
elif record_type == 'TD':
key = row['seq_no'] + row['out_ret']
del row['seq_no'], row['out_ret']
data[code].setdefault('times', {}).setdefault(key, {}).setdefault('dates', []).append(row)
elif record_type == 'SR':
del row['sleeping_ind'], row['quota']
data[code].setdefault('trains', []).append(row)
else:
raise Exception(record_type)
for code, types in data.items():
if 'times' not in types: continue
lll = {}
for key, info in types['times'].items():
l = info['l']
del info['l']
lll.setdefault(l, {})[key] = info
types['times'] = lll
json.dump(data, open('data/restrictions.json', 'w'), separators=(',', ': '), indent=2, sort_keys=True)