Skip to content

Commit fe41594

Browse files
authored
134 improve module structure (#140)
* Split converters and models into modules * Move tests * Add models to public API * Clean up APIs and private modules
1 parent 101303c commit fe41594

30 files changed

+536
-459
lines changed

docs/how-to/config-server-guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ By default, this will return the file's raw string output - which includes thing
2020
(file-converters)=
2121
# File converters
2222

23-
Converters can be used to turn a file into a standard format server-side, reducing the complexity of reading config files client-side. Converters can convert config files to a `dict` or `pydantic model`, and the same `pydantic model` can be reconstructed client-side by the `get_file_contents` method. Available converters exist [here](https://github.com/DiamondLightSource/daq-config-server/blob/main/converters._converters.py) - see if there's a suitable converter you can use before adding your own. [This dictionary](https://github.com/DiamondLightSource/daq-config-server/blob/main/converters._file_converter_map.py) maps files to converters. Add the path of your config file and a suitable converter to this dictionary and it will automatically be used by the config server when a request for that file is made.
23+
Converters can be used to turn a file into a standard format server-side, reducing the complexity of reading config files client-side. Converters can convert config files to a `dict` or pydantic model, and the same pydantic model can be reconstructed client-side by the `get_file_contents` method. Available converters and models exist [here](https://github.com/DiamondLightSource/daq-config-server/blob/main/src/daq_config_server/models/converters/), divided into modules based on the type of config they convert - see if there's a suitable converter you can use before adding your own. [This dictionary](https://github.com/DiamondLightSource/daq-config-server/blob/main/src/daq_config_server/models/converters/_file_converter_map.py) maps files to converters. Add the path of your config file and a suitable converter to this dictionary and it will automatically be used by the config server when a request for that file is made. Models should be added to this [`__init__.py`](https://github.com/DiamondLightSource/daq-config-server/blob/main/src/daq_config_server/models/__init__.py) so that they can be imported with `from daq_config_server.models import MyModel`.
2424

2525
A request for `str` or `bytes` will fetch the raw file with no conversion.
2626

src/daq_config_server/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
from daq_config_server.constants import (
1616
ENDPOINTS,
1717
)
18-
from daq_config_server.converters import get_converted_file_contents
1918
from daq_config_server.log import set_up_logging
19+
from daq_config_server.models.converters.convert import get_converted_file_contents
2020
from daq_config_server.whitelist import get_whitelist
2121

2222
# See https://github.com/DiamondLightSource/daq-config-server/issues/105

src/daq_config_server/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from requests.exceptions import HTTPError
1111

1212
from daq_config_server.app import ValidAcceptHeaders
13-
from daq_config_server.converters.models import ConfigModel
13+
from daq_config_server.models import ConfigModel
1414

1515
from .constants import ENDPOINTS
1616

src/daq_config_server/converters/__init__.py

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

src/daq_config_server/converters/_converters.py

Lines changed: 0 additions & 82 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from .converters import ConfigModel
2+
from .converters.display_config import (
3+
DisplayConfig,
4+
DisplayConfigData,
5+
)
6+
from .converters.lookup_tables import GenericLookupTable
7+
8+
__all__ = ["ConfigModel", "DisplayConfig", "DisplayConfigData", "GenericLookupTable"]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from ._base_model import ConfigModel
2+
from ._converter_utils import parse_value, remove_comments
3+
4+
__all__ = [
5+
"ConfigModel",
6+
"remove_comments",
7+
"parse_value",
8+
]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from pydantic import BaseModel
2+
3+
4+
class ConfigModel(BaseModel): ...
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import ast
2+
from typing import Any
3+
4+
ALLOWED_BEAMLINE_PARAMETER_STRINGS = ["FB", "FULL", "deadtime"]
5+
6+
7+
class ConverterParseError(Exception): ...
8+
9+
10+
def remove_comments(lines: list[str]) -> list[str]:
11+
return [
12+
line.strip().split("#", 1)[0].strip()
13+
for line in lines
14+
if line.strip().split("#", 1)[0]
15+
]
16+
17+
18+
def parse_value(value: str, convert_to: type | None = None) -> Any:
19+
"""Convert a string value into an appropriate Python type. Optionally provide a type
20+
to convert to. If not given, the type will be inferred.
21+
"""
22+
value = ast.literal_eval(value.replace("Yes", "True").replace("No", "False"))
23+
if convert_to:
24+
value = convert_to(value)
25+
return value

src/daq_config_server/converters/_file_converter_map.py renamed to src/daq_config_server/models/converters/_file_converter_map.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@
33

44
import xmltodict
55

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

1621
FILE_TO_CONVERTER_MAP: dict[str, Callable[[str], ConfigModel | dict[str, Any]]] = { # type: ignore
1722
"/tests/test_data/test_good_lut.txt": undulator_energy_gap_lut, # For system tests # noqa

0 commit comments

Comments
 (0)