1 # -*- coding: utf-8 -*-
4 # Copyright 2015 Telefónica Investigación 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$"
33 from jsonschema
import validate
as js_v
, exceptions
as js_e
34 #from bs4 import BeautifulSoup
36 def read_file(file_to_read
):
37 """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"""
39 f
= open(file_to_read
, 'r')
42 except Exception as e
:
43 return (False, str(e
))
45 return (True, read_data
)
47 def write_file(file_to_write
, text
):
48 """Write a file specified by 'file_to_write' and returns (True,NOne) in case of success or (False, <error message>) in case of failure"""
50 f
= open(file_to_write
, 'w')
53 except Exception as e
:
54 return (False, str(e
))
58 def format_in(http_response
, schema
):
60 client_data
= http_response
.json()
61 js_v(client_data
, schema
)
62 #print "Input data: ", str(client_data)
63 return True, client_data
64 except js_e
.ValidationError
as exc
:
65 print "validate_in error, jsonschema exception ", exc
.message
, "at", exc
.path
66 return False, ("validate_in error, jsonschema exception ", exc
.message
, "at", exc
.path
)
68 def remove_extra_items(data
, schema
):
70 if type(data
) is tuple or type(data
) is list:
72 a
= remove_extra_items(d
, schema
['items'])
73 if a
is not None: deleted
.append(a
)
74 elif type(data
) is dict:
75 #TODO deal with patternProperties
76 if 'properties' not in schema
:
79 if k
not in schema
['properties'].keys():
83 a
= remove_extra_items(data
[k
], schema
['properties'][k
])
84 if a
is not None: deleted
.append({k
:a
})
85 if len(deleted
) == 0: return None
86 elif len(deleted
) == 1: return deleted
[0]
89 #def format_html2text(http_content):
90 # soup=BeautifulSoup(http_content)
91 # text = soup.p.get_text() + " " + soup.pre.get_text()
95 def convert_bandwidth(data
, reverse
=False):
96 '''Check the field bandwidth recursivelly and when found, it removes units and convert to number
97 It assumes that bandwidth is well formed
99 'data': dictionary bottle.FormsDict variable to be checked. None or empty is consideted valid
100 'reverse': by default convert form str to int (Mbps), if True it convert from number to units
104 if type(data
) is dict:
105 for k
in data
.keys():
106 if type(data
[k
]) is dict or type(data
[k
]) is tuple or type(data
[k
]) is list:
107 convert_bandwidth(data
[k
], reverse
)
108 if "bandwidth" in data
:
110 value
=str(data
["bandwidth"])
112 pos
= value
.find("bps")
114 if value
[pos
-1]=="G": data
["bandwidth"] = int(data
["bandwidth"][:pos
-1]) * 1000
115 elif value
[pos
-1]=="k": data
["bandwidth"]= int(data
["bandwidth"][:pos
-1]) / 1000
116 else: data
["bandwidth"]= int(data
["bandwidth"][:pos
-1])
118 value
= int(data
["bandwidth"])
119 if value
% 1000 == 0: data
["bandwidth"]=str(value
/1000) + " Gbps"
120 else: data
["bandwidth"]=str(value
) + " Mbps"
122 print "convert_bandwidth exception for type", type(data
["bandwidth"]), " data", data
["bandwidth"]
124 if type(data
) is tuple or type(data
) is list:
126 if type(k
) is dict or type(k
) is tuple or type(k
) is list:
127 convert_bandwidth(k
, reverse
)
131 def convert_datetime2str(var
):
132 '''Converts a datetime variable to a string with the format '%Y-%m-%dT%H:%i:%s'
133 It enters recursively in the dict var finding this kind of variables
135 if type(var
) is dict:
136 for k
,v
in var
.items():
137 if type(v
) is datetime
.datetime
:
138 var
[k
]= v
.strftime('%Y-%m-%dT%H:%M:%S')
139 elif type(v
) is dict or type(v
) is list or type(v
) is tuple:
140 convert_datetime2str(v
)
141 if len(var
) == 0: return True
142 elif type(var
) is list or type(var
) is tuple:
144 convert_datetime2str(v
)
146 def convert_str2boolean(data
, items
):
147 '''Check recursively the content of data, and if there is an key contained in items, convert value from string to boolean
150 'data': dictionary variable to be checked. None or empty is considered valid
151 'items': tuple of keys to convert
155 if type(data
) is dict:
156 for k
in data
.keys():
157 if type(data
[k
]) is dict or type(data
[k
]) is tuple or type(data
[k
]) is list:
158 convert_str2boolean(data
[k
], items
)
160 if type(data
[k
]) is str:
161 if data
[k
]=="false" or data
[k
]=="False": data
[k
]=False
162 elif data
[k
]=="true" or data
[k
]=="True": data
[k
]=True
163 if type(data
) is tuple or type(data
) is list:
165 if type(k
) is dict or type(k
) is tuple or type(k
) is list:
166 convert_str2boolean(k
, items
)
168 def check_valid_uuid(uuid
):
169 id_schema
= {"type" : "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"}
170 id_schema2
= {"type" : "string", "pattern": "^[a-fA-F0-9]{32}$"}
172 js_v(uuid
, id_schema
)
174 except js_e
.ValidationError
:
176 js_v(uuid
, id_schema2
)
178 except js_e
.ValidationError
: