Search code examples
oslo

How to use oslo_config to load config file only without register_opts?


After studying the usage of oslo_config, I found if I want to use few config values from a config file (test.conf), I need to declare these opts in my program and register them, as follows.

test.conf

[test]
enable = True
usr_name = ‘Joe’
hobby = [‘Compute’, ‘python’, ‘OpenStack’]

test.py

from oslo.config import cfg 
opt_group = cfg.OptGroup(name='test', title='Test example') 
test_opts = [
   cfg.BoolOpt('enable',default=False),
   cfg.StrOpt('user_name',default='No Name',help=('Name of user')),
   cfg.ListOpt('hobby',default=None,help=('List of hobby'))     
]

if __name__ == "__main__":
    CONF = cfg.CONF
    CONF.register_group(opt_group)
    CONF.register_opts(test_opts, opt_group)
    CONF(default_config_files=["/etc/test/test.conf"])

    print CONF.test.enable
    print CONF.test.user_name
    print CONF.test.hobby

Why cannot I just define these configs in the config file (test.conf) and load the config file to use them? Thanks.


Solution

  • Actually you can't.The config file and command line is used to parse the value of the options,and what you wanna do is registering schema,this is not only about parsing value,but also helping text,data type and validator which is much more complicated.read in detail.BTW you can also ask question here: Ask Openstack