Skip to content

Commit 9b7f299

Browse files
committed
Split converters and models into modules
1 parent 909b5a6 commit 9b7f299

File tree

17 files changed

+185
-164
lines changed

17 files changed

+185
-164
lines changed
Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import ast
22
from typing import Any
33

4-
from daq_config_server.converters.models import GenericLookupTable
5-
64
ALLOWED_BEAMLINE_PARAMETER_STRINGS = ["FB", "FULL", "deadtime"]
75

86

@@ -25,24 +23,3 @@ def parse_value(value: str, convert_to: type | None = None) -> Any:
2523
if convert_to:
2624
value = convert_to(value)
2725
return value
28-
29-
30-
def parse_lut(contents: str, *params: tuple[str, type | None]) -> GenericLookupTable:
31-
"""Converts a lookup table to a pydantic model, containing the names of each column
32-
and the rows as a 2D list.
33-
34-
Any args after the contents provide the column names and optionally, python types
35-
for values in a column to be converted to. e.g: (energy_EV, float), (pixels, int).
36-
If a type is provided, the values in that column will be converted to that type.
37-
Otherwise, the type will be inferred. Units should be included in the column name.
38-
"""
39-
rows: list[list[Any]] = []
40-
column_names = [param[0] for param in params]
41-
types = [param[1] for param in params]
42-
for line in remove_comments(contents.splitlines()):
43-
if line.startswith("Units"):
44-
continue
45-
rows.append(
46-
[parse_value(value, types[i]) for i, value in enumerate(line.split())]
47-
)
48-
return GenericLookupTable(column_names=column_names, rows=rows)

src/daq_config_server/converters/_converters.py

Lines changed: 0 additions & 82 deletions
This file was deleted.

src/daq_config_server/converters/_file_converter_map.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33

44
import xmltodict
55

6-
from daq_config_server.converters._converters import (
6+
from daq_config_server.converters.beamline_parameters._converters import (
77
beamline_parameters_to_dict,
8+
)
9+
from daq_config_server.converters.display_config._converters import (
10+
display_config_to_model,
11+
)
12+
from daq_config_server.converters.lookup_tables._converters import (
813
beamline_pitch_lut,
914
beamline_roll_lut,
1015
detector_xy_lut,
11-
display_config_to_model,
1216
undulator_energy_gap_lut,
1317
)
1418
from daq_config_server.converters.models import ConfigModel

src/daq_config_server/converters/beamline_parameters/__init__.py

Whitespace-only changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import Any
2+
3+
from daq_config_server.converters._converter_utils import parse_value, remove_comments
4+
5+
ALLOWED_BEAMLINE_PARAMETER_STRINGS = ["FB", "FULL", "deadtime"]
6+
7+
8+
def beamline_parameters_to_dict(contents: str) -> dict[str, Any]:
9+
"""Extracts the key value pairs from a beamline parameters file. If the value
10+
is in ALLOWED_BEAMLINE_PARAMETER_STRINGS, it leaves it as a string. Otherwise, it is
11+
converted to a number or bool."""
12+
lines = contents.splitlines()
13+
config_pairs: dict[str, Any] = {}
14+
15+
# Get dict of parameter keys and values
16+
for line in remove_comments(lines):
17+
splitline = line.split("=")
18+
if len(splitline) >= 2:
19+
param, value = line.split("=")
20+
if param.strip() in config_pairs:
21+
raise ValueError(f"Repeated key in parameters: {param}")
22+
config_pairs[param.strip()] = value.strip()
23+
24+
# Parse each value
25+
for param, value in config_pairs.items():
26+
if value not in ALLOWED_BEAMLINE_PARAMETER_STRINGS:
27+
config_pairs[param] = parse_value(value)
28+
return dict(config_pairs)

src/daq_config_server/converters/beamline_parameters/models.py

Whitespace-only changes.

src/daq_config_server/converters/display_config/__init__.py

Whitespace-only changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from daq_config_server.converters._converter_utils import parse_value, remove_comments
2+
from daq_config_server.converters.display_config.models import (
3+
DisplayConfig,
4+
DisplayConfigData,
5+
)
6+
7+
8+
def display_config_to_model(contents: str) -> DisplayConfig:
9+
lines = contents.splitlines()
10+
config_dict: dict[float, dict[str, int | float]] = {}
11+
zoom_level = None
12+
13+
for line in remove_comments(lines):
14+
key, value = (item.strip() for item in line.split("=", 1))
15+
if key == "zoomLevel":
16+
zoom_level = float(value)
17+
assert zoom_level not in config_dict.keys(), (
18+
f"Multiple instances of zoomLevel {zoom_level}"
19+
)
20+
config_dict[zoom_level] = {}
21+
continue
22+
23+
assert zoom_level is not None, "File must start with a zoom level"
24+
assert key not in config_dict[zoom_level].keys(), (
25+
"File can't have repeated keys for a given zoom level"
26+
)
27+
config_dict[zoom_level][key] = parse_value(value)
28+
zoom_levels = {
29+
key: DisplayConfigData.model_validate(value)
30+
for key, value in config_dict.items()
31+
}
32+
33+
return DisplayConfig(zoom_levels=zoom_levels)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from pydantic import model_validator
2+
3+
from daq_config_server.converters.models import ConfigModel
4+
5+
6+
class DisplayConfigData(ConfigModel):
7+
crosshairX: int
8+
crosshairY: int
9+
topLeftX: int
10+
topLeftY: int
11+
bottomRightX: int
12+
bottomRightY: int
13+
14+
15+
class DisplayConfig(ConfigModel):
16+
zoom_levels: dict[float, DisplayConfigData]
17+
required_zoom_levels: set[float] | None = None
18+
19+
@model_validator(mode="after")
20+
def check_zoom_levels_match_required(self):
21+
existing_keys = set(self.zoom_levels.keys())
22+
if (
23+
self.required_zoom_levels is not None
24+
and self.required_zoom_levels != existing_keys
25+
):
26+
raise ValueError(
27+
f"Zoom levels {existing_keys} "
28+
f"do not match required zoom levels: {self.required_zoom_levels}"
29+
)
30+
return self

src/daq_config_server/converters/lookup_tables/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)