From b9abda94afd1e38bbae97f82af8aa795b7377c39 Mon Sep 17 00:00:00 2001 From: Lawrence Hudson Date: Tue, 20 Sep 2016 06:35:27 +0100 Subject: [PATCH] [#353] cruder scheme for save crops --- CHANGELOG.md | 2 ++ inselect/lib/document_export.py | 60 ++++++++++++--------------------- 2 files changed, 24 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8351330..318d639 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/inselect/lib/document_export.py b/inselect/lib/document_export.py index ccea838..a0b6013 100644 --- a/inselect/lib/document_export.py +++ b/inselect/lib/document_export.py @@ -1,5 +1,5 @@ +import errno import shutil -import tempfile from collections import defaultdict from functools import partial @@ -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')