Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#353] cruder scheme for save crops #355

Merged
merged 1 commit into from
Sep 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ This is an overview of major changes. Refer to the git repository for a full log

Version 0.1.34
-------------
- #353 Error 5
- #351 Metadata templates to allow fields with fixed values
- #349 Linux instructions
- #347 Explicitly reference python2 in scripts
- #342 Conda environment files
- #333 Ugly error message when opening or saving a file
- #332 Error when saving crops in Dropbox folder
- #330 libdmtx URL incorrect
- #328 Menu commands to show cookie cutter and metadata template files
- #325 Command-line option to set window size
Expand Down
60 changes: 22 additions & 38 deletions inselect/lib/document_export.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import errno
import shutil
import tempfile

from collections import defaultdict
from functools import partial
Expand Down Expand Up @@ -44,46 +44,30 @@ def crops_dir(self, document):

def save_crops(self, document, progress=None):
"""Saves images cropped from document.scanned to document.crops_dir.
Crops are first written to a tempdir in the same directory as document.
If it exists, document.crops_dir is unlinked, and the tempdir is
renamed to document.crops_dir. Any existing data in document.crops_dir
is therefore lost.
If document.crops_dir already exists, it is unlinked first.
"""
# Create temp dir alongside scan
tempdir = tempfile.mkdtemp(
dir=str(document.scanned.path.parent),
prefix=document.scanned.path.stem + '_temp_crops'
)
tempdir = Path(tempdir)
debug_print(u'Saving crops to to temp dir [{0}]'.format(tempdir))

crop_fnames = self.crop_fnames(document)
if progress:
progress('Removing existing saved crops')

crops_dir = self.crops_dir(document)
try:
# Save crops
document.save_crops_from_image(document.scanned,
(tempdir / fn for fn in crop_fnames),
progress)

# rm existing crops dir
crops_dir = self.crops_dir(document)
shutil.rmtree(str(crops_dir), ignore_errors=True)

# Rename temp dir
debug_print(u'Moving temp crops dir [{0}] to [{1}]'.format(
tempdir, crops_dir
))
tempdir.rename(crops_dir)
tempdir = None

debug_print(u'Saved [{0}] crops to [{1}]'.format(
document.n_items, crops_dir
))

return crops_dir
finally:
if tempdir:
shutil.rmtree(str(tempdir))
shutil.rmtree(unicode(crops_dir))
except OSError as e:
if errno.ENOENT == e.errno:
# Directory does not exist - do nothing
pass
else:
# Some other error
raise

crop_fnames = self.crop_fnames(document)
crops_dir.mkdir()
document.save_crops_from_image(
document.scanned,
(crops_dir / fn for fn in crop_fnames),
progress
)
return crops_dir

def csv_path(self, document):
return document.document_path.with_suffix('.csv')
Expand Down