Skip to content

Commit

Permalink
Merge pull request #218 from hermes-hmc/feature/class-based-plugins
Browse files Browse the repository at this point in the history
Class-based architecture for deposition plugins
  • Loading branch information
led02 authored Dec 21, 2023
2 parents 07ff3d2 + c398e97 commit 00cd6c4
Show file tree
Hide file tree
Showing 10 changed files with 804 additions and 1,450 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ docs/build/

.idea/
.venv/
.vscode/
dist/

# HERMES workflow specifics
Expand Down
4 changes: 3 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@

project = 'HERMES Workflow'
copyright = '2022, HERMES project'
author = 'Oliver Bertuch, Stephan Druskat, Guido Juckeland, Jeffrey Kelling, Oliver Knodel, Michael Meinel, Tobias Schlauch'
author = 'Oliver Bertuch, Stephan Druskat, Guido Juckeland, Jeffrey Kelling, ' + \
'Oliver Knodel, Michael Meinel, Tobias Schlauch, Sophie Kernchen'


# The full version, including alpha/beta/rc tags
release = '2022-07-01'
Expand Down
2 changes: 1 addition & 1 deletion hermes.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ target = "invenio_rdm"

[deposit.invenio_rdm]
site_url = "https://sandbox.zenodo.org"
communities = ["zenodo"]
communities = []
access_right = "open"

[deposit.invenio_rdm.api_paths]
Expand Down
43 changes: 4 additions & 39 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,45 +86,10 @@ git = "hermes.commands.process.git:process"
git_add_contributors = "hermes.commands.process.git:add_contributors"
git_add_branch = "hermes.commands.process.git:add_branch"

[tool.poetry.plugins."hermes.deposit.prepare"]
invenio = "hermes.commands.deposit.invenio:prepare"
invenio_rdm = "hermes.commands.deposit.invenio_rdm:prepare"
file = "hermes.commands.deposit.file:dummy_noop"

[tool.poetry.plugins."hermes.deposit.map"]
invenio = "hermes.commands.deposit.invenio:map_metadata"
invenio_rdm = "hermes.commands.deposit.invenio_rdm:map_metadata"
file = "hermes.commands.deposit.file:map_metadata"

[tool.poetry.plugins."hermes.deposit.create_initial_version"]
invenio = "hermes.commands.deposit.invenio:create_initial_version"
invenio_rdm = "hermes.commands.deposit.invenio_rdm:create_initial_version"
file = "hermes.commands.deposit.file:dummy_noop"

[tool.poetry.plugins."hermes.deposit.create_new_version"]
invenio = "hermes.commands.deposit.invenio:create_new_version"
invenio_rdm = "hermes.commands.deposit.invenio_rdm:create_new_version"
file = "hermes.commands.deposit.file:dummy_noop"

[tool.poetry.plugins."hermes.deposit.update_metadata"]
invenio = "hermes.commands.deposit.invenio:update_metadata"
invenio_rdm = "hermes.commands.deposit.invenio_rdm:update_metadata"
file = "hermes.commands.deposit.file:dummy_noop"

[tool.poetry.plugins."hermes.deposit.delete_artifacts"]
invenio = "hermes.commands.deposit.invenio:delete_artifacts"
invenio_rdm = "hermes.commands.deposit.invenio_rdm:delete_artifacts"
file = "hermes.commands.deposit.file:dummy_noop"

[tool.poetry.plugins."hermes.deposit.upload_artifacts"]
invenio = "hermes.commands.deposit.invenio:upload_artifacts"
invenio_rdm = "hermes.commands.deposit.invenio_rdm:upload_artifacts"
file = "hermes.commands.deposit.file:dummy_noop"

[tool.poetry.plugins."hermes.deposit.publish"]
invenio = "hermes.commands.deposit.invenio:publish"
invenio_rdm = "hermes.commands.deposit.invenio_rdm:publish"
file = "hermes.commands.deposit.file:publish"
[tool.poetry.plugins."hermes.deposit"]
file = "hermes.commands.deposit.file:FileDepositPlugin"
invenio = "hermes.commands.deposit.invenio:InvenioDepositPlugin"
invenio_rdm = "hermes.commands.deposit.invenio_rdm:IvenioRDMDepositPlugin"

[tool.poetry.plugins."hermes.postprocess"]
config_invenio_record_id = "hermes.commands.postprocess.invenio:config_record_id"
Expand Down
89 changes: 89 additions & 0 deletions src/hermes/commands/deposit/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# SPDX-FileCopyrightText: 2023 Helmholtz-Zentrum Dresden-Rossendorf (HZDR)
#
# SPDX-License-Identifier: Apache-2.0

# SPDX-FileContributor: David Pape
# SPDX-FileContributor: Michael Meinel

import abc

import click

from hermes.model.context import CodeMetaContext


class BaseDepositPlugin(abc.ABC):
def __init__(self, click_ctx: click.Context, ctx: CodeMetaContext) -> None:
self.click_ctx = click_ctx
self.ctx = ctx

def __call__(self) -> None:
"""Initiate the deposition process.
This calls a list of additional methods on the class, none of which need to be implemented.
"""
self.prepare()
self.map_metadata()

if self.is_initial_publication():
self.create_initial_version()
else:
self.create_new_version()

self.update_metadata()
self.delete_artifacts()
self.upload_artifacts()
self.publish()

def prepare(self) -> None:
"""Prepare the deposition.
This method may be implemented to check whether config and context match some initial conditions.
If no exceptions are raised, execution continues.
"""
pass

@abc.abstractmethod
def map_metadata(self) -> None:
"""Map the given metadata to the target schema of the deposition platform."""
pass

def is_initial_publication(self) -> bool:
"""Decide whether to do an initial publication or publish a new version.
Returning ``True`` indicates that publication of an initial version will be executed, resulting in a call of
:meth:`create_initial_version`. ``False`` indicates a new version of an existing publication, leading to a call
of :meth:`create_new_version`.
By default, this returns ``True``.
"""
return True

@abc.abstractmethod
def create_initial_version(self) -> None:
"""Create an initial version of the publication on the target platform."""
pass

@abc.abstractmethod
def create_new_version(self) -> None:
"""Create a new version of an existing publication on the target platform."""
pass

@abc.abstractmethod
def update_metadata(self) -> None:
"""Update the metadata of the newly created version."""
pass

def delete_artifacts(self) -> None:
"""Delete any superfluous artifacts taken from the previous version of the publication."""
pass

def upload_artifacts(self) -> None:
"""Upload new artifacts to the target platform."""
pass

@abc.abstractmethod
def publish(self) -> None:
"""Publish the newly created deposit on the target platform."""
pass
24 changes: 9 additions & 15 deletions src/hermes/commands/deposit/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,18 @@

import json

import click

from hermes import config
from hermes.model.context import CodeMetaContext
from hermes.commands.deposit.base import BaseDepositPlugin
from hermes.model.path import ContextPath


def dummy_noop(click_ctx: click.Context, ctx: CodeMetaContext):
pass


def map_metadata(click_ctx: click.Context, ctx: CodeMetaContext):
ctx.update(ContextPath.parse('deposit.file'), ctx['codemeta'])

class FileDepositPlugin(BaseDepositPlugin):
def map_metadata(self) -> None:
self.ctx.update(ContextPath.parse('deposit.file'), self.ctx['codemeta'])

def publish(click_ctx: click.Context, ctx: CodeMetaContext):
file_config = config.get("deposit").get("file", {})
output_data = ctx['deposit.file']
def publish(self) -> None:
file_config = config.get("deposit").get("file", {})
output_data = self.ctx['deposit.file']

with open(file_config.get('filename', 'hermes.json'), 'w') as deposition_file:
json.dump(output_data, deposition_file)
with open(file_config.get('filename', 'hermes.json'), 'w') as deposition_file:
json.dump(output_data, deposition_file, indent=2)
Loading

0 comments on commit 00cd6c4

Please sign in to comment.