-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerator.py
More file actions
executable file
·166 lines (151 loc) · 6.44 KB
/
generator.py
File metadata and controls
executable file
·166 lines (151 loc) · 6.44 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python3
import fileinput
import json
import sys
from pathlib import Path
from typing import Any
import textcase
from jinja2 import Environment, FileSystemLoader
VALID_FACILITIES = ["SOAR", "LCO", "SAAO", "BLANCO"]
def get_extra_params_fields(
extra_params_validation_schema: dict[Any, Any],
) -> dict[Any, Any]:
"""Loops over the "extra_params" section of a validation_schema dict and creates a dictionary of
field to aeonlib field_class to place into the template
"""
fields = {}
for field, properties in extra_params_validation_schema.items():
field_class = ""
# If a set of allowed values is present, use that to make a Literal unless this is a boolean variable
if "allowed" in properties and properties.get("type") != "boolean":
allowed_values = [
f'"{val}"' if properties["type"] == "string" else val
for val in properties["allowed"]
]
field_class += f"Literal[{', '.join(allowed_values)}]"
else:
# Otherwise form an Annotated field based on its datatype, with min/max validation if present
field_class += "Annotated["
match properties["type"]:
case "string":
field_class += "str"
case "integer":
field_class += "int"
case "float":
field_class += "float"
case "boolean":
field_class += "bool"
if "min" in properties:
field_class += f", Ge({properties['min']})"
if "max" in properties:
field_class += f", Le({properties['max']})"
# Add description to Annotated field. Annotated fields must have at least 2 properties.
field_class += f', "{properties.get("description", "")}"]'
if not properties.get("required", False) and "default" not in properties:
# The field is considered optional if it doesn't have a default or required is not set to True
field_class += " | None = None"
elif "default" in properties:
# If a default value is present, provide it
default = (
f'"{properties["default"]}"'
if properties["type"] == "string"
else properties["default"]
)
field_class += f" = {default}"
fields[field] = field_class
return fields
def get_modes(ins: dict[str, Any], type: str) -> list[str]:
try:
return [m["code"] for m in ins["modes"][type]["modes"]]
except Exception:
return []
def generate_instrument_configs(ins_s: str, facility: str) -> str:
"""
Generate instrument models based on the output of the OCS
instrument data endpoint. For LCO, this endpoint resides
at https://observe.lco.global/api/instruments/
Args:
ins_s (str): The input json containing instrument data.
facility (str): Which facility to generate instruments for.
Returns:
str: Generated Python Pydantic models as a string.
"""
j_env = Environment(
loader=FileSystemLoader(Path(__file__).parent / "templates"),
trim_blocks=True,
lstrip_blocks=True,
)
template = j_env.get_template("instruments.jinja")
ins_data = json.loads(ins_s)
instruments: list[dict[str, Any]] = []
if facility == "SOAR":
# Soar instruments look like SoarGhtsBluecam, already prefixed, so no need to add a prefix.
prefix = ""
filtered = {k: v for k, v in ins_data.items() if "soar" in k.lower()}
elif facility == "BLANCO":
# Blanco instrument(s) look like BLANCO_NEWFIRM
prefix = ""
filtered = {k: v for k, v in ins_data.items() if "blanco" in k.lower()}
elif facility == "LCO":
# We add a prefix for LCO because some instruments start with a number,
# which is not allowed in Python class names. For example: Lco0M4ScicamQhy600
prefix = "Lco"
filtered = {
k: v
for k, v in ins_data.items()
if "soar" not in k.lower() and "blanco" not in k.lower()
}
elif facility == "SAAO":
# SAAO config doesn't share any instruments with other facilities so we don't need
# to filter it
prefix = "SAAO"
filtered = ins_data
else:
raise ValueError(f"Invalid facility. Must be one of {VALID_FACILITIES}")
# Instruments endpoint seems inconsistent, this should keep our output consistent
ordered = dict(sorted(filtered.items()))
for instrument_type, ins in ordered.items():
instruments.append(
{
"instrument_type": instrument_type,
"class_name": f"{prefix}{textcase.pascal(instrument_type)}",
"config_types": [
c["code"] for c in ins["configuration_types"].values()
],
"readout_modes": get_modes(ins, "readout"),
"acquisition_modes": get_modes(ins, "acquisition"),
"guiding_modes": get_modes(ins, "guiding"),
"rotator_modes": get_modes(ins, "rotator"),
"optical_elements": {
# This gets rid of the silly trailing s on "filters" and "narrowband_g_positions"
k.rstrip("s"): v
for k, v in ins["optical_elements"].items()
},
"configuration_extra_params": get_extra_params_fields(
ins.get("validation_schema", {})
.get("extra_params", {})
.get("schema", {})
),
"instrument_config_extra_params": get_extra_params_fields(
ins.get("validation_schema", {})
.get("instrument_configs", {})
.get("schema", {})
.get("schema", {})
.get("extra_params", {})
.get("schema", {})
),
}
)
return template.render(instruments=instruments, facility=facility)
if __name__ == "__main__":
try:
facility = sys.argv.pop(1)
# Accepts input from stdin or a file argument
with fileinput.input() as f:
ins_json = "".join(list(f))
_ = sys.stdout.write(
generate_instrument_configs(ins_json, facility=facility)
)
except IndexError:
_ = sys.stdout.write("Usage: python generator.py <facility>")
exit(1)