confuse icon indicating copy to clipboard operation
confuse copied to clipboard

Import secondary configuration files

Open amadanmath opened this issue 7 years ago • 1 comments

I read in other issues the opinion that there should be one configuration file, and the rest be data. I have a counterexample to that.

I currently need user-switchable configurations for what the app will be doing as the main configuration file. There will be sample configurations for different tasks that users can easily copy. Independently of that, I want database configuration that will not be modified that often. Consequently, I expect to have config.yaml and db.yaml in my .config/APPNAME directory. So currently I have something like this:

config = confuse.Configuration(APPNAME, __name__)
try:
    _db_config_name = os.path.join(config.config_dir(), 'db.yaml')
    config['db'].set(confuse.ConfigSource(confuse.load_yaml(_db_config_name), _db_config_name))
except confuse.ConfigReadError:
    pass

However, it feels like I'm doing something dirty. :) Is there a better way to do something like this?

amadanmath avatar Jul 02 '18 06:07 amadanmath

Cool! Yes, this seems like a good argument in favor of the way Confuse merges configuration sources.

Your way doesn’t look bad at all. But there is a set_file method here that parses and adds a source from a filename, but it doesn’t include the path manipulation or the error handler. That would at least help make your code a little simpler. :smiley:

sampsyo avatar Jul 02 '18 19:07 sampsyo