[DCAE] INFO.yaml update
[dcaegen2/platform.git] / mod / onboardingapi / dcae_cli / commands / util.py
1 # ============LICENSE_START=======================================================
2 # org.onap.dcae
3 # ================================================================================
4 # Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=========================================================
18 #
19 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
20
21 # -*- coding: utf-8 -*-
22 """
23 Provides utilities for commands
24 """
25 import json
26 import textwrap
27 from terminaltables import AsciiTable
28
29 from dcae_cli.util import DcaeException
30
31
32 def parse_input(input_):
33     '''Returns (name, version) tuple parsed from name:version'''
34     arg = input_.split(':')
35     if len(arg) == 1:
36         cname, cver = arg[0], None
37     elif len(arg) == 2:
38         cname, cver = arg
39         cver = None if not cver else cver
40     else:
41         raise DcaeException("Input '{:}' must be NAME or NAME:VERSION".format(input_))
42     return cname, cver
43
44
45 def parse_input_pair(req, resp):
46     '''Returns a tuple output of `parse_input` for convenience'''
47     return parse_input(req), parse_input(resp)
48
49
50 def create_table(header, entries):
51     '''Returns an ASCII table string'''
52     data = [header, ]
53     if entries:
54         data.extend(entries)
55     else:
56         data.append(['']*len(header))
57     return AsciiTable(data).table
58
59
60 # Utility methods used to format records for displaying
61
62 def get_status_string(record):
63     """Get the status label given a record of either data format or component"""
64     if "when_revoked"   not in record or \
65        "when_published" not in record or \
66        "when_added"     not in record:
67         return None
68
69     if record["when_revoked"] is not None:
70         return "revoked"
71     elif record["when_published"] is not None:
72         return "published"
73     else:
74         return "unpublished"
75
76 def get_status_string_camel(record):
77     """Get the status label given a record of either data format or component, in camelCase"""
78     if "whenRevoked"   not in record or \
79        "whenPublished" not in record or \
80        "whenAdded"     not in record:
81         return None
82
83     if record["whenRevoked"] is not None:
84         return "revoked"
85     elif record["whenPublished"] is not None:
86         return "published"
87     else:
88         return "unpublished"
89     
90 def format_description(description, line_width=50, num_lines=3):
91     """Formats the description field
92
93     Description field can be long. This function line wraps to a specified number
94     of lines. The last line trails with ".." if the text still overflows to
95     signal that there is more.
96     """
97     if not description:
98         return ''
99     lines = textwrap.wrap(description)
100     lines = lines[:num_lines]
101     last_line = lines.pop()
102
103     if len(last_line) > line_width and line_width > 2:
104         last_line = "{0}..".format(last_line[:-2])
105
106     lines.append(last_line)
107     return "\n".join(lines)
108
109
110 def format_json(some_json):
111     return json.dumps(some_json, sort_keys=True, indent=4)