Beispiel #1
0
def is_votable(source):
    """
    Reads the header of a file to determine if it is a VOTable file.

    Parameters
    ----------
    source : str or readable file-like object
        Path or file object containing a VOTABLE_ xml file.

    Returns
    -------
    is_votable : bool
        Returns `True` if the given file is a VOTable file.
    """
    try:
        with iterparser.get_xml_iterator(source) as iterator:
            for start, tag, d, pos in iterator:
                if tag != 'xml':
                    return False
                break

            for start, tag, d, pos in iterator:
                if tag != 'VOTABLE':
                    return False
                break

            return True
    except ValueError:
        return False
Beispiel #2
0
def is_votable(source):
    """
    Reads the header of a file to determine if it is a VOTable file.

    Parameters
    ----------
    source : str or readable file-like object
        Path or file object containing a VOTABLE_ xml file.

    Returns
    -------
    is_votable : bool
        Returns `True` if the given file is a VOTable file.
    """
    try:
        with iterparser.get_xml_iterator(source) as iterator:
            for start, tag, d, pos in iterator:
                if tag != 'xml':
                    return False
                break

            for start, tag, d, pos in iterator:
                if tag != 'VOTABLE':
                    return False
                break

            return True
    except ValueError:
        return False
Beispiel #3
0
def parse_for_object(
    source, object_type, pedantic=None, filename=None,
        _debug_python_based_parser=False
):
    """
    Parses an xml file (or file-like object), and returns a
    object of specified object_type. object_type must be a subtype of
    `~pyvo.utils.xml.elements.Element` type

    Parameters
    ----------
    source : str or readable file-like object
        Path or file object containing a tableset xml file.
    object : object type to return (subtype `~pyvo.utils.xml.elements.Element`)
    pedantic : bool, optional
        When `True`, raise an error when the file violates the spec,
        otherwise issue a warning.  Warnings may be controlled using
        the standard Python mechanisms.  See the `warnings`
        module in the Python standard library for more information.
        Defaults to False.
    filename : str, optional
        A filename, URL or other identifier to use in error messages.
        If *filename* is None and *source* is a string (i.e. a path),
        then *source* will be used as a filename for error messages.
        Therefore, *filename* is only required when source is a
        file-like object.

    Returns
    -------
    object : `~pyvo.utils.xml.elements.Element` object or None

    See also
    --------
    pyvo.io.vosi.exceptions : The exceptions this function may raise.
    """
    config = {
        'pedantic': pedantic,
        'filename': filename
    }

    if filename is None and isinstance(source, str):
        config['filename'] = source

    with iterparser.get_xml_iterator(
            source,
            _debug_python_based_parser=_debug_python_based_parser
    ) as iterator:
        return object_type(
            config=config, pos=(1, 1)).parse(iterator, config)
Beispiel #4
0
def _votableparse(source,
                  columns=None,
                  invalid='mask',
                  pedantic=False,
                  table_number=None,
                  filename=None,
                  version="1.1"):
    try:
        import astropy.io.votable.tree as votabletree
        import astropy.io.votable.table as votabletable
        from astropy.utils.xml import iterparser
        #from astropy.io.votable.exceptions import W22
        from astropy.io.votable.exceptions import W03, W06, W20, W21, W42, W46, W47, W49, E10
        for warning in (W03, W06, W20, W21, W42, W46, W47, W49, E10):
            warnings.simplefilter("ignore", warning)
# MJG : 021913 - commented out to get CDS responses to work
#        warnings.simplefilter("error", W22)
    except ImportError:
        raise RuntimeError("astropy votable not available")

    invalid = invalid.lower()
    assert invalid in ('exception', 'mask')

    chunk_size = votabletree.DEFAULT_CHUNK_SIZE

    if pedantic is None:
        pedantic = votabletable.PEDANTIC()

    config = {
        'columns': columns,
        'invalid': invalid,
        'pedantic': pedantic,
        'chunk_size': chunk_size,
        'table_number': table_number,
        'filename': filename,
        'version_1_1_or_later': True
    }

    if filename is None and isinstance(source, basestring):
        config['filename'] = source
    if filename is None:
        config['filename'] = 'dal_query'

    with iterparser.get_xml_iterator(source) as iterator:
        return votabletree.VOTableFile(config=config,
                                       pos=(1, 1),
                                       version=version).parse(
                                           iterator, config)
Beispiel #5
0
def parse_job(
    source, pedantic=None, filename=None, _debug_python_based_parser=False
):
    """
    Parses a job xml file (or file-like object), and returns a
    `~pyvo.io.uws.tree.JobFile` object.

    Parameters
    ----------
    source : str or readable file-like object
        Path or file object containing a tableset xml file.
    pedantic : bool, optional
        When `True`, raise an error when the file violates the spec,
        otherwise issue a warning.  Warnings may be controlled using
        the standard Python mechanisms.  See the `warnings`
        module in the Python standard library for more information.
        Defaults to False.
    filename : str, optional
        A filename, URL or other identifier to use in error messages.
        If *filename* is None and *source* is a string (i.e. a path),
        then *source* will be used as a filename for error messages.
        Therefore, *filename* is only required when source is a
        file-like object.

    Returns
    -------
    votable : `~pyvo.io.vosi.endpoint.TableSetFile` object

    See also
    --------
    pyvo.io.vosi.exceptions : The exceptions this function may raise.
    """
    config = {
        'pedantic': pedantic,
        'filename': filename
    }

    if filename is None and isinstance(source, six.string_types):
        config['filename'] = source

    with iterparser.get_xml_iterator(
        source,
        _debug_python_based_parser=_debug_python_based_parser
    ) as iterator:
        return JobFile(
            config=config, pos=(1, 1)).parse(iterator, config)
Beispiel #6
0
def parse_availability(source,
                       pedantic=None,
                       filename=None,
                       _debug_python_based_parser=False):
    """
    Parses a availability xml file (or file-like object), and returns a
    `~pyvo.io.vosi.endpoint.AvailabilityFile` object.

    Parameters
    ----------
    source : str or readable file-like object
        Path or file object containing a availability xml file.
    pedantic : bool, optional
        When `True`, raise an error when the file violates the spec,
        otherwise issue a warning.  Warnings may be controlled using
        the standard Python mechanisms.  See the `warnings`
        module in the Python standard library for more information.
        Defaults to False.
    filename : str, optional
        A filename, URL or other identifier to use in error messages.
        If *filename* is None and *source* is a string (i.e. a path),
        then *source* will be used as a filename for error messages.
        Therefore, *filename* is only required when source is a
        file-like object.

    Returns
    -------
    votable : `~pyvo.io.vosi.endpoint.AvailabilityFile` object

    See also
    --------
    pyvo.io.vosi.exceptions : The exceptions this function may raise.
    """
    config = _pedantic_settings(pedantic)

    if filename is None and isinstance(source, six.string_types):
        config['filename'] = source
    else:
        config['filename'] = filename

    with iterparser.get_xml_iterator(
            source,
            _debug_python_based_parser=_debug_python_based_parser) as iterator:
        return AvailabilityFile(config=config,
                                pos=(1, 1)).parse(iterator, config)
def _votableparse(source, columns=None, invalid='mask', pedantic=False,
                  table_number=None, filename=None, version="1.1"):
    try:
        import astropy.io.votable.tree as votabletree
        import astropy.io.votable.table as votabletable
        from astropy.utils.xml import iterparser
        #from astropy.io.votable.exceptions import W22
        from astropy.io.votable.exceptions import W03,W06,W20,W21,W42,W46,W47,W49,E10
        for warning in (W03, W06, W20, W21, W42, W46, W47, W49, E10):
            warnings.simplefilter("ignore", warning)
# MJG : 021913 - commented out to get CDS responses to work
#        warnings.simplefilter("error", W22)
    except ImportError:
        raise RuntimeError("astropy votable not available")

    invalid = invalid.lower()
    assert invalid in ('exception', 'mask')

    chunk_size=votabletree.DEFAULT_CHUNK_SIZE

    if pedantic is None:
        pedantic = votabletable.PEDANTIC()

    config = {
        'columns'      :      columns,
        'invalid'      :      invalid,
        'pedantic'     :     pedantic,
        'chunk_size'   :   chunk_size,
        'table_number' : table_number,
        'filename'     :     filename,
        'version_1_1_or_later': True   }

    if filename is None and isinstance(source, basestring):
        config['filename'] = source
    if filename is None:
        config['filename'] = 'dal_query'

    with iterparser.get_xml_iterator(source) as iterator:
        return votabletree.VOTableFile(
          config=config, pos=(1, 1), version=version).parse(iterator, config)
Beispiel #8
0
def parse(source, columns=None, invalid='exception', pedantic=None,
          chunk_size=tree.DEFAULT_CHUNK_SIZE, table_number=None,
          table_id=None, filename=None, unit_format=None,
          datatype_mapping=None, _debug_python_based_parser=False):
    """
    Parses a VOTABLE_ xml file (or file-like object), and returns a
    `~astropy.io.votable.tree.VOTableFile` object.

    Parameters
    ----------
    source : str or readable file-like object
        Path or file object containing a VOTABLE_ xml file.

    columns : sequence of str, optional
        List of field names to include in the output.  The default is
        to include all fields.

    invalid : str, optional
        One of the following values:

            - 'exception': throw an exception when an invalid value is
              encountered (default)

            - 'mask': mask out invalid values

    pedantic : bool, optional
        When `True`, raise an error when the file violates the spec,
        otherwise issue a warning.  Warnings may be controlled using
        the standard Python mechanisms.  See the `warnings`
        module in the Python standard library for more information.
        When not provided, uses the configuration setting
        ``astropy.io.votable.pedantic``, which defaults to False.

    chunk_size : int, optional
        The number of rows to read before converting to an array.
        Higher numbers are likely to be faster, but will consume more
        memory.

    table_number : int, optional
        The number of table in the file to read in.  If `None`, all
        tables will be read.  If a number, 0 refers to the first table
        in the file, and only that numbered table will be parsed and
        read in.  Should not be used with ``table_id``.

    table_id : str, optional
        The ID of the table in the file to read in.  Should not be
        used with ``table_number``.

    filename : str, optional
        A filename, URL or other identifier to use in error messages.
        If *filename* is None and *source* is a string (i.e. a path),
        then *source* will be used as a filename for error messages.
        Therefore, *filename* is only required when source is a
        file-like object.

    unit_format : str, astropy.units.format.Base instance or None, optional
        The unit format to use when parsing unit attributes.  If a
        string, must be the name of a unit formatter. The built-in
        formats include ``generic``, ``fits``, ``cds``, and
        ``vounit``.  A custom formatter may be provided by passing a
        `~astropy.units.UnitBase` instance.  If `None` (default),
        the unit format to use will be the one specified by the
        VOTable specification (which is ``cds`` up to version 1.2 of
        VOTable, and (probably) ``vounit`` in future versions of the
        spec).

    datatype_mapping : dict of str to str, optional
        A mapping of datatype names to valid VOTable datatype names.
        For example, if the file being read contains the datatype
        "unsignedInt" (an invalid datatype in VOTable), include the
        mapping ``{"unsignedInt": "long"}``.

    Returns
    -------
    votable : `~astropy.io.votable.tree.VOTableFile` object

    See also
    --------
    astropy.io.votable.exceptions : The exceptions this function may raise.
    """
    from . import conf

    invalid = invalid.lower()
    if invalid not in ('exception', 'mask'):
        raise ValueError("accepted values of ``invalid`` are: "
                         "``'exception'`` or ``'mask'``.")

    if pedantic is None:
        pedantic = conf.pedantic

    if datatype_mapping is None:
        datatype_mapping = {}

    config = {
        'columns': columns,
        'invalid': invalid,
        'pedantic': pedantic,
        'chunk_size': chunk_size,
        'table_number': table_number,
        'filename': filename,
        'unit_format': unit_format,
        'datatype_mapping': datatype_mapping
    }

    if filename is None and isinstance(source, str):
        config['filename'] = source

    with iterparser.get_xml_iterator(
            source,
            _debug_python_based_parser=_debug_python_based_parser) as iterator:
        return tree.VOTableFile(
            config=config, pos=(1, 1)).parse(iterator, config)
Beispiel #9
0
def parse(source,
          columns=None,
          invalid='exception',
          verify=None,
          chunk_size=tree.DEFAULT_CHUNK_SIZE,
          table_number=None,
          table_id=None,
          filename=None,
          unit_format=None,
          datatype_mapping=None,
          _debug_python_based_parser=False):
    """
    Parses a VOTABLE_ xml file (or file-like object), and returns a
    `~astropy.io.votable.tree.VOTableFile` object.

    Parameters
    ----------
    source : str or readable file-like object
        Path or file object containing a VOTABLE_ xml file.

    columns : sequence of str, optional
        List of field names to include in the output.  The default is
        to include all fields.

    invalid : str, optional
        One of the following values:

            - 'exception': throw an exception when an invalid value is
              encountered (default)

            - 'mask': mask out invalid values

    verify : {'ignore', 'warn', 'exception'}, optional
        When ``'exception'``, raise an error when the file violates the spec,
        otherwise either issue a warning (``'warn'``) or silently continue
        (``'ignore'``). Warnings may be controlled using the standard Python
        mechanisms.  See the `warnings` module in the Python standard library
        for more information. When not provided, uses the configuration setting
        ``astropy.io.votable.verify``, which defaults to 'ignore'.

        .. versionchanged:: 4.0
           ``verify`` replaces the ``pedantic`` argument, which will be
           deprecated in future.

    chunk_size : int, optional
        The number of rows to read before converting to an array.
        Higher numbers are likely to be faster, but will consume more
        memory.

    table_number : int, optional
        The number of table in the file to read in.  If `None`, all
        tables will be read.  If a number, 0 refers to the first table
        in the file, and only that numbered table will be parsed and
        read in.  Should not be used with ``table_id``.

    table_id : str, optional
        The ID of the table in the file to read in.  Should not be
        used with ``table_number``.

    filename : str, optional
        A filename, URL or other identifier to use in error messages.
        If *filename* is None and *source* is a string (i.e. a path),
        then *source* will be used as a filename for error messages.
        Therefore, *filename* is only required when source is a
        file-like object.

    unit_format : str, astropy.units.format.Base instance or None, optional
        The unit format to use when parsing unit attributes.  If a
        string, must be the name of a unit formatter. The built-in
        formats include ``generic``, ``fits``, ``cds``, and
        ``vounit``.  A custom formatter may be provided by passing a
        `~astropy.units.UnitBase` instance.  If `None` (default),
        the unit format to use will be the one specified by the
        VOTable specification (which is ``cds`` up to version 1.2 of
        VOTable, and (probably) ``vounit`` in future versions of the
        spec).

    datatype_mapping : dict of str to str, optional
        A mapping of datatype names to valid VOTable datatype names.
        For example, if the file being read contains the datatype
        "unsignedInt" (an invalid datatype in VOTable), include the
        mapping ``{"unsignedInt": "long"}``.

    Returns
    -------
    votable : `~astropy.io.votable.tree.VOTableFile` object

    See also
    --------
    astropy.io.votable.exceptions : The exceptions this function may raise.
    """
    from . import conf

    invalid = invalid.lower()
    if invalid not in ('exception', 'mask'):
        raise ValueError("accepted values of ``invalid`` are: "
                         "``'exception'`` or ``'mask'``.")

    if verify is None:

        # NOTE: since the pedantic argument isn't fully deprecated yet, we need
        # to catch the deprecation warning that occurs when accessing the
        # configuration item, but only if it is for the pedantic option in the
        # [io.votable] section.
        with warnings.catch_warnings():
            warnings.filterwarnings(
                "ignore",
                r"Config parameter \'pedantic\' in section \[io.votable\]",
                AstropyDeprecationWarning)
            conf_verify_lowercase = conf.verify.lower()

        # We need to allow verify to be booleans as strings since the
        # configuration framework doesn't make it easy/possible to have mixed
        # types.
        if conf_verify_lowercase in ['false', 'true']:
            verify = conf_verify_lowercase == 'true'
        else:
            verify = conf_verify_lowercase

    if isinstance(verify, bool):
        verify = 'exception' if verify else 'warn'
    elif verify not in VERIFY_OPTIONS:
        raise ValueError('verify should be one of {0}'.format(
            '/'.join(VERIFY_OPTIONS)))

    if datatype_mapping is None:
        datatype_mapping = {}

    config = {
        'columns': columns,
        'invalid': invalid,
        'verify': verify,
        'chunk_size': chunk_size,
        'table_number': table_number,
        'filename': filename,
        'unit_format': unit_format,
        'datatype_mapping': datatype_mapping
    }

    if filename is None and isinstance(source, str):
        config['filename'] = source

    with iterparser.get_xml_iterator(
            source,
            _debug_python_based_parser=_debug_python_based_parser) as iterator:
        return tree.VOTableFile(config=config,
                                pos=(1, 1)).parse(iterator, config)
Beispiel #10
0
def parse(source,
          columns=None,
          invalid='exception',
          pedantic=None,
          chunk_size=tree.DEFAULT_CHUNK_SIZE,
          table_number=None,
          table_id=None,
          filename=None,
          unit_format=None,
          datatype_mapping=None,
          _debug_python_based_parser=False):
    """
    Parses a VOTABLE_ xml file (or file-like object), and returns a
    `~astropy.io.votable.tree.VOTableFile` object.

    Parameters
    ----------
    source : str or readable file-like object
        Path or file object containing a VOTABLE_ xml file.

    columns : sequence of str, optional
        List of field names to include in the output.  The default is
        to include all fields.

    invalid : str, optional
        One of the following values:

            - 'exception': throw an exception when an invalid value is
              encountered (default)

            - 'mask': mask out invalid values

    pedantic : bool, optional
        When `True`, raise an error when the file violates the spec,
        otherwise issue a warning.  Warnings may be controlled using
        the standard Python mechanisms.  See the `warnings`
        module in the Python standard library for more information.
        When not provided, uses the configuration setting
        ``astropy.io.votable.pedantic``, which defaults to False.

    chunk_size : int, optional
        The number of rows to read before converting to an array.
        Higher numbers are likely to be faster, but will consume more
        memory.

    table_number : int, optional
        The number of table in the file to read in.  If `None`, all
        tables will be read.  If a number, 0 refers to the first table
        in the file, and only that numbered table will be parsed and
        read in.  Should not be used with ``table_id``.

    table_id : str, optional
        The ID of the table in the file to read in.  Should not be
        used with ``table_number``.

    filename : str, optional
        A filename, URL or other identifier to use in error messages.
        If *filename* is None and *source* is a string (i.e. a path),
        then *source* will be used as a filename for error messages.
        Therefore, *filename* is only required when source is a
        file-like object.

    unit_format : str, astropy.units.format.Base instance or None, optional
        The unit format to use when parsing unit attributes.  If a
        string, must be the name of a unit formatter. The built-in
        formats include ``generic``, ``fits``, ``cds``, and
        ``vounit``.  A custom formatter may be provided by passing a
        `~astropy.units.UnitBase` instance.  If `None` (default),
        the unit format to use will be the one specified by the
        VOTable specification (which is ``cds`` up to version 1.2 of
        VOTable, and (probably) ``vounit`` in future versions of the
        spec).

    datatype_mapping : dict of str to str, optional
        A mapping of datatype names to valid VOTable datatype names.
        For example, if the file being read contains the datatype
        "unsignedInt" (an invalid datatype in VOTable), include the
        mapping ``{"unsignedInt": "long"}``.

    Returns
    -------
    votable : `~astropy.io.votable.tree.VOTableFile` object

    See also
    --------
    astropy.io.votable.exceptions : The exceptions this function may raise.
    """
    from . import conf

    invalid = invalid.lower()
    if invalid not in ('exception', 'mask'):
        raise ValueError("accepted values of ``invalid`` are: "
                         "``'exception'`` or ``'mask'``.")

    if pedantic is None:
        pedantic = conf.pedantic

    if datatype_mapping is None:
        datatype_mapping = {}

    config = {
        'columns': columns,
        'invalid': invalid,
        'pedantic': pedantic,
        'chunk_size': chunk_size,
        'table_number': table_number,
        'filename': filename,
        'unit_format': unit_format,
        'datatype_mapping': datatype_mapping
    }

    if filename is None and isinstance(source, str):
        config['filename'] = source

    with iterparser.get_xml_iterator(
            source,
            _debug_python_based_parser=_debug_python_based_parser) as iterator:
        return tree.VOTableFile(config=config,
                                pos=(1, 1)).parse(iterator, config)
Beispiel #11
0
    invalid = invalid.lower()
    assert invalid in ('exception', 'mask')

    chunk_size=votabletree.DEFAULT_CHUNK_SIZE

    if pedantic is None:
        pedantic = votabletable.PEDANTIC()

    config = {
        'columns'      :      columns,
        'invalid'      :      invalid,
        'pedantic'     :     pedantic,
        'chunk_size'   :   chunk_size,
        'table_number' : table_number,
        'filename'     :     filename,
        'version_1_1_or_later': True   }

    if filename is None and isinstance(source, basestring):
        config['filename'] = source
    if filename is None:
        config['filename'] = 'dal_query'

    with iterparser.get_xml_iterator(source) as iterator:
        return votabletree.VOTableFile(
          config=config, pos=(1, 1), version=version).parse(iterator, config)