Skip to content

Commit

Permalink
Merge branch 'resmgr-use-ocrd-all-json'
Browse files Browse the repository at this point in the history
  • Loading branch information
kba committed Jul 15, 2024
2 parents 6874eec + 9b1455e commit d056dc1
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/ocrd_utils/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from .constants import EXT_TO_MIME
from .config import config
from .logging import getLogger
from .introspect import resource_string

def abspath(url):
"""
Expand Down Expand Up @@ -79,12 +80,16 @@ def get_ocrd_tool_json(executable):
"""
Get the ``ocrd-tool`` description of ``executable``.
"""
ocrd_tool = {}
executable_name = Path(executable).name
try:
ocrd_tool = loads(run([executable, '--dump-json'], stdout=PIPE).stdout)
except (JSONDecodeError, OSError) as e:
getLogger('ocrd.utils.get_ocrd_tool_json').error(f'{executable} --dump-json produced invalid JSON: {e}')
ocrd_tool = {}
ocrd_all_tool = loads(resource_string('ocrd', 'ocrd-all-tool.json'))
ocrd_tool = ocrd_all_tool[executable]
except (JSONDecodeError, OSError, KeyError):
try:
ocrd_tool = loads(run([executable, '--dump-json'], stdout=PIPE).stdout)
except (JSONDecodeError, OSError) as e:
getLogger('ocrd.utils.get_ocrd_tool_json').error(f'{executable} --dump-json produced invalid JSON: {e}')
if 'resource_locations' not in ocrd_tool:
ocrd_tool['resource_locations'] = ['data', 'cwd', 'system', 'module']
return ocrd_tool
Expand All @@ -93,9 +98,13 @@ def get_ocrd_tool_json(executable):
def get_moduledir(executable):
moduledir = None
try:
moduledir = run([executable, '--dump-module-dir'], encoding='utf-8', stdout=PIPE).stdout.rstrip('\n')
except (JSONDecodeError, OSError) as e:
getLogger('ocrd.utils.get_moduledir').error(f'{executable} --dump-module-dir failed: {e}')
ocrd_all_moduledir = loads(resource_string('ocrd', 'ocrd-all-module-dir.json'))
moduledir = ocrd_all_moduledir[executable]
except (JSONDecodeError, OSError, KeyError):
try:
moduledir = run([executable, '--dump-module-dir'], encoding='utf-8', stdout=PIPE).stdout.rstrip('\n')
except (JSONDecodeError, OSError) as e:
getLogger('ocrd.utils.get_moduledir').error(f'{executable} --dump-module-dir failed: {e}')
return moduledir

def list_resource_candidates(executable, fname, cwd=getcwd(), moduled=None, xdg_data_home=None):
Expand Down

0 comments on commit d056dc1

Please sign in to comment.