diff options
author | Jhon Honce <jhonce@redhat.com> | 2018-05-14 18:01:08 -0700 |
---|---|---|
committer | Atomic Bot <atomic-devel@projectatomic.io> | 2018-05-16 14:01:10 +0000 |
commit | 1aaf8df5be32d755a3f72f9259c66c70fbf850d8 (patch) | |
tree | 6e573fc7ae988e36d6d94973cec238aee72feaad /contrib/python/podman/client.py | |
parent | c7bc7580a6a9faf6a3159d6c17ff1dfb3710e318 (diff) | |
download | podman-1aaf8df5be32d755a3f72f9259c66c70fbf850d8.tar.gz podman-1aaf8df5be32d755a3f72f9259c66c70fbf850d8.tar.bz2 podman-1aaf8df5be32d755a3f72f9259c66c70fbf850d8.zip |
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 <jhonce@redhat.com>
Closes: #748
Approved by: baude
Diffstat (limited to 'contrib/python/podman/client.py')
-rw-r--r-- | contrib/python/podman/client.py | 81 |
1 files changed, 81 insertions, 0 deletions
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) |