forked from osm-fr/osmose-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTagFix_Deprecated.py
150 lines (126 loc) · 6.07 KB
/
TagFix_Deprecated.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#-*- coding: utf-8 -*-
###########################################################################
## ##
## Copyrights Frederic Rodrigo 2012 ##
## ##
## This program is free software: you can redistribute it and/or modify ##
## it under the terms of the GNU General Public License as published by ##
## the Free Software Foundation, either version 3 of the License, or ##
## (at your option) any later version. ##
## ##
## This program is distributed in the hope that it will be useful, ##
## but WITHOUT ANY WARRANTY; without even the implied warranty of ##
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ##
## GNU General Public License for more details. ##
## ##
## You should have received a copy of the GNU General Public License ##
## along with this program. If not, see <http://www.gnu.org/licenses/>. ##
## ##
###########################################################################
from plugins.Plugin import Plugin
from modules.downloader import urlread
from modules.Stablehash import stablehash
import re
class TagFix_Deprecated(Plugin):
def deprecated_list(self):
wikiRoot = 'https://wiki.openstreetmap.org/wiki'
data = urlread(wikiRoot + '/Template:Deprecated_features?action=raw', 1)
# Tidy data up for processing
# Eliminate wiki bold formatting
data = data.replace("'''", "")
# Remove HTML newlines
data = re.sub(r'<br\s*/>', ' ', data)
# Remove excess whitespace (also removes all newlines)
data = " ".join(data.split())
# Eliminate any whitespace around pipe characters
# This makes reading the template parameters simple
data = re.sub(r'\s?\|\s?', '|', data)
# Eliminate templates to prevent unexpected pipe characters
data = re.sub(r'{{{\s?lang\s?\|?\s?}}}', '', data, flags=re.I)
# Tag template can take one or two params, with trailing | possible
data = re.sub(
r'{{(?:Tag|Key)\s?\|(.+?)\|?\s?}}',
lambda x: '`{}`'.format(x.group(1).replace("|", "=")),
data,
flags=re.I
)
# Resolve interwiki links now
data = re.sub(
r'\[\[(.+?)\]\]',
lambda x: '[{}]({}/{})'.format(x.group(1), wikiRoot, x.group(1).replace(" ", "_")),
data
)
deprecated = {}
for feature in data.split(r'{{Deprecated features/item')[1:]:
# Unaccounted for template present in this feature
if r'{{' in feature:
continue
src_key, src_val, dest = None, None, None
for param in feature.split('|'):
if '=' not in param:
continue
k, v = param.split('=', 1)
# k will always start with the param because we removed whitespace around | earlier
# We don't use == because there could be space before the = character
if (k.startswith('dkey')):
src_key = v
elif (k.startswith('dvalue')):
src_val = v
elif (k.startswith('suggestion')):
dest = v
# Sanity check in case formatting changes or something
if any((src_key, src_val, dest)):
deprecated.setdefault(src_key, {})[src_val] = dest
return deprecated
def init(self, logger):
Plugin.init(self, logger)
detail = T_(
'''The tag or combination key/value is no longer used. List of deprecated
features comes from [Deprecated
features](https://wiki.openstreetmap.org/wiki/Deprecated_features)''')
self.errors[4010] = self.def_class(item = 4010, level = 2, tags = ['deprecated', 'tag', 'fix:chair'],
title = T_('Deprecated tag'),
detail = detail)
self.errors[40102] = self.def_class(item = 4010, level = 2, tags = ['deprecated', 'value', 'fix:chair'],
title = T_('Deprecated value'),
detail = detail)
self.Deprecated = self.deprecated_list()
self.DeprecatedSet = set(self.Deprecated)
def node(self, data, tags):
err = []
for k in set(tags).intersection(self.DeprecatedSet):
if None in self.Deprecated[k]:
err.append({
"class": 4010,
"subclass": stablehash(k),
"text": T_f('The tag `{0}` is deprecated in favour of {1}', k, self.Deprecated[k][None])
})
elif tags[k] in self.Deprecated[k]:
err.append({
"class": 40102,
"subclass": stablehash(k),
"text": T_f('The tag `{0}` is deprecated in favour of {1}', "=".join([k, tags[k]]), self.Deprecated[k][tags[k]])
})
return err
def way(self, data, tags, nds):
return self.node(data, tags)
def relation(self, data, tags, members):
return self.node(data, tags)
###########################################################################
from plugins.Plugin import TestPluginCommon
class Test(TestPluginCommon):
def test(self):
a = TagFix_Deprecated(None)
a.init(None)
for d in [{"amenity":"ev_charging"},
{"highway":"incline_steep"},
{"power_source":"pedalier"},
{"highway":"ford"},
]:
self.check_err(a.node(None, d), d)
self.check_err(a.way(None, d, None), d)
self.check_err(a.relation(None, d, None), d)
for d in [{"onway":"yes"},
{"waterway":"stream"},
{"highway":"primary"}]:
assert not a.node(None, d), d