Skip to content

Commit

Permalink
move get_ocrd_tool_json from ocrd_utils to ocrd_validators
Browse files Browse the repository at this point in the history
  • Loading branch information
kba committed Aug 21, 2024
1 parent 346f166 commit bd6faa7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
20 changes: 0 additions & 20 deletions src/ocrd_utils/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
'abspath',
'directory_size',
'is_file_in_directory',
'get_ocrd_tool_json',
'get_moduledir',
'get_processor_resource_types',
'guess_media_type',
Expand Down Expand Up @@ -75,25 +74,6 @@ def unzip_file_to_dir(path_to_zip, output_directory):
z.extractall(output_directory)
z.close()

@lru_cache()
def get_ocrd_tool_json(executable):
"""
Get the ``ocrd-tool`` description of ``executable``.
"""
ocrd_tool = {}
executable_name = Path(executable).name
try:
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

@lru_cache()
def get_moduledir(executable):
moduledir = None
Expand Down
2 changes: 2 additions & 0 deletions src/ocrd_validators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Validators for various OCR-D related data structures.
"""
__all__ = [
'get_ocrd_tool_json',
'ParameterValidator',
'WorkspaceValidator',
'PageValidator',
Expand All @@ -26,3 +27,4 @@
from .xsd_page_validator import XsdPageValidator
from .processing_server_config_validator import ProcessingServerConfigValidator
from .ocrd_network_message_validator import OcrdNetworkMessageValidator
from .get_ocrd_tool import get_ocrd_tool_json
32 changes: 32 additions & 0 deletions src/ocrd_validators/get_ocrd_tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

from functools import lru_cache
from json import JSONDecodeError, loads
from pathlib import Path
from subprocess import run, PIPE

from ocrd_utils.introspect import resource_string
from ocrd_utils.logging import getLogger

#from .ocrd_tool_validator import OcrdToolValidator


@lru_cache()
def get_ocrd_tool_json(executable):
"""
Get the ``ocrd-tool`` description of ``executable``.
"""
ocrd_tool = {}
executable_name = Path(executable).name
try:
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']
# OcrdToolValidator.validate(ocrd_tool)
return ocrd_tool

0 comments on commit bd6faa7

Please sign in to comment.