How to use the asdf.generic_io.get_file function in asdf

To help you get started, we’ve selected a few asdf examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github spacetelescope / asdf / asdf / schema.py View on Github external
def _load_schema(url):
    with generic_io.get_file(url) as fd:
        if isinstance(url, str) 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
github spacetelescope / asdf / asdf / fits_embed.py View on Github external
If `True`, validate the blocks against their checksums.
            Requires reading the entire file, so disabled by default.

        extensions : list of AsdfExtension, optional
            A list of extensions to the ASDF to support when reading
            and writing ASDF files.  See `asdftypes.AsdfExtension` for
            more information.

        ignore_version_mismatch : bool, optional
            When `True`, do not raise warnings for mismatched schema versions.
        """
        close_hdulist = False
        if isinstance(fd, fits.hdu.hdulist.HDUList):
            hdulist = fd
        else:
            file_obj = generic_io.get_file(fd, uri=uri)
            uri = file_obj._uri if uri is None and file_obj._uri else ''
            try:
                hdulist = fits.open(file_obj)
                # Since we created this HDUList object, we need to be
                # responsible for cleaning up upon close() or __exit__
                close_hdulist = True
            except IOError:
                file_obj.close()
                msg = "Failed to parse given file '{}'. Is it FITS?"
                raise ValueError(msg.format(file_obj.uri))

        self = cls(hdulist, uri=uri, extensions=extensions,
                   ignore_version_mismatch=ignore_version_mismatch,
                   ignore_unrecognized_tag=ignore_unrecognized_tag)
        self._close_hdulist = close_hdulist
github spacetelescope / asdf / asdf / asdf.py View on Github external
include_block_index : bool, optional
            If `False`, don't include a block index at the end of the
            file.  (Default: `True`)  A block index is never written
            if the file has a streamed block.

        version : str, optional
            The ASDF version to write out.  If not provided, it will
            write out in the latest version supported by asdf.
        """

        if version is not None:
            self.version = version


        with generic_io.get_file(fd, mode='w') as fd:
            # TODO: This is not ideal: we really should pass the URI through
            # explicitly to wherever it is required instead of making it an
            # attribute of the AsdfFile.
            if self._uri is None:
                self._uri = fd.uri
            self._pre_write(fd, all_array_storage, all_array_compression,
                            auto_inline)

            try:
                self._serial_write(fd, pad_blocks, include_block_index)
                fd.flush()
            finally:
                self._post_write(fd)
github spacetelescope / asdf / asdf / asdf.py View on Github external
----------
    fd : str, `~asdf.generic_io.GenericFile`

    """
    if isinstance(fd, generic_io.InputStream):
        # If it's an InputStream let ASDF deal with it.
        return True

    to_close = False
    if isinstance(fd, AsdfFile):
        return True
    elif isinstance(fd, generic_io.GenericFile):
        pass
    else:
        try:
            fd = generic_io.get_file(fd, mode='r', uri=None)
            if not isinstance(fd, io.IOBase):
                to_close = True
        except ValueError:
            return False
    asdf_magic = fd.read(5)
    if fd.seekable():
        fd.seek(0)
    if to_close:
        fd.close()
    if asdf_magic == constants.ASDF_MAGIC:
        return True
    return False
github spacetelescope / asdf / asdf / asdf.py View on Github external
validate_checksums=False,
                   do_not_fill_defaults=False,
                   _get_yaml_content=False,
                   _force_raw_types=False,
                   strict_extension_check=False,
                   ignore_missing_extensions=False):
        """Attempt to populate AsdfFile data from file-like object"""

        if strict_extension_check and ignore_missing_extensions:
            raise ValueError(
                "'strict_extension_check' and 'ignore_missing_extensions' are "
                "incompatible options")

        self._mode = mode

        fd = generic_io.get_file(fd, mode=self._mode, uri=uri)
        self._fd = fd
        # The filename is currently only used for tracing warning information
        self._fname = self._fd._uri if self._fd._uri else ''
        header_line = fd.read_until(b'\r?\n', 2, "newline", include=True)
        self._file_format_version = cls._parse_header_line(header_line)
        self.version = self._file_format_version

        comment_section = fd.read_until(
            b'(%YAML)|(' + constants.BLOCK_MAGIC + b')', 5,
            "start of content", include=False, exception=False)
        self._comments = cls._parse_comment_section(comment_section)

        version = cls._find_asdf_version_in_comments(self._comments)
        if version is not None:
            self.version = version
github spacetelescope / asdf / asdf / fits_embed.py View on Github external
if possible and if created from `AsdfFile.open`.

        validate_checksums : bool, optional
            If `True`, validate the blocks against their checksums.
            Requires reading the entire file, so disabled by default.

        extensions : list of AsdfExtension, optional
            A list of extensions to the ASDF to support when reading
            and writing ASDF files.  See `asdftypes.AsdfExtension` for
            more information.
        """
        close_hdulist = False
        if isinstance(fd, fits.hdu.hdulist.HDUList):
            hdulist = fd
        else:
            file_obj = generic_io.get_file(fd)
            try:
                hdulist = fits.open(file_obj)
                # Since we created this HDUList object, we need to be
                # responsible for cleaning up upon close() or __exit__
                close_hdulist = True
            except IOError:
                msg = "Failed to parse given file '{}'. Is it FITS?"
                raise ValueError(msg.format(file_obj.uri))

        self = cls(hdulist, uri=uri, extensions=extensions)
        self._close_hdulist = close_hdulist

        try:
            asdf_extension = hdulist[ASDF_EXTENSION_NAME]
        except (KeyError, IndexError, AttributeError):
            # This means there is no ASDF extension