Last active
October 19, 2021 16:09
-
-
Save gsnedders/0a475683482552a47e65d55df7bd5289 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
import json | |
import os | |
from collections import defaultdict | |
def visitor(obj, callback): | |
stack = [([], obj)] | |
while stack: | |
path, item = stack.pop() | |
if not isinstance(item, dict): | |
continue | |
for k, v in item.items(): | |
if k == "__compat": | |
callback(path, v) | |
new_path = path[:] | |
new_path.append(k) | |
stack.append((new_path, v)) | |
def globalVisitor(path, callback, dirs=None, rewrite=False): | |
if dirs is None: | |
dirs = ["api", "css", "html", "http", "javascript", "mathml", "svg", "webdriver", "webextensions"] | |
for d in dirs: | |
d_path = os.path.join(path, d) | |
for f in os.listdir(d_path): | |
if f.endswith(".json"): | |
f_path = os.path.join(d_path, f) | |
with open(f_path, "rb") as fd: | |
obj = json.load(fd) | |
visitor(obj, callback) | |
if rewrite: | |
with open(f_path, "w+") as fd: | |
json.dump(obj, fd, ensure_ascii=False, indent=2) | |
fd.seek(fd.tell() - 1) | |
if fd.read(1) != "\n": | |
fd.write("\n") | |
def cb(split_path, obj): | |
if "spec_url" in obj: | |
return | |
if len(split_path) != 3: | |
return | |
if split_path[0] != "api": | |
return | |
_, interface, member = split_path | |
urls = api_links.get((interface, member)) | |
if urls is None: | |
return | |
if len(urls) == 1: | |
obj['spec_url'] = next(iter(urls)) | |
else: | |
obj['spec_url'] = list(sorted(urls)) | |
api_links = defaultdict(set) | |
for path in glob.glob("../webref/ed/dfns/*.json"): | |
with open(path, "rb") as fd: | |
obj = json.load(fd) | |
for dfn in obj['dfns']: | |
if dfn.get("access") != "public": | |
continue | |
if dfn.get("type") not in ("method", "attribute"): | |
continue | |
href = dfn.get("href") | |
for lt in dfn.get("linkingText", []): | |
lt = lt.split("(")[0] | |
for dfn_for in dfn.get("for", []): | |
api_links[(dfn_for, lt)].add(href) | |
globalVisitor(".", cb, dirs=["api"], rewrite=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment