def novabase_to_dict(ref): request_uuid = uuid.uuid1() encoder = Encoder(request_uuid=request_uuid) decoder = Decoder(request_uuid=request_uuid) json_object = encoder.simplify(ref) json_object.pop("_metadata_novabase_classname") return decoder.desimplify(json_object)
class Encoder(object): """A class that is in charge of converting python objects (basic types, dictionnaries, novabase objects, ...) to a representation that can be stored in database.""" def __init__(self, request_uuid=uuid.uuid1()): self.json_encoder = JsonEncoder(request_uuid) self.simple_cache = self.json_encoder.simple_cache self.complex_cache = self.json_encoder.complex_cache self.target_cache = self.json_encoder.target_cache def reset(self): self.json_encoder.reset() def simplify(self, obj): prefix = None is_dict = isinstance(obj, dict) is_list = isinstance(obj, list) if is_dict_and_has_key(obj, "simplify_strategy"): if obj['simplify_strategy'] == 'datetime': prefix = "datetime" if obj['simplify_strategy'] == 'ipnetwork': prefix = "ipnetwork" if obj['simplify_strategy'] == 'novabase': prefix = "novabase" elif is_list: prefix = "list" elif is_dict and obj.has_key("novabase_classname"): prefix = "novabase" elif is_dict and obj.has_key("metadata_novabase_classname"): prefix = "novabase" elif is_dict: prefix = "dict" result = self.json_encoder.simplify(obj) if prefix is not None: return "%s{separator}%s" % (prefix, result) else: return result
class Encoder(object): """A class that is in charge of converting python objects (basic types, dictionnaries, novabase objects, ...) to a representation that can be stored in database.""" def __init__(self, request_uuid=uuid.uuid1()): self.json_encoder = JsonEncoder(request_uuid) self.simple_cache = self.json_encoder.simple_cache self.complex_cache = self.json_encoder.complex_cache self.target_cache = self.json_encoder.target_cache def reset(self): self.json_encoder.reset() def simplify(self, obj): prefix = None is_dict = isinstance(obj, dict) is_list = isinstance(obj, list) if is_dict_and_has_key(obj, "simplify_strategy"): if obj['simplify_strategy'] == 'datetime': prefix = "datetime" if obj['simplify_strategy'] == 'ipnetwork': prefix = "ipnetwork" if obj['simplify_strategy'] == 'novabase': prefix = "novabase" elif is_list: prefix = "list" elif is_dict and obj.has_key("novabase_classname"): prefix = "novabase" elif is_dict and obj.has_key("_metadata_novabase_classname"): prefix = "novabase" elif is_dict: prefix = "dict" result = self.json_encoder.simplify(obj) if prefix is not None: return "%s{separator}%s" % (prefix, result) else: return result
def __init__(self, request_uuid=uuid.uuid1()): self.json_encoder = JsonEncoder(request_uuid) self.simple_cache = self.json_encoder.simple_cache self.complex_cache = self.json_encoder.complex_cache self.target_cache = self.json_encoder.target_cache
def get_encoder(request_uuid=uuid.uuid1()): # if get_config().backend() == "cassandra": # from lib.rome.core.dataformat.string import Encoder # else: from lib.rome.core.dataformat.json import Encoder return Encoder(request_uuid)