Debian Codegraph @

scripts/generate_manifest.py

raw
1 """
2 Script that creates initial astropy manifest from the schemas
3 in the resources directory. This file can be removed once
4 the format of the manifest files has been finalized.
5 """
6
7 import argparse
8 from pathlib import Path
9
10 import yaml
11
12
13 def parse_args():
14 parser = argparse.ArgumentParser()
15 parser.add_argument("schemas_path")
16 parser.add_argument("output_path")
17 return parser.parse_args()
18
19
20 class MultilineString(str):
21 pass
22
23
24 def represent_multiline_string(dumper, data):
25 return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="|")
26
27
28 if __name__ == "__main__":
29 yaml.add_representer(MultilineString, represent_multiline_string)
30
31 args = parse_args()
32
33 manifest = {}
34
35 manifest["id"] = "http://astropy.org/asdf/extensions/astropy/manifests/astropy-1.0"
36 manifest["extension_uri"] = "http://astropy.org/asdf/extensions/astropy-1.0"
37 manifest["title"] = "Astropy extension 1.0"
38 manifest["description"] = MultilineString(
39 "A set of tags for serializing astropy objects. This does not include most\n"
40 "model classes, which are handled by an implementation of the ASDF\n"
41 "transform extension.",
42 )
43 manifest["asdf_standard_requirement"] = {
44 "gte": "1.1.0",
45 }
46 manifest["tags"] = []
47
48 for schema_path in Path(args.schemas_path).glob("**/*.yaml"):
49 schema = yaml.safe_load(schema_path.read_bytes())
50 tag_def = {
51 "tag_uri": schema["id"].replace("http://astropy.org/schemas/astropy/", "tag:astropy.org:astropy/"),
52 "schema_uri": schema["id"],
53 "title": schema["title"].strip(),
54 }
55
56 if "tag" in schema:
57 assert tag_def["tag_uri"] == schema["tag"]
58
59 if "description" in schema:
60 tag_def["description"] = MultilineString(schema["description"].strip())
61
62 manifest["tags"].append(tag_def)
63
64 with Path(args.output_path).open("w") as f:
65 yaml.dump(manifest, f, sort_keys=False)