From fc55d5a99732dbf5403f900dc69eee7cf619da6d Mon Sep 17 00:00:00 2001 From: Matthieu Dorier Date: Fri, 7 Jun 2024 16:25:32 +0100 Subject: [PATCH] added better python APIs --- python/mochi/flock/client.py | 81 ++++++++++++++++++++++++++++++++++++ python/mochi/flock/common.py | 18 ++++++++ python/mochi/flock/server.py | 26 ++++++++++++ python/mochi/flock/view.py | 18 ++++++++ 4 files changed, 143 insertions(+) create mode 100644 python/mochi/flock/client.py create mode 100644 python/mochi/flock/common.py create mode 100644 python/mochi/flock/server.py create mode 100644 python/mochi/flock/view.py diff --git a/python/mochi/flock/client.py b/python/mochi/flock/client.py new file mode 100644 index 0000000..c18e682 --- /dev/null +++ b/python/mochi/flock/client.py @@ -0,0 +1,81 @@ +# (C) 2024 The University of Chicago +# See COPYRIGHT in top-level directory. + + +""" +.. module:: client + :synopsis: This package provides access to the Flock C++ wrapper + +.. moduleauthor:: Matthieu Dorier + + +""" + + +import pyflock_common +import pyflock_client +import pymargo.core +import pymargo + + +class GroupHandle: + + def __init__(self, internal, client): + self._internal = internal + self._client = client + + @property + def client(self): + return self._client + + def update(self): + self._internal.update() + + def view(self): + return self._internal.view + + +class Client: + + def __init__(self, arg): + if isinstance(arg, pymargo.core.Engine): + self._engine = arg + self._owns_engine = False + elif isinstance(arg, str): + self._engine = pymargo.core.Engine(arg, pymargo.client) + self._owns_engine = True + else: + raise TypeError(f'Invalid argument type {type(arg)}') + self._internal = pyflock_client.Client(self._engine.get_internal_mid()) + + def __del__(self): + del self._internal + if self._owns_engine: + self._engine.finalize() + del self._engine + + @property + def mid(self): + return self._internal.margo_instance_id + + @property + def engine(self): + return self._engine + + def make_group_handle(self, address: str|pymargo.core.Address, provider_id: int = 0): + if isinstance(address, pymargo.core.Address): + address = str(address) + return GroupHandle( + self._internal.make_service_handle(address=address, provider_id=provider_id), + self) + + def make_group_handle_from_file(self, filename: str): + return GroupHandle( + self._internal.make_group_handle_from_file(filename=filename), + self) + + def make_group_handle_from_serialized(self, serialized: str): + return GroupHandle( + self._internal.make_group_handle_from_serialized(serialized=serialized), + self) + diff --git a/python/mochi/flock/common.py b/python/mochi/flock/common.py new file mode 100644 index 0000000..5563936 --- /dev/null +++ b/python/mochi/flock/common.py @@ -0,0 +1,18 @@ +# (C) 2024 The University of Chicago +# See COPYRIGHT in top-level directory. + + +""" +.. module:: view + :synopsis: This package provides access to the Flock C++ wrapper + +.. moduleauthor:: Matthieu Dorier + + +""" + + +import pyflock_common + + +FlockException = pyflock_common.Exception diff --git a/python/mochi/flock/server.py b/python/mochi/flock/server.py new file mode 100644 index 0000000..5eac9d6 --- /dev/null +++ b/python/mochi/flock/server.py @@ -0,0 +1,26 @@ +# (C) 2024 The University of Chicago +# See COPYRIGHT in top-level directory. + + +""" +.. module:: server + :synopsis: This package provides access to the Flock C++ wrapper + +.. moduleauthor:: Matthieu Dorier + + +""" + + +import pyflock_common +from .view import GroupView +import pyflock_server +import pymargo.core +import pymargo + + +class Provider: + + def __init__(self, engine: pymargo.core.Engine, provider_id: int, config: str, initial_view: GroupView): + self._internal = pyflock_server.Provider( + self._engine.get_internal_mid(), provider_id, config, initial_view) diff --git a/python/mochi/flock/view.py b/python/mochi/flock/view.py new file mode 100644 index 0000000..bc261bc --- /dev/null +++ b/python/mochi/flock/view.py @@ -0,0 +1,18 @@ +# (C) 2024 The University of Chicago +# See COPYRIGHT in top-level directory. + + +""" +.. module:: view + :synopsis: This package provides access to the Flock C++ wrapper + +.. moduleauthor:: Matthieu Dorier + + +""" + + +import pyflock_common + + +GroupView = pyflock_common.GroupView