diff --git a/.github/workflows/hermes-zenodo-sandbox.yml b/.github/workflows/hermes-zenodo-sandbox.yml deleted file mode 100644 index 5a036b26..00000000 --- a/.github/workflows/hermes-zenodo-sandbox.yml +++ /dev/null @@ -1,31 +0,0 @@ -# SPDX-FileCopyrightText: 2023 German Aerospace Center (DLR), Helmholtz-Zentrum Drseden-Rossendorf, Forschungszentrum Jülich -# -# SPDX-License-Identifier: CC0-1.0 - -# SPDX-FileContributor: Stephan Druskat - -name: Software publication on Zenodo Sandbox - -on: - push: - tags: - - "*" - -jobs: - hermes: - name: HERMES - runs-on: ubuntu-latest - permissions: - contents: read # We will only read content from the repo - # pull-requests: write # Postprocessing should be able to create a pull request with changes - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-python@v4 - with: - python-version: '3.10' - - run: pip install git+https://github.com/sdruskat/workflow.git@deRSE23 - - run: git archive --format zip HEAD src > hermes.zip - - run: hermes - - run: hermes deposit --auth-token ${{ secrets.ZENODO }} --file hermes.zip --file README.md - # - run: hermes postprocess # (Not implemented yet) - # - uses: peter-evans/create-pull-request@v4 (TODO once postprocessing has it) diff --git a/.github/workflows/hermes-zenodo.yml b/.github/workflows/hermes-zenodo.yml new file mode 100644 index 00000000..302fdeb0 --- /dev/null +++ b/.github/workflows/hermes-zenodo.yml @@ -0,0 +1,151 @@ +# SPDX-FileCopyrightText: 2023 German Aerospace Center (DLR), Helmholtz-Zentrum Drseden-Rossendorf, Forschungszentrum Jülich +# +# SPDX-License-Identifier: CC0-1.0 + +# SPDX-FileContributor: Stephan Druskat + +name: Software publication on Zenodo + +on: + push: + tags: + - "*" + + # NOTE: Do not delete the trigger on closed pull requests, the HERMES workflow needs this. + pull_request: + types: + - closed + +jobs: + hermes-prepare: + name: Prepare Metadata for Curation + runs-on: ubuntu-latest + # This condition becomes much easier when we only react to push events on the release branch. + # We still need to exclude the merge commit push of the post processing PR + + # ADAPT + # Depending on the event you react to in the 'on:' section above, you will need to adapt this + # to react on the specific events. + # NOTE: You will probably still need to keep the exclusion check for commit messages provided by the workflow ('hermes/'/'hermes/post'). + if: > + github.event_name == 'push' && ! ( + startsWith(github.ref_name, 'hermes/') || + contains(github.event.head_commit.message, 'hermes/post') + ) + + permissions: + contents: write # Allow creation of new branches + pull-requests: write # Postprocessing should be able to create a pull request with changes + + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: '3.10' + - run: pip install hermes hermes-plugin-python + - run: hermes harvest + - run: hermes process + - run: hermes curate + + - run: | + # Cache current branch for PR close job + git branch --show-current > .hermes/curate/target_branch + + # Shorten the SHA for the PR title + SHORT_SHA=$(echo "$GITHUB_SHA" | cut -c -8) + echo "SHORT_SHA=$SHORT_SHA" >> "$GITHUB_ENV" + + # Create a curation branch + git switch -c "hermes/curate-$SHORT_SHA" + git push origin "hermes/curate-$SHORT_SHA" + + # Explicitly add to-be-curated metadata (which is ignored via .gitignore!) + git add -f .hermes/curate + - uses: peter-evans/create-pull-request@v5 + with: + base: hermes/curate-${{ env.SHORT_SHA }} + branch: hermes/curate-result-${{ env.SHORT_SHA }} + title: Metadata Curation for Commit ${{ env.SHORT_SHA }} + body: | + Please carefully review the attached metadata. + If you are satisfied with the result, you may merge this PR, which will trigger publication. + (Any temporary branches will be cleaned up.) + delete-branch: true + + hermes-curate: + name: Publish Software with Curated Metadata + if: github.event.pull_request.merged == true && startsWith( github.base_ref , 'hermes/curate-') + + runs-on: ubuntu-latest + permissions: + contents: write # Allow creation of new branches + pull-requests: write # Postprocessing should be able to create a pull request with changes + + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: '3.10' + - run: pip install hermes + + # ADAPT + # If you want to publish artifacts (e.g., a zipped snapshot of your repository), + # you can prepare this here. + - run: git archive --format zip HEAD src > hermes.zip + + # Run the HERMES deposition and postprocessing steps. + # ADAPT + # 1. You need to have an authentication token for your target publication platform + # as a GitHub secret in your repository (in this example, this is called ZENODO_SANDBOX). + # 2. Adapt the files you want to deposit. In the example, showcase.zip and README.md are deposited alongside the metadata. + # 3. Check if you want to run with '--initial', as this may potentially create a completely new record (collection), + # rather than a new version of the same collection! + - run: hermes deposit --initial -O invenio_rdm.auth_token ${{ secrets.ZENODO }} --file hermes.zip --file README.md + + # ADAPT + # Remove this command if you don't want to do any postprocessing + - run: hermes postprocess + + # ADAPT + # If you don't want to run postprocessing, remove this complete section (next '-run' and 'uses: peter-evans/...' bullets). + # + # Note 1: We change the base branch here for the PR. This flow runs so far within the "curated-metadata-*" branch, + # but now we want to add the changes done by deposit and post processing to the branch that was initially + # meant to be published using HERMES. + # Note 2: The create-pull-request action will NOT inherit the commits we did in the previous job. It will only look at the + # changes within this local workspace we did *now*. + - run: echo "TARGET_BRANCH=$(cat .hermes/curate/target_branch)" >> "$GITHUB_ENV" + - uses: peter-evans/create-pull-request@v5 + with: + branch: hermes/post-${{ github.run_id }} + base: ${{ env.TARGET_BRANCH }} + title: Review hermes post-processing results + body: | + This is an automated pull request created by HERMES post-processing. + + Please carefully review the changes and finally merge them into your + + # Delete all the curation branches + - run: | + for BRANCH in $(git ls-remote origin 'refs/heads/hermes/curate-*' | cut -f2 | cut -d'/' -f'3-'); do + git push origin --delete "$BRANCH" + done + # TODO: if: failure() --- delete the curation branches when the deposition failed + + + hermes-cleanup: + name: Cleanup aborted curation branches + if: github.event.pull_request.merged == false && startsWith( github.base_ref , 'hermes/curate-') + + runs-on: ubuntu-latest + permissions: + contents: write # Allow creation of new branches + pull-requests: write # Postprocessing should be able to create a pull request with changes + + steps: + - uses: actions/checkout@v3 + # Delete all the curation branches + - run: | + for BRANCH in $(git ls-remote origin 'refs/heads/hermes/curate-*' | cut -f2 | cut -d'/' -f'3-'); do + git push origin --delete "$BRANCH" + done diff --git a/.gitignore b/.gitignore index d7c36ebe..5856425f 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ dist/ # HERMES workflow specifics .hermes/ +hermes.log diff --git a/CITATION.cff b/CITATION.cff index 54830b3a..dfb7efe6 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -14,9 +14,9 @@ title: hermes message: >- If you use this software, please cite it using the metadata from this file. -version: "proof-of-concept" +version: 0.8.1 license: "Apache-2.0" -abstract: "Proof-of-concept implementation of the HERMES workflow." +abstract: "Tool to automate software publication. Not stable yet." type: software authors: - given-names: Michael @@ -49,3 +49,12 @@ authors: email: d.pape@hzdr.de affiliation: Helmholtz-Zentrum Dresden-Rossendorf (HZDR) orcid: 'https://orcid.org/0000-0002-3145-9880' + - given-names: Kernchen + family-names: Sophie + email: sophie.kernchen@dlr.de + affiliation: German Aerospace Center (DLR) + orcid: 'https://orcid.org/0009-0005-4430-6743' +identifiers: + - type: doi + value: 10.5281/zenodo.13221384 + description: Version 0.8.1b1 diff --git a/docs/source/tutorials/writing-a-plugin-for-hermes.md b/docs/source/tutorials/writing-a-plugin-for-hermes.md new file mode 100644 index 00000000..91f0c4bd --- /dev/null +++ b/docs/source/tutorials/writing-a-plugin-for-hermes.md @@ -0,0 +1,158 @@ + + + + +# Write a plugin for HERMES + + +This tutorial will present the basic steps for writing an additional harvester. +At the moment only the architecture for harvester plugins is stable. +The full code and structure is available at [hermes-plugin-git](https://github.com/softwarepub/hermes-plugin-git). +This plugin extracts information from the local git history. +The hermes-plugin-git will help to gather contributing and branch metadata. +```{note} +For this tutorial you should be familiar with HERMES. +If you never used HERMES before, you might want to check the tutorial: [Automated Publication with HERMES](https://docs.software-metadata.pub/en/latest/tutorials/automated-publication-with-ci.html). +``` + +## Plugin Architecture + +HERMES uses a plugin architecture. Therefore, users are invited to contribute own features. +The structure for every plugin follows the same schema. +There is a top-level base class for every plugin. In this `HermesPlugin` class there is one abstract method `__ call __` which needs to be overwritten. +Furthermore, the `HermesCommand` class provides all needs for writing a plugin used in a HERMES command. +So the `HermesPlugin`s call method gets an instance of the `HermesCommand` that triggered this plugin to run. +In our case this will be the `HermesHarvestCommand` which calls all harvest plugins. +The plugin class also uses a derivative of `HermesSettings` to add parameters that can be adapted by the configuration file. +`HermesSettings` are the base class for command specific settings. +It uses [pydantic](https://docs.pydantic.dev/latest/) [settings](https://docs.pydantic.dev/latest/api/pydantic_settings/) to specify and validate the parameters. +The user can either set the parameters in the `hermes.toml` or overwrite them in the command line. +To overwrite a parameter from command line, use the `-O` command line option followed by the dotted parameter name and the value. +E.g., you can set your authentication token for InvenioRDM by adding the following options to your call to `hermes deposit`: +```shell +hermes deposit -O invenio_rdm.auth_token YourSecretAuthToken + +## Set Up Plugin +To write a new plugin, it is important to follow the given structure. +This means your plugins source code has a pydantic class with Settings and the plugin class which inherits from one base class. +For our specific case, we want to write a git harvest plugin. +Our class Structure should look like this: + + +```{code-block} python +from hermes.commands.harvest.base import HermesHarvestPlugin +from pydantic import BaseModel + + +class GitHarvestSettings(BaseModel): + from_branch: str = 'main' + + +class GitHarvestPlugin(HermesHarvestPlugin): + settings_class = GitHarvestSettings + + def __call__(self, command): + print("Hello World!") + + return {}, {} +``` + +The code uses the `HermesHarvestPlugin` as base class and pydantics Basemodel for the settings. In the `GitHarvestSettings` you +can see that an additional parameter is defined. The Parameter `from_branch` is specific for this plugin and can be accessed inside the plugin using `self.settings.harvest.git.git_branch` as long as our plugin will be named git. +In the `hermes.toml` this would be achieved by [harvest.{plugin_name}]. +The `GitHarvestSettings` are associated with the `GitHarvestPlugin`. In the plugin you need to overwrite the `__ call __` method. +For now a simple Hello World will do. The method returns two dictionaries. These will contain the harvested data in CodeMeta (JSON-LD) and additional information, e.g., to provide provenance information. +That is the basic structure for the plugins source code. + +To integrate this code, you have to register it as a plugin in the `pyproject.toml`. To learn more about the `pyproject.toml` check https://python-poetry.org/docs/pyproject/ or refer to [PEP621](https://peps.python.org/pep-0621/). +We will just look at the important places for this plugin. There are two ways to integrate this plugin. First we will show how to use the plugin environment as the running base with HERMES as a dependency. +Then we say how to integrate this plugin in HERMES itself. + +### Include HERMES as Dependency +This is probably the more common way, where you can see HERMES as a framework. +The idea is that your project is the main part. You create the `pyproject.toml` as usual. +In the dependencies block you need to include `hermes`. Then you just have to declare your plugin. +The HERMES software will look for installed plugins and use them. +In the code below you can see the parts of the `pyproject.toml` that are important. +```{code-block} toml +... +[tool.poetry.dependencies] +python = "^3.10" +hermes = "^0.8.0" +... +... +[tool.poetry.plugins."hermes.harvest"] +git = "hermes_plugin_git.harvest:GitHarvestPlugin" +... +``` +As you can see the plugin class from `hermes_plugin_git` is declared as `git` for the `hermes.harvest` entrypoint. +To use the plugin you have to adapt the harvest settings in the `hermes.toml`. +We will discuss the exact step after showing the other `pyproject.toml` configuration. +```{note} +You have to run poetry install to add and install all entrypoints declared in the pyproject.toml. +``` + +### Write Plugin to be included in HERMES +This variant is used to contribute to the HERMES community or adapt the HERMES workflow for own purposes. +If you want to contribute, see the [Contribution Guidelines](https://docs.software-metadata.pub/en/latest/dev/contribute.html). +After cloning the HERMES workflow repository you can adapt the pyproject.toml. +In the code below you see the parts with the important lines. +```{code-block} toml +... +[tool.poetry.dependencies] +... +pydantic-settings = "^2.1.0" +hermes-plugin-git = { git = "https://github.com/softwarepub/hermes-plugin-git.git", branch = "main" } +... +... +[tool.poetry.plugins."hermes.harvest"] +cff = "hermes.commands.harvest.cff:CffHarvestPlugin" +codemeta = "hermes.commands.harvest.codemeta:CodeMetaHarvestPlugin" +git = "hermes_plugin_git.harvest:GitHarvestPlugin" +... +``` +In the dependencies you have to install your plugin. If your Plugin is pip installable than you can just give the name and the version. +If your plugin is in a buildable git repository, you can install it with the given expression. +Note that this differs with the accessibility and your wishes, check [Explicit Package Sources](https://python-poetry.org/docs/repositories/#explicit-package-sources). + +The second thing to adapt is to declare the access point for the plugin. +You can do that with `git = "hermes_plugin_git.harvest:GitHarvestPlugin"`. +This expression makes the GitHarvestPlugin from the hermes_plugin_git package, a hermes.harvest plugin named git. +So you need to configure this line with your plugin properties. + +Now you just need to add the plugin to the hermes.toml and reinstall the adapted poetry package. + +### Configure hermes.toml +To use the plugin, you have to activate it in the `hermes.toml`. +The settings for the plugins are also set there. +For the harvest plugin the `hermes.toml` could look like this: +```{code-block} toml +[harvest] +sources = [ "cff", "git" ] # ordered priority (first one is most important) + +[harvest.cff] +enable_validation = false + +[harvest.git] +from_branch = "develop" +... +``` +In the `[harvest]` section you define that this plugin is used with less priority than the built-in `cff` plugin. +in the `[harvest.git]` section you set the configuration for the plugin. +In the beginning of this tutorial we set the parameter `from_branch` in the git settings. Now we change the default `from_branch` to `develop`. +With this configuration the plugin will be used. If you run `hermes harvest`, you should see the "Hello World" message. + +```{admonition} Congratulations! +You can now write plugins for HERMES. +``` +To fill the plugin with code, you can check our [hermes-plugin-git](https://github.com/softwarepub/hermes-plugin-git) repository. +There is the code to check the local git history and extract contributors of the given branch. + +If you have any questions, wishes or requests, feel free to contact us. diff --git a/hermes.toml b/hermes.toml index 8884aea4..3aa44a8f 100644 --- a/hermes.toml +++ b/hermes.toml @@ -3,18 +3,16 @@ # SPDX-License-Identifier: CC0-1.0 [harvest] -sources = [ "cff" ] # ordered priority (first one is most important) - -[harvest.cff] -enable_validation = false +sources = [ "cff", "toml" ] # ordered priority (first one is most important) [deposit] target = "invenio_rdm" [deposit.invenio_rdm] -site_url = "https://sandbox.zenodo.org" +site_url = "https://zenodo.org" communities = [] access_right = "open" +record_id = 13221384 [deposit.invenio_rdm.api_paths] depositions = "api/deposit/depositions" diff --git a/pyproject.toml b/pyproject.toml index 70e5187a..d175d580 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,7 +9,7 @@ [tool.poetry] # Reference at https://python-poetry.org/docs/pyproject/ name = "hermes" -version = "0.1.0" +version = "0.8.1" description = "Workflow to publish research software with rich metadata" homepage = "https://software-metadata.pub" license = "Apache-2.0" @@ -20,6 +20,7 @@ authors = [ "Jeffrey Kelling ", "Oliver Knodel ", "David Pape ", + "Sophie Kernchen ", ] readme = "README.md" diff --git a/src/hermes/commands/base.py b/src/hermes/commands/base.py index dea22412..82692975 100644 --- a/src/hermes/commands/base.py +++ b/src/hermes/commands/base.py @@ -43,6 +43,7 @@ def __init__(self, parser: argparse.ArgumentParser): self.settings = None self.log = logging.getLogger(f"hermes.{self.command_name}") + self.errors = [] def init_plugins(self): """Collect and initialize the plugins available for the HERMES command.""" diff --git a/src/hermes/commands/cli.py b/src/hermes/commands/cli.py index 44bef30e..564586c6 100644 --- a/src/hermes/commands/cli.py +++ b/src/hermes/commands/cli.py @@ -9,7 +9,9 @@ This module provides the main entry point for the HERMES command line application. """ import argparse +import sys +from hermes import logger from hermes.commands import HermesHelpCommand, HermesCleanCommand, HermesHarvestCommand, HermesProcessCommand, \ HermesCurateCommand, HermesDepositCommand, HermesPostprocessCommand, HermesInitCommand from hermes.commands.base import HermesCommand @@ -57,6 +59,28 @@ def main() -> None: # Actually parse the command line, configure it and execute the selected sub-command. args = parser.parse_args() - args.command.load_settings(args) - args.command.patch_settings(args) - args.command(args) + logger.init_logging() + log = logger.getLogger("hermes.cli") + log.debug("Running hermes with the following command line arguments: %s", args) + + try: + log.debug("Loading settings...") + args.command.load_settings(args) + + log.debug("Update settings from command line...") + args.command.patch_settings(args) + + log.info("Run subcommand %s", args.command.command_name) + args.command(args) + except Exception as e: + log.error("An error occurred during execution of %s", args.command.command_name) + log.debug("Original exception was: %s", e) + + sys.exit(2) + + if args.command.errors: + for e in args.command.errors: + log.error(e) + sys.exit(1) + + sys.exit(0) diff --git a/src/hermes/commands/deposit/base.py b/src/hermes/commands/deposit/base.py index e92249ad..0a8e10f5 100644 --- a/src/hermes/commands/deposit/base.py +++ b/src/hermes/commands/deposit/base.py @@ -120,7 +120,6 @@ def init_command_parser(self, command_parser: argparse.ArgumentParser) -> None: def __call__(self, args: argparse.Namespace) -> None: self.args = args plugin_name = self.settings.target - print(self.args) ctx = CodeMetaContext() codemeta_file = ctx.get_cache("curate", ctx.hermes_name) @@ -135,11 +134,13 @@ def __call__(self, args: argparse.Namespace) -> None: try: plugin_func = self.plugins[plugin_name](self, ctx) - except KeyError: + except KeyError as e: self.log.error("Plugin '%s' not found.", plugin_name) + self.errors.append(e) try: plugin_func(self) except HermesValidationError as e: self.log.error("Error while executing %s: %s", plugin_name, e) + self.errors.append(e) diff --git a/src/hermes/commands/deposit/invenio.py b/src/hermes/commands/deposit/invenio.py index 0e744ae3..2dcbb268 100644 --- a/src/hermes/commands/deposit/invenio.py +++ b/src/hermes/commands/deposit/invenio.py @@ -107,7 +107,7 @@ class InvenioResolver: invenio_client_class = InvenioClient def __init__(self, client=None): - self.client = client or self.invenio_client_class() + self.client = client or self.invenio_client_class(InvenioDepositSettings()) def resolve_latest_id( self, record_id=None, doi=None, codemeta_identifier=None diff --git a/src/hermes/commands/harvest/base.py b/src/hermes/commands/harvest/base.py index dee745c5..4d2a1731 100644 --- a/src/hermes/commands/harvest/base.py +++ b/src/hermes/commands/harvest/base.py @@ -48,10 +48,8 @@ def __call__(self, args: argparse.Namespace) -> None: try: plugin_func = self.plugins[plugin_name]() harvested_data, tags = plugin_func(self) - print(harvested_data) - with HermesHarvestContext( - ctx, plugin_name - ) as harvest_ctx: + + with HermesHarvestContext(ctx, plugin_name) as harvest_ctx: harvest_ctx.update_from(harvested_data, plugin=plugin_name, timestamp=datetime.now().isoformat(), **tags) @@ -59,8 +57,10 @@ def __call__(self, args: argparse.Namespace) -> None: if any(v != _value and t == _tag for v, t in _trace): raise MergeError(_key, None, _value) - except KeyError: + except KeyError as e: self.log.error("Plugin '%s' not found.", plugin_name) + self.errors.append(e) except HermesValidationError as e: self.log.error("Error while executing %s: %s", plugin_name, e) + self.errors.append(e) diff --git a/src/hermes/commands/postprocess/invenio.py b/src/hermes/commands/postprocess/invenio.py index 7d039ae1..a7ba6b53 100644 --- a/src/hermes/commands/postprocess/invenio.py +++ b/src/hermes/commands/postprocess/invenio.py @@ -24,7 +24,7 @@ def config_record_id(ctx): conf.deposit.invenio.record_id = deposition['record_id'] toml.dump(conf, open('hermes.toml', 'w')) except KeyError: - raise RuntimeError("No deposit.invenio configuration available to store record id in") from None + raise RuntimeError("No deposit.invenio configuration available to store record id in") def cff_doi(ctx): diff --git a/src/hermes/commands/postprocess/invenio_rdm.py b/src/hermes/commands/postprocess/invenio_rdm.py index 7b2bf37c..9553f47b 100644 --- a/src/hermes/commands/postprocess/invenio_rdm.py +++ b/src/hermes/commands/postprocess/invenio_rdm.py @@ -23,4 +23,4 @@ def config_record_id(ctx): conf['deposit']['invenio_rdm']['record_id'] = deposition['record_id'] toml.dump(conf, open('hermes.toml', 'w')) except KeyError: - raise RuntimeError("No deposit.invenio configuration available to store record id in") from None + raise RuntimeError("No deposit.invenio_rdm configuration available to store record id in") diff --git a/src/hermes/commands/process/base.py b/src/hermes/commands/process/base.py index 8abeacc5..9e29d1e6 100644 --- a/src/hermes/commands/process/base.py +++ b/src/hermes/commands/process/base.py @@ -58,7 +58,8 @@ def __call__(self, args: argparse.Namespace) -> None: ctx.merge_contexts_from(harvest_context) if ctx._errors: - self.log.error('!!! warning "Errors during merge"') + self.log.error('Errors during merge') + self.errors.extend(ctx._errors) for ep, error in ctx._errors: self.log.info(" - %s: %s", ep.name, error) diff --git a/src/hermes/logger.py b/src/hermes/logger.py index cd8b28bc..7b6dd981 100644 --- a/src/hermes/logger.py +++ b/src/hermes/logger.py @@ -31,7 +31,7 @@ 'class': "logging.FileHandler", 'formatter': "logfile", 'level': "DEBUG", - 'filename': "./.hermes/hermes.log", + 'filename': "./hermes.log", }, 'auditfile': { @@ -50,12 +50,6 @@ }, } -# This dict caches all the different configuration sections already loaded -_config = { - # We need some basic logging configuration to get logging up and running at all - 'logging': _logging_config, -} - _loggers = {} @@ -64,14 +58,14 @@ def init_logging(): return # Make sure the directories to hold the log files exists (or else create) - pathlib.Path(_config['logging']['handlers']['logfile']['filename']).parent.mkdir(exist_ok=True, parents=True) - pathlib.Path(_config['logging']['handlers']['auditfile']['filename']).parent.mkdir(exist_ok=True, parents=True) + pathlib.Path(_logging_config['handlers']['logfile']['filename']).parent.mkdir(exist_ok=True, parents=True) + pathlib.Path(_logging_config['handlers']['auditfile']['filename']).parent.mkdir(exist_ok=True, parents=True) # Inintialize logging system import logging.config - logging.config.dictConfig(_config['logging']) - for log_name in _config['logging']['loggers']: + logging.config.dictConfig(_logging_config) + for log_name in _logging_config['loggers']: _loggers[log_name] = logging.getLogger(log_name) diff --git a/src/hermes/settings.py b/src/hermes/settings.py deleted file mode 100644 index fbae7b92..00000000 --- a/src/hermes/settings.py +++ /dev/null @@ -1,44 +0,0 @@ -# SPDX-FileCopyrightText: 2024 German Aerospace Center (DLR) -# -# SPDX-License-Identifier: Apache-2.0 - -# SPDX-FileContributor: Sophie Kernchen -# SPDX-FileContributor: Michael Meinel - -import pathlib -from typing import Any, Dict, Tuple - -import toml -from pydantic.fields import FieldInfo -from pydantic_settings import PydanticBaseSettingsSource - - -class TomlConfigSettingsSource(PydanticBaseSettingsSource): - """ - A simple settings source class that loads variables from a TOML file - at the project's root. - - Here we happen to choose to use the `env_file_encoding` from Config - when reading `config.json` - """ - def __init__(self, settings_cls, config_path): - super().__init__(settings_cls) - self.__config_path = config_path - - def get_field_value(self, field: FieldInfo, field_name: str) -> Tuple[Any, str, bool]: - file_content_toml = toml.load(pathlib.Path(self.__config_path)) - field_value = file_content_toml.get(field_name) - return field_value, field_name, False - - def prepare_field_value(self, field_name: str, field: FieldInfo, value: Any, value_is_complex: bool) -> Any: - return value - - def __call__(self) -> Dict[str, Any]: - d: Dict[str, Any] = {} - for field_name, field in self.settings_cls.model_fields.items(): - field_value, field_key, value_is_complex = self.get_field_value(field, field_name) - field_value = self.prepare_field_value(field_name, field, field_value, value_is_complex) - if field_value is not None: - d[field_key] = field_value - - return d diff --git a/test/hermes_test/conftest.py b/test/hermes_test/conftest.py index 16f3b7a0..2d3e52b2 100644 --- a/test/hermes_test/conftest.py +++ b/test/hermes_test/conftest.py @@ -23,8 +23,10 @@ def __setitem__(self, path, data): def __enter__(self): self.test_path.mkdir(parents=True, exist_ok=True) + for file_name, data in self.test_files.items(): file_path = self.test_path / file_name + file_path.parent.mkdir(parents=True, exist_ok=True) file_path.write_text(data) os.chdir(self.test_path) diff --git a/test/hermes_test/test_cli.py b/test/hermes_test/test_cli.py index 9261c391..85b40e5e 100644 --- a/test/hermes_test/test_cli.py +++ b/test/hermes_test/test_cli.py @@ -9,23 +9,27 @@ from hermes.commands import cli -def test_hermes_full(capsys): +def test_hermes_full(): with pytest.raises(SystemExit) as se: cli.main() assert "choose from" in se -@pytest.mark.skip(reason="Needs update") def test_hermes_harvest(hermes_env): + hermes_env['hermes.toml'] = "" + with hermes_env: result = hermes_env.run("harvest") assert result.returncode == 0 -@pytest.mark.skip(reason="Needs update") def test_hermes_process(hermes_env): + hermes_env['hermes.toml'] = "" + hermes_env['.hermes/harvest/test.json'] = "" + with hermes_env: result = hermes_env.run("process") + print(result.stdout.read()) assert result.returncode == 0