-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_changes.py
More file actions
executable file
·56 lines (51 loc) · 2.19 KB
/
create_changes.py
File metadata and controls
executable file
·56 lines (51 loc) · 2.19 KB
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
#!/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
# Parse edited data
fileName = 'data/done.txt'
hours = {}
with open(fileName) as f:
while True:
key = f.readline().rstrip('\n')
if key == '':
break
old = f.readline()[6:].rstrip('\n')
new = f.readline()[6:].rstrip('\n')
if old != new:
hours[key] = [old, new]
parser = OpeningHours()
parser.setExpression(new)
# Null is OK here, e.g. to fix "" to empty
if parser.error() == Error.SyntaxError or parser.error() == Error.IncompatibleMode:
print('ERROR: invalid replacement value: {0}'.format(new))
sys.exit(1)
normalized = parser.normalizedExpression()
if normalized != new:
print('ERROR: not normalized: {0} -> {1}'.format(new, normalized))
sys.exit(1)
#print('IN: {} {} -> {}'.format(key, old, new))
# Read XML
xmlfile = open("data/osm.xml", "r")
response = xmlfile.read()
# parse and modify XML
root = ET.fromstring(response)
tree = ET.ElementTree(root)
for child in root:
if child.tag == 'node' or child.tag == 'way' or child.tag == 'relation':
key = child.tag + '/' + child.get('id')
if key in hours:
[old_oh, new_oh] = hours[key]
#print('OUT: {} {} -> {}'.format(key, old_oh, new_oh))
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')
if False and old_opening_hours != old_oh: # just my own mistakes, ignore
print('opening_hours changed in OSM meanwhile! {}\n{}\n{}'.format(key, old_opening_hours, old_oh))
else:
old_opening_hours_tag.set('v', new_oh)
child.set('X-reason', 'update_') # for filter_changes.py
child.set('action', 'modify')
tree.write('data/done.osm', 'unicode', True)