How to use the cfgrib.bindings.GribInternalError function in cfgrib

To help you get started, we’ve selected a few cfgrib 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 ecmwf / cfgrib / tests / test_10_bindings.py View on Github external
def test_codes_get_errors():
    grib = bindings.codes_grib_new_from_file(open(TEST_DATA))

    with pytest.raises(bindings.GribInternalError) as err:
        bindings.codes_get(grib, 'gridType', length=1)  # too short
    assert err.value.code == bindings.lib.GRIB_BUFFER_TOO_SMALL
github ecmwf / cfgrib / cfgrib / bindings.py View on Github external
def grib_get_error_message(code):
    # type: (int) -> str
    message = lib.grib_get_error_message(code)
    return ffi.string(message).decode(ENC)


class GribInternalError(Exception):
    def __init__(self, code, message=None, *args):
        self.code = code
        self.eccode_message = grib_get_error_message(code)
        if message is None:
            message = '%s (%s).' % (self.eccode_message, code)
        super(GribInternalError, self).__init__(message, code, *args)


class KeyValueNotFoundError(GribInternalError):
    """Key/value not found."""


class ReadOnlyError(GribInternalError):
    """Value is read only."""


class FileNotFoundError(GribInternalError):
    """File not found."""


ERROR_MAP = {-18: ReadOnlyError, -10: KeyValueNotFoundError, -7: FileNotFoundError}


def check_last(func):
    @functools.wraps(func)
github ecmwf / cfgrib / cfgrib / bindings.py View on Github external
def wrapper(*args):
        code = ffi.new('int *')
        args += (code,)
        retval = func(*args)
        if code[0] != lib.GRIB_SUCCESS:
            if code[0] in ERROR_MAP:
                raise ERROR_MAP[code[0]](code[0])
            else:
                raise GribInternalError(code[0])
        return retval
github ecmwf / cfgrib / cfgrib / bindings.py View on Github external
def codes_clone(handle):
    # type: (cffi.FFI.CData) -> cffi.FFI.CData
    cloned_handle = lib.codes_handle_clone(handle)
    if cloned_handle is ffi.NULL:
        raise GribInternalError(lib.GRIB_NULL_POINTER)
    return cloned_handle
github ecmwf / cfgrib / cfgrib / bindings.py View on Github external
def __init__(self, code, message=None, *args):
        self.code = code
        self.eccode_message = grib_get_error_message(code)
        if message is None:
            message = '%s (%s).' % (self.eccode_message, code)
        super(GribInternalError, self).__init__(message, code, *args)
github ecmwf / cfgrib / cfgrib / bindings.py View on Github external
def wrapper(*args):
        code = func(*args)
        if code != lib.GRIB_SUCCESS:
            if code in ERROR_MAP:
                raise ERROR_MAP[code](code)
            else:
                raise GribInternalError(code)
github ecmwf / cfgrib / cfgrib / bindings.py View on Github external
def codes_grib_new_from_file(fileobj, product_kind=CODES_PRODUCT_GRIB, context=None):
    if context is None:
        context = ffi.NULL
    try:
        retval = check_last(lib.codes_handle_new_from_file)(context, fileobj, product_kind)
        if retval == ffi.NULL:
            raise EOFError("End of file: %r" % fileobj)
        else:
            return retval
    except GribInternalError as ex:
        if ex.code == lib.GRIB_END_OF_FILE:
            raise EOFError("End of file: %r" % fileobj)
        raise
github ecmwf / cfgrib / cfgrib / bindings.py View on Github external
self.code = code
        self.eccode_message = grib_get_error_message(code)
        if message is None:
            message = '%s (%s).' % (self.eccode_message, code)
        super(GribInternalError, self).__init__(message, code, *args)


class KeyValueNotFoundError(GribInternalError):
    """Key/value not found."""


class ReadOnlyError(GribInternalError):
    """Value is read only."""


class FileNotFoundError(GribInternalError):
    """File not found."""


ERROR_MAP = {-18: ReadOnlyError, -10: KeyValueNotFoundError, -7: FileNotFoundError}


def check_last(func):
    @functools.wraps(func)
    def wrapper(*args):
        code = ffi.new('int *')
        args += (code,)
        retval = func(*args)
        if code[0] != lib.GRIB_SUCCESS:
            if code[0] in ERROR_MAP:
                raise ERROR_MAP[code[0]](code[0])
            else: