本文整理汇总了Python中jsonschema.validators.RefResolver.resolving方法的典型用法代码示例。如果您正苦于以下问题:Python RefResolver.resolving方法的具体用法?Python RefResolver.resolving怎么用?Python RefResolver.resolving使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jsonschema.validators.RefResolver
的用法示例。
在下文中一共展示了RefResolver.resolving方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_cache_remote_on
# 需要导入模块: from jsonschema.validators import RefResolver [as 别名]
# 或者: from jsonschema.validators.RefResolver import resolving [as 别名]
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)
示例2: test_cache_remote_off
# 需要导入模块: from jsonschema.validators import RefResolver [as 别名]
# 或者: from jsonschema.validators.RefResolver import resolving [as 别名]
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
with resolver.resolving(ref):
pass
self.assertEqual(foo_handler.call_count, 2)
示例3: test_if_you_give_it_junk_you_get_a_resolution_error
# 需要导入模块: from jsonschema.validators import RefResolver [as 别名]
# 或者: from jsonschema.validators.RefResolver import resolving [as 别名]
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_custom_uri_scheme_handlers
# 需要导入模块: from jsonschema.validators import RefResolver [as 别名]
# 或者: from jsonschema.validators.RefResolver import resolving [as 别名]
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)
示例5: WalkInstance
# 需要导入模块: from jsonschema.validators import RefResolver [as 别名]
# 或者: from jsonschema.validators.RefResolver import resolving [as 别名]
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
示例6: TestRefResolver
# 需要导入模块: from jsonschema.validators import RefResolver [as 别名]
# 或者: from jsonschema.validators.RefResolver import resolving [as 别名]
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):
#.........这里部分代码省略.........
示例7: SchemaTools
# 需要导入模块: from jsonschema.validators import RefResolver [as 别名]
# 或者: from jsonschema.validators.RefResolver import resolving [as 别名]
#.........这里部分代码省略.........
def validate(self, _data, _schema_ref=None):
"""
Validate the JSON in _data against a JSON schema.
:param _data: The JSON data to validate
:param _schema_ref: If set, validate against the specified schema, and not the one in the data.
:return: the schema object that was validated against.
"""
if _schema_ref is not None:
_json_schema_obj = self.json_schema_objects[_schema_ref]
else:
_json_schema_obj = self.json_schema_objects[_data["schemaRef"]]
self.mongodb_validator.validate(_data, _json_schema_obj)
return _data, _json_schema_obj
def non_base_type(self, _type):
if _type not in ["array", "string", "integer", "object"]:
return [_type]
else:
return []
def resolveSchema(self, _schema):
"""
Recursively resolve all I{$ref} JSON references in a JSON Schema.
:param _schema: A L{dict} with a JSON Schema.
:return: The resolved JSON Schema, a L{dict}.
"""
_result = deepcopy(_schema)
def local_resolve(_obj, _ref_history):
"""
Recurse the JSON-tree and see where there are unresolved remote references.
:param _obj: The node to resolve
:param _ref_history: The previously resolved remote references, for cyclical check
"""
if isinstance(_obj, list):
# Loop any list
for item in _obj:
local_resolve(item, _ref_history)
return
if isinstance(_obj, dict):
if "$ref" in _obj:
_curr_ref = _obj["$ref"]
# Do not resolve local references
if _curr_ref[0] == "#":
return
# Check for cyclical references
if _curr_ref in _ref_history:
raise Exception("Error, cyclical remote reference: " + str(_curr_ref) + ": Formers " + str(_ref_history))
with self.resolver.resolving(_curr_ref) as resolved:
# Resolve the resolved schema
local_resolve(resolved, _ref_history + [_curr_ref])
# Remove the reference
del _obj["$ref"]
# Add the resolved fragment to the schema
_obj.update(resolved)
else:
# Loop all properties
for _key, _value in _obj.items():
if isinstance(_value, dict) and "type" in _value:
local_resolve(_value, _ref_history)
else:
local_resolve(_value, _ref_history)
try:
local_resolve(_result, [])
except Exception as e:
raise Exception("schemaTools.resolveSchema: Error resolving schema:" + str(e) + "Schema " + json.dumps(_schema, indent=4))
# Make top allOf into properties
if "allOf" in _result:
_new_properties = {}
for _curr_properties in _result["allOf"]:
_new_properties.update(_curr_properties["properties"])
_result["properties"] = _new_properties
del _result["allOf"]
_result["$schema"] = "http://json-schema.org/draft-04/schema#"
try:
self.mongodb_validator.check_schema(_result)
except Exception as e:
raise Exception("schemaTools.resolveSchema: error validating resolved schema:" + str(e) + "Schema " + json.dumps(_result, indent=4))
return _result