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

Context manager for the providers.Configuration #394

Closed
gtors opened this issue Feb 10, 2021 · 5 comments
Closed

Context manager for the providers.Configuration #394

gtors opened this issue Feb 10, 2021 · 5 comments
Assignees
Labels

Comments

@gtors
Copy link

gtors commented Feb 10, 2021

My proposal is to add the __enter__ and __exit__ methods to the providers.Configuration class to reduce code duplication and improve the visualization of configuration blocks.

Example of such case:

from dependency_injector import providers
from environs import Env

env = Env()
config = providers.Configuration()

config.some_plugin_name.some_interval_ms.override(
    env.int(
        "SOME_INTERVAL_MS",
        default=30000
    )
)
config.some_plugin_name.kafka.bootstrap_servers.override(
    env.list(
        "KAFKA_BOOTSTRAP_SERVERS"
    )
)
config.some_plugin_name.kafka.security_protocol.override(
    env.str(
        "KAFKA_SECURITY_PROTOCOL", 
        default="SASL_SSL"
    )
)
# ...
from dependency_injector import providers
from environs import Env

env = Env()
config = providers.Configuration()

with config.some_plugin_name as plugin:
    plugin.some_interval_ms.override(
        env.int(
            "SOME_INTERVAL_MS",
            default=30000
        )
    )

    with plugin.kafka as kafka:
        kafka.bootstrap_servers.override(
            env.list(
                "KAFKA_BOOTSTRAP_SERVERS"
            )
        )
        kafka.security_protocol.override(
            env.str(
                "KAFKA_SECURITY_PROTOCOL", 
                default="SASL_SSL"
            )
        )
    # ...
@rmk135 rmk135 self-assigned this Feb 10, 2021
@rmk135
Copy link
Member

rmk135 commented Feb 10, 2021

Interesting idea. The only concern is: could there be any other features pretending to use configuration provider context manager? - There are none of them on my mind right now. Need to think of it more.

@rmk135 rmk135 added the feature label Feb 10, 2021
@rmk135
Copy link
Member

rmk135 commented Feb 10, 2021

Is environs a third party package or your custom code from environs import Env?

@gtors
Copy link
Author

gtors commented Feb 10, 2021

Is environs a third party package or your custom code from environs import Env?

https://pypi.org/project/environs/

@rmk135
Copy link
Member

rmk135 commented Feb 10, 2021

https://pypi.org/project/environs/

👍 Thanks

@rmk135
Copy link
Member

rmk135 commented Feb 15, 2021

Done in version 4.23.0. @gtors thanks for suggesting the feature.

@rmk135 rmk135 closed this as completed Feb 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants