def find(self, reference): """ Resolve a reference using this servicedef as a relative base Returns a jsonschema.Schema instance :param reference: string reference to resolve The `reference` may be one of three supported forms: * `<server><path>#<fragment>` - fully qualified reference * `<path>#<fragment>` - reference is resolved against the same <server> as `servicedef`. <path> starts with '/' * `#<fragment>` - reference is resolved against the same <server> and <path> as `servicedef` :raises InvalidReference: `reference` does not appear to be to the correct syntax """ parsed_reference = urlparse.urlparse(reference) if parsed_reference.netloc or parsed_reference.path: # More than just a fragment, expand the id and find the full # servicedef by id full_reference = Parser.expand_ref(self.id, reference) reference_id = urlparse.urldefrag(full_reference)[0] if reference_id == self.id: servicedef = self else: if self.manager is None: raise NoManager(reference) servicedef = self.manager.find_by_id(reference_id) else: servicedef = self # Now that we have a servicedef, look to the fragment for: # '/resource/<resource_name>/...' # '/types/<type_name>/... p = JsonPointer(parsed_reference.fragment) if p.parts[0] == "resources": schema = servicedef.find_resource(p.parts[1]) elif p.parts[0] == "types": schema = servicedef.find_type(p.parts[1]) else: raise InvalidReference("Expected '/resources' or '/types'", reference) if len(p.parts) > 2 and schema is not None: return schema.by_pointer("/" + "/".join(p.parts[2:])) else: return schema
def test_provider_ref(): expanded = Parser.expand_ref(ID_BASE, ID_ABSPATH_FRAGMENT) assert expanded == '%s%s' % (ID_HOSTNAME, ID_ABSPATH_FRAGMENT)
def test_full_ref(): id_ = '%s%s' % (ID_HOSTNAME, ID_ABSPATH_FRAGMENT) assert Parser.expand_ref(ID_BASE, id_) == id_
def test_local_ref(): assert Parser.expand_ref( ID_BASE, ID_FRAGMENT) == '%s%s' % (ID_BASE, ID_FRAGMENT)