/usr/lib/python3/dist-packages/asdf/schema.py is in python3-asdf 1.3.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 | # Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, unicode_literals, print_function
import datetime
import json
import os
from collections import OrderedDict
import six
from six.moves.urllib import parse as urlparse
from jsonschema import validators as mvalidators
from jsonschema.exceptions import ValidationError
import yaml
from .compat import lru_cache
from . import constants
from . import generic_io
from . import reference
from . import resolver as mresolver
from . import treeutil
from . import util
YAML_SCHEMA_METASCHEMA_ID = 'http://stsci.edu/schemas/yaml-schema/draft-01'
if getattr(yaml, '__with_libyaml__', None): # pragma: no cover
_yaml_base_loader = yaml.CSafeLoader
else: # pragma: no cover
_yaml_base_loader = yaml.SafeLoader
__all__ = ['validate', 'fill_defaults', 'remove_defaults', 'check_schema']
SCHEMA_PATH = os.path.abspath(
os.path.join(os.path.dirname(__file__), 'schemas'))
PYTHON_TYPE_TO_YAML_TAG = {
None: 'null',
six.text_type: 'str',
bytes: 'str',
bool: 'bool',
int: 'int',
float: 'float',
list: 'seq',
dict: 'map',
set: 'set',
OrderedDict: 'omap'
}
if six.PY2: # pragma: no cover
PYTHON_TYPE_TO_YAML_TAG[long] = 'int'
# Prepend full YAML tag prefix
for k, v in PYTHON_TYPE_TO_YAML_TAG.items():
PYTHON_TYPE_TO_YAML_TAG[k] = constants.YAML_TAG_PREFIX + v
def _type_to_tag(type_):
for base in type_.mro():
if base in PYTHON_TYPE_TO_YAML_TAG:
return PYTHON_TYPE_TO_YAML_TAG[base]
def validate_tag(validator, tagname, instance, schema):
# Shortcut: If the instance is a subclass of YAMLObject then we know it
# should have a yaml_tag attribute attached; otherwise we have to use a
# hack of reserializing the object and seeing what tags get attached to it
# (though there may be a better way than this).
if hasattr(instance, '_tag'):
instance_tag = instance._tag
else:
# Try tags for known Python builtins
instance_tag = _type_to_tag(type(instance))
if instance_tag is not None and instance_tag != tagname:
yield ValidationError(
"mismatched tags, wanted '{0}', got '{1}'".format(
tagname, instance_tag))
def validate_propertyOrder(validator, order, instance, schema):
"""
Stores a value on the `tagged.TaggedDict` instance so that
properties can be written out in the preferred order. In that
sense this isn't really a "validator", but using the `jsonschema`
library's extensible validation system is the easiest way to get
this property assigned.
"""
if not validator.is_type(instance, 'object'):
return
if not order:
# propertyOrder may be an empty list
return
instance.property_order = order
def validate_flowStyle(validator, flow_style, instance, schema):
"""
Sets a flag on the `tagged.TaggedList` or `tagged.TaggedDict`
object so that the YAML generator knows which style to use to
write the element. In that sense this isn't really a "validator",
but using the `jsonschema` library's extensible validation system
is the easiest way to get this property assigned.
"""
if not (validator.is_type(instance, 'object') or
validator.is_type(instance, 'array')):
return
instance.flow_style = flow_style
def validate_style(validator, style, instance, schema):
"""
Sets a flag on the `tagged.TaggedString` object so that the YAML
generator knows which style to use to write the string. In that
sense this isn't really a "validator", but using the `jsonschema`
library's extensible validation system is the easiest way to get
this property assigned.
"""
if not validator.is_type(instance, 'string'):
return
instance.style = style
def validate_type(validator, types, instance, schema):
"""
PyYAML returns strings that look like dates as datetime objects.
However, as far as JSON is concerned, this is type==string and
format==date-time. That detects for that case and doesn't raise
an error, otherwise falling back to the default type checker.
"""
if (isinstance(instance, datetime.datetime) and
schema.get('format') == 'date-time' and
'string' in types):
return
return mvalidators.Draft4Validator.VALIDATORS['type'](
validator, types, instance, schema)
YAML_VALIDATORS = util.HashableDict(
mvalidators.Draft4Validator.VALIDATORS.copy())
YAML_VALIDATORS.update({
'tag': validate_tag,
'propertyOrder': validate_propertyOrder,
'flowStyle': validate_flowStyle,
'style': validate_style,
'type': validate_type
})
def validate_fill_default(validator, properties, instance, schema):
if not validator.is_type(instance, 'object'):
return
for property, subschema in six.iteritems(properties):
if "default" in subschema:
instance.setdefault(property, subschema["default"])
for err in mvalidators.Draft4Validator.VALIDATORS['properties'](
validator, properties, instance, schema):
yield err
FILL_DEFAULTS = util.HashableDict()
for key in ('allOf', 'anyOf', 'oneOf', 'items'):
FILL_DEFAULTS[key] = mvalidators.Draft4Validator.VALIDATORS[key]
FILL_DEFAULTS['properties'] = validate_fill_default
def validate_remove_default(validator, properties, instance, schema):
if not validator.is_type(instance, 'object'):
return
for property, subschema in six.iteritems(properties):
if subschema.get("default", None) is not None:
if instance.get(property, None) == subschema["default"]:
del instance[property]
for err in mvalidators.Draft4Validator.VALIDATORS['properties'](
validator, properties, instance, schema):
yield err
REMOVE_DEFAULTS = util.HashableDict()
for key in ('allOf', 'anyOf', 'oneOf', 'items'):
REMOVE_DEFAULTS[key] = mvalidators.Draft4Validator.VALIDATORS[key]
REMOVE_DEFAULTS['properties'] = validate_remove_default
@lru_cache()
def _create_validator(validators=YAML_VALIDATORS):
meta_schema = load_schema(YAML_SCHEMA_METASCHEMA_ID,
mresolver.default_url_mapping)
base_cls = mvalidators.create(meta_schema=meta_schema,
validators=validators)
class ASDFValidator(base_cls):
DEFAULT_TYPES = base_cls.DEFAULT_TYPES.copy()
DEFAULT_TYPES['array'] = (list, tuple)
def iter_errors(self, instance, _schema=None, _seen=set()):
# We can't validate anything that looks like an external reference,
# since we don't have the actual content, so we just have to defer
# it for now. If the user cares about complete validation, they
# can call `AsdfFile.resolve_references`.
if id(instance) in _seen:
return
if _schema is None:
schema = self.schema
else:
schema = _schema
if ((isinstance(instance, dict) and '$ref' in instance) or
isinstance(instance, reference.Reference)):
return
if _schema is None:
tag = getattr(instance, '_tag', None)
if tag is not None:
schema_path = self.ctx.tag_to_schema_resolver(tag)
if schema_path != tag:
s = load_schema(schema_path, self.ctx.url_mapping)
if s:
with self.resolver.in_scope(schema_path):
for x in super(ASDFValidator, self).iter_errors(instance, s):
yield x
if isinstance(instance, dict):
new_seen = _seen | set([id(instance)])
for val in six.itervalues(instance):
for x in self.iter_errors(val, _seen=new_seen):
yield x
elif isinstance(instance, list):
new_seen = _seen | set([id(instance)])
for val in instance:
for x in self.iter_errors(val, _seen=new_seen):
yield x
else:
for x in super(ASDFValidator, self).iter_errors(instance, _schema=schema):
yield x
return ASDFValidator
# We want to load mappings in schema as ordered dicts
class OrderedLoader(_yaml_base_loader):
pass
def construct_mapping(loader, node):
loader.flatten_mapping(node)
return OrderedDict(loader.construct_pairs(node))
OrderedLoader.add_constructor(
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
construct_mapping)
if six.PY2: # pragma: no cover
# Load strings in as Unicode on Python 2
OrderedLoader.add_constructor('tag:yaml.org,2002:str',
OrderedLoader.construct_scalar)
@lru_cache()
def _load_schema(url):
with generic_io.get_file(url) as fd:
if isinstance(url, six.text_type) and url.endswith('json'):
json_data = fd.read().decode('utf-8')
result = json.loads(json_data, object_pairs_hook=OrderedDict)
else:
result = yaml.load(fd, Loader=OrderedLoader)
return result, fd.uri
def _make_schema_loader(resolver):
def load_schema(url):
url = resolver(url)
return _load_schema(url)
return load_schema
def _make_resolver(url_mapping):
handlers = {}
schema_loader = _make_schema_loader(url_mapping)
def get_schema(url):
return schema_loader(url)[0]
for x in ['http', 'https', 'file']:
handlers[x] = get_schema
# We set cache_remote=False here because we do the caching of
# remote schemas here in `load_schema`, so we don't need
# jsonschema to do it on our behalf. Setting it to `True`
# counterintuitively makes things slower.
return mvalidators.RefResolver(
'', {}, cache_remote=False, handlers=handlers)
def _load_draft4_metaschema():
from jsonschema import _utils
return _utils.load_schema('draft4')
# This is a list of schema that we have locally on disk but require
# special methods to obtain
HARDCODED_SCHEMA = {
'http://json-schema.org/draft-04/schema': _load_draft4_metaschema
}
@lru_cache()
def load_schema(url, resolver=None, resolve_references=False):
"""
Load a schema from the given URL.
Parameters
----------
url : str
The path to the schema
resolver : callable, optional
A callback function used to map URIs to other URIs. The
callable must take a string and return a string or `None`.
This is useful, for example, when a remote resource has a
mirror on the local filesystem that you wish to use.
resolve_references : bool, optional
If `True`, resolve all `$ref` references.
"""
if resolver is None:
resolver = mresolver.default_url_mapping
loader = _make_schema_loader(resolver)
if url in HARDCODED_SCHEMA:
schema = HARDCODED_SCHEMA[url]()
else:
schema, url = loader(url)
if resolve_references:
def resolve_refs(node, json_id):
if json_id is None:
json_id = url
if isinstance(node, dict) and '$ref' in node:
suburl = generic_io.resolve_uri(json_id, resolver(node['$ref']))
parts = urlparse.urlparse(suburl)
fragment = parts.fragment
if len(fragment):
suburl_path = suburl[:-(len(fragment) + 1)]
else:
suburl_path = suburl
suburl_path = resolver(suburl_path)
if suburl_path == url:
subschema = schema
else:
subschema = load_schema(suburl_path, resolver, True)
subschema_fragment = reference.resolve_fragment(
subschema, fragment)
return subschema_fragment
return node
schema = treeutil.walk_and_modify(schema, resolve_refs)
return schema
def get_validator(schema={}, ctx=None, validators=None, url_mapping=None,
*args, **kwargs):
"""
Get a JSON schema validator object for the given schema.
The additional *args and **kwargs are passed along to
`jsonschema.validate`.
Parameters
----------
schema : schema, optional
Explicit schema to use. If not provided, the schema to use
is determined by the tag on instance (or subinstance).
ctx : AsdfFile context
Used to resolve tags and urls
validators : dict, optional
A dictionary mapping properties to validators to use (instead
of the built-in ones and ones provided by extension types).
url_mapping : resolver.Resolver, optional
A resolver to convert remote URLs into local ones.
Returns
-------
validator : jsonschema.Validator
"""
if ctx is None:
from .asdf import AsdfFile
ctx = AsdfFile()
if validators is None:
validators = util.HashableDict(YAML_VALIDATORS.copy())
validators.update(ctx._extensions.validators)
kwargs['resolver'] = _make_resolver(url_mapping)
# We don't just call validators.validate() directly here, because
# that validates the schema itself, wasting a lot of time (at the
# time of this writing, it was half of the runtime of the unit
# test suite!!!). Instead, we assume that the schemas are valid
# through the running of the unit tests, not at run time.
cls = _create_validator(validators=validators)
validator = cls(schema, *args, **kwargs)
validator.ctx = ctx
return validator
if six.PY2: # pragma: no cover
def validate_large_literals(instance):
"""
Validate that the tree has no large numeric literals.
"""
# We can count on 52 bits of precision
upper = ((long(1) << 51) - 1)
lower = -((long(1) << 51) - 2)
for instance in treeutil.iter_tree(instance):
if (isinstance(instance, six.integer_types) and
(instance > upper or instance < lower)):
raise ValidationError(
"Integer value {0} is too large to safely represent as a "
"literal in ASDF".format(instance))
else:
def validate_large_literals(instance):
"""
Validate that the tree has no large numeric literals.
"""
# We can count on 52 bits of precision
for instance in treeutil.iter_tree(instance):
if (isinstance(instance, int) and (
instance > ((1 << 51) - 1) or
instance < -((1 << 51) - 2))):
raise ValidationError(
"Integer value {0} is too large to safely represent as a "
"literal in ASDF".format(instance))
def validate(instance, ctx=None, schema={},
validators=None,
*args, **kwargs):
"""
Validate the given instance (which must be a tagged tree) against
the appropriate schema. The schema itself is located using the
tag on the instance.
The additional *args and **kwargs are passed along to
`jsonschema.validate`.
Parameters
----------
instance : tagged tree
ctx : AsdfFile context
Used to resolve tags and urls
schema : schema, optional
Explicit schema to use. If not provided, the schema to use
is determined by the tag on instance (or subinstance).
validators : dict, optional
A dictionary mapping properties to validators to use (instead
of the built-in ones and ones provided by extension types).
"""
if ctx is None:
from .asdf import AsdfFile
ctx = AsdfFile()
validator = get_validator(schema, ctx, validators, ctx.url_mapping,
*args, **kwargs)
validator.validate(instance, _schema=(schema or None))
validate_large_literals(instance)
def fill_defaults(instance, ctx):
"""
For any default values in the schema, add them to the tree if they
don't exist.
Parameters
----------
instance : tagged tree
ctx : AsdfFile context
Used to resolve tags and urls
"""
validate(instance, ctx, validators=FILL_DEFAULTS)
def remove_defaults(instance, ctx):
"""
For any values in the tree that are the same as the default values
specified in the schema, remove them from the tree.
Parameters
----------
instance : tagged tree
ctx : AsdfFile context
Used to resolve tags and urls
"""
validate(instance, ctx, validators=REMOVE_DEFAULTS)
def check_schema(schema):
"""
Check a given schema to make sure it is valid YAML schema.
"""
# We also want to validate the "default" values in the schema
# against the schema itself. jsonschema as a library doesn't do
# this on its own.
def validate_default(validator, default, instance, schema):
if not validator.is_type(instance, 'object'):
return
if 'default' in instance:
with instance_validator.resolver.in_scope(scope):
for err in instance_validator.iter_errors(
instance['default'], instance):
yield err
VALIDATORS = util.HashableDict(
mvalidators.Draft4Validator.VALIDATORS.copy())
VALIDATORS.update({
'default': validate_default
})
meta_schema = load_schema(YAML_SCHEMA_METASCHEMA_ID,
mresolver.default_url_mapping)
resolver = _make_resolver(mresolver.default_url_mapping)
cls = mvalidators.create(meta_schema=meta_schema,
validators=VALIDATORS)
validator = cls(meta_schema, resolver=resolver)
instance_validator = mvalidators.Draft4Validator(schema, resolver=resolver)
scope = schema.get('id', '')
validator.validate(schema, _schema=meta_schema)
|