Passed
Push — master ( 901c2b...6be349 )
by Humberto
02:14
created

kytos.cli.commands.napps.parser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 119
rs 10
c 0
b 0
f 0
ccs 0
cts 28
cp 0
wmc 7

4 Functions

Rating   Name   Duplication   Size   Complexity  
A parse_napps() 0 22 2
A parse() 0 8 2
A parse_napp() 0 31 2
A call() 0 5 1
1
"""kytos - The kytos command line.
2
3
You are at the "napps" command.
4
5
Usage:
6
       kytos napps create [--meta]
7
       kytos napps prepare
8
       kytos napps upload
9
       kytos napps delete    <napp>...
10
       kytos napps list
11
       kytos napps install   <napp>...
12
       kytos napps uninstall <napp>...
13
       kytos napps enable    (all| <napp>...)
14
       kytos napps disable   (all| <napp>...)
15
       kytos napps reload    (all| <napp>...)
16
       kytos napps search    <pattern>
17
       kytos napps -h | --help
18
19
Options:
20
21
  -h, --help    Show this screen.
22
23
Common napps subcommands:
24
25
  create        Create a bootstrap NApp structure for development.
26
  prepare       Prepare NApp to be uploaded (called by "upload").
27
  upload        Upload current NApp to Kytos repository.
28
  delete        Delete NApps from NApps Server.
29
  list          List all NApps installed into your system.
30
  install       Install a local or remote NApp into a controller.
31
  uninstall     Remove a NApp from your controller.
32
  enable        Enable a installed NApp.
33
  disable       Disable a NApp.
34
  reload        Reload NApps code.
35
  search        Search for NApps in NApps Server.
36
37
"""
38
import re
39
import sys
40
41
from docopt import docopt
42
43
from kytos.cli.commands.napps.api import NAppsAPI
44
from kytos.utils.exceptions import KytosException
45
46
47
def parse(argv):
48
    """Parse cli args."""
49
    args = docopt(__doc__, argv=argv)
50
    try:
51
        call(sys.argv[2], args)
52
    except KytosException as exception:
53
        print("Error parsing args: {}".format(exception))
54
        exit()
55
56
57
def call(subcommand, args):
58
    """Call a subcommand passing the args."""
59
    args['<napp>'] = parse_napps(args['<napp>'])
60
    func = getattr(NAppsAPI, subcommand)
61
    func(args)
62
63
64
def parse_napps(napp_ids):
65
    """Return a list of tuples with username, napp_name and version.
66
67
    napp_ids elements are of the form username/name[:version]
68
    (version is optional). If no version is found, it will be None.
69
70
    If napp_ids is equal to 'all', this string will be returned.
71
72
    Args:
73
        napp_ids (list): NApps from the cli.
74
75
    Return:
76
        list: list of tuples with (username, napp_name, version).
77
78
    Raises:
79
        KytosException: If a NApp has not the form _username/name_.
80
81
    """
82
    if 'all' in napp_ids:
83
        return 'all'
84
85
    return [parse_napp(napp_id) for napp_id in napp_ids]
86
87
88
def parse_napp(napp_id):
89
    """Convert a napp_id in tuple with username, napp name and version.
90
91
    Args:
92
        napp_id: String with the form 'username/napp[:version]' (version is
93
                  optional). If no version is found, it will be None.
94
95
    Returns:
96
        tuple: A tuple with (username, napp, version)
97
98
    Raises:
99
        KytosException: If a NApp has not the form _username/name_.
100
101
    """
102
    # `napp_id` regex, composed by two mandatory parts (username, napp_name)
103
    # and one optional (version).
104
    # username and napp_name need to start with a letter, are composed of
105
    # letters, numbers and uderscores and must have at least three characters.
106
    # They are separated by a colon.
107
    # version is optional and can take any format. Is is separated by a hyphen,
108
    # if a version is defined.
109
    regex = r'([a-zA-Z][a-zA-Z0-9_]{2,})/([a-zA-Z][a-zA-Z0-9_]{2,}):?(.+)?'
110
    compiled_regex = re.compile(regex)
111
112
    matched = compiled_regex.fullmatch(napp_id)
113
114
    if not matched:
115
        msg = '"{}" NApp has not the form username/napp_name[:version].'
116
        raise KytosException(msg.format(napp_id))
117
118
    return matched.groups()
119