summaryrefslogtreecommitdiff
path: root/contrib/python/pypodman/lib/action_base.py
blob: ff2922262499ff2588db96cf28e1d46aca4a8548 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""Base class for all actions of remote client."""
import abc
from functools import lru_cache

import podman


class AbstractActionBase(abc.ABC):
    """Base class for all actions of remote client."""

    @classmethod
    @abc.abstractmethod
    def subparser(cls, parser):
        """Define parser for this action.  Subclasses must implement.

        API:
        Use set_defaults() to set attributes "class_" and "method". These will
        be invoked as class_(parsed_args).method()
        """
        parser.add_argument(
            '--all',
            action='store_true',
            help=('list all items.'
                  ' (default: no-op, included for compatibility.)'))
        parser.add_argument(
            '--no-trunc',
            '--notruncate',
            action='store_false',
            dest='truncate',
            default=True,
            help='Display extended information. (default: False)')
        parser.add_argument(
            '--noheading',
            action='store_false',
            dest='heading',
            default=True,
            help=('Omit the table headings from the output.'
                  ' (default: False)'))
        parser.add_argument(
            '--quiet',
            action='store_true',
            help='List only the IDs. (default: %(default)s)')

    def __init__(self, args):
        """Construct class."""
        self._args = args

    @property
    def remote_uri(self):
        """URI for remote side of connection."""
        return self._args.remote_uri

    @property
    def local_uri(self):
        """URI for local side of connection."""
        return self._args.local_uri

    @property
    def identity_file(self):
        """Key for authenication."""
        return self._args.identity_file

    @property
    @lru_cache(maxsize=1)
    def client(self):
        """Podman remote client for communicating."""
        if self._args.host is None:
            return podman.Client(
                uri=self.local_uri)
        else:
            return podman.Client(
                uri=self.local_uri,
                remote_uri=self.remote_uri,
                identity_file=self.identity_file)

    def __repr__(self):
        """Compute the “official” string representation of object."""
        return ("{}(local_uri='{}', remote_uri='{}',"
                " identity_file='{}')").format(
                    self.__class__,
                    self.local_uri,
                    self.remote_uri,
                    self.identity_file,
                )