def test_tag_object(): class SomeObject(object): pass tag = 'tag:nowhere.org:none/some/thing' instance = tagged.tag_object(tag, SomeObject()) assert instance._tag == tag
def _to_tree_from_model_tree(cls, tree, ctx): if not isinstance(tree.left, CompoundModel): left = yamlutil.custom_tree_to_tagged_tree(tree.left, ctx) else: left = cls._to_tree_from_model_tree(tree.left, ctx) if not isinstance(tree.right, CompoundModel): if isinstance(tree.right, dict): right = { 'keys': list(tree.right.keys()), 'values': list(tree.right.values()) } else: right = yamlutil.custom_tree_to_tagged_tree(tree.right, ctx) else: right = cls._to_tree_from_model_tree(tree.right, ctx) node = {'forward': [left, right]} try: tag_name = 'transform/' + _operator_to_tag_mapping[tree.op] except KeyError: raise ValueError(f"Unknown operator '{tree.op}'") node = tagged.tag_object(cls.make_yaml_tag(tag_name), node, ctx=ctx) return node
def callback(subschema, path, combiner, ctx, recurse): tag = subschema.get('tag') if tag is not None: cursor = tree for part in path[:-1]: if included(cursor, part): cursor = cursor[part] else: return part = path[-1] if included(cursor, part): cursor[part] = tagged.tag_object(tag, cursor[part])
def to_tree_tagged(cls, frame, ctx): if type(frame) not in frame_transform_graph.frame_set: raise ValueError("Can only save frames that are registered with the " "transformation graph.") node = {} if frame.has_data: node['data'] = custom_tree_to_tagged_tree(frame.data, ctx) frame_attributes = {} for attr in frame.frame_attributes.keys(): value = getattr(frame, attr, None) if value is not None: frame_attributes[attr] = value node['frame_attributes'] = custom_tree_to_tagged_tree(frame_attributes, ctx) return tagged.tag_object(cls._frame_name_to_tag(frame.name), node, ctx=ctx)
def _cast(val, schema): val = _unmake_node(val) if val is not None: if 'datatype' in schema: val = util.gentle_asarray( val, ndarray.asdf_datatype_to_numpy_dtype(schema['datatype'])) if 'ndim' in schema and len(val.shape) != schema['ndim']: raise ValueError( "Array has wrong number of dimensions. Expected {0}, got {1}". format(schema['ndim'], len(val.shape))) if 'max_ndim' in schema and len(val.shape) > schema['max_ndim']: raise ValueError( "Array has wrong number of dimensions. Expected <= {0}, got {1}" .format(schema['max_ndim'], len(val.shape))) tag = schema.get('tag') if tag is not None: val = tagged.tag_object(tag, val) return val
def _fits_keyword_loader(hdulist, fits_keyword, schema, hdu_index, known_keywords): hdu_name = _get_hdu_name(schema) try: hdu = get_hdu(hdulist, hdu_name, hdu_index) except AttributeError: return None try: val = hdu.header[fits_keyword] except KeyError: return None tag = schema.get('tag') if tag is not None: val = tagged.tag_object(tag, val) known_keywords.setdefault(hdu, set()).add(fits_keyword) return val
def _cast(val, schema): val = _unmake_node(val) if val is not None: if 'datatype' in schema: val = util.gentle_asarray( val, ndarray.asdf_datatype_to_numpy_dtype(schema['datatype'])) if 'ndim' in schema and len(val.shape) != schema['ndim']: raise ValueError( "Array has wrong number of dimensions. Expected {0}, got {1}".format( schema['ndim'], len(val.shape))) if 'max_ndim' in schema and len(val.shape) > schema['max_ndim']: raise ValueError( "Array has wrong number of dimensions. Expected <= {0}, got {1}".format( schema['max_ndim'], len(val.shape))) tag = schema.get('tag') if tag is not None: val = tagged.tag_object(tag, val) return val
def test_time_tag(): schema = asdf_schema.load_schema( 'http://stsci.edu/schemas/asdf/time/time-1.1.0', resolve_references=True) schema = _flatten_combiners(schema) date = time.Time(datetime.datetime.now()) tree = {'date': date} asdf = AsdfFile(tree=tree) instance = yamlutil.custom_tree_to_tagged_tree(tree['date'], asdf) asdf_schema.validate(instance, schema=schema) tag = 'tag:stsci.edu:asdf/time/time-1.1.0' date = tagged.tag_object(tag, date) tree = {'date': date} asdf = AsdfFile(tree=tree) instance = yamlutil.custom_tree_to_tagged_tree(tree['date'], asdf) asdf_schema.validate(instance, schema=schema)
def _to_tree_from_model_tree(cls, tree, ctx): if tree.left.isleaf: left = yamlutil.custom_tree_to_tagged_tree(tree.left.value, ctx) else: left = cls._to_tree_from_model_tree(tree.left, ctx) if tree.right.isleaf: right = yamlutil.custom_tree_to_tagged_tree(tree.right.value, ctx) else: right = cls._to_tree_from_model_tree(tree.right, ctx) node = {'forward': [left, right]} try: tag_name = 'transform/' + _operator_to_tag_mapping[tree.value] except KeyError: raise ValueError("Unknown operator '{0}'".format(tree.value)) node = tagged.tag_object(cls.make_yaml_tag(tag_name), node, ctx=ctx) return node
def to_tree_tagged(cls, model, ctx): left = model.left if isinstance(model.right, dict): right = { 'keys': list(model.right.keys()), 'values': list(model.right.values()) } else: right = model.right node = {'forward': [left, right]} try: tag_name = 'transform/' + _operator_to_tag_mapping[model.op] except KeyError: raise ValueError(f"Unknown operator '{model.op}'") node = tagged.tag_object(cls.make_yaml_tag(tag_name), node, ctx=ctx) cls._to_tree_base_transform_members(model, node, ctx) return node
def to_tree_transform(self, model): left = model.left if isinstance(model.right, dict): right = { 'keys': list(model.right.keys()), 'values': list(model.right.values()) } else: right = model.right node = {'forward': [left, right]} try: tag_name = ('http://asdf-format.org/schemas/transform/' + _operator_to_tag_mapping[model.op] + '-2.0.0') except KeyError: raise ValueError(f"Unknown operator '{model.op}'") node = tagged.tag_object(tag_name, node) return node
def _to_tree_from_model_tree(cls, tree, ctx): if tree.left.isleaf: left = yamlutil.custom_tree_to_tagged_tree( tree.left.value, ctx) else: left = cls._to_tree_from_model_tree(tree.left, ctx) if tree.right.isleaf: right = yamlutil.custom_tree_to_tagged_tree( tree.right.value, ctx) else: right = cls._to_tree_from_model_tree(tree.right, ctx) node = { 'forward': [left, right] } try: tag_name = 'transform/' + _operator_to_tag_mapping[tree.value] except KeyError: raise ValueError("Unknown operator '{0}'".format(tree.value)) node = tagged.tag_object(cls.make_yaml_tag(tag_name), node, ctx=ctx) return node
def to_tree_tagged(cls, node: IsoBaseGroove, ctx): """Serialize tree with custom tag definition.""" tree = cls.to_tree(node, ctx) tag = (WELDX_TAG_BASE + "/" + _ISO_GROOVE_SCHEMA + type(node).__name__ + "-" + str(cls.version)) return tag_object(tag, tree, ctx=ctx)
def to_tree_tagged(cls, node: GmawProcess, ctx): """Serialize tree with custom tag definition.""" tree = cls.to_tree(node, ctx) tag = "tag:weldx.bam.de:weldx/process/" + tree["tag"] + "-" + str( cls.version) return tag_object(tag, tree, ctx=ctx)