Skip to content

Commit

Permalink
Processor.metadata_location: find location package prefix (necessary …
Browse files Browse the repository at this point in the history
…for namespace packages)
  • Loading branch information
bertsky committed Aug 30, 2024
1 parent cb758e8 commit 1ed38a6
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/ocrd/processor/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,14 @@ def metadata_location(self) -> Path:
(Override if ``ocrd-tool.json`` is not distributed with the Python package.)
"""
# XXX HACK
module_tokens = self.__module__.split('.')
if module_tokens[0] == 'src':
module_tokens.pop(0)
return resource_filename(module_tokens[0], self.metadata_filename)
module = inspect.getmodule(self)
module_tokens = module.__package__.split('.')
# for namespace packages, we cannot just use the first token
for i in range(len(module_tokens)):
prefix = '.'.join(module_tokens[:i + 1])
if sys.modules[prefix].__spec__.has_location:
return resource_filename(prefix, self.metadata_filename)
raise Exception("cannot find top-level module prefix for %s", module.__package__)

@cached_property
def metadata_rawdict(self) -> dict:
Expand Down

0 comments on commit 1ed38a6

Please sign in to comment.