Skip to content

Commit 33f9444

Browse files
committed
Use the new JSON tag2links #230 #264 #335 #418 #475 #498 #405
1 parent 642a745 commit 33f9444

File tree

6 files changed

+2791
-427
lines changed

6 files changed

+2791
-427
lines changed

api/issue.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from modules.query import fixes_default
1313
from modules.utils import LangsNegociation
1414

15-
from .issue_utils import _expand_tags, _get, t2l
15+
from .issue_utils import _get, _keys, t2l
1616

1717
router = APIRouter()
1818

@@ -218,7 +218,7 @@ def _error(
218218
data_type[elem["type"]]: True,
219219
"type": data_type[elem["type"]],
220220
"id": elem["id"],
221-
"tags": _expand_tags(tags, t2l.checkTags(tags)),
221+
"tags": t2l.addLinks(tags),
222222
"fixes": [],
223223
}
224224
for fix_index, fix_group in enumerate(marker["fixes"] or []):
@@ -231,13 +231,9 @@ def _error(
231231
tmp_elem["fixes"].append(
232232
{
233233
"num": fix_index,
234-
"add": _expand_tags(
235-
fix["create"], t2l.checkTags(fix["create"])
236-
),
237-
"mod": _expand_tags(
238-
fix["modify"], t2l.checkTags(fix["modify"])
239-
),
240-
"del": _expand_tags(fix["delete"], {}, True),
234+
"add": t2l.addLinks(fix["create"]),
235+
"mod": t2l.addLinks(fix["modify"]),
236+
"del": _keys(fix["delete"]),
241237
}
242238
)
243239
elems.append(tmp_elem)
@@ -255,13 +251,9 @@ def _error(
255251
new_elems.append(
256252
{
257253
"num": fix_index,
258-
"add": _expand_tags(
259-
fix["create"], t2l.checkTags(fix["create"])
260-
),
261-
"mod": _expand_tags(
262-
fix["modify"], t2l.checkTags(fix["modify"])
263-
),
264-
"del": _expand_tags(fix["delete"], {}, True),
254+
"add": t2l.addLinks(fix["create"]),
255+
"mod": t2l.addLinks(fix["modify"]),
256+
"del": _keys(fix["delete"]),
265257
}
266258
)
267259

api/issue_utils.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,5 @@ async def _get(
9292
}
9393

9494

95-
def _expand_tags(
96-
tags: Dict[str, str], links: Dict[str, str], short: bool = False
97-
) -> List[Dict[str, str]]:
98-
t = []
99-
if short:
100-
for k in tags:
101-
t.append({"k": k})
102-
else:
103-
for k, v in sorted(tags.items()):
104-
if links and k in links:
105-
t.append({"k": k, "v": v, "vlink": links[k]})
106-
else:
107-
t.append({"k": k, "v": v})
108-
return t
95+
def _keys(tags: Dict[str, str]) -> List[Dict[str, str]]:
96+
return list(map(lambda k: {"k": k}, tags.keys()))

api/tool/tag2link.py

Lines changed: 32 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,45 @@
1-
# https://trac.openstreetmap.org/browser/subversion/applications/editors/josm/plugins/tag2link/resources/tag2link_sources.xml?rev=30720&format=txt
1+
# https://raw.githubusercontent.com/JOSM/tag2link/master/index.json
22

3-
import re
4-
import xml.sax
5-
from typing import Any, Dict, List
6-
from xml.sax.xmlreader import AttributesImpl
7-
8-
9-
class Exact(xml.sax.handler.ContentHandler):
10-
def __init__(self):
11-
self.rules: List[Dict[str, Any]] = []
12-
self.val: Regex = re.compile("(%[^%]+%|\{[^}]+})") # noqa
13-
14-
def unposix(self, regex: str) -> str:
15-
regex = regex.replace("\p{Lower}", "[a-z]") # noqa
16-
regex = regex.replace("\p{Upper}", "[A-Z]") # noqa
17-
regex = regex.replace("\p{Digit}", "[0-9]") # noqa
18-
return regex
19-
20-
def startElement(self, name: str, attrs: AttributesImpl) -> None:
21-
if name == "rule":
22-
self.rules.append({"conditions": [], "link": None})
23-
elif name == "condition":
24-
self.rules[-1]["conditions"].append(
25-
{
26-
"kk": attrs["k"],
27-
"k": re.compile("^" + self.unposix(attrs["k"]) + "$"),
28-
"v": (
29-
re.compile("^" + self.unposix(attrs["v"]) + "$")
30-
if "v" in attrs
31-
else None
32-
),
33-
"id": attrs["id"] if "id" in attrs else None,
34-
}
35-
)
36-
elif name == "link":
37-
link = attrs["href"]
38-
subs = []
39-
if "%" in link or "{" in link:
40-
for valmatch in self.val.finditer(link):
41-
sub = []
42-
v = valmatch.group(1)
43-
link = link.replace(v, "%s", 1)
44-
var = v[1:-1].split(":")
45-
for ll in var:
46-
if "." not in ll and ll != "v" and ll != "k":
47-
vv = ll
48-
else:
49-
vv = ll.split(".")
50-
if vv[0] in ("k", "v"):
51-
vv = [None] + vv
52-
if len(vv) == 2:
53-
vv.append(0)
54-
else:
55-
vv[2] = int(vv[2])
56-
sub.append(vv)
57-
subs.append(sub)
58-
self.rules[-1]["link"] = {"url": link, "subs": subs}
3+
import json
4+
from typing import Dict, List
595

606

617
class tag2link:
628
def __init__(self, rulesFiles: str):
63-
parser = xml.sax.make_parser()
64-
handler = Exact()
65-
parser.setContentHandler(handler)
66-
parser.parse(open(rulesFiles, "r", encoding="utf-8"))
67-
self.rules = handler.rules
68-
self.all = re.compile(".*")
9+
rules_json = json.load(open(rulesFiles, "r", encoding="utf-8"))
10+
self.rules: Dict[str, str] = dict(
11+
map(
12+
lambda rule: [
13+
rule["key"][4:],
14+
rule["url"],
15+
],
16+
filter(lambda rule: rule["key"].startswith("Key:"), rules_json),
17+
)
18+
)
19+
print(self.rules)
6920

70-
def checkTags(self, tags: Dict[str, str]) -> Dict[str, str]:
71-
try:
72-
urls: Dict[str, str] = {}
73-
for rule in self.rules:
74-
valid = True
75-
id = {}
76-
for condition in rule["conditions"]:
77-
match = False
78-
for key in tags.keys():
79-
kmatch = condition["k"].match(key)
80-
if kmatch:
81-
id[condition["id"]] = {"k": kmatch}
82-
if condition["v"] is None:
83-
vmatch = self.all.match(tags[key])
84-
id[condition["id"]]["v"] = vmatch
85-
match = True
86-
break
87-
else:
88-
vmatch = condition["v"].match(tags[key])
89-
if vmatch:
90-
id[condition["id"]]["v"] = vmatch
91-
match = True
92-
break
93-
if not match:
94-
valid = False
95-
break
96-
if valid:
97-
replace = []
98-
for sub in rule["link"]["subs"]:
99-
for v in sub:
100-
if isinstance(v, str):
101-
replace.append(v)
102-
break
103-
else:
104-
val = id[v[0]][v[1]].group(v[2])
105-
if val:
106-
replace.append(val)
107-
break
108-
ret = rule["link"]["url"] % tuple(replace)
109-
if "://" not in ret:
110-
ret = "http://" + ret
111-
urls[id[rule["link"]["subs"][0][0][0]]["k"].group(0)] = ret
112-
return urls
113-
except Exception:
114-
return {}
21+
def addLinks(self, tags: Dict[str, str]) -> List[Dict[str, str]]:
22+
links = []
23+
for key, value in tags.items():
24+
links.append({"k": key, "v": value})
25+
if key in self.rules:
26+
links[-1]["vlink"] = self.rules[key].replace("$1", tags[key])
27+
return links
11528

11629

11730
if __name__ == "__main__":
118-
t2l = tag2link("tag2link_sources.xml")
119-
print(t2l.checkTags({"oneway": "yes"}))
120-
print(t2l.checkTags({"url": "plop.com"}))
121-
print(t2l.checkTags({"url": "http://plop.com"}))
122-
print(t2l.checkTags({"ref:UAI": "123"}))
31+
t2l = tag2link("tag2link_sources.json")
32+
print(t2l.addLinks({"oneway": "yes"}))
33+
print(t2l.addLinks({"url": "plop.com"}))
34+
print(t2l.addLinks({"url": "http://plop.com"}))
35+
print(t2l.addLinks({"ref:UAI": "123"}))
12336
print(
124-
t2l.checkTags(
37+
t2l.addLinks(
12538
{"man_made": "survey_point", "source": "©IGN 2012", "ref": "1234567 - A"}
12639
)
12740
)
12841
print(
129-
t2l.checkTags(
42+
t2l.addLinks(
13043
{
13144
"url": "span://bad",
13245
"man_made": "survey_point",
@@ -135,7 +48,7 @@ def checkTags(self, tags: Dict[str, str]) -> Dict[str, str]:
13548
}
13649
)
13750
)
138-
print(t2l.checkTags({"wikipedia:fr": "toto"}))
139-
print(t2l.checkTags({"wikipedia": "fr:toto"}))
140-
print(t2l.checkTags({"wikipedia": "toto"}))
141-
print(t2l.checkTags({"source": "source", "source:url": "http://example.com"}))
51+
print(t2l.addLinks({"wikipedia:fr": "toto"}))
52+
print(t2l.addLinks({"wikipedia": "fr:toto"}))
53+
print(t2l.addLinks({"wikipedia": "toto"}))
54+
print(t2l.addLinks({"source": "source", "source:url": "http://example.com"}))

0 commit comments

Comments
 (0)