def get_link(link_string, source, registry): ''' Create a Link from a string rendering. Also note that the link_id is set but is not yet registered as resource. link_string -- A string rendering of a link. source -- The source entity. registry -- Registry used for this call. ''' tmp = link_string.find('<') + 1 target_id = link_string[tmp:link_string.rfind('>', tmp)].strip() try: link_id = find_in_string(link_string, 'self') except AttributeError: link_id = None try: tmp_category = find_in_string(link_string, 'category') except AttributeError: raise AttributeError('Could not determine the Category of the Link.') tempus = tmp_category.split('#') link_category = get_category( tempus[1].strip() + ';scheme="' + tempus[0].strip() + '#"', registry.get_categories()) attributes = {} attr_begin = link_string.find('category="') + 12 + len(tmp_category) attributes_str = link_string[attr_begin:] for attribute in attributes_str.split(';'): tmp = attribute.strip().split('=') if len(tmp) == 2: attributes[tmp[0].strip()] = tmp[1].rstrip('"').lstrip('"').strip() try: if target_id.find(registry.get_hostname()) == 0: target_id = target_id.replace(registry.get_hostname(), '') target = registry.get_resource(target_id) except KeyError: raise AttributeError('The target for the link cannot be found: ' + target_id) link = Link(link_id, link_category, [], source, target) link.attributes = attributes return link
def get_link(link_string, source, registry): ''' Create a Link from a string rendering. Also note that the link_id is set but is not yet registered as resource. link_string -- A string rendering of a link. source -- The source entity. registry -- Registry used for this call. ''' tmp = link_string.find('<') + 1 target_id = link_string[tmp:link_string.rfind('>', tmp)].strip() try: link_id = find_in_string(link_string, 'self') except AttributeError: link_id = None try: tmp_category = find_in_string(link_string, 'category') except AttributeError: raise AttributeError('Could not determine the Category of the Link.') tempus = tmp_category.split('#') link_category = get_category(tempus[1].strip() + ';scheme="' + tempus[0].strip() + '#"', registry.get_categories()) attributes = {} attr_begin = link_string.find('category="') + 12 + len(tmp_category) attributes_str = link_string[attr_begin:] for attribute in attributes_str.split(';'): tmp = attribute.strip().split('=') if len(tmp) == 2: attributes[tmp[0].strip()] = tmp[1].rstrip('"').lstrip('"').strip() try: if target_id.find(registry.get_hostname()) == 0: target_id = target_id.replace(registry.get_hostname(), '') target = registry.get_resource(target_id) except KeyError: raise AttributeError('The target for the link cannot be found: ' + target_id) link = Link(link_id, link_category, [], source, target) link.attributes = attributes return link
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())