Passed
Pull Request — master (#73)
by macartur
01:34
created

parse_napp()   A

Complexity

Conditions 4

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 0
loc 7
rs 9.2
1
"""kytos - The kytos command line.
2
3
You are at the "napps" command.
4
5
Usage:
6
       kytos napps create
7
       kytos napps upload
8
       kytos napps delete    <napp>...
9
       kytos napps list
10
       kytos napps install   <napp>...
11
       kytos napps uninstall <napp>...
12
       kytos napps enable    <napp>...
13
       kytos napps disable   <napp>...
14
       kytos napps search    <pattern>
15
       kytos napps -h | --help
16
17
Options:
18
19
  -h, --help    Show this screen.
20
21
Common napps subcommands:
22
23
  create        Create a bootstrap NApp structure for development.
24
  upload        Upload current NApp to Kytos repository.
25
  delete        Delete NApps from NApps Server.
26
  list          List all NApps installed into your system.
27
  install       Install a local or remote NApp into a controller.
28
  uninstall     Remove a NApp from your controller.
29
  enable        Enable a installed NApp.
30
  disable       Disable a NApp.
31
  search        Search for NApps in NApps Server.
32
33
"""
34
import sys
35
36
from docopt import docopt
37
from kytos.cli.commands.napps.api import NAppsAPI
38
from kytos.utils.exceptions import KytosException
39
40
41
def parse(argv):
42
    """Parse cli args."""
43
    args = docopt(__doc__, argv=argv)
44
    try:
45
        call(sys.argv[2], args)
46
    except KytosException as e:
47
        print("Error parsing args: {}".format(e))
48
        exit()
49
50
51
def call(subcommand, args):
52
    """Call a subcommand passing the args."""
53
    args['<napp>'] = parse_napps(args['<napp>'])
54
    func = getattr(NAppsAPI, subcommand)
55
    func(args)
56
57
58
def parse_napps(napp_args):
59
    """Return a list of username and napp_name from the napp list argument.
60
61
    The expected format of a NApp is napp_username/napp_name.
62
63
    Args:
64
        napp_args (list): NApps from the cli.
65
66
    Return:
67
        list: tuples (username, napp_name).
68
69
    Raises:
70
        KytosException: If a NApp has not the form _username/name_.
71
    """
72
    def parse_napp(arg):
73
        """Parse one argument."""
74
        napp = arg.split('/')
75
        size_is_valid = len(napp) != 2 or lennapp[0]) == 0 or lennapp[1]) == 0
76
        if size_is_valid:
77
            msg = '"{}" NApp has not the form username/napp_name.'.format(arg)
78
            raise KytosException(msg)
79
        return tuple(napp)
80
81
    return [parse_napp(arg) for arg in napp_args]
82