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

Commit

Permalink
Make DarkFile act like a tempfile object
Browse files Browse the repository at this point in the history
  • Loading branch information
SamWhited committed Jun 28, 2015
1 parent 2850f22 commit 932b8da
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 21 deletions.
2 changes: 1 addition & 1 deletion rawkit/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ def dark_frame(self, params):
try:
self._dark_frame.save()
params.dark_frame = ctypes.c_char_p(
self._dark_frame.tmp.encode('utf-8')
self._dark_frame.name.encode('utf-8')
)
except AttributeError:
params.dark_frame = ctypes.c_char_p(
Expand Down
53 changes: 44 additions & 9 deletions rawkit/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import ctypes
import os
import shutil
import random
import string
import tempfile

from rawkit.errors import NoFileSpecified, InvalidFileType
Expand Down Expand Up @@ -199,18 +200,52 @@ def __init__(self, filename=None):
'gamma': (1, 1),
'rotation': 0,
})
self._td = tempfile.mkdtemp()
self.tmp = os.path.join(self._td, 'df')
self._tmp = os.path.join(
tempfile.gettempdir(),
'{prefix}{rand}'.format(
prefix=tempfile.gettempprefix(),
rand=''.join(random.SystemRandom().choice(
string.ascii_uppercase + string.digits) for _ in range(8)
)
)
)
self._filetype = None

def save(self, filename=None, filetype='ppm'):
"""
Save the image data, defaults to using a temp file.
def save(self):
:param filename: the name of an image file to save
:type filename: :class:`str`
:param filetype: the type of file to output (``ppm`` or ``tiff``)
:type filetype: :class:`str`
:raises: :exc:`rawkit.errors.InvalidFileTypeError`
"""
Save the image data, defaults to using the temporary file.

if filename is None:
filename = self._tmp

if not os.path.isfile(filename):
super(DarkFrame, self).save(filename=filename, filetype=filetype)

@property
def name(self):
"""
A tempfile in a unique directory.
:returns: The name of a temp file
:rtype: :class:`str`
"""
return self._tmp

if not os.path.isfile(self.tmp):
super(DarkFrame, self).save(filename=self.tmp, filetype='ppm')
def cleanup(self):
"""Cleanup temp files."""
try:
os.unlink(self._tmp)
except OSError:
pass

def close(self):
"""Free the underlying raw representation and temp file."""
"""Free the underlying raw representation and cleanup temp files."""
super(DarkFrame, self).close()
shutil.rmtree(self._td, True)
self.cleanup()
3 changes: 2 additions & 1 deletion tests/unit/options_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ def test_dark_frame_writer(options):
assert params.dark_frame.value == b'Some String'

df = Mock()
df.tmp = 'fakefile'
df._tmp = 'fakefile'
df.name = df._tmp
options.dark_frame = df
params = options._map_to_libraw_params(Mock())
assert params.dark_frame.value == b'fakefile'
29 changes: 19 additions & 10 deletions tests/unit/raw_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import mock
import os
import pytest
from rawkit.errors import InvalidFileType, NoFileSpecified
from rawkit.metadata import Metadata
Expand All @@ -24,7 +25,7 @@ def raw(input_file):


@pytest.yield_fixture
def darkframe(input_file):
def dark_frame(input_file):
with mock.patch('rawkit.raw.LibRaw'):
with DarkFrame(filename=input_file) as raw_obj:
yield raw_obj
Expand All @@ -44,8 +45,8 @@ def test_create_no_filename():
Raw()


def test_darkframe_is_raw(darkframe):
assert isinstance(darkframe, Raw)
def test_dark_frame_is_raw(dark_frame):
assert isinstance(dark_frame, Raw)


def test_unpack(raw):
Expand All @@ -70,20 +71,28 @@ def test_unpack_thumb_twice(raw):
raw.libraw.libraw_unpack_thumb.assert_called_once_with(raw.data)


def test_save_darkframe_cached(darkframe):
darkframe.save()
def test_save_dark_frame_cached(dark_frame, tmpdir):
dark_frame.save()

# Touch the file (as if LibRaw were installed and saved a file)
with open(darkframe.tmp, 'a'):
with open(dark_frame.name, 'a'):
pass

darkframe.save()
darkframe.libraw.libraw_dcraw_ppm_tiff_writer.assert_called_once_with(
darkframe.data,
darkframe.tmp.encode('ascii'),
dark_frame.save()
dark_frame.libraw.libraw_dcraw_ppm_tiff_writer.assert_called_once_with(
dark_frame.data,
dark_frame.name.encode('ascii'),
)


def test_save_dark_frame_with_filename_cached(dark_frame, tmpdir):
tmpdir.join('somefile').write('')
fn = os.path.join(str(tmpdir), 'somefile')
dark_frame.save(filename=fn)
dark_frame.save(filename=fn, filetype='tiff')
assert not dark_frame.libraw.libraw_dcraw_ppm_tiff_writer.called


def _test_save(raw, output_file, filetype):
raw.save(filename=output_file, filetype=filetype)

Expand Down

0 comments on commit 932b8da

Please sign in to comment.