Skip to content
This repository has been archived by the owner on Oct 4, 2020. It is now read-only.

Commit

Permalink
Merge pull request #52 from photoshell/libraw_errors
Browse files Browse the repository at this point in the history
Libraw errors
  • Loading branch information
campaul committed Jun 5, 2015
2 parents eccc8c4 + 9997a65 commit 2ba2379
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 12 deletions.
64 changes: 64 additions & 0 deletions rawkit/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
class LibrawUnspecifiedError(Exception):
pass


class LibrawFileUnsupported(Exception):
pass


class LibrawRequestForNonexistentImage(Exception):
pass


class LibrawOutOfOrderCall(Exception):
pass


class LibrawNoThumbnail(Exception):
pass


class LibrawUnsupportedThumbnail(Exception):
pass


class LibrawInputClosed(Exception):
pass


class LibrawInsufficientMemory(Exception):
pass


class LibrawDataError(Exception):
pass


class LibrawIOError(Exception):
pass


class LibrawCancelledByCallback(Exception):
pass


class LibrawBadCrop(Exception):
pass


def check_call(exit_code):
if exit_code is not 0:
raise {
-1: LibrawUnspecifiedError,
-2: LibrawFileUnsupported,
-3: LibrawRequestForNonexistentImage,
-4: LibrawOutOfOrderCall,
-5: LibrawNoThumbnail,
-6: LibrawUnsupportedThumbnail,
-7: LibrawInputClosed,
-100007: LibrawInsufficientMemory,
-100008: LibrawDataError,
-100009: LibrawIOError,
-100010: LibrawCancelledByCallback,
-100011: LibrawBadCrop
}[exit_code]
25 changes: 14 additions & 11 deletions rawkit/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import ctypes

from rawkit import errors as e
from rawkit.libraw import libraw
from rawkit.metadata import Metadata
from rawkit.options import Options
Expand Down Expand Up @@ -34,7 +35,9 @@ class Raw(object):
def __init__(self, filename=None):
"""Initializes a new Raw object."""
self.data = libraw.libraw_init(0)
libraw.libraw_open_file(self.data, filename.encode('ascii'))
e.check_call(
libraw.libraw_open_file(self.data, filename.encode('ascii'))
)

self.options = Options()

Expand All @@ -51,24 +54,24 @@ def __exit__(self, exc_type, exc_value, traceback):

def close(self):
"""Free the underlying raw representation."""
libraw.libraw_close(self.data)
e.check_call(libraw.libraw_close(self.data))

def unpack(self):
"""Unpack the raw data."""
if not self.image_unpacked:
libraw.libraw_unpack(self.data)
e.check_call(libraw.libraw_unpack(self.data))
self.image_unpacked = True

def unpack_thumb(self):
"""Unpack the thumbnail data."""
if not self.thumb_unpacked:
libraw.libraw_unpack_thumb(self.data)
e.check_call(libraw.libraw_unpack_thumb(self.data))
self.thumb_unpacked = True

def process(self):
"""Process the raw data based on self.options"""
self.options._map_to_libraw_params(self.data.contents.params)
libraw.libraw_dcraw_process(self.data)
e.check_call(libraw.libraw_dcraw_process(self.data))

def save(self, filename=None, filetype='ppm'):
"""
Expand All @@ -85,8 +88,8 @@ def save(self, filename=None, filetype='ppm'):
self.unpack()
self.process()

libraw.libraw_dcraw_ppm_tiff_writer(
self.data, filename.encode('ascii'))
e.check_call(libraw.libraw_dcraw_ppm_tiff_writer(
self.data, filename.encode('ascii')))

def save_thumb(self, filename=None):
"""
Expand All @@ -97,8 +100,8 @@ def save_thumb(self, filename=None):
"""
self.unpack_thumb()

libraw.libraw_dcraw_thumb_writer(
self.data, filename.encode('ascii'))
e.check_call(libraw.libraw_dcraw_thumb_writer(
self.data, filename.encode('ascii')))

def to_buffer(self):
"""
Expand All @@ -116,7 +119,7 @@ def to_buffer(self):
ctypes.POINTER(ctypes.c_byte * processed_image.contents.data_size)
)
data = bytearray(data_pointer.contents)
libraw.libraw_dcraw_clear_mem(processed_image)
e.check_call(libraw.libraw_dcraw_clear_mem(processed_image))

return data

Expand All @@ -135,7 +138,7 @@ def thumbnail_to_buffer(self):
ctypes.POINTER(ctypes.c_byte * processed_image.contents.data_size)
)
data = bytearray(data_pointer.contents)
libraw.libraw_dcraw_clear_mem(processed_image)
e.check_call(libraw.libraw_dcraw_clear_mem(processed_image))

return data

Expand Down
13 changes: 13 additions & 0 deletions tests/unit/error_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest

from rawkit.errors import check_call
from rawkit.errors import LibrawUnspecifiedError


def test_check_call_success():
check_call(0)


def test_check_call_error():
with pytest.raises(LibrawUnspecifiedError):
check_call(-1)
8 changes: 7 additions & 1 deletion tests/unit/raw_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@


@pytest.yield_fixture
def mock_libraw():
def mock_check_call():
with mock.patch('rawkit.raw.e') as check_call:
yield check_call


@pytest.yield_fixture
def mock_libraw(mock_check_call):
with mock.patch('rawkit.raw.libraw') as libraw:
yield libraw

Expand Down

0 comments on commit 2ba2379

Please sign in to comment.