Exemple #1
0
def save_crops(dir, overwrite_existing, template):
    dir = Path(dir)
    export = DocumentExport(UserTemplate.load(template) if template else DWC)
    for p in dir.glob('*' + InselectDocument.EXTENSION):
        try:
            debug_print('Loading [{0}]'.format(p))
            doc = InselectDocument.load(p)
            validation = export.validation_problems(doc)
            if validation.any_problems:
                print(
                    u'Not saving crops for [{0}] because there are validation '
                    u'problems'.format(p)
                )
                for msg in format_validation_problems(validation):
                    print(msg)
            elif not overwrite_existing and doc.crops_dir.is_dir():
                print(u'Crops dir [{0}] exists - skipping'.format(doc.crops_dir))
            else:
                print(u'Will save crops for [{0}] to [{1}]'.format(p, doc.crops_dir))

                debug_print(u'Loading full-resolution scanned image')
                doc.scanned.array

                debug_print(u'Saving crops')
                export.save_crops(doc)
        except Exception:
            print(u'Error saving crops from [{0}]'.format(p))
            traceback.print_exc()
def export_csv(dir, overwrite_existing, template):
    dir = Path(dir)
    export = DocumentExport(UserTemplate.load(template) if template else DWC)
    for p in dir.glob('*' + InselectDocument.EXTENSION):
        try:
            debug_print('Loading [{0}]'.format(p))
            doc = InselectDocument.load(p)
            validation = export.validation_problems(doc)
            csv_path = export.csv_path(doc)
            if validation.any_problems:
                print(
                    'Not exporting metadata for [{0}] because there are '
                    'validation problems'.format(p)
                )
                for msg in format_validation_problems(validation):
                    print(msg)
            elif not overwrite_existing and csv_path.is_file():
                print('CSV file [{0}] exists - skipping'.format(csv_path))
            else:
                print('Writing CSV for [{0}]'.format(p))
                export.export_csv(doc)
        except KeyboardInterrupt:
            raise
        except Exception:
            print('Error saving CSV from [{0}]'.format(p))
            traceback.print_exc()
def save_crops(dir, overwrite_existing, template):
    dir = Path(dir)
    export = DocumentExport(UserTemplate.load(template) if template else DWC)
    for p in dir.glob('*' + InselectDocument.EXTENSION):
        try:
            debug_print('Loading [{0}]'.format(p))
            doc = InselectDocument.load(p)
            validation = export.validation_problems(doc)
            if validation.any_problems:
                print(
                    'Not saving crops for [{0}] because there are validation '
                    'problems'.format(p)
                )
                for msg in format_validation_problems(validation):
                    print(msg)
            elif not overwrite_existing and doc.crops_dir.is_dir():
                print('Crops dir [{0}] exists - skipping'.format(doc.crops_dir))
            else:
                print('Will save crops for [{0}] to [{1}]'.format(p, doc.crops_dir))

                debug_print('Loading full-resolution scanned image')
                doc.scanned.array

                debug_print('Saving crops')
                export.save_crops(doc)
        except KeyboardInterrupt:
            raise
        except Exception:
            print('Error saving crops from [{0}]'.format(p))
            traceback.print_exc()
 def test_from_file(self):
     "Load from a YAML file"
     doc = UserTemplate.load(TESTDATA / 'test.inselect_template')
     self.assertEqual(doc.name, "Test user template")
     self.assertEqual(doc.cropped_file_suffix, '.jpg')
     self.assertEqual(doc.thumbnail_width_pixels, 4096)
     self.assertEqual(4, len(doc.fields))
     self.assertEqual('Department', doc.fields[0].name)
     self.assertEqual('Palaeontology', doc.fields[0].fixed_value)
     self.assertEqual('catalogNumber', doc.fields[1].name)
     self.assertEqual('Catalog number', doc.fields[1].label)
     self.assertEqual('http://rs.tdwg.org/dwc/terms/catalogNumber',
                      doc.fields[1].uri)
     self.assertTrue(doc.fields[1].mandatory)
     self.assertEqual('Catalog number', doc.fields[1].label)
     self.assertEqual('Location', doc.fields[2].name)
     self.assertTrue(doc.fields[2].mandatory)
     self.assertEqual('Taxonomy', doc.fields[3].name)
     self.assertTrue(doc.fields[3].mandatory)
Exemple #5
0
    def test_save_crops_invalid_document(self, mock_question, mock_setdetailed,
                                         mock_settext):
        """The user wants to export crops for a document with many validation
        failures
        """
        w = self.window

        # This document has 15 validation problems with this template
        w.open_document(path=TESTDATA / 'shapes.inselect')
        template = UserTemplate.load(TESTDATA / 'test.inselect_template')

        w.save_crops(user_template=template)

        # Close the document
        self.assertTrue(w.close_document())

        # Should have been called
        self.assertEqual(1, mock_settext.call_count)
        self.assertIn('The document contains 15 validation problems',
                      mock_settext.call_args[0][0])

        # Detailed text should not have been given
        self.assertEqual(1, mock_setdetailed.call_count)
 def _load(self, path):
     "Loads the UserTemplate in path"
     debug_print('UserTemplateChoice._load [{0}]'.format(path))
     return UserTemplate.load(path)
 def _load(self, path):
     "Loads the UserTemplate in path"
     debug_print('UserTemplateChoice._load [{0}]'.format(path))
     return UserTemplate.load(path)