Configuration options which may be set on the command line or in config files.
The schema for each option is defined using the Opt sub-classes e.g.
- common_opts = [
- cfg.StrOpt(‘bind_host’,
- default=‘0.0.0.0’, help=’IP address to listen on’),
- cfg.IntOpt(‘bind_port’,
- default=9292, help=’Port number to listen on’)
]
Options can be strings, integers, floats, booleans, lists or ‘multi strings’:
- enabled_apis_opt = cfg.ListOpt(‘enabled_apis’,
- default=[‘cloudwatch’, ‘osapi_compute’], help=’List of APIs to enable by default’)
- DEFAULT_EXTENSIONS = [
- ‘synaps.api.openstack.contrib.standard_extensions’
] osapi_compute_extension_opt = cfg.MultiStrOpt(‘osapi_compute_extension’,
default=DEFAULT_EXTENSIONS)
Option schemas are registered with with the config manager at runtime, but before the option is referenced:
class ExtensionManager(object):
enabled_apis_opt = cfg.ListOpt(...)
- def __init__(self, conf):
- self.conf = conf self.conf.register_opt(enabled_apis_opt) ...
- def _load_extensions(self):
- for ext_factory in self.conf.osapi_compute_extension:
A common usage pattern is for each option schema to be defined in the module or class which uses the option:
opts = ...
- def add_common_opts(conf):
- conf.register_opts(opts)
- def get_bind_host(conf):
- return conf.bind_host
- def get_bind_port(conf):
- return conf.bind_port
An option may optionally be made available via the command line. Such options must registered with the config manager before the command line is parsed (for the purposes of –help and CLI arg validation):
- cli_opts = [
- cfg.BoolOpt(‘verbose’,
- short=’v’, default=False, help=’Print more verbose output’),
- cfg.BoolOpt(‘debug’,
- short=’d’, default=False, help=’Print debugging output’),
]
- def add_common_opts(conf):
- conf.register_cli_opts(cli_opts)
The config manager has a single CLI option defined by default, –config-file:
class ConfigOpts(object):
- config_file_opt = MultiStrOpt(‘config-file’,
- ...
- def __init__(self, ...):
- ... self.register_cli_opt(self.config_file_opt)
Option values are parsed from any supplied config files using SafeConfigParser. If none are specified, a default set is used e.g. glance-api.conf and glance-common.conf:
- glance-api.conf:
- [DEFAULT] bind_port = 9292
- glance-common.conf:
- [DEFAULT] bind_host = 0.0.0.0
Option values in config files override those on the command line. Config files are parsed in order, with values in later files overriding those in earlier files.
The parsing of CLI args and config files is initiated by invoking the config manager e.g.
conf = ConfigOpts() conf.register_opt(BoolOpt(‘verbose’, ...)) conf(sys.argv[1:]) if conf.verbose:
...
Options can be registered as belonging to a group:
- rabbit_group = cfg.OptionGroup(name=’rabbit’,
- title=’RabbitMQ options’)
- rabbit_host_opt = cfg.StrOpt(‘host’,
- default=’localhost’, help=’IP/hostname to listen on’),
- rabbit_port_opt = cfg.IntOpt(‘port’,
- default=5672, help=’Port number to listen on’)
- def register_rabbit_opts(conf):
- conf.register_group(rabbit_group) # options can be registered under a group in either of these ways: conf.register_opt(rabbit_host_opt, group=rabbit_group) conf.register_opt(rabbit_port_opt, group=’rabbit’)
If no group is specified, options belong to the ‘DEFAULT’ section of config files:
- glance-api.conf:
[DEFAULT] bind_port = 9292 ...
[rabbit] host = localhost port = 5672 use_ssl = False userid = guest password = guest virtual_host = /
Command-line options in a group are automatically prefixed with the group name:
–rabbit-host localhost –rabbit-port 9999
Option values in the default group are referenced as attributes/properties on the config manager; groups are also attributes on the config manager, with attributes for each of the options associated with the group:
server.start(app, conf.bind_port, conf.bind_host, conf)
- self.connection = kombu.connection.BrokerConnection(
- hostname=conf.rabbit.host, port=conf.rabbit.port, ...)
Option values may reference other values using PEP 292 string substitution:
- opts = [
- cfg.StrOpt(‘state_path’,
- default=os.path.join(os.path.dirname(__file__), ‘../’), help=’Top-level directory for maintaining synaps state’),
- cfg.StrOpt(‘sqlite_db’,
- default=’synaps.sqlite’, help=’file name for sqlite’),
- cfg.StrOpt(‘sql_connection’,
- default=’sqlite:///$state_path/$sqlite_db’, help=’connection string for sql database’),
]
Note that interpolation can be avoided by using ‘$$’.
For command line utilities that dispatch to other command line utilities, the disable_interspersed_args() method is available. If this this method is called, then parsing e.g.
script –verbose cmd –debug /tmp/mything
will no longer return:
[‘cmd’, ‘/tmp/mything’]
as the leftover arguments, but will instead return:
[‘cmd’, ‘–debug’, ‘/tmp/mything’]
i.e. argument parsing is stopped at the first non-option argument.
Bases: synaps.openstack.common.cfg.Error
Raised if a CLI opt is registered after parsing.
Bases: synaps.openstack.common.cfg.Opt
Bool opts are set to True or False on the command line using –optname or –noopttname respectively.
In config files, boolean values are case insensitive and can be set using 1/0, yes/no, true/false or on/off.
Bases: synaps.openstack.common.cfg.ConfigOpts
Bases: synaps.openstack.common.cfg.Error
Raised if there is an error parsing a config file.
Bases: synaps.openstack.common.cfg.Error
Raised if a config file value does not match its opt type.
Bases: synaps.openstack.common.cfg.Error
Raised if one or more config files are not found.
Bases: _abcoll.Mapping, object
Config options which may be set on the command line or in config files.
ConfigOpts is a configuration option manager with APIs for registering option schemas, grouping options, parsing option values and retrieving the values of options.
Bases: _abcoll.Mapping, object
A helper class representing the option values of a group as a mapping and attributes.
Bases: object
A helper class exposing opt values as a dict for string substitution.
Set parsing to stop on the first non-option.
If this this method is called, then parsing e.g.
script –verbose cmd –debug /tmp/mything
will no longer return:
[‘cmd’, ‘/tmp/mything’]
as the leftover arguments, but will instead return:
[‘cmd’, ‘–debug’, ‘/tmp/mything’]
i.e. argument parsing is stopped at the first non-option argument.
Set parsing to not stop on the first non-option.
This it the default behaviour.
Log the value of all registered opts.
It’s often useful for an app to log its configuration to a log file at startup for debugging. This method dumps to the entire config state to the supplied logger at a given log level.
Parameters: |
|
---|
Print the usage message for the current program.
Register a CLI option schema.
CLI option schemas must be registered before the command line and config files are parsed. This is to ensure that all CLI options are show in –help and option validation works as expected.
Parameters: |
|
---|---|
Returns: | False if the opt was already register, True otherwise |
Raises : | DuplicateOptError, ArgsAlreadyParsedError |
Register multiple CLI option schemas at once.
Register an option group.
An option group must be registered before options can be registered with the group.
Parameters: | group – an OptGroup object |
---|
Register an option schema.
Registering an option schema makes any option value which is previously or subsequently parsed from the command line or config files available as an attribute of this object.
Parameters: |
|
---|---|
Returns: | False if the opt was already register, True otherwise |
Raises : | DuplicateOptError |
Register multiple option schemas at once.
Reset the state of the object to before it was called.
Override an opt’s default value.
Override the default value of given option. A command line or config file value will still take precedence over this default.
Parameters: |
|
---|---|
Raises : | NoSuchOptError, NoSuchGroupError |
Override an opt value.
Override the command line, config file and default values of a given option.
Parameters: |
|
---|---|
Raises : | NoSuchOptError, NoSuchGroupError |
Bases: synaps.openstack.common.cfg.Error
Raised if multiple opts with the same name are registered.
Bases: exceptions.Exception
Base class for cfg exceptions.
Bases: synaps.openstack.common.cfg.Opt
Float opt values are converted to floats using the float() builtin.
Bases: synaps.openstack.common.cfg.Opt
Int opt values are converted to integers using the int() builtin.
Bases: synaps.openstack.common.cfg.Opt
List opt values are simple string values separated by commas. The opt value is a list containing these strings.
Bases: synaps.openstack.common.cfg.Opt
Multistr opt values are string opts which may be specified multiple times. The opt value is a list containing all the string values specified.
Bases: synaps.openstack.common.cfg.Error
Raised if a group which doesn’t exist is referenced.
Bases: synaps.openstack.common.cfg.Error, exceptions.AttributeError
Raised if an opt which doesn’t exist is referenced.
Bases: object
Base class for all configuration options.
An Opt object has no public methods, but has a number of public string properties:
- name:
- the name of the option, which may include hyphens
- dest:
- the (hyphen-less) ConfigOpts property which contains the option value
- short:
- a single character CLI option name
- default:
- the default value of the option
- metavar:
- the name shown as the argument to a CLI option in –help output
- help:
- an string explaining how the options value is used
Bases: object
Represents a group of opts.
CLI opts in the group are automatically prefixed with the group name.
Each group corresponds to a section in config files.
An OptGroup object has no public methods, but has a number of public string properties:
- name:
- the name of the group
- title:
- the group title as displayed in –help
- help:
- the group description as displayed in –help
Bases: synaps.openstack.common.cfg.Opt
String opts do not have their values transformed and are returned as str objects.
Bases: synaps.openstack.common.cfg.Error
Raised if an error occurs substituting a variable in an opt value.
Return a list of default configuration files.
We default to two config files: [${project}.conf, ${prog}.conf]
And we look for those config files in the following directories:
~/.${project}/ ~/ /etc/${project}/ /etc/
We return an absolute path for (at most) one of each the default config files, for the topmost directory it exists in.
For example, if project=foo, prog=bar and /etc/foo/foo.conf, /etc/bar.conf and ~/.foo/bar.conf all exist, then we return [‘/etc/foo/foo.conf’, ‘~/.foo/bar.conf’]
If no project name is supplied, we only look for ${prog.conf}.
Parameters: |
|
---|