-
Notifications
You must be signed in to change notification settings - Fork 6
/
extract_files.py
74 lines (60 loc) · 1.84 KB
/
extract_files.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import argparse
import difflib
import json
from pathlib import Path
from pprint import pformat
import sys
from esprr_api.main import app
from esprr_api.models import SURFACE_ALBEDOS
dashdir = Path(__file__).parent / "dashboard"
OPENAPI_PATH = dashdir / "tests/unit/openapi.json"
constdir = dashdir / "src/constants"
ALBEDO_PATH = constdir / "surface_albedo.json"
def printdiff(old, new):
print(
"\n".join(
list(
difflib.context_diff(
pformat(old, sort_dicts=False).split("\n"),
pformat(new, sort_dicts=False).split("\n"),
lineterm="",
fromfile="current",
tofile="latest",
)
)
)
)
def check_file(path, name, new):
if not path.is_file():
print(f"{name} does not exist")
return 1
else:
old = json.loads(path.read_text())
if old != new:
print(f"{name} out of date")
printdiff(old, new)
return 1
return 0
def write_json(path, new):
with path.open("w") as f:
json.dump(new, f, separators=(",", ":"))
f.write("\n")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Extract API files for dashboard consumption"
)
parser.add_argument("--check", action="store_true", help="Check for file changes")
args = parser.parse_args()
new_openapi = app.openapi()
new_openapi["info"]["version"] = "1"
if args.check:
ecode = sum(
[
check_file(OPENAPI_PATH, "OpenAPI spec", new_openapi),
check_file(ALBEDO_PATH, "Surface albedo", SURFACE_ALBEDOS),
]
)
sys.exit(ecode)
else:
write_json(OPENAPI_PATH, new_openapi)
write_json(ALBEDO_PATH, SURFACE_ALBEDOS)