| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2017 CNIT - Consorzio Nazionale Interuniversitario per le Telecomunicazioni |
| 3 | # |
| 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 |
| 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 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. |
| 15 | # |
| 16 | |
| 17 | import json |
| 18 | import yaml |
| 19 | import pyaml |
| 20 | import logging |
| 21 | import jsonschema |
| 22 | import uuid |
| 23 | |
| 24 | _lib_name = 'Util' |
| 25 | |
| 26 | |
| 27 | logging.basicConfig(level=logging.DEBUG) |
| 28 | log = logging.getLogger('lib/util.py') |
| 29 | |
| 30 | |
| 31 | class Util(object): |
| 32 | |
| 33 | def __init__(self): |
| 34 | # logging.basicConfig(level=logging.DEBUG) |
| 35 | # self.log = logging.getLogger('UtilLogger') |
| 36 | pass |
| 37 | |
| 38 | |
| 39 | @classmethod |
| 40 | def json_load_byteified(cls, file_handle): |
| 41 | return cls._byteify( |
| 42 | json.load(file_handle, object_hook=cls._byteify), |
| 43 | ignore_dicts=True |
| 44 | ) |
| 45 | |
| 46 | @classmethod |
| 47 | def json_loads_byteified(cls, json_text): |
| 48 | return cls._byteify( |
| 49 | json.loads(json_text, object_hook=cls._byteify), |
| 50 | ignore_dicts=True |
| 51 | ) |
| 52 | |
| 53 | @classmethod |
| 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: |
| 64 | return { |
| 65 | cls._byteify(key, ignore_dicts=True): cls._byteify(value, ignore_dicts=True) |
| 66 | for key, value in data.iteritems() |
| 67 | } |
| 68 | # if it's anything else, return it in its original form |
| 69 | return data |
| 70 | |
| 71 | @classmethod |
| 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 |
| 76 | |
| 77 | @classmethod |
| 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) if not object_json is None else None |
| 82 | |
| 83 | @classmethod |
| 84 | def openfile(cls, filepath, mode='r', buffering=1): |
| 85 | """Returns an open file given a filepath |
| 86 | |
| 87 | If the filepath is already an open file, returns into |
| 88 | Raises Exception |
| 89 | """ |
| 90 | |
| 91 | log.debug('reading file ' + filepath) |
| 92 | try: |
| 93 | if isinstance(filepath, file): |
| 94 | return filepath |
| 95 | else: |
| 96 | return open(filepath, mode, buffering) |
| 97 | |
| 98 | except IOError as e: |
| 99 | log.exception('openfile', e) |
| 100 | raise |
| 101 | |
| 102 | @classmethod |
| 103 | def loadyamlfile(cls, name): |
| 104 | """Returns a yaml object from a filename or an open file |
| 105 | |
| 106 | Raises Exception |
| 107 | """ |
| 108 | |
| 109 | yaml_object = None |
| 110 | try: |
| 111 | if isinstance(name, file): |
| 112 | yaml_object = yaml.load(name) |
| 113 | else: |
| 114 | yaml_file = cls.openfile(name) |
| 115 | yaml_object = yaml.load(yaml_file) |
| 116 | |
| 117 | return yaml_object |
| 118 | except Exception as e: |
| 119 | log.exception('Exception loadYamlFile', e) |
| 120 | raise |
| 121 | |
| 122 | @classmethod |
| 123 | def loadjsonfile(cls, name): |
| 124 | """Returns a json object from a filename or an open file |
| 125 | |
| 126 | Raises Exception |
| 127 | """ |
| 128 | |
| 129 | json_object = None |
| 130 | try: |
| 131 | #raise IOError('error from throws') |
| 132 | if isinstance(name, file): |
| 133 | json_object = json.load(name) |
| 134 | else: |
| 135 | # json_file = self.openfile(name) |
| 136 | json_file = cls.openfile(name) |
| 137 | json_object = json.load(json_file) |
| 138 | |
| 139 | return json_object |
| 140 | except Exception as e: |
| 141 | log.exception('Exception loadJsonFile', e) |
| 142 | raise |
| 143 | |
| 144 | @classmethod |
| 145 | def writejsonfile(cls, name, json_object): |
| 146 | """Writes the dump of a json obj to a filename or an open file |
| 147 | |
| 148 | Raises Exception |
| 149 | """ |
| 150 | |
| 151 | try: |
| 152 | log.debug('writejsonfile ' + name) |
| 153 | if isinstance(name, file): |
| 154 | json_object = json.dump(json_object, name) |
| 155 | else: |
| 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) |
| 160 | raise |
| 161 | |
| 162 | @classmethod |
| 163 | def writeyamlfile(cls, name, yaml_object): |
| 164 | """Writes the dump of a yaml obj to a filename or an open file |
| 165 | |
| 166 | Raises Exception |
| 167 | """ |
| 168 | |
| 169 | try: |
| 170 | log.debug('writeyamlfile ' + name) |
| 171 | if isinstance(name, file): |
| 172 | yaml_object = pyaml.dump(yaml_object, name, safe=True) |
| 173 | else: |
| 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') |
| 178 | raise |
| 179 | |
| 180 | @classmethod |
| 181 | def validate_json_schema(cls, reference_schema, data): |
| 182 | """Validates a json data against a json schema |
| 183 | |
| 184 | Raises Exception |
| 185 | """ |
| 186 | |
| 187 | try: |
| 188 | # schema = cls.loadjsonfile("lib/etsi/schemas/"+type_descriptor+".json") |
| 189 | #print 'type_descriptor : '+type_descriptor |
| 190 | jsonschema.validate(data, reference_schema) |
| 191 | return True |
| 192 | except Exception as e: |
| 193 | log.exception('Exception validate json schema', e) |
| 194 | return False |
| 195 | |
| 196 | @classmethod |
| 197 | def get_unique_id(cls): |
| 198 | return uuid.uuid4().hex[:6].upper() |