Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# since not reading an index isn't a deal breaker --
# everything can still be read from the file, only slower.
# Importantly, it must remain "transactionally clean", and not
# create any blocks until we're sure the block index makes
# sense.
if not fd.seekable():
return
if not len(self._internal_blocks):
return
first_block = self._internal_blocks[0]
first_block_end = first_block.end_offset
fd.seek(0, generic_io.SEEK_END)
file_size = block_end = fd.tell()
# We want to read on filesystem block boundaries. We use
# "block_end - 5" here because we need to read at least 5
# bytes in the first block.
block_start = ((block_end - 5) // fd.block_size) * fd.block_size
buff_size = block_end - block_start
content = b''
fd.seek(block_start, generic_io.SEEK_SET)
buff = fd.read(buff_size)
# Extra '\0' bytes are allowed after the ..., mainly to
# workaround poor truncation support on Windows
buff = buff.rstrip(b'\0')
content = buff
def runtest(self):
name, version = parse_schema_filename(self.filename)
if should_skip(name, version):
return
standard_version = self._find_standard_version(name, version)
# Make sure that the examples in the schema files (and thus the
# ASDF standard document) are valid.
buff = helpers.yaml_to_asdf(
'example: ' + self.example.strip(), standard_version=standard_version)
ff = AsdfFile(
uri=util.filepath_to_url(os.path.abspath(self.filename)),
extensions=TestExtension())
# Fake an external file
ff2 = AsdfFile({'data': np.empty((1024*1024*8), dtype=np.uint8)})
ff._external_asdf_by_uri[
util.filepath_to_url(
os.path.abspath(
os.path.join(
os.path.dirname(self.filename), 'external.asdf')))] = ff2
# Add some dummy blocks so that the ndarray examples work
for i in range(3):
b = block.Block(np.zeros((1024*1024*8), dtype=np.uint8))
b._used = True
standard_version = self._find_standard_version(name, version)
# Make sure that the examples in the schema files (and thus the
# ASDF standard document) are valid.
buff = helpers.yaml_to_asdf(
'example: ' + self.example.strip(), standard_version=standard_version)
ff = AsdfFile(
uri=util.filepath_to_url(os.path.abspath(self.filename)),
extensions=TestExtension())
# Fake an external file
ff2 = AsdfFile({'data': np.empty((1024*1024*8), dtype=np.uint8)})
ff._external_asdf_by_uri[
util.filepath_to_url(
os.path.abspath(
os.path.join(
os.path.dirname(self.filename), 'external.asdf')))] = ff2
# Add some dummy blocks so that the ndarray examples work
for i in range(3):
b = block.Block(np.zeros((1024*1024*8), dtype=np.uint8))
b._used = True
ff.blocks.add(b)
b._array_storage = "streamed"
try:
with pytest.warns(None) as w:
import warnings
ff._open_impl(ff, buff, mode='rw')
# Do not tolerate any warnings that occur during schema validation
import yaml
import pytest
import numpy as np
import asdf
from asdf import AsdfFile
from asdf import block
from asdf import schema
from asdf import extension
from asdf import treeutil
from asdf import util
from asdf import versioning
from asdf.tests import helpers, CustomTestType
_ctx = AsdfFile()
_resolver = _ctx.resolver
class LabelMapperTestType(CustomTestType):
version = '1.0.0'
name = 'transform/label_mapper'
class RegionsSelectorTestType(CustomTestType):
version = '1.0.0'
name = 'transform/regions_selector'
class TestExtension(extension.BuiltinExtension):
"""This class defines an extension that represents tags whose
implementations current reside in other repositories (such as GWCS) but
ff = AsdfFile(
uri=util.filepath_to_url(os.path.abspath(self.filename)),
extensions=TestExtension())
# Fake an external file
ff2 = AsdfFile({'data': np.empty((1024*1024*8), dtype=np.uint8)})
ff._external_asdf_by_uri[
util.filepath_to_url(
os.path.abspath(
os.path.join(
os.path.dirname(self.filename), 'external.asdf')))] = ff2
# Add some dummy blocks so that the ndarray examples work
for i in range(3):
b = block.Block(np.zeros((1024*1024*8), dtype=np.uint8))
b._used = True
ff.blocks.add(b)
b._array_storage = "streamed"
try:
with pytest.warns(None) as w:
import warnings
ff._open_impl(ff, buff, mode='rw')
# Do not tolerate any warnings that occur during schema validation
assert len(w) == 0, helpers.display_warnings(w)
except Exception:
print("From file:", self.filename)
raise
# Just test we can write it out. A roundtrip test
# wouldn't always yield the correct result, so those have
def find_examples_in_schema(self):
"""Returns generator for all examples in schema at given path"""
with open(str(self.fspath), 'rb') as fd:
schema_tree = yaml.safe_load(fd)
for node in treeutil.iter_tree(schema_tree):
if (isinstance(node, dict) and
'examples' in node and
isinstance(node['examples'], list)):
for desc, example in node['examples']:
yield example
def write_asdf_test(filename, explode_variables = None, inline_variables = None, **data):
"""Writes an asdf file to be tested"""
# print '\nwriting to', filename
ff = asdf.AsdfFile(data)
if explode_variables is None:
pass
else:
for v in explode_variables:
ff.set_array_storage(v, 'external')
if inline_variables is None:
pass
else:
for v in inline_variables:
ff.set_array_storage(v, 'inline')
ff.write_to(filename)
def _parse_header_line(cls, line):
"""
Parses the header line in a ASDF file to obtain the ASDF version.
"""
parts = line.split()
if len(parts) != 2 or parts[0] != constants.ASDF_MAGIC:
raise ValueError("Does not appear to be a ASDF file.")
try:
version = versioning.AsdfVersion(parts[1].decode('ascii'))
except ValueError:
raise ValueError(
"Unparseable version in ASDF file: {0}".format(parts[1]))
return version
# 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 six
from ...asdftypes import AsdfType
class UnitType(AsdfType):
name = 'unit/unit'
types = ['astropy.units.UnitBase']
requires = ['astropy']
@classmethod
def to_tree(cls, node, ctx):
from astropy.units import Unit, UnitBase
if isinstance(node, six.string_types):
node = Unit(node, format='vounit', parse_strict='warn')
if isinstance(node, UnitBase):
return node.to_string(format='vounit')
raise TypeError("'{0}' is not a valid unit".format(node))
@classmethod
def from_tree(cls, node, ctx):
def _validate(self, tree, custom=True, reading=False):
tagged_tree = yamlutil.custom_tree_to_tagged_tree(
tree, self)
schema.validate(tagged_tree, self, reading=reading)
# Perform secondary validation pass if requested
if custom and self._custom_schema:
schema.validate(tagged_tree, self, self._custom_schema,
reading=reading)