Skip to content

Commit

Permalink
First draft of automatic generation of Pydantic settings classes from…
Browse files Browse the repository at this point in the history
… plugins
  • Loading branch information
led02 committed Feb 26, 2024
1 parent f9be324 commit b19df54
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/hermes/commands/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
import pathlib
from importlib import metadata

from pydantic import BaseModel

from hermes.settings import DepositSettings, HarvestSettings, PostprocessSettings


class HermesCommand:
""" Base class for a HERMES workflow command.
Expand All @@ -20,6 +24,7 @@ class HermesCommand:
"""

NAME: str = None
settings_class: type = BaseModel

def __init__(self, parser: argparse.ArgumentParser):
""" Initialize a new instance of any HERMES command.
Expand All @@ -30,11 +35,29 @@ def __init__(self, parser: argparse.ArgumentParser):
self.plugins = self.init_plugins()

def init_plugins(self):
# Collect all entry points for this group (i.e., all valid plug-ins for the step)
entry_point_group = f"hermes.{self.NAME}"
group_plugins = [
(entry_point.name, entry_point.load())
for entry_point in metadata.entry_points(group=entry_point_group)
]

# Collect the plug-in specific configurations
plugin_settings = {
plugin_name: getattr(plugin_class, 'settings_class', None)
for plugin_name, plugin_class in group_plugins
}

# Derive a new settings model class that contains all the plug-in extensions
self.settings_class = type(f'{self.__class__.__name__}Settings', (self.settings_class, ), {
'__annotations__': plugin_settings,
**{
plugin_name: plugin_settings()
for plugin_name, plugin_settings in plugin_settings.items()
if plugin_settings is not None
}
})

return group_plugins

def init_common_parser(self, parser: argparse.ArgumentParser) -> None:
Expand Down Expand Up @@ -103,6 +126,7 @@ class HermesHarvestCommand(HermesCommand):
""" Harvest metadata from configured sources. """

NAME = "harvest"
settings_class = HarvestSettings


class HermesProcessCommand(HermesCommand):
Expand All @@ -121,12 +145,14 @@ class HermesDepositCommand(HermesCommand):
""" Deposit the curated metadata to repositories. """

NAME = "deposit"
settings_class = DepositSettings


class HermesPostprocessCommand(HermesCommand):
""" Post-process the published metadata after deposition. """

NAME = "postprocess"
settings_class = PostprocessSettings


def main() -> None:
Expand Down
3 changes: 3 additions & 0 deletions src/hermes/commands/deposit/invenio_rdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
InvenioDepositPlugin,
InvenioResolver,
)
from hermes.settings import DepositTargetSettings


class InvenioRDMClient(InvenioClient):
Expand Down Expand Up @@ -88,3 +89,5 @@ class IvenioRDMDepositPlugin(InvenioDepositPlugin):
platform_name = "invenio_rdm"
invenio_client_class = InvenioRDMClient
invenio_resolver_class = InvenioRDMResolver

settings_class = DepositTargetSettings

0 comments on commit b19df54

Please sign in to comment.