Skip to content

Commit 579f69a

Browse files
committed
moved options to be an attribute on the IOC
Also added options for formatting the pv names.
1 parent ba3a72c commit 579f69a

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

src/fastcs/backends/epics/backend.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@
77

88

99
class EpicsBackend(Backend):
10-
def __init__(self, controller: Controller, pv_prefix: str = "MY-DEVICE-PREFIX"):
10+
def __init__(
11+
self,
12+
controller: Controller,
13+
pv_prefix: str = "MY-DEVICE-PREFIX",
14+
options: EpicsIOCOptions | None = None,
15+
):
1116
super().__init__(controller)
1217

1318
self._pv_prefix = pv_prefix
14-
self._ioc = EpicsIOC(pv_prefix, self._mapping)
19+
options = options or EpicsIOCOptions()
20+
self._ioc = EpicsIOC(pv_prefix, self._mapping, options=options)
1521

1622
def create_docs(self, options: EpicsDocsOptions | None = None) -> None:
1723
EpicsDocs(self._mapping).create_docs(options)
1824

1925
def create_gui(self, options: EpicsGUIOptions | None = None) -> None:
2026
EpicsGUI(self._mapping, self._pv_prefix).create_gui(options)
2127

22-
def _run(self, options: EpicsIOCOptions | None = None):
23-
self._ioc.run(self._dispatcher, self._context, options)
28+
def _run(self):
29+
self._ioc.run(self._dispatcher, self._context)

src/fastcs/backends/epics/ioc.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections.abc import Callable
22
from dataclasses import dataclass
3+
from enum import Enum
34
from types import MethodType
45
from typing import Any, Literal
56

@@ -22,32 +23,41 @@
2223
EPICS_MAX_NAME_LENGTH = 60
2324

2425

26+
class PvNamingConvention(Enum):
27+
NO_CONVERSION = 0
28+
PASCAL = 1
29+
CAPITALIZED = 2
30+
31+
2532
@dataclass
2633
class EpicsIOCOptions:
2734
terminal: bool = True
35+
pv_naming_convention: PvNamingConvention = PvNamingConvention.PASCAL
2836

2937

3038
class EpicsIOC:
31-
def __init__(self, pv_prefix: str, mapping: Mapping):
39+
def __init__(
40+
self, pv_prefix: str, mapping: Mapping, options: EpicsIOCOptions | None = None
41+
):
42+
self.options = options or EpicsIOCOptions()
3243
_add_pvi_info(f"{pv_prefix}:PVI")
3344
_add_sub_controller_pvi_info(pv_prefix, mapping.controller)
3445

3546
_create_and_link_attribute_pvs(pv_prefix, mapping)
36-
_create_and_link_command_pvs(pv_prefix, mapping)
47+
_create_and_link_command_pvs(
48+
pv_prefix, mapping, self.options.pv_naming_convention
49+
)
3750

3851
def run(
3952
self,
4053
dispatcher: AsyncioDispatcher,
4154
context: dict[str, Any],
42-
options: EpicsIOCOptions | None = None,
4355
) -> None:
44-
if options is None:
45-
options = EpicsIOCOptions()
46-
4756
builder.LoadDatabase()
4857
softioc.iocInit(dispatcher)
4958

50-
softioc.interactive_ioc(context)
59+
if self.options.terminal:
60+
softioc.interactive_ioc(context)
5161

5262

5363
def _add_pvi_info(
@@ -222,9 +232,6 @@ async def async_write_display(value: T):
222232
record = _get_output_record(
223233
f"{pv_prefix}:{pv_name}", attribute, on_update=on_update
224234
)
225-
pascal_case_pv_name = pv_name.title()
226-
if pascal_case_pv_name != pv_name:
227-
record.add_alias(f"{pv_prefix}:{pascal_case_pv_name}")
228235

229236
_add_attr_pvi_info(record, pv_prefix, attr_name, "w")
230237

@@ -279,11 +286,23 @@ def _get_output_record(pv: str, attribute: AttrW, on_update: Callable) -> Any:
279286
)
280287

281288

282-
def _create_and_link_command_pvs(pv_prefix: str, mapping: Mapping) -> None:
289+
def _convert_attr_name_to_pv_name(
290+
attr_name: str, naming_convention: PvNamingConvention
291+
) -> str:
292+
if naming_convention == PvNamingConvention.PASCAL:
293+
return attr_name.title().replace("_", "")
294+
elif naming_convention == PvNamingConvention.CAPITALIZED:
295+
return attr_name.upper().replace("_", "-")
296+
return attr_name
297+
298+
299+
def _create_and_link_command_pvs(
300+
pv_prefix: str, mapping: Mapping, naming_convention: PvNamingConvention
301+
) -> None:
283302
for single_mapping in mapping.get_controller_mappings():
284303
path = single_mapping.controller.path
285304
for attr_name, method in single_mapping.command_methods.items():
286-
pv_name = attr_name.title().replace("_", "")
305+
pv_name = _convert_attr_name_to_pv_name(attr_name, naming_convention)
287306
_pv_prefix = ":".join([pv_prefix] + path)
288307
if len(f"{_pv_prefix}:{pv_name}") > EPICS_MAX_NAME_LENGTH:
289308
print(

0 commit comments

Comments
 (0)