9f758e2d23854e8949f12244bb32936523fb22f4
[dcaegen2/platform/cli.git] / dcae-cli / 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 "when_published" not in record or \
65         "when_added" not in record:
66         return None
67
68     if record["when_revoked"] is not None:
69         return "revoked"
70     elif record["when_published"] is not None:
71         return "published"
72     else:
73         return "staged"
74
75
76 def format_description(description, line_width=50, num_lines=3):
77     """Formats the description field
78
79     Description field can be long. This function line wraps to a specified number
80     of lines. The last line trails with ".." if the text still overflows to
81     signal that there is more.
82     """
83     lines = textwrap.wrap(description)
84     lines = lines[:num_lines]
85     last_line = lines.pop()
86
87     if len(last_line) > line_width and line_width > 2:
88         last_line = "{0}..".format(last_line[:-2])
89
90     lines.append(last_line)
91     return "\n".join(lines)
92
93
94 def format_json(some_json):
95     return json.dumps(some_json, sort_keys=True, indent=4)