|
1 | 1 | import logging |
2 | 2 |
|
| 3 | +# Base class. |
| 4 | +from dls_utilpack.client_context_base import ClientContextBase |
| 5 | + |
3 | 6 | # Things created in the context. |
4 | 7 | from dls_servbase_api.datafaces.datafaces import ( |
5 | 8 | Datafaces, |
|
9 | 12 | logger = logging.getLogger(__name__) |
10 | 13 |
|
11 | 14 |
|
12 | | -class Context: |
| 15 | +class Context(ClientContextBase): |
13 | 16 | """ |
14 | 17 | Client context for a dls_servbase_dataface object. |
15 | 18 | On entering, it creates the object according to the specification (a dict). |
16 | 19 | On exiting, it closes client connection. |
17 | 20 |
|
18 | | - The aenter and aexit methods are exposed for use by an enclosing context. |
| 21 | + The aenter and aexit methods are exposed for use by an enclosing context and the base class. |
19 | 22 | """ |
20 | 23 |
|
21 | 24 | # ---------------------------------------------------------------------------------------- |
22 | 25 | def __init__(self, specification): |
23 | | - self.__specification = specification |
24 | | - self.__dls_servbase_dataface = None |
25 | | - |
26 | | - # ---------------------------------------------------------------------------------------- |
27 | | - async def __aenter__(self): |
28 | | - """ """ |
29 | | - |
30 | | - await self.aenter() |
31 | | - |
32 | | - # ---------------------------------------------------------------------------------------- |
33 | | - async def __aexit__(self, type, value, traceback): |
34 | | - """ """ |
35 | | - |
36 | | - await self.aexit() |
| 26 | + ClientContextBase.__init__(self, specification) |
37 | 27 |
|
38 | 28 | # ---------------------------------------------------------------------------------------- |
39 | 29 | async def aenter(self): |
40 | 30 | """ """ |
41 | 31 |
|
42 | 32 | # Build the object according to the specification. |
43 | | - self.__dls_servbase_dataface = Datafaces().build_object(self.__specification) |
| 33 | + self.interface = Datafaces().build_object(self.specification) |
44 | 34 |
|
45 | 35 | # If there is more than one dataface, the last one defined will be the default. |
46 | | - dls_servbase_datafaces_set_default(self.__dls_servbase_dataface) |
| 36 | + dls_servbase_datafaces_set_default(self.interface) |
| 37 | + |
| 38 | + # Open client session to the service or direct connection. |
| 39 | + await self.interface.open_client_session() |
47 | 40 |
|
48 | 41 | # ---------------------------------------------------------------------------------------- |
49 | 42 | async def aexit(self): |
50 | 43 | """ """ |
51 | 44 |
|
52 | | - if self.__dls_servbase_dataface is not None: |
53 | | - await self.__dls_servbase_dataface.close_client_session() |
| 45 | + if self.interface is not None: |
| 46 | + # Close client session to the service or direct connection. |
| 47 | + await self.interface.close_client_session() |
54 | 48 |
|
55 | 49 | # Clear the global variable. Important between pytests. |
56 | 50 | dls_servbase_datafaces_set_default(None) |
0 commit comments