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

Improve settings definition in a namespace like fashion #109

Open
fmigneault opened this issue Apr 29, 2020 · 0 comments
Open

Improve settings definition in a namespace like fashion #109

fmigneault opened this issue Apr 29, 2020 · 0 comments
Assignees
Labels
triage/enhancement New feature or request triage/experimental Something related to experimental features and/or package version

Comments

@fmigneault
Copy link
Collaborator

fmigneault commented Apr 29, 2020

Instead of having weaver.ini settings names like weaver.fancy_item_parameter, which makes it hard to read and quite long, we could improve it to be as follows:

[app:main]
weaver.fancy_item_parameter = true     # old
weaver.fancy_item_other = true         # old

[weaver]
fancy_item.paramter   # new
fancy_item.other      # new

This would achieve 2 goals :
1- separate the 'weaver' (or any other thing like mongo, caching, etc.) specific configurations into their relevant section.
2- leave the specific app:main and server specifications only to running the app (ie: pyramid config and eggs) to not pollute it with everything else.

Will require to load the settings using:

from configparser import ConfigParser
conf = ConfigParser()
conf.read([<ini>])
conf.get("<section>", "<name>")

For compatibility, we should plug the loaded config from ConfigParser into registry.config like follows (especially L21, don't need the other part as registry is accessible through request object):

def includeme(config):
LOGGER.info("Adding database...")
from weaver.database.mongodb import MongoDatabase
config.registry.db = MongoDatabase(config.registry)
def _add_db(request):
db = request.registry.db
# if db_url.username and db_url.password:
# db.authenticate(db_url.username, db_url.password)
return db
config.add_request_method(_add_db, "db", reify=True)

And the get_setting function should be improved to return a Settings(dict) object that knows how to return the appropriate setting (ie: weaver.setting would try all variants below), making it fairly transparent for the whole source code. The Settings class will have to hold references to the settings dictionary returned by registry.get_settings() (ie: pyramid-config [app:main] values) and the other settings from various INI sections.

[weaver]
setting = 

[app:main]
weaver.setting = 

To simplify the retrieval of settings, ConfigBox of https://pypi.org/project/python-box/ could be used.

@fmigneault fmigneault added triage/enhancement New feature or request triage/experimental Something related to experimental features and/or package version labels Apr 29, 2020
@fmigneault fmigneault self-assigned this Apr 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage/enhancement New feature or request triage/experimental Something related to experimental features and/or package version
Projects
None yet
Development

No branches or pull requests

1 participant