diff --git a/src/dls_servbase_api/context_base.py b/src/dls_servbase_api/context_base.py deleted file mode 100644 index b536781..0000000 --- a/src/dls_servbase_api/context_base.py +++ /dev/null @@ -1,35 +0,0 @@ -import logging - -logger = logging.getLogger(__name__) - - -class ContextBase: - """ """ - - # ---------------------------------------------------------------------------------------- - def __init__(self, specification): - self.__specification = specification - self.__interface = None - - # ---------------------------------------------------------------------------------------- - def get_interface(self): - return self.__interface - - def set_interface(self, interface): - self.__interface = interface - - interface = property(get_interface, set_interface) - - # ---------------------------------------------------------------------------------------- - async def __aenter__(self): - """ """ - - await self.aenter() - - return self.interface - - # ---------------------------------------------------------------------------------------- - async def __aexit__(self, type, value, traceback): - """ """ - - await self.aexit() diff --git a/src/dls_servbase_api/datafaces/context.py b/src/dls_servbase_api/datafaces/context.py index 2f11209..f76fb86 100644 --- a/src/dls_servbase_api/datafaces/context.py +++ b/src/dls_servbase_api/datafaces/context.py @@ -1,5 +1,8 @@ import logging +# Base class. +from dls_utilpack.client_context_base import ClientContextBase + # Things created in the context. from dls_servbase_api.datafaces.datafaces import ( Datafaces, @@ -9,48 +12,39 @@ logger = logging.getLogger(__name__) -class Context: +class Context(ClientContextBase): """ Client context for a dls_servbase_dataface object. On entering, it creates the object according to the specification (a dict). On exiting, it closes client connection. - The aenter and aexit methods are exposed for use by an enclosing context. + The aenter and aexit methods are exposed for use by an enclosing context and the base class. """ # ---------------------------------------------------------------------------------------- def __init__(self, specification): - self.__specification = specification - self.__dls_servbase_dataface = None - - # ---------------------------------------------------------------------------------------- - async def __aenter__(self): - """ """ - - await self.aenter() - - # ---------------------------------------------------------------------------------------- - async def __aexit__(self, type, value, traceback): - """ """ - - await self.aexit() + ClientContextBase.__init__(self, specification) # ---------------------------------------------------------------------------------------- async def aenter(self): """ """ # Build the object according to the specification. - self.__dls_servbase_dataface = Datafaces().build_object(self.__specification) + self.interface = Datafaces().build_object(self.specification) # If there is more than one dataface, the last one defined will be the default. - dls_servbase_datafaces_set_default(self.__dls_servbase_dataface) + dls_servbase_datafaces_set_default(self.interface) + + # Open client session to the service or direct connection. + await self.interface.open_client_session() # ---------------------------------------------------------------------------------------- async def aexit(self): """ """ - if self.__dls_servbase_dataface is not None: - await self.__dls_servbase_dataface.close_client_session() + if self.interface is not None: + # Close client session to the service or direct connection. + await self.interface.close_client_session() # Clear the global variable. Important between pytests. dls_servbase_datafaces_set_default(None) diff --git a/src/dls_servbase_api/guis/context.py b/src/dls_servbase_api/guis/context.py index 12d97fb..58a1b19 100644 --- a/src/dls_servbase_api/guis/context.py +++ b/src/dls_servbase_api/guis/context.py @@ -1,7 +1,7 @@ import logging # Base class. -from dls_servbase_api.context_base import ContextBase +from dls_utilpack.client_context_base import ClientContextBase # Things created in the context. from dls_servbase_api.guis.guis import Guis, dls_servbase_guis_set_default @@ -9,25 +9,25 @@ logger = logging.getLogger(__name__) -class Context(ContextBase): +class Context(ClientContextBase): """ Client context for a dls_servbase_gui object. On entering, it creates the object according to the specification (a dict). On exiting, it closes client connection. - The aenter and aexit methods are exposed for use by an enclosing context. + The aenter and aexit methods are exposed for use by an enclosing context and the base class. """ # ---------------------------------------------------------------------------------------- def __init__(self, specification): - self.__specification = specification + ClientContextBase.__init__(self, specification) # ---------------------------------------------------------------------------------------- async def aenter(self): """ """ # Build the object according to the specification. - self.interface = Guis().build_object(self.__specification) + self.interface = Guis().build_object(self.specification) # If there is more than one gui, the last one defined will be the default. dls_servbase_guis_set_default(self.interface)