def test_get_link_for_sanity(self): ''' Verifies that source and target are set... ''' link_string = parser.get_link_str(self.link1) link = parser.get_link(link_string, self.source, self.registry) self.assertEquals(link.kind, self.network_link) self.assertEquals(link.source, self.source) self.assertEquals(link.target, self.target) # 4 = 1 attr + core.id + core.src + core.target self.assertTrue(len(link.attributes) == 4) # identifier checks... link_string = parser.get_link_str(self.link1) link = parser.get_link(link_string, self.source, self.registry) self.assertEquals(link.identifier, '/link/1') tmp = link_string.split('; ') tmp.pop(2) link_string = '; '.join(tmp) link = parser.get_link(link_string, self.source, self.registry) self.assertEquals(link.identifier, None)
def _to_entity(data, def_kind, registry): ''' Extract an entity from the HTTP data object. kind -- The kind definition. registry -- The registry. ''' # disable 'Too many local vars' pylint check (It's a bit ugly but will do) # disable 'Too many branches' pylint check (Needs to be improved) # pylint: disable=R0914,R0912 kind = None mixins = [] # first kind & mixins kind_found = False for category_string in data.categories: category = parser.get_category(category_string.strip(), registry.get_categories()) if repr(category) == 'kind' and not kind_found: kind = category kind_found = True else: mixins.append(category) # the attributes attributes = {} for attr_string in data.attributes: key, value = parser.get_attributes(attr_string) attributes[key] = value # now create the entity if kind_found is False and def_kind is None: raise AttributeError('Could not find a valid kind.') elif def_kind is not None: kind = def_kind if Resource.kind in kind.related: # links entity = Resource(None, kind, mixins, []) for link_string in data.links: entity.links.append( parser.get_link(link_string.strip(), entity, registry)) elif Link.kind in kind.related: try: source_attr = attributes['occi.core.source'] target_attr = attributes['occi.core.target'] if source_attr.find(registry.get_hostname()) == 0: source_attr = source_attr.replace(registry.get_hostname(), '') if target_attr.find(registry.get_hostname()) == 0: target_attr = target_attr.replace(registry.get_hostname(), '') source = registry.get_resource(source_attr) target = registry.get_resource(target_attr) except KeyError: raise AttributeError('Both occi.core.[source, target]' + ' attributes need to be resources.') entity = Link(None, kind, mixins, source, target) else: raise AttributeError('This kind seems not to be related to either' + ' resource or link.') entity.attributes = attributes return entity
def _to_entity(data, def_kind, registry): """ Extract an entity from the HTTP data object. kind -- The kind definition. registry -- The registry. """ # disable 'Too many local vars' pylint check (It's a bit ugly but will do) # disable 'Too many branches' pylint check (Needs to be improved) # pylint: disable=R0914,R0912 kind = None mixins = [] # first kind & mixins kind_found = False for category_string in data.categories: category = parser.get_category(category_string.strip(), registry.get_categories()) if repr(category) == "kind" and not kind_found: kind = category kind_found = True else: mixins.append(category) # the attributes attributes = {} for attr_string in data.attributes: key, value = parser.get_attributes(attr_string) attributes[key] = value # now create the entity if kind_found is False and def_kind is None: raise AttributeError("Could not find a valid kind.") elif def_kind is not None: kind = def_kind if Resource.kind in kind.related: # links entity = Resource(None, kind, mixins, []) for link_string in data.links: entity.links.append(parser.get_link(link_string.strip(), entity, registry)) elif Link.kind in kind.related: try: source_attr = attributes["occi.core.source"] target_attr = attributes["occi.core.target"] if source_attr.find(registry.get_hostname()) == 0: source_attr = source_attr.replace(registry.get_hostname(), "") if target_attr.find(registry.get_hostname()) == 0: target_attr = target_attr.replace(registry.get_hostname(), "") source = registry.get_resource(source_attr) target = registry.get_resource(target_attr) except KeyError: raise AttributeError("Both occi.core.[source, target]" + " attributes need to be resources.") entity = Link(None, kind, mixins, source, target) else: raise AttributeError("This kind seems not to be related to either" + " resource or link.") entity.attributes = attributes return entity