Skip to content

Commit 79110f5

Browse files
committed
Simplify for non-ISIS use
1 parent 23aa11b commit 79110f5

3 files changed

Lines changed: 57 additions & 69 deletions

File tree

p4p_ext/server.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from abc import ABC, abstractmethod
2+
3+
from p4p.server.raw import SharedPV
4+
5+
from p4p_ext.pvrecipe import BasePVRecipe
6+
7+
8+
class BaseSimpleServer(ABC):
9+
"""
10+
Creates PVs and manages their lifetimes
11+
"""
12+
13+
@abstractmethod
14+
def __init__(self, prefix: str = "") -> None:
15+
"""
16+
Initialize the ISIS Server instance.
17+
18+
:param prefix: The prefix to be added to the PVs (Process Variables) of the server e.g. DEV: Defaults to "".
19+
"""
20+
# the prefix determines the prefix of the PVs to be added to the server e.g. DEV:
21+
self.prefix: str = prefix #: The prefix to be prepended to the PVs generated by this server.
22+
self._pvs: dict[str, SharedPV] = {}
23+
24+
@abstractmethod
25+
def add_pv(self, pv_name: str, pv_recipe: BasePVRecipe) -> SharedPV:
26+
"""
27+
Add a PV to the server
28+
29+
:param pv_name: The name of the PV to be added.
30+
:param pv_recipe: The recipe with instructions for creating the PV.
31+
:return: The created PV.
32+
"""
33+
34+
@abstractmethod
35+
def remove_pv(self, pv_name: str) -> None:
36+
"""
37+
Remove a PV from the server
38+
39+
:param pv_name: The name of the PV to be removed.
40+
:raises KeyError: If the PV is not found in the list managed by the server.
41+
"""
42+
43+
@property
44+
def pvlist(self) -> list[str]:
45+
"""The PVs managed by the server"""
46+
return list(self._pvs.keys())
47+
48+
def __getitem__(self, pv_name: str) -> SharedPV | None:
49+
"""Return one of the PVs managed by the server given its name"""
50+
if not pv_name.startswith(self.prefix):
51+
pv_name = self.prefix + pv_name
52+
return self._pvs.get(pv_name)

p4p_ext/thread/server.py

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,36 @@
11
"""
2-
ISISServer is used to create PVs and manage their lifetimes
2+
SimpleServer is used to create PVs and manage their lifetimes
33
"""
44

55
from __future__ import annotations
66

77
import logging
88

9-
from p4p import Value
109
from p4p.client.thread import Context
1110
from p4p.server import Server, StaticProvider
1211
from p4p.server.thread import SharedPV
1312

1413
from p4p_ext.pvrecipe import BasePVRecipe
14+
from p4p_ext.server import BaseSimpleServer
1515

1616
logger = logging.getLogger(__name__)
1717

1818

19-
class SimpleServer:
19+
class SimpleServer(BaseSimpleServer):
2020
"""
2121
Creates PVs and manages their lifetimes
22-
23-
2422
"""
2523

26-
def __init__(self, ioc_name: str, section: str, description: str, prefix="") -> None:
24+
def __init__(self, prefix: str = "") -> None:
2725
"""
2826
Initialize the ISIS Server instance.
2927
30-
:param ioc_name: A PV naming convention compatible name for the IOC (Input/Output Controller).
31-
:param section: The section or group responsible for the server e.g. Controls Software Applications, Diagnostics etc.
32-
:param description: A detailed description of the server and what it does.
3328
:param prefix: The prefix to be added to the PVs (Process Variables) of the server e.g. DEV: Defaults to "".
3429
"""
35-
# provide information for IOC stats PVs
36-
self.ioc_name: str = ioc_name #: The name of the IOC (Input/Output Controller).
37-
self.section: str = section #: The section or group responsible for this server, e.g. Synch RF
38-
self.description: str = description #: A description of the purpose of this server.
39-
# the prefix determines the prefix of the PVs to be added to the server e.g. DEV:
40-
self.prefix: str = prefix #: The prefix to be prepended to the PVs generated by this server.
30+
super().__init__(prefix)
4131

4232
self._provider = StaticProvider()
4333
self._server: Server | None = None
44-
self._pvs: dict[str, SharedPV] = {}
4534

4635
self._running = False
4736

@@ -122,35 +111,3 @@ def remove_pv(self, pv_name: str) -> None:
122111
# from the live system
123112
self._provider.remove(pv_name)
124113
logger.debug("Removed %s from server", pv_name)
125-
126-
@property
127-
def pvlist(self) -> list[str]:
128-
"""The PVs managed by the server"""
129-
return list(self._pvs.keys())
130-
131-
def __getitem__(self, pv_name: str) -> SharedPV | None:
132-
"""Return one of the PVs managed by the server given its name"""
133-
if not pv_name.startswith(self.prefix):
134-
pv_name = self.prefix + pv_name
135-
return self._pvs.get(pv_name)
136-
137-
def get_pv_value(self, pv_name: str) -> Value:
138-
"""
139-
Get the value of a PV using SharedPV.current() if the PV is on this server
140-
or Context.get() if it is not.
141-
"""
142-
if pv_name in self.pvlist:
143-
logger.debug("Getting value using SharedPV for pv %s", pv_name)
144-
shared_pv = self._pvs.get(pv_name, None)
145-
if shared_pv:
146-
return shared_pv.current()
147-
148-
logger.debug("Doing Context.get() for pv %s", pv_name)
149-
return self._ctxt.get(pv_name) # type: ignore
150-
151-
def put_pv_value(self, pv_name: str, value: Value):
152-
"""
153-
Put the value to a PV using the server Context member self._ctxt
154-
"""
155-
logger.debug("Trying putting value %r to pv %s", value, pv_name)
156-
self._ctxt.put(pv_name, value)

tests/unit/thread/test_servers.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,8 @@
1212

1313
def test_server_instantiation():
1414
server = SimpleServer(
15-
ioc_name="TESTIOC",
16-
section="controls testing",
17-
description="server for unit tests",
1815
prefix="DEV:",
1916
)
20-
assert server.ioc_name == "TESTIOC"
21-
assert server.section == "controls testing"
22-
assert server.description == "server for unit tests"
2317
assert server.prefix == "DEV:"
2418

2519
# before we explicitly call `start()`, the server shouldn't exist
@@ -35,9 +29,6 @@ def test_server_instantiation():
3529
)
3630
def test_server_retrieve_pvs(mock_recipe, pv_name):
3731
server = SimpleServer(
38-
ioc_name="TESTIOC",
39-
section="controls testing",
40-
description="server for unit tests",
4132
prefix="DEV:",
4233
)
4334
server.add_pv(pv_name, mock_recipe)
@@ -52,9 +43,6 @@ def test_server_retrieve_pvs(mock_recipe, pv_name):
5243
@patch("p4p_ext.thread.server.SimpleServer", autospec=True)
5344
def test_server_start(server, provider, caplog, mock_isispv):
5445
test_server = SimpleServer(
55-
ioc_name="TESTIOC",
56-
section="controls testing",
57-
description="server for unit tests",
5846
prefix="DEV:",
5947
)
6048

@@ -77,9 +65,6 @@ def test_server_start(server, provider, caplog, mock_isispv):
7765
@patch("p4p_ext.pvrecipe.PVScalarRecipe", autospec=True)
7866
def test_server_add_pv(recipe, server, provider, caplog):
7967
test_server = SimpleServer(
80-
ioc_name="TESTIOC",
81-
section="controls testing",
82-
description="server for unit tests",
8368
prefix="DEV:",
8469
)
8570

@@ -99,9 +84,6 @@ def test_server_add_pv(recipe, server, provider, caplog):
9984
@patch("p4p_ext.thread.server.SimpleServer", autospec=True)
10085
def test_server_stop(server, provider, caplog, mock_isispv):
10186
test_server = SimpleServer(
102-
ioc_name="TESTIOC",
103-
section="controls testing",
104-
description="server for unit tests",
10587
prefix="DEV:",
10688
)
10789

@@ -123,9 +105,6 @@ def test_server_stop(server, provider, caplog, mock_isispv):
123105
@patch("p4p_ext.pvrecipe.PVScalarRecipe", autospec=True)
124106
def test_server_remove_pv(recipe, server, provider, caplog, mock_isispv):
125107
test_server = SimpleServer(
126-
ioc_name="TESTIOC",
127-
section="controls testing",
128-
description="server for unit tests",
129108
prefix="DEV:",
130109
)
131110

0 commit comments

Comments
 (0)