1
Fork 0
mirror of https://github.com/thegeneralist01/twitter-openapi synced 2026-01-11 23:50:26 +01:00

update output to a single file

Signed-off-by: ふぁ <yuki@yuki0311.com>
This commit is contained in:
ふぁ 2023-09-19 10:08:39 +09:00
parent 944f091025
commit 05ea773f9c
No known key found for this signature in database
GPG key ID: 83A8A5E74872A8AA

View file

@ -1,79 +1,83 @@
import os import os
import glob import glob
import json
import yaml import yaml
import shutil import shutil
import copy
import re
from build_config import Config from build_config import Config
from hooks import OpenapiHookBase, RequestHookBase, SchemasHookBase,OtherHookBase from hooks import OpenapiHookBase, RequestHookBase, SchemasHookBase, OtherHookBase
from tqdm import tqdm from tqdm import tqdm
def replace_ref(x):
if isinstance(x, dict):
return {
key: "#" + value.split("#")[-1] if key == "$ref" else replace_ref(value)
for key, value in x.items()
}
elif isinstance(x, list):
return [replace_ref(value) for value in x]
return x
print("=== Build Start ===") print("=== Build Start ===")
config = Config() config = Config()
try: shutil.rmtree("dist", ignore_errors=True)
shutil.rmtree("dist")
except:
pass
for lang, profile in tqdm(config.main().items(), leave=False): for lang, profile in tqdm(config.main().items(), leave=False):
dist_replace = lambda x: x.replace(
config.INPUT_DIR, config.OUTPUT_DIR.format(lang), 1
)
for dir in glob.glob(os.path.join(config.INPUT_DIR, "**/")):
os.makedirs(dist_replace(dir), exist_ok=True)
paths = {} paths = {}
schemas = {}
files = glob.glob(os.path.join(config.INPUT_DIR, "**/*.yaml")) files = glob.glob(os.path.join(config.INPUT_DIR, "**/*.yaml"))
for file in tqdm(files, leave=False): for file in tqdm(files, leave=False):
file = file.replace(os.path.sep, "/") file = file.replace(os.path.sep, "/")
with open(file, mode="r", encoding="utf-8") as f: with open(file, mode="r", encoding="utf-8") as f:
load = yaml.safe_load(f) load = yaml.safe_load(f)
# RequestHookBase hook
for path in list(load["paths"]): for path in list(load["paths"]):
for method in list(load["paths"][path]): for method in list(load["paths"][path]):
for tag in list(load["paths"][path][method].get("tags", ["default"])):
key, value = path, load["paths"][path][method] key, value = path, load["paths"][path][method]
for tag in list(load["paths"][path][method].get("tags", ["default"])):
for hook in profile["request"][tag]: for hook in profile["request"][tag]:
hook: RequestHookBase hook: RequestHookBase
key, value = hook.hook(key, value) key, value = hook.hook(key, value)
load["paths"][path][method] = value paths.update({key: {}})
load["paths"][key] = load["paths"].pop(path) paths[key].update({method: value})
escape = key.replace("/", "~1") # SchemasHookBase hook
relative = file.replace(config.INPUT_DIR, "", 1)
paths.update({key: {"$ref": f".{relative}#/paths/{escape}"}})
for name in list(load.get("components", {}).get("schemas", {})): for name in list(load.get("components", {}).get("schemas", {})):
value = load["components"]["schemas"][name] value = load["components"]["schemas"][name]
for hook in profile["schemas"]: for hook in profile["schemas"]:
hook: SchemasHookBase hook: SchemasHookBase
value = hook.hook(value) value = hook.hook(value)
load["components"]["schemas"][name] = value schemas.update({name: value})
# OtherHookBase hook
if file == "src/openapi/paths/other.yaml": if file == "src/openapi/paths/other.yaml":
for hook in profile["other"]: for hook in profile["other"]:
hook: OtherHookBase hook: OtherHookBase
key, value = hook.hook() key, value = hook.hook()
load["components"]["schemas"][key] = value schemas["OtherResponse"]["properties"].append({key: value})
load["components"]["schemas"]["OtherResponse"]["properties"][key] = {
"$ref": f"#/components/schemas/{key}"
}
with open(dist_replace(file), mode="w+", encoding="utf-8") as f:
f.write(yaml.dump(load))
file = "src/openapi/openapi-3.0.yaml" file = "src/openapi/openapi-3.0.yaml"
with open(file, mode="r", encoding="utf-8") as f: with open(file, mode="r", encoding="utf-8") as f:
openapi = yaml.safe_load(f) openapi = yaml.safe_load(f)
for path in paths:
openapi["paths"] = paths # OpenapiHookBase hook
for hook in profile["openapi"]: for hook in profile["openapi"]:
hook: OpenapiHookBase hook: OpenapiHookBase
openapi = hook.hook(openapi) openapi = hook.hook(openapi)
with open(dist_replace(file), mode="w+", encoding="utf-8") as f:
openapi["paths"] = replace_ref(paths)
openapi["components"]["schemas"] = replace_ref(schemas)
output = config.OUTPUT_DIR.format(lang)
os.makedirs(output, exist_ok=True)
with open(output + "/openapi-3.0.yaml", mode="w+", encoding="utf-8") as f:
f.write(yaml.dump(openapi)) f.write(yaml.dump(openapi))
print("=== Build End ===") print("=== Build End ===")