-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_opening_hours.py
executable file
·81 lines (75 loc) · 3.02 KB
/
process_opening_hours.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
#!/usr/bin/env python3
# https://docs.python.org/3/library/xml.etree.elementtree.html#module-xml.etree.ElementTree
import xml.etree.ElementTree as ET
import os
import sys
from PyKOpeningHours.PyKOpeningHours import OpeningHours, Error
xmlfile = open("data/osm.xml", "r")
response = xmlfile.read()
blacklist = {}
if os.path.exists('data/blacklist'):
with open('data/blacklist') as f:
for line in f.readlines():
key = line.rstrip('\n')
if key != '':
blacklist[key] = 1
# parse XML
root = ET.fromstring(response)
tree = ET.ElementTree(root)
hours = {} # type/id -> [old, new]
for child in root:
if child.tag == 'node' or child.tag == 'way' or child.tag == 'relation':
key = child.tag + '/' + child.get('id')
if not key in blacklist:
old_opening_hours_tag = child.find("./tag[@k='opening_hours']")
if not old_opening_hours_tag is None:
old_opening_hours = old_opening_hours_tag.get('v')
parser = OpeningHours()
parser.setExpression(old_opening_hours)
if parser.error() == Error.SyntaxError or parser.error() == Error.IncompatibleMode:
new_oh = ''
else:
new_oh = parser.normalizedExpression()
if 'Sa-Sa' in new_oh or 'Su-Su' in new_oh:
new_oh = ''
if new_oh != '':
new_oh = new_oh.replace('PH Su ', 'Su,PH ')
new_oh = new_oh.replace('7j/7', 'Mo-Su')
new_oh = new_oh.replace('7/7', 'Mo-Su')
parser.setExpression(new_oh)
if parser.error() == Error.SyntaxError:
print('ERROR: cannot parse my own expression back:\n' + old_opening_hours + '\n' + new_oh)
simplify = lambda s: s.replace('24:00', '00:00')
if simplify(old_opening_hours) != simplify(new_oh):
hours[key] = [old_opening_hours.replace('\n','\\n'), new_oh]
else:
print('ERROR: no opening_hours found in ' + key)
# save
class CurrentOutput:
file_counter = 0 # C++ static
def __init__(self, prefix):
self.fileName = 'data/' + prefix + 'editme-' + str(CurrentOutput.file_counter) + '.txt'
self.file = open(self.fileName, 'w')
self.line_counter = 0
def add(self, lines):
self.file.write(lines)
self.line_counter += 1
return self.line_counter
def close(self):
self.file.close()
CurrentOutput.file_counter += 1
out = CurrentOutput('')
out_err = CurrentOutput('err-')
for key in sorted(hours.keys()):
entry = hours[key]
lines = '{}\n OLD {}\n NEW {}\n'.format(key, entry[0], entry[1])
if entry[1] == '':
if out_err.add(lines) > 99:
out_err.close()
out_err = CurrentOutput('err-')
else:
if out.add(lines) > 99:
out.close()
out = CurrentOutput('')
out.close()
out_err.close()