1 # ============LICENSE_START=======================================================
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
19 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 # -*- coding: utf-8 -*-
23 Provides utilities for commands
27 from terminaltables import AsciiTable
29 from dcae_cli.util import DcaeException
32 def parse_input(input_):
33 '''Returns (name, version) tuple parsed from name:version'''
34 arg = input_.split(':')
36 cname, cver = arg[0], None
39 cver = None if not cver else cver
41 raise DcaeException("Input '{:}' must be NAME or NAME:VERSION".format(input_))
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)
50 def create_table(header, entries):
51 '''Returns an ASCII table string'''
56 data.append(['']*len(header))
57 return AsciiTable(data).table
60 # Utility methods used to format records for displaying
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:
69 if record["when_revoked"] is not None:
71 elif record["when_published"] is not None:
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:
83 if record["whenRevoked"] is not None:
85 elif record["whenPublished"] is not None:
90 def format_description(description, line_width=50, num_lines=3):
91 """Formats the description field
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.
99 lines = textwrap.wrap(description)
100 lines = lines[:num_lines]
101 last_line = lines.pop()
103 if len(last_line) > line_width and line_width > 2:
104 last_line = "{0}..".format(last_line[:-2])
106 lines.append(last_line)
107 return "\n".join(lines)
110 def format_json(some_json):
111 return json.dumps(some_json, sort_keys=True, indent=4)