本文整理汇总了Python中jsonschema.validators.RefResolver类的典型用法代码示例。如果您正苦于以下问题:Python RefResolver类的具体用法?Python RefResolver怎么用?Python RefResolver使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RefResolver类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_cache_remote_off
def test_cache_remote_off(self):
ref = "foo://bar"
foo_handler = mock.Mock()
resolver = RefResolver("", {}, cache_remote=False, handlers={"foo": foo_handler})
with resolver.resolving(ref):
pass
self.assertEqual(foo_handler.call_count, 1)
示例2: test_custom_uri_scheme_handlers
def test_custom_uri_scheme_handlers(self):
schema = {"foo": "bar"}
ref = "foo://bar"
foo_handler = mock.Mock(return_value=schema)
resolver = RefResolver("", {}, handlers={"foo": foo_handler})
with resolver.resolving(ref) as resolved:
self.assertEqual(resolved, schema)
foo_handler.assert_called_once_with(ref)
示例3: test_if_you_give_it_junk_you_get_a_resolution_error
def test_if_you_give_it_junk_you_get_a_resolution_error(self):
ref = "foo://bar"
foo_handler = mock.Mock(side_effect=ValueError("Oh no! What's this?"))
resolver = RefResolver("", {}, handlers={"foo" : foo_handler})
with self.assertRaises(RefResolutionError) as err:
with resolver.resolving(ref):
pass
self.assertEqual(str(err.exception), "Oh no! What's this?")
示例4: test_cache_remote_on
def test_cache_remote_on(self):
ref = "foo://bar"
foo_handler = mock.Mock()
resolver = RefResolver("", {}, cache_remote=True, handlers={"foo": foo_handler})
with resolver.resolving(ref):
pass
with resolver.resolving(ref):
pass
foo_handler.assert_called_once_with(ref)
示例5: test_it_resolves_local_refs_with_id
def test_it_resolves_local_refs_with_id(self):
schema = {"id": "foo://bar/schema#", "a": {"foo": "bar"}}
resolver = RefResolver.from_schema(schema)
with resolver.resolving("#/a") as resolved:
self.assertEqual(resolved, schema["a"])
with resolver.resolving("foo://bar/schema#/a") as resolved:
self.assertEqual(resolved, schema["a"])
示例6: __init__
def __init__(self, spec_dict, origin_url=None, http_client=None,
config=None):
self.spec_dict = spec_dict
self.origin_url = origin_url
self.http_client = http_client
self.api_url = None
self.config = dict(CONFIG_DEFAULTS, **(config or {}))
# (key, value) = (simple format def name, Model type)
# (key, value) = (#/ format def ref, Model type)
self.definitions = {}
# (key, value) = (simple resource name, Resource)
# (key, value) = (#/ format resource ref, Resource)
self.resources = None
# (key, value) = (simple ref name, param_spec in dict form)
# (key, value) = (#/ format ref name, param_spec in dict form)
self.params = None
# Built on-demand - see get_op_for_request(..)
self._request_to_op_map = None
# (key, value) = (format name, SwaggerFormat)
self.user_defined_formats = {}
self.format_checker = FormatChecker()
self.resolver = RefResolver(
base_uri=origin_url or '',
referrer=self.spec_dict,
handlers=build_http_handlers(http_client))
示例7: test_it_can_construct_a_base_uri_from_a_schema_without_id
def test_it_can_construct_a_base_uri_from_a_schema_without_id(self):
schema = {}
resolver = RefResolver.from_schema(schema)
self.assertEqual(resolver.base_uri, "")
with resolver.resolving("") as resolved:
self.assertEqual(resolved, schema)
with resolver.resolving("#") as resolved:
self.assertEqual(resolved, schema)
示例8: __init__
def __init__(self, name, schema):
self.name = name
self.base_uri = schema.get('id', '')
# replace all relative references with absolute refs to avoid
# clashes in self.definitions
self.schema = Reference.replace_relative_references(self.base_uri, schema)
# maps full jsonschema fragment path to internal structure,
# for example a StructDef or a TypeDef.
self.definitions = {}
self.refresolver = RefResolver.from_schema(schema)
示例9: WalkInstance
class WalkInstance(Walk):
def __init__(self, merger, base, head):
Walk.__init__(self, merger)
self.base_resolver = RefResolver("", base.val)
self.head_resolver = RefResolver("", head.val)
def add_meta(self, head, meta):
if meta is None:
rv = dict()
else:
rv = dict(meta)
rv['value'] = head
return rv
def default_strategy(self, schema, base, head, meta, **kwargs):
if self.is_type(head, "object"):
return "objectMerge"
else:
return "overwrite"
def work(self, strategy, schema, base, head, meta, **kwargs):
assert isinstance(schema, JSONValue)
assert isinstance(base, JSONValue)
assert isinstance(head, JSONValue)
log.debug("work : %sbase %s, head %s" % (self._indent(), base.ref, head.ref))
if not base.is_undef():
with self.base_resolver.resolving(base.ref) as resolved:
assert base.val == resolved
if not head.is_undef():
with self.head_resolver.resolving(head.ref) as resolved:
assert head.val == resolved
rv = strategy.merge(self, base, head, schema, meta, **kwargs)
assert isinstance(rv, JSONValue)
return rv
示例10: test_it_can_construct_a_base_uri_from_a_schema
def test_it_can_construct_a_base_uri_from_a_schema(self):
schema = {"id": "foo"}
resolver = RefResolver.from_schema(schema)
self.assertEqual(resolver.base_uri, "foo")
self.assertEqual(resolver.resolution_scope, "foo")
with resolver.resolving("") as resolved:
self.assertEqual(resolved, schema)
with resolver.resolving("#") as resolved:
self.assertEqual(resolved, schema)
with resolver.resolving("foo") as resolved:
self.assertEqual(resolved, schema)
with resolver.resolving("foo#") as resolved:
self.assertEqual(resolved, schema)
示例11: __init__
def __init__(self, spec_dict, origin_url=None, http_client=None,
config=None):
self.spec_dict = spec_dict
self.origin_url = origin_url
self.http_client = http_client
self.api_url = None
self.config = dict(CONFIG_DEFAULTS, **(config or {}))
# (key, value) = (simple format def name, Model type)
# (key, value) = (#/ format def ref, Model type)
self.definitions = {}
# (key, value) = (simple resource name, Resource)
# (key, value) = (#/ format resource ref, Resource)
self.resources = None
# (key, value) = (simple ref name, param_spec in dict form)
# (key, value) = (#/ format ref name, param_spec in dict form)
self.params = None
# Built on-demand - see get_op_for_request(..)
self._request_to_op_map = None
# (key, value) = (format name, SwaggerFormat)
self.user_defined_formats = {}
self.format_checker = FormatChecker()
self.resolver = RefResolver(
base_uri=origin_url or '',
referrer=self.spec_dict,
handlers=build_http_handlers(http_client),
)
self._validate_config()
if self.config['internally_dereference_refs']:
# If internally_dereference_refs is enabled we do NOT need to resolve references anymore
# it's useless to evaluate is_ref every time
self.deref = lambda ref_dict: ref_dict
else:
self.deref = self._force_deref
示例12: __init__
def __init__(self, _json_schema_folders=[], _uri_handlers=None):
"""
Initiate the SchemaTools class
:param _json_schema_folders: A list of folders where schema files are stored
:param : _uri_handlers: A dict of uri_handlers, resolves a URI prefix to a actual schema.
"""
if not _json_schema_folders:
_json_schema_folders = []
if not _uri_handlers:
self.uri_handlers = {}
else:
self.uri_handlers = _uri_handlers
# All methods that have no handlers should use the cache handler.
for _curr_key, _curr_value in _uri_handlers.items():
if _curr_value is None:
_uri_handlers[_curr_key] = self.cache_handler
self.resolver = RefResolver(base_uri="",
handlers=self.uri_handlers, referrer=None, cache_remote=True)
self.mongodb_validator = MongodbValidator(resolver= self.resolver)
self.json_schema_objects = {}
# Load application specific schemas
for _curr_folder in _json_schema_folders:
_loaded_uris = self.load_schemas_from_directory(os.path.abspath(_curr_folder))
# Resolve all the schemas
for _curr_uri in _loaded_uris:
self.json_schema_objects[_curr_uri] = self.resolveSchema(self.json_schema_objects[_curr_uri])
write_to_log("Schemas loaded and resolved: " +
str.join(", ", ["\"" +_curr_schema["title"] + "\"" for _curr_schema in self.json_schema_objects.values()])
, _category=EC_NOTIFICATION, _severity=SEV_DEBUG)
示例13: check_against_qal_schema
def check_against_qal_schema(_ref, _data):
""" Check JSON against given schema
:param _ref: The name of the schema; "resources"
:param _data: The data to be validated
"""
# First create a resolver to resolve the current schema.
_resolver = RefResolver(base_uri="",
handlers={"qal": qal_uri_handler}, referrer=None, cache_remote=True)
_schema = _resolver.resolve(_ref)[1]
_base_ref = urlparse(_ref).scheme + "://" + urlparse(_ref).netloc
if _ref != _base_ref:
_resolver.referrer = _resolver.resolve(_base_ref)[1]
else:
_resolver.referrer = _schema
_resolver.store[""] = _resolver.referrer
Draft4Validator(schema=_schema, resolver=_resolver).validate(_data)
示例14: setUp
def setUp(self):
self.referrer = {}
self.store = {self.stored_uri : self.stored_schema}
self.resolver = RefResolver(self.base_uri, self.referrer, self.store)
示例15: TestRefResolver
class TestRefResolver(unittest.TestCase):
base_uri = ""
stored_uri = "foo://stored"
stored_schema = {"stored" : "schema"}
def setUp(self):
self.referrer = {}
self.store = {self.stored_uri : self.stored_schema}
self.resolver = RefResolver(self.base_uri, self.referrer, self.store)
def test_it_does_not_retrieve_schema_urls_from_the_network(self):
ref = Draft3Validator.META_SCHEMA["id"]
with mock.patch.object(self.resolver, "resolve_remote") as remote:
with self.resolver.resolving(ref) as resolved:
self.assertEqual(resolved, Draft3Validator.META_SCHEMA)
self.assertFalse(remote.called)
def test_it_resolves_local_refs(self):
ref = "#/properties/foo"
self.referrer["properties"] = {"foo" : object()}
with self.resolver.resolving(ref) as resolved:
self.assertEqual(resolved, self.referrer["properties"]["foo"])
def test_it_resolves_local_refs_with_id(self):
schema = {"id": "foo://bar/schema#", "a": {"foo": "bar"}}
resolver = RefResolver.from_schema(schema)
with resolver.resolving("#/a") as resolved:
self.assertEqual(resolved, schema["a"])
with resolver.resolving("foo://bar/schema#/a") as resolved:
self.assertEqual(resolved, schema["a"])
def test_it_retrieves_stored_refs(self):
with self.resolver.resolving(self.stored_uri) as resolved:
self.assertIs(resolved, self.stored_schema)
self.resolver.store["cached_ref"] = {"foo" : 12}
with self.resolver.resolving("cached_ref#/foo") as resolved:
self.assertEqual(resolved, 12)
def test_it_retrieves_unstored_refs_via_requests(self):
ref = "http://bar#baz"
schema = {"baz" : 12}
with mock.patch("jsonschema.validators.requests") as requests:
requests.get.return_value.json.return_value = schema
with self.resolver.resolving(ref) as resolved:
self.assertEqual(resolved, 12)
requests.get.assert_called_once_with("http://bar")
def test_it_retrieves_unstored_refs_via_urlopen(self):
ref = "http://bar#baz"
schema = {"baz" : 12}
with mock.patch("jsonschema.validators.requests", None):
with mock.patch("jsonschema.validators.urlopen") as urlopen:
urlopen.return_value.read.return_value = (
json.dumps(schema).encode("utf8"))
with self.resolver.resolving(ref) as resolved:
self.assertEqual(resolved, 12)
urlopen.assert_called_once_with("http://bar")
def test_it_can_construct_a_base_uri_from_a_schema(self):
schema = {"id" : "foo"}
resolver = RefResolver.from_schema(schema)
self.assertEqual(resolver.base_uri, "foo")
with resolver.resolving("") as resolved:
self.assertEqual(resolved, schema)
with resolver.resolving("#") as resolved:
self.assertEqual(resolved, schema)
with resolver.resolving("foo") as resolved:
self.assertEqual(resolved, schema)
with resolver.resolving("foo#") as resolved:
self.assertEqual(resolved, schema)
def test_it_can_construct_a_base_uri_from_a_schema_without_id(self):
schema = {}
resolver = RefResolver.from_schema(schema)
self.assertEqual(resolver.base_uri, "")
with resolver.resolving("") as resolved:
self.assertEqual(resolved, schema)
with resolver.resolving("#") as resolved:
self.assertEqual(resolved, schema)
def test_custom_uri_scheme_handlers(self):
schema = {"foo": "bar"}
ref = "foo://bar"
foo_handler = mock.Mock(return_value=schema)
resolver = RefResolver("", {}, handlers={"foo": foo_handler})
with resolver.resolving(ref) as resolved:
self.assertEqual(resolved, schema)
foo_handler.assert_called_once_with(ref)
def test_cache_remote_on(self):
ref = "foo://bar"
foo_handler = mock.Mock()
resolver = RefResolver(
"", {}, cache_remote=True, handlers={"foo" : foo_handler},
)
with resolver.resolving(ref):
#.........这里部分代码省略.........