File: cmds.py

package info (click to toggle)
python-dcos 0.2.0-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,440 kB
  • sloc: python: 8,196; sh: 194; makefile: 36
file content (45 lines) | stat: -rw-r--r-- 1,376 bytes parent folder | download | duplicates (4)
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
import collections

from dcos.errors import DCOSException

Command = collections.namedtuple(
    'Command',
    ['hierarchy', 'arg_keys', 'function'])
"""Describe a CLI command.

:param hierarchy: the noun and verbs that need to be set for the command to
                  execute
:type hierarchy: list of str
:param arg_keys: the arguments that must get passed to the function; the order
                 of the keys determines the order in which they get passed to
                 the function
:type arg_keys: list of str
:param function: the function to execute
:type function: func(args) -> int
"""


def execute(cmds, args):
    """Executes one of the commands based on the arguments passed.

    :param cmds: commands to try to execute; the order determines the order of
                 evaluation
    :type cmds: list of Command
    :param args: command line arguments
    :type args: dict
    :returns: the process status
    :rtype: int
    """

    for hierarchy, arg_keys, function in cmds:
        # Let's find if the function matches the command
        match = True
        for positional in hierarchy:
            if not args[positional]:
                match = False

        if match:
            params = [args[name] for name in arg_keys]
            return function(*params)

    raise DCOSException('Could not find a command with the passed arguments')