diff --git a/CHANGELOG.md b/CHANGELOG.md index fea95df864..692debf665 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ Fixes: * CLI: `-k` on `workspace find` for non-existant fields, #133 * CLI: Persist downloads in METS, #136 + * CLI: `workspace find --download` will download to subdir of fileGrp, #137 + +Added: + * `OcrdFile` has getter `fileGrp` for the `USE` attribute of parent `mets:fileGrp`, #139 ## [0.4.3] - 2018-06-27 diff --git a/ocrd/cli/workspace.py b/ocrd/cli/workspace.py index ec72993e75..ad2031892d 100644 --- a/ocrd/cli/workspace.py +++ b/ocrd/cli/workspace.py @@ -148,6 +148,7 @@ def workspace_add_file(ctx, file_grp, file_id, mimetype, group_id, local_filenam @click.option('-g', '--group-id', help="GROUPID") @click.option('-i', '--file-id', help="ID") @click.option('-L', '--local-only', help="Find only file://-URL files", is_flag=True) +# pylint: disable=bad-continuation @click.option('-k', '--output-field', help="Output field. Repeat for multiple fields, will be joined with tab", default=['url'], multiple=True, @@ -175,12 +176,10 @@ def workspace_find(ctx, file_grp, local_only, mimetype, group_id, file_id, outpu groupId=group_id, ): if download: - workspace.download_file(f) - # print(output_field, file=sys.stderr) + workspace.download_file(f, subdir=f.fileGrp) + workspace.save_mets() ret = '\t'.join([getattr(f, field) or '' for field in output_field]) print(ret) - if download: - workspace.save_mets() # ---------------------------------------------------------------------- # ocrd workspace list-group diff --git a/ocrd/model/ocrd_file.py b/ocrd/model/ocrd_file.py index ad28987bb4..0fca2e57e7 100644 --- a/ocrd/model/ocrd_file.py +++ b/ocrd/model/ocrd_file.py @@ -69,6 +69,13 @@ def groupId(self, groupId): def mimetype(self): return self._el.get('MIMETYPE') + @property + def fileGrp(self): + """ + The ``USE`` attribute of the parent ``mets:fileGrp`` + """ + return self._el.getparent().get('USE') + @mimetype.setter def mimetype(self, mimetype): if mimetype is None: diff --git a/ocrd/resolver.py b/ocrd/resolver.py index e8237f4083..8bcaee0a5c 100644 --- a/ocrd/resolver.py +++ b/ocrd/resolver.py @@ -139,6 +139,7 @@ def download_to_directory(self, directory, url, basename=None, overwrite=False, Local filename """ log = getLogger('ocrd.resolver.download_to_directory') # pylint: disable=redefined-outer-name + # log.debug("url=|%s| subdir=|%s| overwrite=|%s|", url, subdir, overwrite) if basename is None: if subdir is not None: basename = url.rsplit('/', 1)[-1] diff --git a/ocrd/utils.py b/ocrd/utils.py index ea2292c605..081f2c2850 100644 --- a/ocrd/utils.py +++ b/ocrd/utils.py @@ -83,6 +83,7 @@ def xmllint_format(xml): return output def is_string(val): + # pylint: disable=undefined-variable return isinstance(val, (str, unicode)) if sys.version_info < (3, 0) else isinstance(val, str) def concat_padded(base, *args): diff --git a/test/model/test_ocrd_mets.py b/test/model/test_ocrd_mets.py index 6398c13831..fa2af9f471 100644 --- a/test/model/test_ocrd_mets.py +++ b/test/model/test_ocrd_mets.py @@ -51,8 +51,11 @@ def test_add_file_ID_fail(self): self.assertEqual(f.ID, 'best-id-ever', "ID kept") with self.assertRaises(Exception) as cm: self.mets.add_file('OUTPUT', ID='best-id-ever') - self.assertEquals(str(cm.exception), "File with ID='best-id-ever' already exists") + self.assertEqual(str(cm.exception), "File with ID='best-id-ever' already exists") + def test_filegrp_from_file(self): + f = self.mets.find_files(fileGrp='OCR-D-IMG')[0] + self.assertEqual(f.fileGrp, 'OCR-D-IMG') def test_file_groupid(self): f = self.mets.find_files()[0]