def setUp(self): self.kind = Kind('http://example.com/foo#', 'bar') self.link_kind = Kind('http://example.com/foo#', 'link') self.mixin = Mixin('http://example.com/foo#', 'mixin') target = Resource('/foo/target', self.kind, [], []) source = Resource('/foo/src', self.kind, [self.mixin], []) source.attributes = {'foo': 'bar'} link = Link('/link/foo', self.link_kind, [], source, target) link.attributes = {'foo': 'bar'} source.links = [link] self.resources = [source, target, link] for item in self.resources: self.registry.add_resource(item.identifier, item) self.registry.set_backend(self.kind, KindBackend()) self.registry.set_backend(self.mixin, MixinBackend())
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