From 1aaf8df5be32d755a3f72f9259c66c70fbf850d8 Mon Sep 17 00:00:00 2001 From: Jhon Honce Date: Mon, 14 May 2018 18:01:08 -0700 Subject: Refactor libpod python varlink bindings - More pythonic - Leverage context managers to help with socket leaks - Add system unittest's - Add image unittest's - Add container unittest's - Add models for system, containers and images, and their collections - Add helper functions for datetime parsing/formatting - GetInfo() implemented - Add support for setuptools - Update documentation - Support for Python 3.4-3.6 Signed-off-by: Jhon Honce Closes: #748 Approved by: baude --- contrib/python/podman/client.py | 81 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 contrib/python/podman/client.py (limited to 'contrib/python/podman/client.py') diff --git a/contrib/python/podman/client.py b/contrib/python/podman/client.py new file mode 100644 index 000000000..8a1acdd9b --- /dev/null +++ b/contrib/python/podman/client.py @@ -0,0 +1,81 @@ +"""A client for communicating with a Podman varlink service.""" +import contextlib +import functools + +from varlink import Client as VarlinkClient +from varlink import VarlinkError + +from .libs import cached_property +from .libs.containers import Containers +from .libs.errors import error_factory +from .libs.images import Images +from .libs.system import System + + +class Client(object): + """A client for communicating with a Podman varlink service. + + Example: + + >>> import podman + >>> c = podman.Client() + >>> c.system.versions + """ + + # TODO: Port to contextlib.AbstractContextManager once + # Python >=3.6 required + + def __init__(self, + uri='unix:/run/podman/io.projectatomic.podman', + interface='io.projectatomic.podman'): + """Construct a podman varlink Client. + + uri from default systemd unit file. + interface from io.projectatomic.podman.varlink, do not change unless + you are a varlink guru. + """ + self._podman = None + + @contextlib.contextmanager + def _podman(uri, interface): + """Context manager for API children to access varlink.""" + client = VarlinkClient(address=uri) + try: + iface = client.open(interface) + yield iface + except VarlinkError as e: + raise error_factory(e) from e + finally: + if hasattr(client, 'close'): + client.close() + iface.close() + + self._client = functools.partial(_podman, uri, interface) + + # Quick validation of connection data provided + if not System(self._client).ping(): + raise ValueError('Failed varlink connection "{}/{}"'.format( + uri, interface)) + + def __enter__(self): + """Return `self` upon entering the runtime context.""" + return self + + def __exit__(self, exc_type, exc_value, traceback): + """Raise any exception triggered within the runtime context.""" + return None + + @cached_property + def system(self): + """Manage system model for podman.""" + return System(self._client) + + @cached_property + def images(self): + """Manage images model for libpod.""" + return Images(self._client) + + @cached_property + def containers(self): + """Manage containers model for libpod.""" + return Containers(self._client) -- cgit v1.2.3-54-g00ecf