Skip to content

Commit

Permalink
Makes config global, renames config.py to logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Kernchen, Sophie committed Jan 11, 2024
1 parent cf4c6c9 commit 7c67de5
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 26 deletions.
11 changes: 6 additions & 5 deletions src/hermes/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@

import click

from hermes import config
import hermes.logger as logger
from hermes.commands import workflow
from hermes.config import configure, init_logging
from hermes.logger import configure, init_logging
from hermes.settings import HermesSettings



def log_header(header, summary=None):
_log = config.getLogger('cli')
_log = logger.getLogger('cli')

dist = metadata.distribution('hermes')
meta = dist.metadata
Expand Down Expand Up @@ -92,7 +93,7 @@ def invoke(self, ctx: click.Context) -> t.Any:
config_path = ctx.params.get('config').absolute()
try:
with open(config_path, 'r') as config_file:
settings = HermesSettings(config_path)
config = HermesSettings(config_path)
except ValidationError as e:
print(e, file=sys.stderr)
sys.exit(1)
Expand All @@ -101,7 +102,7 @@ def invoke(self, ctx: click.Context) -> t.Any:
# An explicit filename (different from default) was given, so the file should be available...
print(f"Configuration not present at {config_path}.", file=sys.stderr)
sys.exit(1)
configure(settings, working_path)
configure(config, working_path)
init_logging()
log_header(None)

Expand Down
4 changes: 2 additions & 2 deletions src/hermes/commands/deposit/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import json

from hermes import config
import hermes.logger as logger
from hermes.commands.deposit.base import BaseDepositPlugin
from hermes.model.path import ContextPath

Expand All @@ -17,7 +17,7 @@ def map_metadata(self) -> None:
self.ctx.update(ContextPath.parse('deposit.file'), self.ctx['codemeta'])

def publish(self) -> None:
file_config = config.deposit.file
file_config = logger.config.deposit.file
output_data = self.ctx['deposit.file']

with open(file_config.get('filename', 'hermes.json'), 'w') as deposition_file:
Expand Down
2 changes: 1 addition & 1 deletion src/hermes/commands/deposit/invenio.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import click
import requests

from hermes import config
from hermes import logger
from hermes.commands.deposit.base import BaseDepositPlugin
from hermes.commands.deposit.error import DepositionUnauthorizedError
from hermes.error import MisconfigurationError
Expand Down
1 change: 1 addition & 0 deletions src/hermes/commands/harvest/cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def harvest_cff(click_ctx: click.Context, ctx: HermesHarvestContext):

# Validate the content to be correct CFF
cff_dict = _load_cff_from_file(cff_data)
print(ctx, "\n***\n",ctx.config)
if ctx.config.cff_validate and not _validate(cff_file, cff_dict):
raise HermesValidationError(cff_file)

Expand Down
4 changes: 2 additions & 2 deletions src/hermes/commands/postprocess/invenio.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import toml
from ruamel import yaml

from hermes import config
import hermes.logger as logger


_log = logging.getLogger('deposit.invenio')
Expand All @@ -21,7 +21,7 @@ def config_record_id(ctx):
deposition_path = ctx.get_cache('deposit', 'deposit')
with deposition_path.open("r") as deposition_file:
deposition = json.load(deposition_file)
conf = config.hermes
conf = logger.config.hermes
try:
conf['deposit']['invenio']['record_id'] = deposition['record_id']
toml.dump(conf, open('hermes.toml', 'w'))
Expand Down
5 changes: 2 additions & 3 deletions src/hermes/commands/postprocess/invenio_rdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import logging

import toml

from hermes import config
import hermes.logger as logger


_log = logging.getLogger('deposit.invenio_rdm')
Expand All @@ -20,7 +19,7 @@ def config_record_id(ctx):
deposition_path = ctx.get_cache('deposit', 'deposit')
with deposition_path.open("r") as deposition_file:
deposition = json.load(deposition_file)
conf = config.hermes
conf = logger.config.hermes
try:
conf['deposit']['invenio_rdm']['record_id'] = deposition['record_id']
toml.dump(conf, open('hermes.toml', 'w'))
Expand Down
10 changes: 5 additions & 5 deletions src/hermes/commands/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import click

from hermes import config
import hermes.logger as logger
from hermes.commands.deposit.base import BaseDepositPlugin
from hermes.error import MisconfigurationError
from hermes.model.context import HermesContext, HermesHarvestContext, CodeMetaContext
Expand All @@ -40,7 +40,7 @@ def harvest(click_ctx: click.Context):
ctx.init_cache("harvest")

# Get all harvesters
harvest_config = config.harvest
harvest_config = logger.config.harvest
harvester_names = harvest_config.sources if type(harvest_config.sources) else [ep.name for ep in
metadata.entry_points(
group='hermes.harvest')]
Expand Down Expand Up @@ -85,7 +85,7 @@ def process(click_ctx: click.Context):
click_ctx.exit(1)

# Get all harvesters
harvest_config = config.harvest
harvest_config = logger.config.harvest
harvester_names = harvest_config.sources if type(harvest_config.sources) else [ep.name for ep in
metadata.entry_points(
group='hermes.harvest')]
Expand Down Expand Up @@ -192,7 +192,7 @@ def deposit(click_ctx: click.Context, initial, auth_token, file):
with open(codemeta_file) as codemeta_fh:
ctx.update(codemeta_path, json.load(codemeta_fh))

deposit_config = config.deposit
deposit_config = logger.config.deposit

plugin_group = "hermes.deposit"
# TODO: Is having a default a good idea?
Expand Down Expand Up @@ -242,7 +242,7 @@ def postprocess(click_ctx: click.Context):
click_ctx.exit(1)

# Get all postprocessors
postprocess_config = config.postprocess
postprocess_config = logger.config.postprocess
postprocess_names = postprocess_config.execute

for postprocess_name in postprocess_names:
Expand Down
6 changes: 4 additions & 2 deletions src/hermes/config.py → src/hermes/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import hermes.settings
from hermes.model.context import HermesContext

config = None
# This is the default logging configuration, required to see log output at all.
# - Maybe it could possibly somehow be a somewhat good idea to move this into an own module ... later perhaps
_logging_config = {
Expand Down Expand Up @@ -75,13 +76,14 @@ def configure(settings: hermes.settings.HermesSettings, working_path: pathlib.Pa
working_path / HermesContext.hermes_cache_name / "audit.log"

_config['hermes'] = settings
print(_config['logging'], settings.harvest.sources)
global config
config = settings
print(config)
_config['logging'] = settings.logging if settings.logging != {} else _config['logging'] # TODO: 'dict' object is not callable

# Might be a good idea to move somewhere else (see comment for _logging_config)?
_loggers = {}


def init_logging():
if _loggers:
return
Expand Down
18 changes: 12 additions & 6 deletions src/hermes/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pydantic import BaseModel
from pydantic import BaseModel, field_validator
import toml
import pathlib
from typing import Any, Dict, Tuple, Type, ClassVar
Expand All @@ -20,20 +20,22 @@ class HarvestCff(BaseModel):
class HarvestSettings(BaseModel):
sources: list[str]
cff: HarvestCff = HarvestCff
cff_validate: bool = True

harvester: Dict = {}


class DepositTargetSettings(BaseModel):
site_url: str
site_url: str = ""

communities: list[str] = None
access_right: str
embargo_date: str = None
access_conditions: str = None
api_paths: Dict = {}

record_id: int
doi: str
record_id: int = None
doi: str = None


class DepositSettings(BaseModel):
Expand Down Expand Up @@ -62,9 +64,13 @@ class HermesSettings(BaseSettings):
hermes: Dict = {}
file: Dict = {}
filename: pathlib.Path = "hermes.toml"
cff_validate: bool = True
logging: Dict = {}
site_url: str = None
site_url: str = ""
@field_validator("site_url")
def none_to_empty(cls, v: object) -> object:
if v is None:
return ""
return v
config_path: ClassVar[pathlib.Path] = "hermes.toml" #TODO : Set config Path in cli

def __init__(self, conf_path):
Expand Down

0 comments on commit 7c67de5

Please sign in to comment.