From 019b25251581b10067473666f406aa075d456bab Mon Sep 17 00:00:00 2001 From: David Erb Date: Tue, 6 Jun 2023 14:30:44 +0100 Subject: [PATCH] removes contexts --- src/echolocator_api/context_base.py | 35 ------------ src/echolocator_api/guis/aiohttp.py | 51 +++--------------- src/echolocator_api/guis/context.py | 8 +-- src/echolocator_api/guis/guis.py | 5 ++ src/echolocator_cli/subcommands/service.py | 6 +-- src/echolocator_lib/contexts/__init__.py | 0 src/echolocator_lib/contexts/base.py | 62 ---------------------- src/echolocator_lib/guis/context.py | 15 +++--- src/echolocator_lib/guis/guis.py | 21 -------- tests/test_droplocation.py | 7 +-- tests/test_export_to_csv.py | 6 +-- tests/test_export_to_soakdb3.py | 6 +-- tests/test_fetch_image.py | 6 +-- tests/test_fetch_images.py | 6 +-- tests/test_report_plates.py | 6 +-- 15 files changed, 43 insertions(+), 197 deletions(-) delete mode 100644 src/echolocator_api/context_base.py delete mode 100644 src/echolocator_lib/contexts/__init__.py delete mode 100644 src/echolocator_lib/contexts/base.py diff --git a/src/echolocator_api/context_base.py b/src/echolocator_api/context_base.py deleted file mode 100644 index b536781..0000000 --- a/src/echolocator_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/echolocator_api/guis/aiohttp.py b/src/echolocator_api/guis/aiohttp.py index 7abb28a..39d428f 100644 --- a/src/echolocator_api/guis/aiohttp.py +++ b/src/echolocator_api/guis/aiohttp.py @@ -3,60 +3,21 @@ # Class for an aiohttp client. from echolocator_api.aiohttp_client import AiohttpClient -# Dataface protocolj things. -from echolocator_api.guis.constants import Commands, Keywords - logger = logging.getLogger(__name__) # ------------------------------------------------------------------------------------------ -class Aiohttp: +class Aiohttp(AiohttpClient): """ - Object implementing client side API for talking to the echolocator_gui server. + Object implementing client side API for talking to the echolocator_lib gui server. Please see doctopic [A01]. """ # ---------------------------------------------------------------------------------------- - def __init__(self, specification=None): - self.__specification = specification + def __init__(self, specification): - self.__aiohttp_client = AiohttpClient( + # We will get an umbrella specification which must contain an aiohttp_specification within it. + AiohttpClient.__init__( + self, specification["type_specific_tbd"]["aiohttp_specification"], ) - - # ---------------------------------------------------------------------------------------- - def specification(self): - return self.__specification - - # ---------------------------------------------------------------------------------------- - async def report_health(self): - """""" - return await self.__send_protocolj("report_health") - - # ---------------------------------------------------------------------------------------- - async def __send_protocolj(self, function, *args, **kwargs): - """""" - - return await self.__aiohttp_client.client_protocolj( - { - Keywords.COMMAND: Commands.EXECUTE, - Keywords.PAYLOAD: { - "function": function, - "args": args, - "kwargs": kwargs, - }, - }, - ) - - # ---------------------------------------------------------------------------------------- - async def close_client_session(self): - """""" - - if self.__aiohttp_client is not None: - await self.__aiohttp_client.close_client_session() - - # ---------------------------------------------------------------------------------------- - async def client_report_health(self): - """""" - - return await self.__aiohttp_client.client_report_health() diff --git a/src/echolocator_api/guis/context.py b/src/echolocator_api/guis/context.py index e87e73b..12ddb9c 100644 --- a/src/echolocator_api/guis/context.py +++ b/src/echolocator_api/guis/context.py @@ -1,7 +1,7 @@ import logging # Base class. -from echolocator_api.context_base import ContextBase +from dls_utilpack.client_context_base import ClientContextBase # Things created in the context. from echolocator_api.guis.guis import Guis, echolocator_guis_set_default @@ -9,7 +9,7 @@ logger = logging.getLogger(__name__) -class Context(ContextBase): +class Context(ClientContextBase): """ Client context for a echolocator_gui object. On entering, it creates the object according to the specification (a dict). @@ -20,14 +20,14 @@ class Context(ContextBase): # ---------------------------------------------------------------------------------------- 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. echolocator_guis_set_default(self.interface) diff --git a/src/echolocator_api/guis/guis.py b/src/echolocator_api/guis/guis.py index 21b3691..cd4ef1b 100644 --- a/src/echolocator_api/guis/guis.py +++ b/src/echolocator_api/guis/guis.py @@ -28,6 +28,11 @@ def echolocator_guis_get_default(): return __default_echolocator_gui +def echolocator_guis_has_default(): + global __default_echolocator_gui + return __default_echolocator_gui is not None + + # ----------------------------------------------------------------------------------------- diff --git a/src/echolocator_cli/subcommands/service.py b/src/echolocator_cli/subcommands/service.py index 0dd6d8a..4ce51a3 100644 --- a/src/echolocator_cli/subcommands/service.py +++ b/src/echolocator_cli/subcommands/service.py @@ -9,15 +9,15 @@ # Xchembku client context. from xchembku_api.datafaces.context import Context as XchembkuDatafacesContext +# Things created in the context. +from echolocator_api.guis.guis import echolocator_guis_get_default + # Base class for cli subcommands. from echolocator_cli.subcommands.base import ArgKeywords, Base # Rockingest context creator. from echolocator_lib.guis.context import Context as GuiContext -# Things created in the context. -from echolocator_lib.guis.guis import echolocator_guis_get_default - logger = logging.getLogger() diff --git a/src/echolocator_lib/contexts/__init__.py b/src/echolocator_lib/contexts/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/src/echolocator_lib/contexts/base.py b/src/echolocator_lib/contexts/base.py deleted file mode 100644 index 4d91bf1..0000000 --- a/src/echolocator_lib/contexts/base.py +++ /dev/null @@ -1,62 +0,0 @@ -import logging - -# Utilities. -from dls_utilpack.callsign import callsign - -# Base class for a Thing which has a name and traits. -from dls_utilpack.thing import Thing - -logger = logging.getLogger(__name__) - - -class Base(Thing): - """ """ - - # ---------------------------------------------------------------------------------------- - def __init__(self, thing_type, specification=None, predefined_uuid=None): - Thing.__init__(self, thing_type, specification, predefined_uuid=predefined_uuid) - - # Reference to object which is a server, such as BaseAiohttp. - self.server = None - - self.context_specification = self.specification().get("context", {}) - - # ---------------------------------------------------------------------------------------- - async def is_process_started(self): - """""" - - if self.server is None: - raise RuntimeError(f"{callsign(self)} a process has not been defined") - - try: - return await self.server.is_process_started() - except Exception: - raise RuntimeError( - f"unable to determing process started for server {callsign(self.server)}" - ) - - # ---------------------------------------------------------------------------------------- - async def is_process_alive(self): - """""" - - if self.server is None: - raise RuntimeError(f"{callsign(self)} a process has not been defined") - - try: - return await self.server.is_process_alive() - except Exception: - raise RuntimeError( - f"unable to determing dead or alive for server {callsign(self.server)}" - ) - - # ---------------------------------------------------------------------------------------- - async def __aenter__(self): - """ """ - - await self.aenter() - - # ---------------------------------------------------------------------------------------- - async def __aexit__(self, type, value, traceback): - """ """ - - await self.aexit() diff --git a/src/echolocator_lib/guis/context.py b/src/echolocator_lib/guis/context.py index 8ab3e23..1aec2f9 100644 --- a/src/echolocator_lib/guis/context.py +++ b/src/echolocator_lib/guis/context.py @@ -1,10 +1,10 @@ import logging -# Base class which maps flask requests to methods. -from echolocator_lib.contexts.base import Base as ContextBase +# Base class for an asyncio server context. +from dls_utilpack.server_context_base import ServerContextBase # Things created in the context. -from echolocator_lib.guis.guis import Guis, echolocator_guis_set_default +from echolocator_lib.guis.guis import Guis logger = logging.getLogger(__name__) @@ -12,14 +12,14 @@ thing_type = "echolocator_lib.echolocator_guis.context" -class Context(ContextBase): +class Context(ServerContextBase): """ Object representing an event echolocator_dataface connection. """ # ---------------------------------------------------------------------------------------- def __init__(self, specification): - ContextBase.__init__(self, thing_type, specification) + ServerContextBase.__init__(self, thing_type, specification) # ---------------------------------------------------------------------------------------- async def aenter(self): @@ -27,9 +27,6 @@ async def aenter(self): self.server = Guis().build_object(self.specification()) - # If there is more than one gui, the last one defined will be the default. - echolocator_guis_set_default(self.server) - if self.context_specification.get("start_as") == "coro": await self.server.activate_coro() @@ -41,7 +38,7 @@ async def aenter(self): await self.server.start_process() # ---------------------------------------------------------------------------------------- - async def aexit(self): + async def aexit(self, type, value, traceback): """ """ logger.debug(f"[DISSHU] {thing_type} aexit") diff --git a/src/echolocator_lib/guis/guis.py b/src/echolocator_lib/guis/guis.py index 9e8917d..92dde81 100644 --- a/src/echolocator_lib/guis/guis.py +++ b/src/echolocator_lib/guis/guis.py @@ -12,27 +12,6 @@ logger = logging.getLogger(__name__) -# ----------------------------------------------------------------------------------------- -__default_echolocator_gui = None - - -def echolocator_guis_set_default(echolocator_gui): - global __default_echolocator_gui - __default_echolocator_gui = echolocator_gui - - -def echolocator_guis_get_default(): - global __default_echolocator_gui - if __default_echolocator_gui is None: - raise RuntimeError("echolocator_guis_get_default instance is None") - return __default_echolocator_gui - - -def echolocator_guis_has_default(): - global __default_echolocator_gui - return __default_echolocator_gui is not None - - # ----------------------------------------------------------------------------------------- diff --git a/tests/test_droplocation.py b/tests/test_droplocation.py index de6080f..a24ba3b 100644 --- a/tests/test_droplocation.py +++ b/tests/test_droplocation.py @@ -16,15 +16,15 @@ # Client context creator. from echolocator_api.guis.context import Context as GuiClientContext +# Object managing gui +from echolocator_api.guis.guis import echolocator_guis_get_default + # GUI constants. from echolocator_lib.guis.constants import Commands, Cookies, Keywords # Server context creator. from echolocator_lib.guis.context import Context as GuiServerContext -# Object managing gui -from echolocator_lib.guis.guis import echolocator_guis_get_default - # Base class for the tester. from tests.base import Base @@ -91,6 +91,7 @@ async def _main_coroutine(self, constants, output_directory): async with xchembku_client_context: # Start the server context xchembku which starts the process. async with xchembku_server_context: + # Start the servbase (cookies) server. async with servbase_dataface_context: # Start the gui client context. async with gui_client_context: diff --git a/tests/test_export_to_csv.py b/tests/test_export_to_csv.py index 88a0f25..bc56ee4 100644 --- a/tests/test_export_to_csv.py +++ b/tests/test_export_to_csv.py @@ -14,15 +14,15 @@ # Client context creator. from echolocator_api.guis.context import Context as GuiClientContext +# Object managing gui +from echolocator_api.guis.guis import echolocator_guis_get_default + # GUI constants. from echolocator_lib.guis.constants import Commands, Keywords # Server context creator. from echolocator_lib.guis.context import Context as GuiServerContext -# Object managing gui -from echolocator_lib.guis.guis import echolocator_guis_get_default - # Base class for the tester. from tests.base import Base diff --git a/tests/test_export_to_soakdb3.py b/tests/test_export_to_soakdb3.py index 2522a76..5b1d43d 100644 --- a/tests/test_export_to_soakdb3.py +++ b/tests/test_export_to_soakdb3.py @@ -26,15 +26,15 @@ # Client context creator. from echolocator_api.guis.context import Context as GuiClientContext +# Object managing gui +from echolocator_api.guis.guis import echolocator_guis_get_default + # GUI constants. from echolocator_lib.guis.constants import Commands, Keywords # Server context creator. from echolocator_lib.guis.context import Context as GuiServerContext -# Object managing gui -from echolocator_lib.guis.guis import echolocator_guis_get_default - # Base class for the tester. from tests.base import Base diff --git a/tests/test_fetch_image.py b/tests/test_fetch_image.py index c61d30e..c43bc42 100644 --- a/tests/test_fetch_image.py +++ b/tests/test_fetch_image.py @@ -17,15 +17,15 @@ # Client context creator. from echolocator_api.guis.context import Context as GuiClientContext +# Object managing gui +from echolocator_api.guis.guis import echolocator_guis_get_default + # GUI constants. from echolocator_lib.guis.constants import Commands, Cookies, Keywords # Server context creator. from echolocator_lib.guis.context import Context as GuiServerContext -# Object managing gui -from echolocator_lib.guis.guis import echolocator_guis_get_default - # Base class for the tester. from tests.base import Base diff --git a/tests/test_fetch_images.py b/tests/test_fetch_images.py index b7c6d8c..b581597 100644 --- a/tests/test_fetch_images.py +++ b/tests/test_fetch_images.py @@ -18,15 +18,15 @@ # Client context creator. from echolocator_api.guis.context import Context as GuiClientContext +# Object managing gui +from echolocator_api.guis.guis import echolocator_guis_get_default + # GUI constants. from echolocator_lib.guis.constants import Commands, Cookies, Keywords # Server context creator. from echolocator_lib.guis.context import Context as GuiServerContext -# Object managing gui -from echolocator_lib.guis.guis import echolocator_guis_get_default - # Base class for the tester. from tests.base import Base diff --git a/tests/test_report_plates.py b/tests/test_report_plates.py index b5ada73..3a8dab9 100644 --- a/tests/test_report_plates.py +++ b/tests/test_report_plates.py @@ -17,15 +17,15 @@ # Client context creator. from echolocator_api.guis.context import Context as GuiClientContext +# Object managing gui +from echolocator_api.guis.guis import echolocator_guis_get_default + # GUI constants. from echolocator_lib.guis.constants import Commands, Cookies, Keywords # Server context creator. from echolocator_lib.guis.context import Context as GuiServerContext -# Object managing gui -from echolocator_lib.guis.guis import echolocator_guis_get_default - # Base class for the tester. from tests.base import Base