Test Failed
Pull Request — master (#324)
by
unknown
07:52 queued 03:21
created

kytos.cli.commands.napps.parser.parse_napp()   A

Complexity

Conditions 2

Size

Total Lines 31
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 8
nop 1
dl 0
loc 31
rs 10
c 0
b 0
f 0
ccs 0
cts 8
cp 0
crap 6
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.config import KytosConfig
45
from kytos.utils.exceptions import KytosException
46
47
48
def parse(argv):
49
    """Parse cli args."""
50
    args = docopt(__doc__, argv=argv)
51
    try:
52
        call(sys.argv[2], args)
53
    except KytosException as exception:
54
        print("Error parsing args: {}".format(exception))
55
        sys.exit()
56
57
58
def call(subcommand, args):
59
    """Call a subcommand passing the args."""
60
    KytosConfig.check_versions()
61
    args['<napp>'] = parse_napps(args['<napp>'])
62
    func = getattr(NAppsAPI, subcommand)
63
    func(args)
64
65
66
def parse_napps(napp_ids):
67
    """Return a list of tuples with username, napp_name and version.
68
69
    napp_ids elements are of the form username/name[:version]
70
    (version is optional). If no version is found, it will be None.
71
72
    If napp_ids is equal to 'all', this string will be returned.
73
74
    Args:
75
        napp_ids (list): NApps from the cli.
76
77
    Return:
78
        list: list of tuples with (username, napp_name, version).
79
80
    Raises:
81
        KytosException: If a NApp has not the form _username/name_.
82
83
    """
84
    if 'all' in napp_ids:
85
        return 'all'
86
87
    return [parse_napp(napp_id) for napp_id in napp_ids]
88
89
90
def parse_napp(napp_id):
91
    """Convert a napp_id in tuple with username, napp name and version.
92
93
    Args:
94
        napp_id: String with the form 'username/napp[:version]' (version is
95
                  optional). If no version is found, it will be None.
96
97
    Returns:
98
        tuple: A tuple with (username, napp, version)
99
100
    Raises:
101
        KytosException: If a NApp has not the form _username/name_.
102
103
    """
104
    # `napp_id` regex, composed by two mandatory parts (username, napp_name)
105
    # and one optional (version).
106
    # username and napp_name need to start with a letter, are composed of
107
    # letters, numbers and uderscores and must have at least three characters.
108
    # They are separated by a colon.
109
    # version is optional and can take any format. Is is separated by a hyphen,
110
    # if a version is defined.
111
    regex = r'([a-zA-Z][a-zA-Z0-9_]{2,})/([a-zA-Z][a-zA-Z0-9_]{2,}):?(.+)?'
112
    compiled_regex = re.compile(regex)
113
114
    matched = compiled_regex.fullmatch(napp_id)
115
116
    if not matched:
117
        msg = '"{}" NApp has not the form username/napp_name[:version].'
118
        raise KytosException(msg.format(napp_id))
119
120
    return matched.groups()
121