Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declarative approach to settings #234

Merged
merged 40 commits into from
Feb 9, 2024

Conversation

SKernchen
Copy link
Contributor

@SKernchen SKernchen linked an issue Nov 15, 2023 that may be closed by this pull request
@SKernchen SKernchen changed the title Feature/219 declarative approach to settings Declarative approach to settings Nov 15, 2023
Kernchen, Sophie and others added 2 commits November 15, 2023 16:52
This is required as Pydantic uses this style for their doc strings.
Copy link
Member

@led02 led02 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor change.

pyproject.toml Show resolved Hide resolved
src/hermes/config.py Outdated Show resolved Hide resolved
src/hermes/config.py Outdated Show resolved Hide resolved
Co-authored-by: Michael Meinel <michael.meinel@dlr.de>
pyproject.toml Outdated Show resolved Hide resolved
src/hermes/config.py Outdated Show resolved Hide resolved
Co-authored-by: David Pape <d.pape@hzdr.de>
@zyzzyxdonta
Copy link
Contributor

Similiarly to the JsonConfigSettingsSource defined here, you should be able to write a TomlConfigSettingsSource that reads the config from hermes.toml.

Then you can instantiate the settings like this:

settings = HermesSettings()

at the end of config.py and they will be filled with values from the file Then no manual calls to to model_validate are needed.

(I'm not sure if we can go this route with the plan of plugins being able to configure their own settings.)

@SKernchen
Copy link
Contributor Author

Similiarly to the JsonConfigSettingsSource defined here, you should be able to write a TomlConfigSettingsSource that reads the config from hermes.toml.

Then you can instantiate the settings like this:

settings = HermesSettings()

at the end of config.py and they will be filled with values from the file Then no manual calls to to model_validate are needed.

(I'm not sure if we can go this route with the plan of plugins being able to configure their own settings.)

I can already skip the model_validate by using the json in the Settings class which uses the HermesSettings. Still defining the Source and automatically reading the Toml could be better for usage.

@zyzzyxdonta
Copy link
Contributor

zyzzyxdonta commented Nov 29, 2023

You call model_validate here. Which makes sense because the settings are read manually, validated once and then accessed using the old code. I.e. hermes.config.get. I meant that this could be avoided.

@led02
Copy link
Member

led02 commented Jan 5, 2024

After reviewing the code, some general remarks here:

praise: Worked better than I expected according to your reports. Also your code is well structured and your names seem well chosen. :)

opinion: I think it is not required / advisible to try to keep the exiting code working with declarative model.
In contrast, especially the config.get('foo', 'bar') calls should be avoided.
Instead, there should be a HermesSettings derived class that defines the defaults (where applicable).
While this will bring in lots of minor code breaks, this also brings some major improvements:

  • We have a centeral place (for each plugin) where all the required and default values are declared.
  • The code to access to the single configuration values is much cleaner.
  • We have validation "up front" (i.e., not only when the plugin tries to read the value).
    This is also already given by the validation during configuration but emulation of the dictionaries get behaviour might lead to unforseen effects.

We could discuss whether it would be good to have both options available for transition but I'd prefer to break and repair instead of maintaining deprecated stuff. We could also change your __getattr__ method into a get implementation but this triggers a similar feeling of wrongness deep within my programmers ❤️ ... ;)

hint: I suppose you are using an automatic code formatter? You should definately extend the allowed line length to 120 chars. This will make the code a bit more readable at some places... (This is also the value we are using in our liniting).

Copy link
Member

@led02 led02 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Not working yet, hence no approval.

I added some inline hints and also some general thoughts about the implementation.
I think the way forward here is to implement / adapt all the plugins that access configuration values (i.e., get rid of all the get calls).
Then we can in parallel further refactor the class-based plugin API (i.e., no central settings.py collecting all models anymore) and implement the CLI switch.

Maybe a litte co-programming could help with pushing this forward... ;)

src/hermes/commands/deposit/file.py Outdated Show resolved Hide resolved
src/hermes/commands/deposit/invenio.py Outdated Show resolved Hide resolved
src/hermes/commands/deposit/invenio.py Outdated Show resolved Hide resolved
src/hermes/config.py Outdated Show resolved Hide resolved
src/hermes/settings.py Outdated Show resolved Hide resolved
src/hermes/settings.py Outdated Show resolved Hide resolved
src/hermes/settings.py Outdated Show resolved Hide resolved
@SKernchen
Copy link
Contributor Author

Config is not in Context rigth now, maybe @sdruskat attempt for changing to argparse can help there

@led02 led02 mentioned this pull request Feb 9, 2024
21 tasks
Copy link
Member

@led02 led02 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are still several places where the hermes.logger module is used. However, the config should be accessible using the ctx object (which is already part of the refactoring strategy).

src/hermes/cli.py Outdated Show resolved Hide resolved
src/hermes/cli.py Show resolved Hide resolved
src/hermes/commands/postprocess/invenio.py Outdated Show resolved Hide resolved
src/hermes/commands/workflow.py Outdated Show resolved Hide resolved
src/hermes/commands/workflow.py Outdated Show resolved Hide resolved
src/hermes/commands/workflow.py Outdated Show resolved Hide resolved
src/hermes/commands/workflow.py Outdated Show resolved Hide resolved
src/hermes/commands/postprocess/invenio_rdm.py Outdated Show resolved Hide resolved
led02 added 6 commits February 9, 2024 12:49
There is still one place... a hack that is required to introduce the
new interface while not having the front-end refactored yet.
This happens when there is no configuration file given and no `hermes.toml` available in the current directory.
@led02 led02 self-requested a review February 9, 2024 12:25
@led02 led02 marked this pull request as ready for review February 9, 2024 14:09
@led02 led02 merged commit 7c2f3e5 into develop Feb 9, 2024
4 checks passed
@poikilotherm poikilotherm deleted the feature/219-declarative-approach-to-settings branch March 6, 2024 20:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Declarative approach to settings
3 participants