def __call__(self, *args_call, **kwargs_call): """ This reorders keys to the ontology order. This is not strictly necessary because the information is the same, but helps for testing, because the resulting string is always the same. Notably, it avoids the problem of Python2 which does not preserve the order of **kwargs in a function. It also detects if a key is missing. """ class_keys = lib_util.OntologyClassKeys(self.m_class_name) entity_id = self.m_class_name + "." + ",".join( "%s=%s" % (key, lib_util.Base64EncodeConditional(kwargs_call[key])) for key in class_keys) return entity_id
def _build_entity_id(self, entity_type, *entity_id_arr): """This works only if the attribute values are in the same order as the ontology.""" keys = lib_util.OntologyClassKeys(entity_type) len_keys = len(keys) len_ent_ids = len(entity_id_arr) assert len_keys == len_ent_ids # TODO: See lib_util.EntityUri which does something similar. entity_id = ",".join( "%s=%s" % (key_it, lib_util.Base64EncodeConditional(str(val_it))) for key_it, val_it in zip(keys, entity_id_arr)) return entity_id
def node_from_dict(self, entity_type, entity_id_dict): """ Example of call node_from_dict("CIM_Datafile/portable_executable/section", {"Name": fileName, "Section": sectionName}) TODO: It would help to specific the entity_type from the current directory. So that, when the script is moved, it would not need to be changed. For example: UriMakeFromDictCurrentPackage({ "Name" : fileName, "Section" : sectionName }) Or even: UriMakeFromDictCurrentPackage( Name = fileName, Section = sectionName ) Or even: UriMakeFromDictCurrentPackage(fileName,sectionName) """ # TODO: See lib_util.EntityUri which does something similar. entity_id = ",".join( "%s=%s" % (key_it, lib_util.Base64EncodeConditional(str(val_it))) for key_it, val_it in entity_id_dict.items()) return self.node_from_path(entity_type, entity_id)
def create_instance_path(class_name, **key_value_dict): """ :param class_name: :param key_value_dict: The values in the dictionary might have any type. :return: """ # v might be an integer, a double, a string. # TODO: See lib_util.EntityUri which does something similar. suffix = ",".join([ "%s=%s" % (k, lib_util.Base64EncodeConditional(str(v))) for k, v in key_value_dict.items() ]) if class_name: rest_qry = class_name + "." + suffix else: rest_qry = suffix return rest_qry
def test_encode_base64(self): self.assertEqual(lib_util.Base64EncodeConditional("abc"), "abc") self.assertEqual(lib_util.Base64EncodeConditional("DSN=Excel Files"), "B64_RFNOPUV4Y2VsIEZpbGVz") self.assertEqual(lib_util.Base64EncodeConditional("B64_abcdefgh"), "B64_QjY0X2FiY2RlZmdo")