Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@kba
Copy link
Member

@kba kba commented Aug 21, 2023

We've been accumulating quite a lot of environment variables that define behavior in addition to the CLI options and other explicit mechanisms in the codebase. @bertsky started documenting all of those in 2d85503 and this PR goes a step further to bundle all the functionality and documentation in one place.

In ocrd_utils.config, there is a singleton instance config of ocrd_utils.config.OcrdEnvConfig that should be used for registering environment variables and parsing them.

For example to define a new environemnt variable OCRD_LOGGING_CONFIG, we register it:

from pathlib import Path
from ocrd_utils import config
config.add('OCRD_LOGGING_CONFIG',
description="Path to logging configuration",
validator: lambda val: Path.exists(),
parser: lambda val: Path(val))

This environment variable can now be accessed as config.OCRD_LOGGING_CONFIG which will return a pathlib.Path because the parser. Since we're defining validator, this will fail as soon as config.OCRD_LOGGING_CONFIG is accessed, if the value is not an existing file and config.OCRD_LOGGING.CONFIG will return a Path.

From the description field, we generate the epilog to the ocrd --help output listing all the variables.

In short, whenever we need a new environment variable X:

  • register it in ocrd_utils.config
  • add it to the CLI epilog
  • use it as config.X

@kba kba requested review from MehmedGIT, bertsky and joschrew August 21, 2023 16:45
@kba kba marked this pull request as ready for review August 21, 2023 16:45
Copy link
Contributor

@MehmedGIT MehmedGIT left a comment

Choose a reason for hiding this comment

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

It looks more organized now, I like it.

@kba kba merged commit 46cd1dd into master Sep 11, 2023
@kba kba deleted the env-config branch November 17, 2023 13:11
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.

3 participants