1 # -*- coding: utf-8 -*-
4 # Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
5 # This file is part of openmano
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
12 # http://www.apache.org/licenses/LICENSE-2.0
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
25 utils is a module that implements functions that are used by all openmano modules,
26 dealing with aspects such as reading/writing files, formatting inputs/outputs for quick translation
27 from dictionaries to appropriate database dictionaries, etc.
29 __author__
="Alfonso Tierno, Gerardo Garcia"
30 __date__
="$08-sep-2014 12:21:22$"
34 from jsonschema
import validate
as js_v
, exceptions
as js_e
35 #from bs4 import BeautifulSoup
37 def read_file(file_to_read
):
38 """Reads a file specified by 'file_to_read' and returns (True,<its content as a string>) in case of success or (False, <error message>) in case of failure"""
40 f
= open(file_to_read
, 'r')
43 except Exception as e
:
44 return (False, str(e
))
46 return (True, read_data
)
48 def write_file(file_to_write
, text
):
49 """Write a file specified by 'file_to_write' and returns (True,NOne) in case of success or (False, <error message>) in case of failure"""
51 f
= open(file_to_write
, 'w')
54 except Exception as e
:
55 return (False, str(e
))
59 def format_in(http_response
, schema
):
61 client_data
= http_response
.json()
62 js_v(client_data
, schema
)
63 #print "Input data: ", str(client_data)
64 return True, client_data
65 except js_e
.ValidationError
as exc
:
66 print "validate_in error, jsonschema exception ", exc
.message
, "at", exc
.path
67 return False, ("validate_in error, jsonschema exception ", exc
.message
, "at", exc
.path
)
69 def remove_extra_items(data
, schema
):
71 if type(data
) is tuple or type(data
) is list:
73 a
= remove_extra_items(d
, schema
['items'])
74 if a
is not None: deleted
.append(a
)
75 elif type(data
) is dict:
76 #TODO deal with patternProperties
77 if 'properties' not in schema
:
80 if k
not in schema
['properties'].keys():
84 a
= remove_extra_items(data
[k
], schema
['properties'][k
])
85 if a
is not None: deleted
.append({k
:a
})
86 if len(deleted
) == 0: return None
87 elif len(deleted
) == 1: return deleted
[0]
90 #def format_html2text(http_content):
91 # soup=BeautifulSoup(http_content)
92 # text = soup.p.get_text() + " " + soup.pre.get_text()
96 def convert_bandwidth(data
, reverse
=False):
97 '''Check the field bandwidth recursivelly and when found, it removes units and convert to number
98 It assumes that bandwidth is well formed
100 'data': dictionary bottle.FormsDict variable to be checked. None or empty is consideted valid
101 'reverse': by default convert form str to int (Mbps), if True it convert from number to units
105 if type(data
) is dict:
106 for k
in data
.keys():
107 if type(data
[k
]) is dict or type(data
[k
]) is tuple or type(data
[k
]) is list:
108 convert_bandwidth(data
[k
], reverse
)
109 if "bandwidth" in data
:
111 value
=str(data
["bandwidth"])
113 pos
= value
.find("bps")
115 if value
[pos
-1]=="G": data
["bandwidth"] = int(data
["bandwidth"][:pos
-1]) * 1000
116 elif value
[pos
-1]=="k": data
["bandwidth"]= int(data
["bandwidth"][:pos
-1]) / 1000
117 else: data
["bandwidth"]= int(data
["bandwidth"][:pos
-1])
119 value
= int(data
["bandwidth"])
120 if value
% 1000 == 0: data
["bandwidth"]=str(value
/1000) + " Gbps"
121 else: data
["bandwidth"]=str(value
) + " Mbps"
123 print "convert_bandwidth exception for type", type(data
["bandwidth"]), " data", data
["bandwidth"]
125 if type(data
) is tuple or type(data
) is list:
127 if type(k
) is dict or type(k
) is tuple or type(k
) is list:
128 convert_bandwidth(k
, reverse
)
132 def convert_datetime2str(var
):
133 '''Converts a datetime variable to a string with the format '%Y-%m-%dT%H:%i:%s'
134 It enters recursively in the dict var finding this kind of variables
136 if type(var
) is dict:
137 for k
,v
in var
.items():
138 if type(v
) is datetime
.datetime
:
139 var
[k
]= v
.strftime('%Y-%m-%dT%H:%M:%S')
140 elif type(v
) is dict or type(v
) is list or type(v
) is tuple:
141 convert_datetime2str(v
)
142 if len(var
) == 0: return True
143 elif type(var
) is list or type(var
) is tuple:
145 convert_datetime2str(v
)
147 def convert_str2boolean(data
, items
):
148 '''Check recursively the content of data, and if there is an key contained in items, convert value from string to boolean
151 'data': dictionary variable to be checked. None or empty is considered valid
152 'items': tuple of keys to convert
156 if type(data
) is dict:
157 for k
in data
.keys():
158 if type(data
[k
]) is dict or type(data
[k
]) is tuple or type(data
[k
]) is list:
159 convert_str2boolean(data
[k
], items
)
161 if type(data
[k
]) is str:
162 if data
[k
]=="false" or data
[k
]=="False": data
[k
]=False
163 elif data
[k
]=="true" or data
[k
]=="True": data
[k
]=True
164 if type(data
) is tuple or type(data
) is list:
166 if type(k
) is dict or type(k
) is tuple or type(k
) is list:
167 convert_str2boolean(k
, items
)
169 def check_valid_uuid(uuid
):
170 id_schema
= {"type" : "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"}
171 id_schema2
= {"type" : "string", "pattern": "^[a-fA-F0-9]{32}$"}
173 js_v(uuid
, id_schema
)
175 except js_e
.ValidationError
:
177 js_v(uuid
, id_schema2
)
179 except js_e
.ValidationError
:
184 def expand_brackets(text
):
186 Change a text with TEXT[ABC..] into a list with [TEXTA, TEXTB, TEXC, ...
187 if no bracket is used it just return the a list with the single text
188 It uses recursivity to allow several [] in the text
192 start
= text
.find("[")
194 if start
< 0 or end
< 0:
197 for char
in text
[start
+1:end
]:
198 text_list
+= expand_brackets(text
[:start
] + char
+ text
[end
+1:])
201 def deprecated(message
):
202 def deprecated_decorator(func
):
203 def deprecated_func(*args
, **kwargs
):
204 warnings
.warn("{} is a deprecated function. {}".format(func
.__name
__, message
),
205 category
=DeprecationWarning,
207 warnings
.simplefilter('default', DeprecationWarning)
208 return func(*args
, **kwargs
)
209 return deprecated_func
210 return deprecated_decorator