Fix pylint issues appeared with version 3.2.2 of pylint
[osm/osmclient.git] / osmclient / cli_commands / utils.py
1 # Copyright ETSI Contributors and Others.
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 import textwrap
17 import logging
18 import yaml
19 from osmclient.common.exceptions import ClientException
20
21 logger = logging.getLogger("osmclient")
22
23
24 def wrap_text(text, width):
25 wrapper = textwrap.TextWrapper(width=width)
26 lines = text.splitlines()
27 return "\n".join(map(wrapper.fill, lines))
28
29
30 def trunc_text(text, length):
31 if len(text) > length:
32 return text[: (length - 3)] + "..."
33 else:
34 return text
35
36
37 def check_client_version(obj, what, version="sol005"):
38 """
39 Checks the version of the client object and raises error if it not the expected.
40
41 :param obj: the client object
42 :what: the function or command under evaluation (used when an error is raised)
43 :return: -
44 :raises ClientError: if the specified version does not match the client version
45 """
46 logger.debug("")
47 if version == "sol005":
48 message = f"The following commands or options are only supported with the version v1 of OSM SOL005': {what}"
49 elif version == "sol005_v2":
50 message = f"The following commands or options are only supported with the version v2 of OSM SOL005': {what}"
51 else:
52 message = f"The following commands or options are only supported with the version {version} of OSM SOL005': {what}"
53 fullclassname = obj.__module__ + "." + obj.__class__.__name__
54 if fullclassname != "osmclient.{}.client.Client".format(version):
55 raise ClientException(message)
56 return
57
58
59 def get_project(project_list, item):
60 # project_list = ctx.obj.project.list()
61 item_project_list = item.get("_admin", {}).get("projects_read")
62 project_id = "None"
63 project_name = "None"
64 if item_project_list:
65 for p1 in item_project_list:
66 project_id = p1
67 for p2 in project_list:
68 if p2["_id"] == project_id:
69 project_name = p2["name"]
70 return project_id, project_name
71 return project_id, project_name
72
73
74 def get_vim_name(vim_list, vim_id):
75 vim_name = "-"
76 for v in vim_list:
77 if v["uuid"] == vim_id:
78 vim_name = v["name"]
79 break
80 return vim_name
81
82
83 def create_config(config_file, json_string):
84 """
85 Combines a YAML or JSON file with a JSON string into a Python3 structure
86 It loads the YAML or JSON file 'cfile' into a first dictionary.
87 It loads the JSON string into a second dictionary.
88 Then it updates the first dictionary with the info in the second dictionary.
89 If the field is present in both cfile and cdict, the field in cdict prevails.
90 If both cfile and cdict are None, it returns an empty dict (i.e. {})
91 """
92 config = {}
93 if config_file:
94 with open(config_file, "r") as cf:
95 config = yaml.safe_load(cf.read())
96 if json_string:
97 cdict = yaml.safe_load(json_string)
98 for k, v in cdict.items():
99 config[k] = v
100 return config