2 # Copyright 2017 CNIT - Consorzio Nazionale Interuniversitario per le Telecomunicazioni
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
27 logging
.basicConfig(level
=logging
.DEBUG
)
28 log
= logging
.getLogger('lib/util.py')
34 # logging.basicConfig(level=logging.DEBUG)
35 # self.log = logging.getLogger('UtilLogger')
40 def json_load_byteified(cls
, file_handle
):
42 json
.load(file_handle
, object_hook
=cls
._byteify
),
47 def json_loads_byteified(cls
, json_text
):
49 json
.loads(json_text
, object_hook
=cls
._byteify
),
54 def _byteify(cls
, data
, ignore_dicts
= False):
55 # if this is a unicode string, return its string representation
56 if isinstance(data
, unicode):
57 return data
.encode('utf-8')
58 # if this is a list of values, return list of byteified values
59 if isinstance(data
, list):
60 return [ cls
._byteify
(item
, ignore_dicts
=True) for item
in data
]
61 # if this is a dictionary, return dictionary of byteified keys and values
62 # but only if we haven't already byteified it
63 if isinstance(data
, dict) and not ignore_dicts
:
65 cls
._byteify
(key
, ignore_dicts
=True): cls
._byteify
(value
, ignore_dicts
=True)
66 for key
, value
in data
.iteritems()
68 # if it's anything else, return it in its original form
72 def yaml2json(cls
, object_yaml
):
73 """Converts a yaml object into a json representation"""
74 log
.debug('yaml2json')
75 return json
.dumps(object_yaml
, sort_keys
=True, indent
=2) if not object_yaml
is None else None
78 def json2yaml(cls
, object_json
):
79 """Converts a json object into a yaml representation"""
80 log
.debug('json2yaml')
81 return yaml
.safe_dump(object_json
, default_flow_style
=False, indent
=4) if not object_json
is None else None
84 def openfile(cls
, filepath
, mode
='r', buffering
=1):
85 """Returns an open file given a filepath
87 If the filepath is already an open file, returns into
91 log
.debug('reading file ' + filepath
)
93 if isinstance(filepath
, file):
96 return open(filepath
, mode
, buffering
)
99 log
.exception('openfile', e
)
103 def loadyamlfile(cls
, name
):
104 """Returns a yaml object from a filename or an open file
111 if isinstance(name
, file):
112 yaml_object
= yaml
.load(name
)
114 yaml_file
= cls
.openfile(name
)
115 yaml_object
= yaml
.load(yaml_file
)
118 except Exception as e
:
119 log
.exception('Exception loadYamlFile', e
)
123 def loadjsonfile(cls
, name
):
124 """Returns a json object from a filename or an open file
131 #raise IOError('error from throws')
132 if isinstance(name
, file):
133 json_object
= json
.load(name
)
135 # json_file = self.openfile(name)
136 json_file
= cls
.openfile(name
)
137 json_object
= json
.load(json_file
)
140 except Exception as e
:
141 log
.exception('Exception loadJsonFile', e
)
145 def writejsonfile(cls
, name
, json_object
):
146 """Writes the dump of a json obj to a filename or an open file
152 log
.debug('writejsonfile ' + name
)
153 if isinstance(name
, file):
154 json_object
= json
.dump(json_object
, name
)
156 json_file
= cls
.openfile(name
, 'w')
157 json_object
= json
.dump(json_object
, json_file
,separators
=(',',': '), indent
=4)
158 except Exception as e
:
159 log
.exception('Exception writejsonfile', e
)
163 def writeyamlfile(cls
, name
, yaml_object
):
164 """Writes the dump of a yaml obj to a filename or an open file
170 log
.debug('writeyamlfile ' + name
)
171 if isinstance(name
, file):
172 yaml_object
= pyaml
.dump(yaml_object
, name
, safe
=True)
174 yaml_file
= cls
.openfile(name
, 'w')
175 yaml_object
= pyaml
.dump(yaml_object
, yaml_file
, safe
=True)
176 except Exception as e
:
177 log
.exception('Exception writeyamlfile')
181 def validate_json_schema(cls
, reference_schema
, data
):
182 """Validates a json data against a json schema
188 # schema = cls.loadjsonfile("lib/etsi/schemas/"+type_descriptor+".json")
189 #print 'type_descriptor : '+type_descriptor
190 jsonschema
.validate(data
, reference_schema
)
192 except Exception as e
:
193 log
.exception('Exception validate json schema', e
)
197 def get_unique_id(cls
):
198 return uuid
.uuid4().hex[:6].upper()