Skip to content

Commit

Permalink
Merge pull request #140 from kba/fixes3
Browse files Browse the repository at this point in the history
Fixes3
  • Loading branch information
kba authored Jul 3, 2018
2 parents f04b721 + 9768673 commit 8bc9fa4
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 3 additions & 4 deletions ocrd/cli/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions ocrd/model/ocrd_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions ocrd/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions ocrd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 4 additions & 1 deletion test/model/test_ocrd_mets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 8bc9fa4

Please sign in to comment.