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

Templates do not resolve recursively #117

Open
dolfandringa opened this issue Oct 13, 2020 · 1 comment
Open

Templates do not resolve recursively #117

dolfandringa opened this issue Oct 13, 2020 · 1 comment

Comments

@dolfandringa
Copy link

I am trying to create a template for my config file using confuse but have trouble understanding how this works. Unfortunately I can't find much about it in the docs.
I am have the following sample in MyApp.py:

import confuse

template = {"api": {"token_file": confuse.templates.Filename()}}

config = confuse.Configuration("MyApp")
config.get(template)
print(config["api"]["token_file"])
print(config["api"].get(template["api"])["token_file"])
print(config["api"]["token_file"].as_filename())

If I then put the following in ~/.config/MyApp/config.yaml

api:
    token_file: ./my_token.pickle

This is the output of MyApp.py:

./my_token.pickle
/home/me/.config/MyApp/my_token.pickle
/home/me/.config/MyApp/my_token.pickle

As you can see the first print statement doesn't actually resolve the filename, even though the template specifies it as a filename, but the second and third version do resolve it correctly. Since I want my application to be ignorant wrt confuse, I just want it to use config['api']['token_file'] without having to worry about the templates. So is there a way to have confuse recursively resolve the template types when I call config.get(template), so the first print statement also resolves as I was expecting?

@sampsyo
Copy link
Member

sampsyo commented Oct 13, 2020

I think the main thing to realize is that the line config.get(template) isn't doing anything—it doesn't change anything about the configuration. get is really just a getter and won't affect further calls.

You can do config.get(template)['api']['token_file'] and that should do what you expect, equivalent to config["api"]["token_file"].as_filename(). So to directly answer your question, you can store the result of config.get(template), which is just a normal, completely standard Python dict-of-dicts data structure and use that.

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

No branches or pull requests

2 participants