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

resmgr download: use list_available for dynamic discovery #901

Merged
merged 1 commit into from
Aug 14, 2022
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
9 changes: 6 additions & 3 deletions ocrd/ocrd/cli/resmgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ def list_installed(executable=None):

@resmgr_cli.command('download')
@click.option('-n', '--any-url', help='URL of unregistered resource to download/copy from', default='')
@click.option('-D', '--no-dynamic', is_flag=True, default=False, help="Whether to skip looking into each processor's --dump-json for module-level resources")
@click.option('-t', '--resource-type', help='Type of resource', type=click.Choice(['file', 'directory', 'archive']), default='file')
@click.option('-P', '--path-in-archive', help='Path to extract in case of archive type', default='.')
@click.option('-a', '--allow-uninstalled', help="Allow installing resources for uninstalled processors", is_flag=True)
@click.option('-o', '--overwrite', help='Overwrite existing resources', is_flag=True)
@click.option('-l', '--location', help='Where to store resources', type=click.Choice(RESOURCE_LOCATIONS), default='data', show_default=True)
@click.argument('executable', required=True)
@click.argument('name', required=False)
def download(any_url, resource_type, path_in_archive, allow_uninstalled, overwrite, location, executable, name):
def download(any_url, no_dynamic, resource_type, path_in_archive, allow_uninstalled, overwrite, location, executable, name):
"""
Download resource NAME for processor EXECUTABLE.

Expand Down Expand Up @@ -106,9 +107,11 @@ def download(any_url, resource_type, path_in_archive, allow_uninstalled, overwri
else:
log.info("Executable %s is not installed, but " \
"downloading resources anyway", executable)
reslist = resmgr.find_resources(executable=executable, name=name)
reslist = resmgr.list_available(executable=executable, dynamic=not no_dynamic)
if name:
reslist = [(executable, r) for _, rs in reslist for r in rs if r['name'] == name]
if not reslist:
log.info("No resources found in registry")
log.info(f"No resources {name} found in registry for executable {executable}")
if executable and name:
reslist = [(executable, {'url': any_url or '???', 'name': name,
'type': resource_type,
Expand Down
2 changes: 1 addition & 1 deletion ocrd_utils/ocrd_utils/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def get_processor_resource_types(executable, ocrd_tool=None):
# if the processor in question is not installed, assume both files and directories
if not which(executable):
return ['*/*']
ocrd_tool = get_ocrd_tool_json(executable)
ocrd_tool = get_ocrd_tool_json(executable)
if not next((True for p in ocrd_tool.get('parameters', {}).values() if 'content-type' in p), False):
# None of the parameters for this processor are resources (or not
# the resource parametrs are not properly declared, so output both
Expand Down
3 changes: 2 additions & 1 deletion tests/cli/test_resmgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ def test_url_tool_name_unregistered(mgr_with_tmp_path):
# add an unregistered resource
url = 'https://github.com/tesseract-ocr/tessdata_best/raw/main/dzo.traineddata'
name = 'dzo.traineddata'
r = runner.invoke(resmgr_cli, ['download', '-a', '--any-url', url, executable, name], env=env)
r = runner.invoke(resmgr_cli, ['download', '--allow-uninstalled', '--any-url', url, executable, name], env=env)
mgr.load_resource_list(mgr.user_list)
print(r.output)
with open(mgr.user_list, 'r') as f:
print(f.read())

# assert
# print(mgr.list_installed('ocrd-tesserocr-recognize'))
rsrcs = mgr.list_installed('ocrd-tesserocr-recognize')[0][1]
assert len(rsrcs) == rsrcs_before + 1
assert rsrcs[0]['name'] == name
Expand Down