Esempio n. 1
0
def _get_schema_for_property(schema, attr):
    subschema = schema.get('properties', {}).get(attr, None)
    if subschema is not None:
        return subschema
    for combiner in ['allOf', 'anyOf']:
        for subschema in schema.get(combiner, []):
            subsubschema = _get_schema_for_property(subschema, attr)
            if subsubschema != {}:
                return subsubschema
    return {}
Esempio n. 2
0
def _get_schema_for_property(schema, attr):
    subschema = schema.get('properties', {}).get(attr, None)
    if subschema is not None:
        return subschema
    for combiner in ['allOf', 'anyOf']:
        for subschema in schema.get(combiner, []):
            subsubschema = _get_schema_for_property(subschema, attr)
            if subsubschema != {}:
                return subsubschema
    return {}
Esempio n. 3
0
def _make_default(attr, schema, ctx):
    if 'max_ndim' in schema or 'ndim' in schema or 'datatype' in schema:
        return _make_default_array(attr, schema, ctx)
    elif 'default' in schema:
        return schema['default']
    elif (schema.get('type') == 'object' or schema.get('allOf')
          or schema.get('anyOf')):
        return {}
    elif schema.get('type') == 'array':
        return []
    return copy.deepcopy(schema.get('default'))
Esempio n. 4
0
def _make_default(attr, schema, ctx):
    if 'max_ndim' in schema or 'ndim' in schema or 'datatype' in schema:
        return _make_default_array(attr, schema, ctx)
    elif 'default' in schema:
        return schema['default']
    elif (schema.get('type') == 'object' or
          schema.get('allOf') or
          schema.get('anyOf')):
        return {}
    elif schema.get('type') == 'array':
        return []
    return copy.deepcopy(schema.get('default'))
Esempio n. 5
0
def _get_schema_for_index(schema, i):
    items = schema.get('items', {})
    if isinstance(items, list):
        if i >= len(items):
            return {}
        else:
            return items[i]
    else:
        return items
Esempio n. 6
0
def _get_schema_for_index(schema, i):
    items = schema.get('items', {})
    if isinstance(items, list):
        if i >= len(items):
            return {}
        else:
            return items[i]
    else:
        return items
Esempio n. 7
0
def _make_default_array(attr, schema, ctx):
    dtype = schema.get('datatype')
    if dtype is not None:
        dtype = ndarray.asdf_datatype_to_numpy_dtype(dtype)
    ndim = schema.get('ndim', schema.get('max_ndim'))
    default = schema.get('default', None)
    primary_array_name = ctx.get_primary_array_name()

    if attr == primary_array_name:
        if ctx.shape is not None:
            shape = ctx.shape
        elif ndim is not None:
            shape = tuple([0] * ndim)
        else:
            shape = (0, )
    else:
        if dtype.names is not None:
            if ndim is None:
                shape = (0, )
            else:
                shape = tuple([0] * ndim)
            default = None
        else:
            has_primary_array_shape = False
            if primary_array_name is not None:
                primary_array = getattr(ctx, primary_array_name, None)
                has_primary_array_shape = primary_array is not None

            if has_primary_array_shape:
                if ndim is None:
                    shape = primary_array.shape
                else:
                    shape = primary_array.shape[-ndim:]
            elif ndim is None:
                shape = (0, )
            else:
                shape = tuple([0] * ndim)

    array = np.empty(shape, dtype=dtype)
    if default is not None:
        array[...] = default
    return array
Esempio n. 8
0
def _make_default_array(attr, schema, ctx):
    dtype = schema.get('datatype')
    if dtype is not None:
        dtype = ndarray.asdf_datatype_to_numpy_dtype(dtype)
    ndim = schema.get('ndim', schema.get('max_ndim'))
    default = schema.get('default', None)
    primary_array_name = ctx.get_primary_array_name()

    if attr == primary_array_name:
        if ctx.shape is not None:
            shape = ctx.shape
        elif ndim is not None:
            shape = tuple([0] * ndim)
        else:
            shape = (0,)
    else:
        if dtype.names is not None:
            if ndim is None:
                shape = (0,)
            else:
                shape = tuple([0] * ndim)
            default = None
        else:
            has_primary_array_shape = False
            if primary_array_name is not None:
                primary_array = getattr(ctx, primary_array_name, None)
                has_primary_array_shape = primary_array is not None

            if has_primary_array_shape:
                if ndim is None:
                    shape = primary_array.shape
                else:
                    shape = primary_array.shape[-ndim:]
            elif ndim is None:
                shape = (0,)
            else:
                shape = tuple([0] * ndim)

    array = np.empty(shape, dtype=dtype)
    if default is not None:
        array[...] = default
    return array
Esempio n. 9
0
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
Esempio n. 10
0
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