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

combine SBB_BINARIZE_DATA and model parameter #9

Merged
merged 1 commit into from
Oct 27, 2020
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
4 changes: 2 additions & 2 deletions sbb_binarize/ocrd-tool.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"description": "PAGE XML hierarchy level to operate on"
},
"model": {
"description": "models directory.",
"description": "Directory containing HDF5 models. Can be an absolute path or a path relative to the current working directory or $SBB_BINARIZE_DATA environment variable (if set)",
"type": "string",
"required": false
"required": true
}
}
}
Expand Down
21 changes: 13 additions & 8 deletions sbb_binarize/ocrd_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os.path
from os import environ
from os.path import join
from pathlib import Path
from pkg_resources import resource_string
from json import loads

Expand Down Expand Up @@ -38,13 +40,16 @@ def __init__(self, *args, **kwargs):
kwargs['ocrd_tool'] = OCRD_TOOL['tools'][TOOL]
kwargs['version'] = OCRD_TOOL['version']
if not(kwargs.get('show_help', None) or kwargs.get('dump_json', None) or kwargs.get('show_version')):
if not 'parameter' in kwargs:
kwargs['parameter'] = {}
if not 'model' in kwargs['parameter']:
if 'SBB_BINARIZE_DATA' in os.environ:
kwargs['parameter']['model'] = os.environ['SBB_BINARIZE_DATA']
else:
raise ValueError("Must pass 'model' parameter or set SBB_BINARIZE_DATA environment variable")
raise ValueError("'model' parameter is required")
model_path = Path(kwargs['parameter']['model'])
if not model_path.is_absolute():
if 'SBB_BINARIZE_DATA' in environ:
model_path = Path(environ['SBB_BINARIZE_DATA']).joinpath(model_path)
model_path = model_path.resolve()
if not model_path.is_dir():
raise FileNotFoundError("Does not exist or is not a directory: %s" % model_path)
kwargs['parameter']['model'] = str(model_path)
super().__init__(*args, **kwargs)

def process(self):
Expand Down Expand Up @@ -114,7 +119,7 @@ def process(self):
file_grp=self.output_file_grp,
pageId=input_file.pageId,
mimetype=MIMETYPE_PAGE,
local_filename=os.path.join(self.output_file_grp, file_id + '.xml'),
local_filename=join(self.output_file_grp, file_id + '.xml'),
content=to_xml(pcgts))

@command()
Expand Down