Пример #1
0
def register_sampler(data_format, data_class, function, force=False, usage=None):
    """Register a new method to InitialBinaryTable.sampler() for a given format

    Parameters
    ----------
    data_format : `str`
        name of the format to be registered

    data_class : `type`
        the class that the sampler returns

    function : `callable`
        the method to call from :meth:`InitialBinaryTable.sampler`

    force : `bool`, optional
        overwrite existing registration for ``data_format`` if found,
        default: `False`
    """
    key = (data_format, data_class)
    if key not in _SAMPLERS or force:
        _SAMPLERS[key] = (function, usage)
    else:
        raise IORegistryError(
            "Fetcher for format '{0}' and class '{1}' "
            "has already been "
            "defined".format(data_format, data_class)
        )
    _update__doc__(data_class)
Пример #2
0
def get_fetcher(data_format, data_class):
    """Return the :meth:`~EventTable.fetch` function for the given format

    Parameters
    ----------
    data_format : `str`
        name of the format

    data_class : `type`
        the class that the fetcher returns

    Raises
    ------
    astropy.io.registry.IORegistryError
        if not registration is found matching ``data_format``
    """
    # this is a copy of astropy.io.regsitry.get_reader
    fetchers = [(fmt, cls) for fmt, cls in _FETCHERS if fmt == data_format]
    for fetch_fmt, fetch_cls in fetchers:
        if io_registry._is_best_match(data_class, fetch_cls, fetchers):
            return _FETCHERS[(fetch_fmt, fetch_cls)][0]
    else:
        formats = [
            fmt for fmt, cls in _FETCHERS
            if io_registry._is_best_match(fmt, cls, fetchers)
        ]
        formatstr = '\n'.join(sorted(formats))
        raise IORegistryError(
            "No fetcher definer for format '{0}' and class '{1}'.\n"
            "The available formats are:\n{2}".format(data_format,
                                                     data_class.__name__,
                                                     formatstr))
Пример #3
0
def get_model(data_format, data_class):
    """Return the :meth:`~KNTable.model` function for the given format

    Parameters
    ----------
    data_format : `str`
        name of the format

    data_class : `type`
        the class that the model returns

    Raises
    ------
    astropy.io.registry.IORegistryError
        if not registration is found matching ``data_format``
    """
    try:
        return _MODELS[(data_format, data_class)][0]
    except KeyError:
        formats = '\n'.join(_MODELS.keys())
        raise IORegistryError("No model definer for format %r. "
                              "The available formats are:\n%r" %
                              (data_format, formats))
Пример #4
0
def get_sampler(data_format, data_class):
    """Return the :meth:`~InitialBinaryTable.sampler` function for the given format

    Parameters
    ----------
    data_format : `str`
        name of the format

    data_class : `type`
        the class that the sampler returns

    Raises
    ------
    astropy.io.registry.IORegistryError
        if not registration is found matching ``data_format``
    """
    try:
        return _SAMPLERS[(data_format, data_class)][0]
    except KeyError:
        formats = '\n'.join(_SAMPLERS.keys())
        raise IORegistryError("No sampler definer for format %r. "
                              "The available formats are:\n%r" %
                              (data_format, formats))
Пример #5
0
def test_IORegistryError():

    with pytest.raises(IORegistryError, match="just checking"):
        raise IORegistryError("just checking")