generate_data.py 1.2 KB
Newer Older
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
import os
import json
import argparse

import attr

import parser
import tags


p = argparse.ArgumentParser(description="Process some integers.")
p.add_argument("--source", type=str, help=".md source directory", required=True)
p.add_argument("--output", type=str, help="output directory", required=True)


if __name__ == "__main__":
    args = p.parse_args()
    os.makedirs(os.path.join(args.output, "api/v1/models"), exist_ok=True)

    models = []
    for name in os.listdir(args.source):
        if not name.endswith(".md"):
            continue
        if name in ("README.md",):
            continue

        id = name[:-3]
        with open(os.path.join(args.source, name)) as f:
            model = parser.parse_file(id, f)
        data = model.to_jsondict()
        with open(
            os.path.join(args.output, "api/v1/models", "{}.json".format(id)), "w"
        ) as f:
            json.dump(data, f)
        models.append(data["meta"])

    tags_dict = {}
    for t in tags.TAGS:
        tags_dict[t.id] = t.name

    with open(os.path.join(args.output, "api/v1/models.json"), "w") as f:
        data = {
            "models": models,
            "tags": tags_dict,
        }
        json.dump(data, f)