-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rewrite code which tags complex objects #223
Changes from all commits
a2c3c08
3afa841
8357c54
5fdcdd3
3336fd5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,7 +87,7 @@ def _to_tree_from_model_tree(cls, tree, ctx): | |
except KeyError: | ||
raise ValueError("Unknown operator '{0}'".format(tree.value)) | ||
|
||
node = tagged.tag_object(cls.make_yaml_tag(tag_name), node) | ||
node = tagged.tag_object(cls.make_yaml_tag(tag_name), node, ctx=ctx) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm. The |
||
return node | ||
|
||
@classmethod | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import io | ||
import six | ||
import datetime | ||
from astropy.time import Time | ||
from collections import OrderedDict | ||
|
||
from .. import tagged | ||
from .. import yamlutil | ||
from .. import AsdfFile | ||
from .. import schema as asdf_schema | ||
from jsonschema import ValidationError | ||
|
||
def walk_schema(schema, callback, ctx={}): | ||
def recurse(schema, path, combiner, ctx): | ||
if callback(schema, path, combiner, ctx, recurse): | ||
return | ||
|
||
for c in ['allOf', 'not']: | ||
for sub in schema.get(c, []): | ||
recurse(sub, path, c, ctx) | ||
|
||
for c in ['anyOf', 'oneOf']: | ||
for i, sub in enumerate(schema.get(c, [])): | ||
recurse(sub, path + [i], c, ctx) | ||
|
||
if schema.get('type') == 'object': | ||
for key, val in six.iteritems(schema.get('properties', {})): | ||
recurse(val, path + [key], combiner, ctx) | ||
|
||
if schema.get('type') == 'array': | ||
items = schema.get('items', {}) | ||
if isinstance(items, list): | ||
for i, item in enumerate(items): | ||
recurse(item, path + [i], combiner, ctx) | ||
elif len(items): | ||
recurse(items, path + ['items'], combiner, ctx) | ||
|
||
recurse(schema, [], None, ctx) | ||
|
||
|
||
def flatten_combiners(schema): | ||
newschema = OrderedDict() | ||
|
||
def add_entry(path, schema, combiner): | ||
# TODO: Simplify? | ||
cursor = newschema | ||
for i in range(len(path)): | ||
part = path[i] | ||
if isinstance(part, int): | ||
cursor = cursor.setdefault('items', []) | ||
while len(cursor) <= part: | ||
cursor.append({}) | ||
cursor = cursor[part] | ||
elif part == 'items': | ||
cursor = cursor.setdefault('items', OrderedDict()) | ||
else: | ||
cursor = cursor.setdefault('properties', OrderedDict()) | ||
if i < len(path) - 1 and isinstance(path[i+1], int): | ||
cursor = cursor.setdefault(part, []) | ||
else: | ||
cursor = cursor.setdefault(part, OrderedDict()) | ||
|
||
cursor.update(schema) | ||
|
||
def callback(schema, path, combiner, ctx, recurse): | ||
type = schema.get('type') | ||
schema = OrderedDict(schema) | ||
if type == 'object': | ||
del schema['properties'] | ||
elif type == 'array': | ||
del schema['items'] | ||
if 'allOf' in schema: | ||
del schema['allOf'] | ||
if 'anyOf' in schema: | ||
del schema['anyOf'] | ||
|
||
add_entry(path, schema, combiner) | ||
|
||
walk_schema(schema, callback) | ||
|
||
return newschema | ||
|
||
|
||
def test_time_tag(): | ||
schema = asdf_schema.load_schema('http://stsci.edu/schemas/asdf/time/time-1.0.0', | ||
resolve_references=True) | ||
schema = flatten_combiners(schema) | ||
|
||
date = Time(datetime.datetime.now()) | ||
tree = {'date': date} | ||
asdf = AsdfFile(tree=tree) | ||
instance = yamlutil.custom_tree_to_tagged_tree(tree, asdf) | ||
|
||
try: | ||
asdf_schema.validate(instance, schema=schema) | ||
except ValidationError: | ||
fail = True | ||
else: | ||
fail = False | ||
|
||
assert fail, True | ||
|
||
tag = 'tag:stsci.edu:asdf/time/time-1.0.0' | ||
date = tagged.tag_object(tag, date) | ||
tree = {'date': date} | ||
instance = yamlutil.custom_tree_to_tagged_tree(tree, asdf) | ||
|
||
try: | ||
asdf_schema.validate(instance, schema=schema) | ||
except ValidationError: | ||
fail = True | ||
else: | ||
fail = False | ||
|
||
assert fail, False | ||
|
||
if __name__ == "__main__": | ||
import pdb | ||
pdb.set_trace() | ||
test_time_tag() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't
obj
already containingctx
? Does it need to be passed separately and wouldn't this increase memory usage?