Retire repo

This repo was created by accident, use deb-python-oslo.config
instead.

Needed-By: I1ac1a06931c8b6dd7c2e73620a0302c29e605f03
Change-Id: I81894aea69b9d09b0977039623c26781093a397a
This commit is contained in:
Andreas Jaeger 2017-04-17 19:35:14 +02:00
parent 14ec2c67eb
commit 7dfe42032f
75 changed files with 13 additions and 15663 deletions

View File

@ -1,8 +0,0 @@
[run]
branch = True
source = oslo_config
omit = oslo_config/tests/*
[report]
ignore_errors = True
precision = 2

22
.gitignore vendored
View File

@ -1,22 +0,0 @@
AUTHORS
ChangeLog
*~
*.egg*
*.swp
*.pyc
*.log
.tox
.coverage*
cover
oslo.config.egg-info/
build/
doc/build/
doc/source/api/
dist/
.testrepository/
.project
.pydevproject
/doc/source/sample.config
# Files created by releasenotes build
releasenotes/build

View File

@ -1,4 +0,0 @@
[gerrit]
host=review.openstack.org
port=29418
project=openstack/oslo.config.git

View File

@ -1,4 +0,0 @@
[DEFAULT]
test_command=OS_STDOUT_CAPTURE=1 OS_STDERR_CAPTURE=1 OS_TEST_TIMEOUT=60 ${PYTHON:-python} -m subunit.run discover -t ./ . $LISTOPT $IDOPTION
test_id_option=--load-list $IDFILE
test_list_option=--list

View File

@ -1,16 +0,0 @@
If you would like to contribute to the development of OpenStack,
you must follow the steps in this page:
http://docs.openstack.org/infra/manual/developers.html
Once those steps have been completed, changes to OpenStack
should be submitted for review via the Gerrit tool, following
the workflow documented at:
http://docs.openstack.org/infra/manual/developers.html#development-workflow
Pull requests submitted through GitHub will be ignored.
Bugs should be filed on Launchpad, not GitHub:
https://bugs.launchpad.net/oslo.config

204
LICENSE
View File

@ -1,204 +0,0 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
--- License for python-keystoneclient versions prior to 2.1 ---
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of this project nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -1,19 +0,0 @@
==========================
Oslo Configuration Library
==========================
.. image:: https://img.shields.io/pypi/v/oslo.config.svg
:target: https://pypi.python.org/pypi/oslo.config/
:alt: Latest Version
.. image:: https://img.shields.io/pypi/dm/oslo.config.svg
:target: https://pypi.python.org/pypi/oslo.config/
:alt: Downloads
The Oslo configuration API supports parsing command line arguments and
.ini style configuration files.
* License: Apache License, Version 2.0
* Documentation: http://docs.openstack.org/developer/oslo.config
* Source: http://git.openstack.org/cgit/openstack/oslo.config
* Bugs: http://bugs.launchpad.net/oslo.config

13
README.txt Normal file
View File

@ -0,0 +1,13 @@
This project is no longer maintained.
The contents of this repository are still available in the Git
source code management system. To see the contents of this
repository before it reached its end of life, please check out the
previous commit with "git checkout HEAD^1".
Use instead the project deb-python-oslo.config at
http://git.openstack.org/cgit/openstack/deb-python-oslo.config .
For any further questions, please email
openstack-dev@lists.openstack.org or join #openstack-dev on
Freenode.

View File

@ -1,5 +0,0 @@
==================
Built-in Options
==================
.. show-options:: oslo.config

View File

@ -1,5 +0,0 @@
--------------
The cfg Module
--------------
.. automodule:: oslo_config.cfg

View File

@ -1,5 +0,0 @@
--------------------
The cfgfilter Module
--------------------
.. automodule:: oslo_config.cfgfilter

View File

@ -1,78 +0,0 @@
# -*- coding: utf-8 -*-
import os
import subprocess
import sys
import warnings
sys.path.insert(0, os.path.abspath('../..'))
# -- General configuration ----------------------------------------------------
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = ['sphinx.ext.autodoc', 'oslosphinx',
'oslo_config.sphinxconfiggen',
'oslo_config.sphinxext']
config_generator_config_file = 'config-generator.conf'
# autodoc generation is a bit aggressive and a nuisance when doing heavy
# text edit cycles.
# execute "export SPHINX_DEBUG=1" in your terminal to disable
# Add any paths that contain templates here, relative to this directory.
# templates_path = []
# The suffix of source filenames.
source_suffix = '.rst'
# The master toctree document.
master_doc = 'index'
# General information about the project.
project = u'oslo.config'
copyright = u'2013, OpenStack Foundation'
# If true, '()' will be appended to :func: etc. cross-reference text.
add_function_parentheses = True
# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
add_module_names = True
# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'
# A list of ignored prefixes for module index sorting.
modindex_common_prefix = ['oslo_config.']
# -- Options for HTML output --------------------------------------------------
# The theme to use for HTML and HTML Help pages. Major themes that come with
# Sphinx are currently 'default' and 'sphinxdoc'.
# html_theme_path = ["."]
# html_theme = '_theme'
html_static_path = ['static']
# Output file base name for HTML help builder.
htmlhelp_basename = '%sdoc' % project
git_cmd = ["git", "log", "--pretty=format:'%ad, commit %h'", "--date=local",
"-n1"]
try:
html_last_updated_fmt = subprocess.Popen(
git_cmd, stdout=subprocess.PIPE).communicate()[0].decode('utf-8')
except Exception:
warnings.warn('Cannot get last updated time from git repository. '
'Not setting "html_last_updated_fmt".')
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass
# [howto/manual]).
latex_documents = [
('index',
'%s.tex' % project,
'%s Documentation' % project,
'OpenStack Foundation', 'manual'),
]

View File

@ -1,3 +0,0 @@
[DEFAULT]
wrap_width = 79
namespace = oslo.config

View File

@ -1,8 +0,0 @@
--------------------
The ConfigOpts Class
--------------------
.. currentmodule:: oslo_config.cfg
.. autoclass:: ConfigOpts
:members:

View File

@ -1,5 +0,0 @@
==============
Contributing
==============
.. include:: ../../CONTRIBUTING.rst

View File

@ -1,16 +0,0 @@
----------
Exceptions
----------
.. currentmodule:: oslo_config.cfg
.. autoexception:: Error
.. autoexception:: ArgsAlreadyParsedError
.. autoexception:: NoSuchOptError
.. autoexception:: NoSuchGroupError
.. autoexception:: DuplicateOptError
.. autoexception:: RequiredOptError
.. autoexception:: TemplateSubstitutionError
.. autoexception:: ConfigFilesNotFoundError
.. autoexception:: ConfigFileParseError
.. autoexception:: ConfigFileValueError

View File

@ -1,98 +0,0 @@
============================
Frequently Asked Questions
============================
Why does oslo.config have a CONF object? Global objects SUCK!
=============================================================
.. original source: https://wiki.openstack.org/wiki/Oslo#Why_does_oslo.config_have_a_CONF_object.3F_Global_object_SUCK.21
Indeed. Well, it's a long story and well documented in mailing list
archives if anyone cares to dig up some links.
Around the time of the Folsom Design Summit, an attempt was made to
remove our dependence on a global object like this. There was massive
debate and, in the end, the rough consensus was to stick with using
this approach.
Nova, through its use of the gflags library, used this approach from
`commit zero
<https://github.com/openstack/nova/blob/bf6e6e7/nova/flags.py#L27>`__. Some
OpenStack projects didn't initially use this approach, but most now
do. The idea is that having all projects use the same approach is more
important than the objections to the approach. Sharing code between
projects is great, but by also having projects use the same idioms for
stuff like this it makes it much easier for people to work on multiple
projects.
This debate will probably never completely go away, though. See `this
latest discussion in August, 2014
<http://lists.openstack.org/pipermail/openstack-dev/2014-August/044050.html>`__
Why are configuration options not part of a library's API?
==========================================================
Configuration options are a way for deployers to change the behavior
of OpenStack. Applications are not supposed to be aware of the
configuration options defined and used within libraries, because the
library API is supposed to work transparently no matter which backend
is configured.
Configuration options in libraries can be renamed, moved, and
deprecated just like configuration options in applications. However,
if applications are allowed to read or write the configuration options
directly, treating them as an API, the option cannot be renamed
without breaking the application. Instead, libraries should provide a
programmatic API (usually a :func:`set_defaults` function) for setting
the defaults for configuration options. For example, this function
from ``oslo.log`` lets the caller change the format string and default
logging levels:
::
def set_defaults(logging_context_format_string=None,
default_log_levels=None):
"""Set default values for the configuration options used by oslo.log."""
# Just in case the caller is not setting the
# default_log_level. This is insurance because
# we introduced the default_log_level parameter
# later in a backwards in-compatible change
if default_log_levels is not None:
cfg.set_defaults(
_options.log_opts,
default_log_levels=default_log_levels)
if logging_context_format_string is not None:
cfg.set_defaults(
_options.log_opts,
logging_context_format_string=logging_context_format_string)
If the name of either option changes, the API of :func:`set_defaults`
can be updated to allow both names, and warn if the old one is
provided. Using a supported API like this is better than having an
application call :func:`set_default` on the configuration object
directly, such as:
::
cfg.CONF.set_default('default_log_levels', default_log_levels)
This form will trigger an error if the logging options are moved out
of the default option group into their own section of the
configuration file. It will also fail if the ``default_log_levels``
option is not yet registered, or if it is renamed. All of those cases
can be protected against with a :func:`set_defaults` function in the
library that owns the options.
Similarly, code that does not *own* the configuration option
definition should not read the option value. An application should
never, for example, do something like:
::
log_file = cfg.CONF.log_file
The type, name, and existence of the ``log_file`` configuration option
is subject to change. ``oslo.config`` makes it easy to communicate
that change to a deployer in a way that allows their old configuration
files to continue to work. It has no mechanism for doing that in
application code, however.

View File

@ -1,8 +0,0 @@
------------
Test Fixture
------------
.. currentmodule:: oslo_config.fixture
.. autoclass:: Config
:members:

View File

@ -1,195 +0,0 @@
=======================
oslo-config-generator
=======================
oslo-config-generator is a utility for generating sample config files. For
example, to generate a sample config file for oslo.messaging you would run::
$> oslo-config-generator --namespace oslo.messaging > oslo.messaging.conf
This generated sample lists all of the available options, along with their help
string, type, deprecated aliases and defaults.
To generate a sample config file for an application ``myapp`` that has
its own options and uses oslo.messaging, you can list both
namespaces::
$> oslo-config-generator --namespace myapp --namespace oslo.messaging > myapp.conf
.. versionadded:: 1.4
Defining Option Discovery Entry Points
--------------------------------------
The ``--namespace`` option specifies an entry point name registered under the
'oslo.config.opts' entry point namespace. For example, in oslo.messaging's
setup.cfg we have::
[entry_points]
oslo.config.opts =
oslo.messaging = oslo.messaging.opts:list_opts
The callable referenced by the entry point should take no arguments and return
a list of (``group``, [opt_1, opt_2]) tuples, where ``group`` is either a
group name as a string or an ``OptGroup`` object. Passing the ``OptGroup``
object allows the consumer of the ``list_opts`` method to access and publish
group help. An example, using both styles::
opts1 = [
cfg.StrOpt('foo'),
cfg.StrOpt('bar'),
]
opts2 = [
cfg.StrOpt('baz'),
]
baz_group = cfg.OptGroup(name='baz_group'
title='Baz group options',
help='Baz group help text')
cfg.CONF.register_group(baz_group)
cfg.CONF.register_opts(opts1, group='blaa')
cfg.CONF.register_opts(opts2, group=baz_group)
def list_opts():
# Allows the generation of the help text for
# the baz_group OptGroup object. No help
# text is generated for the 'blaa' group.
return [('blaa', opts1), (baz_group, opts2)]
.. note::
You should return the original options, not a copy, because the
default update hooks depend on the original option object being
returned.
The module holding the entry point *must* be importable, even if the
dependencies of that module are not installed. For example, driver
modules that define options but have optional dependencies on
third-party modules must still be importable if those modules are not
installed. To accomplish this, the optional dependency can either be
imported using :func:`oslo.utils.importutils.try_import` or the option
definitions can be placed in a file that does not try to import the
optional dependency.
Modifying Defaults from Other Namespaces
----------------------------------------
Occasionally applications need to override the defaults for options
defined in libraries. At runtime this is done using an API within the
library. Since the config generator cannot guarantee the order in
which namespaces will be imported, we can't ensure that application
code can change the option defaults before the generator loads the
options from a library. Instead, a separate optional processing hook
is provided for applications to register a function to update default
values after *all* options are loaded.
The hooks are registered in a separate entry point namespace
(``oslo.config.opts.defaults``), using the same entry point name as
**the application's** ``list_opts()`` function.
::
[entry_points]
oslo.config.opts.defaults =
keystone = keystone.common.config:update_opt_defaults
.. warning::
Never, under any circumstances, register an entry point using a
name owned by another project. Doing so causes unexpected interplay
between projects within the config generator and will result in
failure to generate the configuration file or invalid values
showing in the sample.
In this case, the name of the entry point for the default override
function *must* match the name of one of the entry points defining
options for the application in order to be detected and
used. Applications that have multple list_opts functions should use
one that is present in the inputs for the config generator where
the changed defaults need to appear. For example, if an application
defines ``foo.api`` to list the API-related options, and needs to
override the defaults in the ``oslo.middleware.cors`` library, the
application should register ``foo.api`` under
``oslo.config.opts.defaults`` and point to a function within the
application code space that changes the defaults for
``oslo.middleware.cors``.
The update function should take no arguments. It should invoke the
public :func:`set_defaults` functions in any libraries for which it
has option defaults to override, just as the application does during
its normal startup process.
::
from oslo_log import log
def update_opt_defaults():
log.set_defaults(
default_log_levels=log.get_default_log_levels() + ['noisy=WARN'],
)
Generating Multiple Sample Configs
----------------------------------
A single codebase might have multiple programs, each of which use a subset of
the total set of options registered by the codebase. In that case, you can
register multiple entry points::
[entry_points]
oslo.config.opts =
nova.common = nova.config:list_common_opts
nova.api = nova.config:list_api_opts
nova.compute = nova.config:list_compute_opts
and generate a config file specific to each program::
$> oslo-config-generator --namespace oslo.messaging \
--namespace nova.common \
--namespace nova.api > nova-api.conf
$> oslo-config-generator --namespace oslo.messaging \
--namespace nova.common \
--namespace nova.compute > nova-compute.conf
To make this more convenient, you can use config files to describe your config
files::
$> cat > config-generator/api.conf <<EOF
[DEFAULT]
output_file = etc/nova/nova-api.conf
namespace = oslo.messaging
namespace = nova.common
namespace = nova.api
EOF
$> cat > config-generator/compute.conf <<EOF
[DEFAULT]
output_file = etc/nova/nova-compute.conf
namespace = oslo.messaging
namespace = nova.common
namespace = nova.compute
EOF
$> oslo-config-generator --config-file config-generator/api.conf
$> oslo-config-generator --config-file config-generator/compute.conf
Sample Default Values
---------------------
The default runtime values of configuration options are not always the most
suitable values to include in sample config files - for example, rather than
including the IP address or hostname of the machine where the config file
was generated, you might want to include something like '10.0.0.1'. To
facilitate this, options can be supplied with a 'sample_default' attribute::
cfg.StrOpt('base_dir'
default=os.getcwd(),
sample_default='/usr/lib/myapp')
API
---
.. currentmodule:: oslo_config.generator
.. autofunction:: main
.. autofunction:: generate
.. autofunction:: register_cli_opts

View File

@ -1,8 +0,0 @@
----------------
Helper Functions
----------------
.. currentmodule:: oslo_config.cfg
.. autofunction:: find_config_files
.. autofunction:: set_defaults

View File

@ -1,2 +0,0 @@
.. include:: ../../ChangeLog

View File

@ -1,46 +0,0 @@
oslo.config
===========
An OpenStack library for parsing configuration options from the command
line and configuration files.
Contents
========
.. toctree::
:maxdepth: 2
cfg
opts
types
configopts
cfgfilter
helpers
fixture
parser
exceptions
namespaces
styleguide
mutable
generator
builtins
sphinxext
sphinxconfiggen
faq
contributing
Release Notes
=============
.. toctree::
:maxdepth: 1
history
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

View File

@ -1,121 +0,0 @@
Enabling your project for mutable config
========================================
As of OpenStack Newton, config options can be marked as 'mutable'. This means
they can be reloaded (usually via SIGHUP) at runtime, without a service
restart. However, each project has to be enabled before this will work and some
care needs to be taken over how each option is used before it can safely be
marked mutable.
.. contents:: Table of Contents
:local:
Calling mutate_config_files
---------------------------
Config mutation is triggered by ``ConfigOpts#mutate_config_files`` being
called. Services launched with oslo.service get a signal handler on SIGHUP but
by default that calls the older ``ConfigOpts#reload_config_files`` method. To
get the new behaviour, we have to pass ``restart_method='mutate'``. For
example::
service.ProcessLauncher(CONF, restart_method='mutate')
An example patch is here: https://review.openstack.org/#/c/280851
Some projects may call ``reload_config_files`` directly, in this case just
change that call to ``mutate_config_files``. If there is no signal handler or
you want to trigger reload by a different method, maybe via a web UI or
watching a file, just ensure your trigger calls ``mutate_config_files``.
Making options mutable-safe
---------------------------
When options are mutated, they change in the ConfigOpts object but this will
not necessarily affect your service immediately. There are three main cases to
deal with:
* The option is checked every time
* The option is cached on the stack
* The option affects state
The option is checked every time
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This pattern is already safe. Example code::
while True:
progress_timeout = CONF.libvirt.live_migration_progress_timeout
completion_timeout = int(
CONF.libvirt.live_migration_completion_timeout * data_gb)
if libvirt_migrate.should_abort(instance, now, progress_time,
progress_timeout, completion_timeout):
guest.abort_job()
The option is cached on the stack
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Just putting the option value in a local variable is enough to cache it. This
is tempting to do with loops. Example code::
progress_timeout = CONF.libvirt.live_migration_progress_timeout
completion_timeout = int(
CONF.libvirt.live_migration_completion_timeout * data_gb)
while True:
if libvirt_migrate.should_abort(instance, now, progress_time,
progress_timeout, completion_timeout):
guest.abort_job()
The goal is to check the option exactly once every time it could have an
effect. Usually this is as simple as checking it every time, for example by
moving the locals into the loop. Example patch:
https://review.openstack.org/#/c/319203
Sometimes multiple computations have to be performed using the option values
and it's important that the result is consistent. In this case, it's necessary
to cache the option values in locals. Example patch:
https://review.openstack.org/#/c/319254
The option affects state
^^^^^^^^^^^^^^^^^^^^^^^^
An option value can also be cached, after a fashion, by state - either system
or external. For example, the 'debug' option of oslo.log is used to set the
default log level on startup. The option is not normally checked again, so if
it is mutated, the system state will not reflect the new value of the option.
In this case we have to use a *mutate hook*::
def _mutate_hook(conf, fresh):
if (None, 'debug') in fresh:
if conf.debug:
log_root.setLevel(logging.DEBUG)
def register_options(conf):
... snip ...
conf.register_mutate_hook(_mutate_hook)
Mutate hook functions will be passed two positional parameters, 'conf' and
'fresh'. 'conf' is a reference to the updated ConfigOpts object. 'fresh' looks
like::
{ (group, option_name): (old_value, new_value), ... }
for example::
{ (None, 'debug'): (False, True),
('libvirt', 'live_migration_progress_timeout'): (50, 75) }
Hooks may be called in any order.
Each project should register one hook, which does whatever is necessary to
apply all the new option values. This hook function could grow very large. For
good style, modularise the hook using secondary functions rather than accreting
a monolith or registering multiple hooks.
Example patch: https://review.openstack.org/#/c/254821/

View File

@ -1,12 +0,0 @@
----------------------------------------------
Choosing group names for configuration options
----------------------------------------------
Applications should use a meaningful name without a prefix. For Oslo
libraries, when naming groups for configuration options using the
name of the library itself instead of a descriptive name to help avoid
collisions. If the library name is namespaced then use '_' as a separator
in the group name.
For example, the ``oslo.log`` library should use ``oslo_log`` as the
group name.

View File

@ -1,22 +0,0 @@
------------------
Option Definitions
------------------
.. currentmodule:: oslo_config.cfg
.. autoclass:: Opt
.. autoclass:: StrOpt
.. autoclass:: BoolOpt
.. autoclass:: IntOpt
.. autoclass:: FloatOpt
.. autoclass:: ListOpt
.. autoclass:: DictOpt
.. autoclass:: MultiOpt
.. autoclass:: MultiStrOpt
.. autoclass:: IPOpt
.. autoclass:: PortOpt
.. autoclass:: HostnameOpt
.. autoclass:: URIOpt
.. autoclass:: DeprecatedOpt
.. autoclass:: SubCommandOpt
.. autoclass:: OptGroup

View File

@ -1,11 +0,0 @@
------------
File Parsing
------------
.. autoclass:: oslo_config.iniparser.BaseParser
.. autoclass:: oslo_config.cfg.ConfigParser
:members: parse
.. autoclass:: oslo_config.cfg.MultiConfigParser
:members: read, get

View File

@ -1,50 +0,0 @@
====================================
Sphinx Oslo Sample Config Generation
====================================
Included with oslo.config is a sphinx extension to generate a sample config
file at the beginning of each sphinx build. To activate the extension add
``oslo_config.sphinxconfiggen`` to the list of extensions in your sphinx
``conf.py``.
Then you just need to use the ``config_generator_config_file`` option to point
the config generator at the config file which tells it how to generate the
sample config. If one isn't specified or it doesn't point to a real file the
sample config file generation will be skipped.
To generate multiple files, set ``config_generator_config_file`` to a
list of tuples containing the input filename and the base name for the
output file.
The output value can be ``None``, in which case the name is taken from
the input value.
The input name can be an full path or a value relative to the
documentation source directory.
For example::
config_generator_config_file = [
('../../etc/glance-api.conf', 'api'),
('../../etc/glance-cache.conf', 'cache'),
('../../etc/glance-glare.conf', None),
('../../etc/glance-registry.conf', None),
('../../etc/glance-scrubber.conf', None),
]
Produces the output files ``api.conf.sample``, ``cache.conf.sample``,
``glance-glare.conf.sample``, ``glance-registry.conf.sample``, and
``glance-scrubber.conf.sample``.
Output File Name
----------------
By default the sphinx plugin will generate the sample config file and
name the file ``sample.config``. However, if for whatever reason you'd
like the name to be more specific to the project name you can use the
``sample_config_basename`` config option to specify the project
name. If it's set the output filename will be that value with a
``.conf.sample`` extension. For example if you set the value to
"``nova``" the output filename will be "``nova.conf.sample``". You can
also include a subdirectory off of the documentation source directory
as part of this value.

View File

@ -1,60 +0,0 @@
====================
Sphinx Integration
====================
The ``oslo_config.sphinxext`` module defines a custom domain for
documenting configuration options. The domain includes a directive and
two roles.
.. rst:directive:: show-options
Given a list of namespaces, show all of the options exported from
them.
::
.. show-options::
oslo.config
oslo.log
To show each namespace separately, add the ``split-namespaces``
flag.
::
.. show-options::
:split-namespaces:
oslo.config
oslo.log
To use an existing configuration file for the sample configuration
generator, use the ``config-file`` option instead of specifying the
namespaces inline.
::
.. show-options::
:config-file: etc/oslo-config-generator/glance-api.conf
.. rst:role:: option
Link to an option.
::
#. :oslo.config:option:`config_file`
#. :oslo.config:option:`DEFAULT.config_file`
#. :oslo.config:option:`config_file`
#. :oslo.config:option:`DEFAULT.config_file`
.. rst:role:: group
Link to an option group.
::
:oslo.config:group:`DEFAULT`
:oslo.config:group:`DEFAULT`

View File

@ -1,84 +0,0 @@
----------------------------
Style Guide for Help Strings
----------------------------
This document provides style guidance for writing the required help
strings for configuration options using the ``oslo.config`` code.
The help strings are parsed from the code to appear in sample
configuration files, such as ``etc/cinder/cinder.conf`` in the
cinder repository. They are also displayed in the `OpenStack
Configuration Reference
<http://docs.openstack.org/trunk/config-reference/content/index.html>`_.
Examples::
cfg.StrOpt('bind_host',
default='0.0.0.0',
help='IP address to listen on.'),
cfg.PortOpt('bind_port',
default=9292,
help='Port number to listen on.')
Style Guide
-----------
1. Use sentence-style capitalization for help strings: Capitalize or
uppercase the first character (see the examples above).
2. Only use single spaces, no double spaces.
3. Properly capitalize words. If in doubt check the `OpenStack Glossary <http://docs.openstack.org/glossary/>`_.
4. End each segment with a period and write complete sentences if
possible. Examples::
cfg.StrOpt('osapi_volume_base_URL',
default=None,
help='Base URL that appears in links to the OpenStack '
'Block Storage API.')
cfg.StrOpt('host',
default=socket.gethostname(),
help='Name of this node. This can be an opaque identifier. '
'It is not necessarily a host name, FQDN, or IP address.')
5. Use valid service names and API names. Valid service names include
nova, cinder, swift, glance, heat, neutron, trove, ceilometer,
horizon, keystone, and marconi.
Valid API names include Compute API, Image Service API, Identity
Service API, Object Storage API, Block Storage API, Database API,
and Networking API.
Format
------
1. For multi-line strings, remember that strings are concatenated
directly and thus spaces need to be inserted normally.
This document recommends to add the space at the end of a line and
not at the beginning. Example::
cfg.BoolOpt('glance_api_ssl_compression',
default=False,
help='Enables or disables negotiation of SSL layer '
'compression. In some cases disabling compression '
'can improve data throughput, such as when high '
'network bandwidth is available and you use '
'compressed image formats like qcow2.')
2. It is possible to preformat the multi-line strings to increase readability.
Line break characters ``\n`` will be kept as they are used in the help text.
Example::
cfg.IntOpt('sync_power_state_interval',
default=600,
help='Interval to sync power states between the database and '
'the hypervisor.\n'
'\n'
'-1: disables the sync \n'
' 0: run at the default rate.\n'
'>0: the interval in seconds')

View File

@ -1,6 +0,0 @@
---------------------------
Option Types and Validation
---------------------------
.. automodule:: oslo_config.types
:members:

View File

@ -1,50 +0,0 @@
# Copyright 2016 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""oslo.i18n integration module.
See http://docs.openstack.org/developer/oslo.i18n/usage.html .
"""
import oslo_i18n
DOMAIN = "oslo.config"
_translators = oslo_i18n.TranslatorFactory(domain=DOMAIN)
# The primary translation function using the well-known name "_"
_ = _translators.primary
# The contextual translation function using the name "_C"
# requires oslo.i18n >=2.1.0
_C = _translators.contextual_form
# The plural translation function using the name "_P"
# requires oslo.i18n >=2.1.0
_P = _translators.plural_form
# Translators for log levels.
#
# The abbreviated names are meant to reflect the usual use of a short
# name like '_'. The "L" is for "log" and the other letter comes from
# the level.
_LI = _translators.log_info
_LW = _translators.log_warning
_LE = _translators.log_error
_LC = _translators.log_critical
def get_available_languages():
return oslo_i18n.get_available_languages(DOMAIN)

View File

@ -1,25 +0,0 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_config import cfg
def list_opts():
default_config_files = [
'~/.project/project.conf',
'~/project.conf',
'/etc/project/project.conf',
'/etc/project.conf',
]
return [
(None, cfg.ConfigOpts._make_config_options(default_config_files)),
]

File diff suppressed because it is too large Load Diff

View File

@ -1,383 +0,0 @@
# Copyright 2014 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
r"""
There are three use cases for the ConfigFilter class:
1. Help enforce that a given module does not access options registered
by another module, without first declaring those cross-module
dependencies using import_opt().
2. Prevent private configuration opts from being visible to modules
other than the one which registered it.
3. Limit the options on a Cfg object that can be accessed.
.. versionadded:: 1.4
Cross-Module Option Dependencies
--------------------------------
When using the global cfg.CONF object, it is quite common for a module
to require the existence of configuration options registered by other
modules.
For example, if module 'foo' registers the 'blaa' option and the module
'bar' uses the 'blaa' option then 'bar' might do::
import foo
print(CONF.blaa)
However, it's completely non-obvious why foo is being imported (is it
unused, can we remove the import) and where the 'blaa' option comes from.
The CONF.import_opt() method allows such a dependency to be explicitly
declared::
CONF.import_opt('blaa', 'foo')
print(CONF.blaa)
However, import_opt() has a weakness - if 'bar' imports 'foo' using the
import builtin and doesn't use import_opt() to import 'blaa', then 'blaa'
can still be used without problems. Similarly, where multiple options
are registered a module imported via importopt(), a lazy programmer can
get away with only declaring a dependency on a single option.
The ConfigFilter class provides a way to ensure that options are not
available unless they have been registered in the module or imported using
import_opt() for example with::
CONF = ConfigFilter(cfg.CONF)
CONF.import_opt('blaa', 'foo')
print(CONF.blaa)
no other options other than 'blaa' are available via CONF.
Private Configuration Options
-----------------------------
Libraries which register configuration options typically do not want
users of the library API to access those configuration options. If
API users do access private configuration options, those users will
be disrupted if and when a configuration option is renamed. In other
words, one does not typically wish for the name of the private config
options to be part of the public API.
The ConfigFilter class provides a way for a library to register
options such that they are not visible via the ConfigOpts instance
which the API user supplies to the library. For example::
from __future__ import print_function
from oslo_config.cfg import *
from oslo_config.cfgfilter import *
class Widget(object):
def __init__(self, conf):
self.conf = conf
self._private_conf = ConfigFilter(self.conf)
self._private_conf.register_opt(StrOpt('foo'))
@property
def foo(self):
return self._private_conf.foo
conf = ConfigOpts()
widget = Widget(conf)
print(widget.foo)
print(conf.foo) # raises NoSuchOptError
Limited Configuration Options
-----------------------------
It may be required that when passing a CONF object to other functions we want
to filter that the receiving code is only able to access a restricted subset
of the options that are available on the CONF object. This is essentially a
more general case of the Private Configuration Options and Cross-Module Options
whereby we expose an option that is already present on the underlying CONF
object without providing any means to load it if not present.
So given a CONF object with options defined::
CONF.register_opt(StrOpt('foo'))
CONF.register_opt(StrOpt('bar'))
we can expose options such that only those options are present::
restricted_conf = CfgFilter(CONF)
restricted_conf.expose_opt('foo')
print(restricted_conf.foo)
print(restricted_conf.bar) # raises NoSuchOptError
"""
import collections
import itertools
from oslo_config import cfg
class CliOptRegisteredError(cfg.Error):
"""Raised when registering cli opt not in original ConfigOpts.
.. versionadded:: 1.12
"""
def __str__(self):
ret = "Cannot register a cli option that was not present in the" \
" original ConfigOpts."
if self.msg:
ret += ": " + self.msg
return ret
class ConfigFilter(collections.Mapping):
"""A helper class which wraps a ConfigOpts object.
ConfigFilter enforces the explicit declaration of dependencies on external
options and allows private options which are not registered with the
wrapped Configopts object.
"""
def __init__(self, conf):
"""Construct a ConfigFilter object.
:param conf: a ConfigOpts object
"""
self._conf = conf
self._fconf = cfg.ConfigOpts()
self._sync()
self._imported_opts = set()
self._imported_groups = dict()
def _sync(self):
if self._fconf._namespace is not self._conf._namespace:
self._fconf.clear()
self._fconf._namespace = self._conf._namespace
self._fconf._args = self._conf._args
def __getattr__(self, name):
"""Look up an option value.
:param name: the opt name (or 'dest', more precisely)
:returns: the option value (after string subsititution) or a GroupAttr
:raises: NoSuchOptError,ConfigFileValueError,TemplateSubstitutionError
"""
if name in self._imported_groups:
return self._imported_groups[name]
elif name in self._imported_opts:
return getattr(self._conf, name)
else:
self._sync()
return getattr(self._fconf, name)
def __getitem__(self, key):
"""Look up an option value."""
return getattr(self, key)
def __contains__(self, key):
"""Return True if key is the name of a registered opt or group."""
return (key in self._fconf or
key in self._imported_opts or
key in self._imported_groups)
def __iter__(self):
"""Iterate over all registered opt and group names."""
return itertools.chain(self._fconf.keys(),
self._imported_opts,
self._imported_groups.keys())
def __len__(self):
"""Return the number of options and option groups."""
return (len(self._fconf) +
len(self._imported_opts) +
len(self._imported_groups))
@staticmethod
def _already_registered(conf, opt, group=None):
group_name = group.name if isinstance(group, cfg.OptGroup) else group
return ((group_name is None and
opt.dest in conf) or
(group_name is not None and
group_name in conf and
opt.dest in conf[group_name]))
def register_opt(self, opt, group=None):
"""Register an option schema.
:param opt: an instance of an Opt sub-class
:param group: an optional OptGroup object or group name
:return: False if the opt was already registered, True otherwise
:raises: DuplicateOptError
"""
if self._already_registered(self._conf, opt, group):
# Raises DuplicateError if there is another opt with the same name
ret = self._conf.register_opt(opt, group)
self._import_opt(opt.dest, group)
return ret
else:
return self._fconf.register_opt(opt, group)
def register_opts(self, opts, group=None):
"""Register multiple option schemas at once."""
for opt in opts:
self.register_opt(opt, group)
def register_cli_opt(self, opt, group=None):
"""Register a CLI option schema.
:param opt: an instance of an Opt sub-class
:param group: an optional OptGroup object or group name
:return: False if the opt was already register, True otherwise
:raises: DuplicateOptError, ArgsAlreadyParsedError
"""
if self._already_registered(self._conf, opt, group):
# Raises DuplicateError if there is another opt with the same name
ret = self._conf.register_opt(
opt, group, cli=True, clear_cache=False)
self._import_opt(opt.dest, group)
return ret
else:
raise CliOptRegisteredError(
"Opt '{0}' cannot be registered.".format(opt.dest))
def register_cli_opts(self, opts, group=None):
"""Register multiple CLI option schemas at once."""
for opt in opts:
self.register_cli_opt(opt, group)
def register_group(self, group):
"""Register an option group.
:param group: an OptGroup object
"""
self._fconf.register_group(group)
def import_opt(self, opt_name, module_str, group=None):
"""Import an option definition from a module.
:param name: the name/dest of the opt
:param module_str: the name of a module to import
:param group: an option OptGroup object or group name
:raises: NoSuchOptError, NoSuchGroupError
"""
self._conf.import_opt(opt_name, module_str, group)
self._import_opt(opt_name, group)
def import_group(self, group, module_str):
"""Import an option group from a module.
Note that this allows access to all options registered with
the group whether or not those options were registered by
the given module.
:param group: an option OptGroup object or group name
:param module_str: the name of a module to import
:raises: ImportError, NoSuchGroupError
"""
self._conf.import_group(group, module_str)
group = self._import_group(group)
group._all_opts = True
def _import_opt(self, opt_name, group):
if group is None:
self._imported_opts.add(opt_name)
return True
else:
group = self._import_group(group)
return group._import_opt(opt_name)
def _import_group(self, group_or_name):
if isinstance(group_or_name, cfg.OptGroup):
group_name = group_or_name.name
else:
group_name = group_or_name
if group_name in self._imported_groups:
return self._imported_groups[group_name]
else:
group = self.GroupAttr(self._conf, group_name)
self._imported_groups[group_name] = group
return group
def expose_opt(self, opt_name, group=None):
"""Expose an option from the underlying conf object.
This allows an object that has already been imported or used from the
base conf object to be seen from the filter object.
:param opt_name: the name/dest of the opt
:param group: an option OptGroup object or group name
"""
self._import_opt(opt_name, group)
def expose_group(self, group):
"""Expose all option from a group in the underlying conf object.
This allows an object that has already been imported or used from the
base conf object to be seen from the filter object.
:param group: an option OptGroup object or group name
"""
group = self._import_group(group)
group._all_opts = True
class GroupAttr(collections.Mapping):
"""Helper class to wrap a group object.
Represents the option values of a group as a mapping and attributes.
"""
def __init__(self, conf, group):
"""Construct a GroupAttr object.
:param conf: a ConfigOpts object
:param group: an OptGroup object
"""
self._conf = conf
self._group = group
self._imported_opts = set()
self._all_opts = False
def __getattr__(self, name):
"""Look up an option value."""
if not self._all_opts and name not in self._imported_opts:
raise cfg.NoSuchOptError(name)
return getattr(self._conf[self._group], name)
def __getitem__(self, key):
"""Look up an option value."""
return getattr(self, key)
def __contains__(self, key):
"""Return True if key is the name of a registered opt or group."""
return key in self._imported_opts
def __iter__(self):
"""Iterate over all registered opt and group names."""
for key in self._imported_opts:
yield key
def __len__(self):
"""Return the number of options and option groups."""
return len(self._imported_opts)
def _import_opt(self, opt_name):
self._imported_opts.add(opt_name)

View File

@ -1,206 +0,0 @@
#
# Copyright 2013 Mirantis, Inc.
# Copyright 2013 OpenStack Foundation
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import fixtures
import six
from oslo_config import cfg
class Config(fixtures.Fixture):
"""Allows overriding configuration settings for the test.
`conf` will be reset on cleanup.
"""
def __init__(self, conf=cfg.CONF):
self.conf = conf
def setUp(self):
super(Config, self).setUp()
# NOTE(morganfainberg): unregister must be added to cleanup before
# reset is because cleanup works in reverse order of registered items,
# and a reset must occur before unregistering options can occur.
self.addCleanup(self._reset_default_config_files)
self.addCleanup(self._unregister_config_opts)
self.addCleanup(self.conf.reset)
self._registered_config_opts = {}
# Grab an old copy of the default config files - if it exists - for
# subsequent cleanup.
if hasattr(self.conf, 'default_config_files'):
self._default_config_files = self.conf.default_config_files
else:
self._default_config_files = None
def config(self, **kw):
"""Override configuration values.
The keyword arguments are the names of configuration options to
override and their values.
If a `group` argument is supplied, the overrides are applied to
the specified configuration option group, otherwise the overrides
are applied to the ``default`` group.
If a `enforce_type` is supplied, will convert the override
value to the option's type before overriding.
"""
group = kw.pop('group', None)
enforce_type = kw.pop('enforce_type', False)
for k, v in six.iteritems(kw):
self.conf.set_override(k, v, group, enforce_type=enforce_type)
def _unregister_config_opts(self):
for group in self._registered_config_opts:
self.conf.unregister_opts(self._registered_config_opts[group],
group=group)
def _reset_default_config_files(self):
if not hasattr(self.conf, 'default_config_files'):
return
if self._default_config_files:
self.conf.default_config_files = self._default_config_files
else:
# Delete, because we could conceivably begin with the property
# being unset.
self.conf.default_config_files = None
def register_opt(self, opt, group=None):
"""Register a single option for the test run.
Options registered in this manner will automatically be unregistered
during cleanup.
If a `group` argument is supplied, it will register the new option
to that group, otherwise the option is registered to the ``default``
group.
"""
self.conf.register_opt(opt, group=group)
self._registered_config_opts.setdefault(group, set()).add(opt)
def register_opts(self, opts, group=None):
"""Register multiple options for the test run.
This works in the same manner as register_opt() but takes a list of
options as the first argument. All arguments will be registered to the
same group if the ``group`` argument is supplied, otherwise all options
will be registered to the ``default`` group.
"""
for opt in opts:
self.register_opt(opt, group=group)
def register_cli_opt(self, opt, group=None):
"""Register a single CLI option for the test run.
Options registered in this manner will automatically be unregistered
during cleanup.
If a `group` argument is supplied, it will register the new option
to that group, otherwise the option is registered to the ``default``
group.
CLI options must be registered before the command line and config files
are parsed. This is to ensure that all CLI options are shown in --help
and option validation works as expected.
"""
self.conf.register_cli_opt(opt, group=group)
self._registered_config_opts.setdefault(group, set()).add(opt)
def register_cli_opts(self, opts, group=None):
"""Register multiple CLI options for the test run.
This works in the same manner as register_opt() but takes a list of
options as the first argument. All arguments will be registered to the
same group if the ``group`` argument is supplied, otherwise all options
will be registered to the ``default`` group.
CLI options must be registered before the command line and config files
are parsed. This is to ensure that all CLI options are shown in --help
and option validation works as expected.
"""
for opt in opts:
self.register_cli_opt(opt, group=group)
def load_raw_values(self, group=None, **kwargs):
"""Load raw values into the configuration without registering them.
This method adds a series of parameters into the current config
instance, as if they had been loaded by a ConfigParser. This method
does not require that you register the configuration options first,
however the values loaded will not be accessible until you do.
"""
# Make sure the namespace exists for our tests.
if not self.conf._namespace:
self.conf.__call__(args=[])
# Default out the group name
group = 'DEFAULT' if not group else group
raw_config = dict()
raw_config[group] = dict()
for key, value in six.iteritems(kwargs):
# Parsed values are an array of raw strings.
raw_config[group][key] = [str(value)]
self.conf._namespace._add_parsed_config_file(raw_config, raw_config)
def set_config_files(self, config_files):
"""Specify a list of config files to read.
This method allows you to predefine the list of configuration files
that are loaded by oslo_config. It will ensure that your tests do not
attempt to autodetect, and accidentally pick up config files from
locally installed services.
"""
if not isinstance(config_files, list):
raise AttributeError("Please pass a list() to set_config_files()")
# Make sure the namespace exists for our tests.
if not self.conf._namespace:
self.conf.__call__(args=[])
self.conf.default_config_files = config_files
self.conf.reload_config_files()
def set_default(self, name, default, group=None):
"""Set a default value for an option.
This method is not necessarily meant to be invoked
directly. It is here to allow the set_defaults() functions in
various Oslo libraries to work with a Config fixture instead
of a ConfigOpts instance.
Use it like::
class MyTest(testtools.TestCase):
def setUp(self):
super(MyTest, self).setUp()
self.conf = self.useFixture(fixture.Config())
def test_something(self):
some_library.set_defaults(self.conf, name='value')
some_library.do_something_exciting()
"""
self.conf.set_default(name, default, group)
self.addCleanup(self.conf.clear_default, name, group)

View File

@ -1,483 +0,0 @@
# Copyright 2012 SINA Corporation
# Copyright 2014 Cisco Systems, Inc.
# All Rights Reserved.
# Copyright 2014 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Sample configuration generator
Tool for generating a sample configuration file. See
../doc/source/generator.rst for details.
.. versionadded:: 1.4
"""
import collections
import logging
import operator
import sys
import textwrap
import pkg_resources
import six
from oslo_config._i18n import _LW
from oslo_config import cfg
import stevedore.named # noqa
LOG = logging.getLogger(__name__)
_generator_opts = [
cfg.StrOpt('output-file',
help='Path of the file to write to. Defaults to stdout.'),
cfg.IntOpt('wrap-width',
default=70,
help='The maximum length of help lines.'),
cfg.MultiStrOpt('namespace',
required=True,
help='Option namespace under "oslo.config.opts" in which '
'to query for options.'),
cfg.BoolOpt('minimal', default=False,
help='Generate a minimal required configuration.'),
]
def register_cli_opts(conf):
"""Register the formatter's CLI options with a ConfigOpts instance.
Note, this must be done before the ConfigOpts instance is called to parse
the configuration.
:param conf: a ConfigOpts instance
:raises: DuplicateOptError, ArgsAlreadyParsedError
"""
conf.register_cli_opts(_generator_opts)
def _format_defaults(opt):
"Return a list of formatted default values."
if isinstance(opt, cfg.MultiStrOpt):
if opt.sample_default is not None:
defaults = opt.sample_default
elif not opt.default:
defaults = ['']
else:
defaults = opt.default
else:
if opt.sample_default is not None:
default_str = str(opt.sample_default)
elif opt.default is None:
default_str = '<None>'
elif (isinstance(opt, (cfg.StrOpt, cfg.IPOpt,
cfg.HostnameOpt, cfg.URIOpt))):
default_str = opt.default
elif isinstance(opt, cfg.BoolOpt):
default_str = str(opt.default).lower()
elif isinstance(opt, (cfg.IntOpt, cfg.FloatOpt,
cfg.PortOpt)):
default_str = str(opt.default)
elif isinstance(opt, (cfg.ListOpt, cfg._ConfigFileOpt,
cfg._ConfigDirOpt)):
default_str = ','.join(opt.default)
elif isinstance(opt, cfg.DictOpt):
sorted_items = sorted(opt.default.items(),
key=operator.itemgetter(0))
default_str = ','.join(['%s:%s' % i for i in sorted_items])
else:
LOG.warning(_LW('Unknown option type: %s'), repr(opt))
default_str = str(opt.default)
defaults = [default_str]
results = []
for default_str in defaults:
if default_str.strip() != default_str:
default_str = '"%s"' % default_str
results.append(default_str)
return results
_TYPE_NAMES = {
str: 'string value',
int: 'integer value',
float: 'floating point value',
}
def _format_type_name(opt_type):
"""Format the type name to use in describing an option"""
try:
return opt_type.type_name
except AttributeError: # nosec
pass
try:
return _TYPE_NAMES[opt_type]
except KeyError: # nosec
pass
return 'unknown value'
class _OptFormatter(object):
"""Format configuration option descriptions to a file."""
def __init__(self, output_file=None, wrap_width=70):
"""Construct an OptFormatter object.
:param output_file: a writeable file object
:param wrap_width: The maximum length of help lines, 0 to not wrap
"""
self.output_file = output_file or sys.stdout
self.wrap_width = wrap_width
def _format_help(self, help_text):
"""Format the help for a group or option to the output file.
:param help_text: The text of the help string
"""
if self.wrap_width is not None and self.wrap_width > 0:
wrapped = ""
for line in help_text.splitlines():
text = "\n".join(textwrap.wrap(line, self.wrap_width,
initial_indent='# ',
subsequent_indent='# ',
break_long_words=False,
replace_whitespace=False))
wrapped += "#" if text == "" else text
wrapped += "\n"
lines = [wrapped]
else:
lines = ['# ' + help_text + '\n']
return lines
def _get_choice_text(self, choice):
if choice is None:
return '<None>'
elif choice == '':
return "''"
return six.text_type(choice)
def format_group(self, group_or_groupname):
"""Format the description of a group header to the output file
:param group_or_groupname: a cfg.OptGroup instance or a name of group
"""
if isinstance(group_or_groupname, cfg.OptGroup):
group = group_or_groupname
lines = ['[%s]\n' % group.name]
if group.help:
lines += self._format_help(group.help)
else:
groupname = group_or_groupname
lines = ['[%s]\n' % groupname]
self.writelines(lines)
def format(self, opt, group_name, minimal=False):
"""Format a description of an option to the output file.
:param opt: a cfg.Opt instance
"""
if not opt.help:
LOG.warning(_LW('"%s" is missing a help string'), opt.dest)
opt_type = _format_type_name(opt.type)
opt_prefix = ''
if (opt.deprecated_for_removal and
not opt.help.startswith('DEPRECATED')):
opt_prefix = 'DEPRECATED: '
if opt.help:
help_text = u'%s%s (%s)' % (opt_prefix,
opt.help,
opt_type)
else:
help_text = u'(%s)' % opt_type
lines = self._format_help(help_text)
if getattr(opt.type, 'min', None) is not None:
lines.append('# Minimum value: %d\n' % opt.type.min)
if getattr(opt.type, 'max', None) is not None:
lines.append('# Maximum value: %d\n' % opt.type.max)
if getattr(opt.type, 'choices', None):
choices_text = ', '.join([self._get_choice_text(choice)
for choice in opt.type.choices])
lines.append('# Allowed values: %s\n' % choices_text)
try:
if opt.mutable:
lines.append(
'# Note: This option can be changed without restarting.\n'
)
except AttributeError as err:
# NOTE(dhellmann): keystoneauth defines its own Opt class,
# and neutron (at least) returns instances of those
# classes instead of oslo_config Opt instances. The new
# mutable attribute is the first property where the API
# isn't supported in the external class, so we can use
# this failure to emit a warning. See
# https://bugs.launchpad.net/keystoneauth/+bug/1548433 for
# more details.
import warnings
if not isinstance(opt, cfg.Opt):
warnings.warn(
'Incompatible option class for %s (%r): %s' %
(opt.dest, opt.__class__, err),
)
else:
warnings.warn('Failed to fully format sample for %s: %s' %
(opt.dest, err))
for d in opt.deprecated_opts:
lines.append('# Deprecated group/name - [%s]/%s\n' %
(d.group or group_name, d.name or opt.dest))
if opt.deprecated_for_removal:
if opt.deprecated_since:
lines.append(
'# This option is deprecated for removal since %s.\n' % (
opt.deprecated_since))
else:
lines.append(
'# This option is deprecated for removal.\n')
lines.append(
'# Its value may be silently ignored in the future.\n')
if opt.deprecated_reason:
lines.extend(
self._format_help('Reason: ' + opt.deprecated_reason))
if opt.advanced:
lines.append(
'# Advanced Option: intended for advanced users and not used\n'
'# by the majority of users, and might have a significant\n'
'# effect on stability and/or performance.\n'
)
if hasattr(opt.type, 'format_defaults'):
defaults = opt.type.format_defaults(opt.default,
opt.sample_default)
else:
LOG.debug(
"The type for option %(name)s which is %(type)s is not a "
"subclass of types.ConfigType and doesn't provide a "
"'format_defaults' method. A default formatter is not "
"available so the best-effort formatter will be used.",
{'type': opt.type, 'name': opt.name})
defaults = _format_defaults(opt)
for default_str in defaults:
if default_str:
default_str = ' ' + default_str
if minimal:
lines.append('%s =%s\n' % (opt.dest, default_str))
else:
lines.append('#%s =%s\n' % (opt.dest, default_str))
self.writelines(lines)
def write(self, s):
"""Write an arbitrary string to the output file.
:param s: an arbitrary string
"""
self.output_file.write(s)
def writelines(self, l):
"""Write an arbitrary sequence of strings to the output file.
:param l: a list of arbitrary strings
"""
self.output_file.writelines(l)
def _cleanup_opts(read_opts):
"""Cleanup duplicate options in namespace groups
Return a structure which removes duplicate options from a namespace group.
NOTE:(rbradfor) This does not remove duplicated options from repeating
groups in different namespaces:
:param read_opts: a list (namespace, [(group, [opt_1, opt_2])]) tuples
:returns: a list of (namespace, [(group, [opt_1, opt_2])]) tuples
"""
# OrderedDict is used specifically in the three levels to maintain the
# source order of namespace/group/opt values
clean = collections.OrderedDict()
for namespace, listing in read_opts:
if namespace not in clean:
clean[namespace] = collections.OrderedDict()
for group, opts in listing:
if group not in clean[namespace]:
clean[namespace][group] = collections.OrderedDict()
for opt in opts:
clean[namespace][group][opt.dest] = opt
# recreate the list of (namespace, [(group, [opt_1, opt_2])]) tuples
# from the cleaned structure.
cleaned_opts = [
(namespace, [(group, list(clean[namespace][group].values()))
for group in clean[namespace]])
for namespace in clean
]
return cleaned_opts
def _get_raw_opts_loaders(namespaces):
"""List the options available via the given namespaces.
:param namespaces: a list of namespaces registered under 'oslo.config.opts'
:returns: a list of (namespace, [(group, [opt_1, opt_2])]) tuples
"""
mgr = stevedore.named.NamedExtensionManager(
'oslo.config.opts',
names=namespaces,
on_load_failure_callback=on_load_failure_callback,
invoke_on_load=False)
return [(e.name, e.plugin) for e in mgr]
def _get_opt_default_updaters(namespaces):
mgr = stevedore.named.NamedExtensionManager(
'oslo.config.opts.defaults',
names=namespaces,
on_load_failure_callback=on_load_failure_callback,
invoke_on_load=False)
return [ep.plugin for ep in mgr]
def _update_defaults(namespaces):
"Let application hooks update defaults inside libraries."
for update in _get_opt_default_updaters(namespaces):
update()
def _list_opts(namespaces):
"""List the options available via the given namespaces.
Duplicate options from a namespace are removed.
:param namespaces: a list of namespaces registered under 'oslo.config.opts'
:returns: a list of (namespace, [(group, [opt_1, opt_2])]) tuples
"""
# Load the functions to get the options.
loaders = _get_raw_opts_loaders(namespaces)
# Update defaults, which might change global settings in library
# modules.
_update_defaults(namespaces)
# Ask for the option definitions. At this point any global default
# changes made by the updaters should be in effect.
opts = [
(namespace, loader())
for namespace, loader in loaders
]
return _cleanup_opts(opts)
def on_load_failure_callback(*args, **kwargs):
raise
def _output_opts(f, group, group_data, minimal=False):
f.format_group(group_data['object'] or group)
for (namespace, opts) in sorted(group_data['namespaces'],
key=operator.itemgetter(0)):
f.write('\n#\n# From %s\n#\n' % namespace)
for opt in sorted(opts, key=operator.attrgetter('advanced')):
try:
if minimal and not opt.required:
pass
else:
f.write('\n')
f.format(opt, group, minimal)
except Exception as err:
f.write('# Warning: Failed to format sample for %s\n' %
(opt.dest,))
f.write('# %s\n' % (err,))
def _get_groups(conf_ns):
"""Invert a list of groups by namespace into a dict by group name.
:param conf_ns: a list of (namespace, [(<group>, [opt_1, opt_2])]) tuples,
such as returned by _list_opts.
:returns: {<group_name>, {'object': <group_object>,
'namespaces': [(<namespace>, <opts>)]}}
<group> may be a string or a group object.
<group_name> is always a string.
<group_object> will only be set if <group> was a group object in at least
one namespace.
Keying by group_name avoids adding duplicate group names in case a group is
added as both an OptGroup and as a str, but still makes the additional
OptGroup data available to the output code when possible.
"""
groups = {'DEFAULT': {'object': None, 'namespaces': []}}
for namespace, listing in conf_ns:
for group, opts in listing:
if not opts:
continue
group = group if group else 'DEFAULT'
is_optgroup = hasattr(group, 'name')
group_name = group.name if is_optgroup else group
if group_name not in groups:
groups[group_name] = {'object': None, 'namespaces': []}
if is_optgroup:
groups[group_name]['object'] = group
groups[group_name]['namespaces'].append((namespace, opts))
return groups
def generate(conf):
"""Generate a sample config file.
List all of the options available via the namespaces specified in the given
configuration and write a description of them to the specified output file.
:param conf: a ConfigOpts instance containing the generator's configuration
"""
conf.register_opts(_generator_opts)
output_file = (open(conf.output_file, 'w')
if conf.output_file else sys.stdout)
formatter = _OptFormatter(output_file=output_file,
wrap_width=conf.wrap_width)
groups = _get_groups(_list_opts(conf.namespace))
# Output the "DEFAULT" section as the very first section
_output_opts(formatter, 'DEFAULT', groups.pop('DEFAULT'), conf.minimal)
# output all other config sections with groups in alphabetical order
for group, group_data in sorted(groups.items()):
formatter.write('\n\n')
_output_opts(formatter, group, group_data, conf.minimal)
def main(args=None):
"""The main function of oslo-config-generator."""
version = pkg_resources.get_distribution('oslo.config').version
logging.basicConfig(level=logging.WARN)
conf = cfg.ConfigOpts()
register_cli_opts(conf)
conf(args, version=version)
generate(conf)
if __name__ == '__main__':
main()

View File

@ -1,127 +0,0 @@
# Copyright 2012 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
class ParseError(Exception):
def __init__(self, message, lineno, line):
self.msg = message
self.line = line
self.lineno = lineno
def __str__(self):
return 'at line %d, %s: %r' % (self.lineno, self.msg, self.line)
class BaseParser(object):
lineno = 0
parse_exc = ParseError
def _assignment(self, key, value):
self.assignment(key, value)
return None, []
def _get_section(self, line):
if not line.endswith(']'):
return self.error_no_section_end_bracket(line)
if len(line) <= 2:
return self.error_no_section_name(line)
return line[1:-1]
def _split_key_value(self, line):
colon = line.find(':')
equal = line.find('=')
if colon < 0 and equal < 0:
return self.error_invalid_assignment(line)
if colon < 0 or (equal >= 0 and equal < colon):
key, value = line[:equal], line[equal + 1:]
else:
key, value = line[:colon], line[colon + 1:]
value = value.strip()
if value and value[0] == value[-1] and value.startswith(("\"", "'")):
value = value[1:-1]
return key.strip(), [value]
def parse(self, lineiter):
key = None
value = []
for line in lineiter:
self.lineno += 1
line = line.rstrip()
if not line:
# Blank line, ends multi-line values
if key:
key, value = self._assignment(key, value)
continue
elif line.startswith((' ', '\t')):
# Continuation of previous assignment
if key is None:
self.error_unexpected_continuation(line)
else:
value.append(line.lstrip())
continue
if key:
# Flush previous assignment, if any
key, value = self._assignment(key, value)
if line.startswith('['):
# Section start
section = self._get_section(line)
if section:
self.new_section(section)
elif line.startswith(('#', ';')):
self.comment(line[1:].lstrip())
else:
key, value = self._split_key_value(line)
if not key:
return self.error_empty_key(line)
if key:
# Flush previous assignment, if any
self._assignment(key, value)
def assignment(self, key, value):
"""Called when a full assignment is parsed."""
raise NotImplementedError()
def new_section(self, section):
"""Called when a new section is started."""
raise NotImplementedError()
def comment(self, comment):
"""Called when a comment is parsed."""
pass
def error_invalid_assignment(self, line):
raise self.parse_exc("No ':' or '=' found in assignment",
self.lineno, line)
def error_empty_key(self, line):
raise self.parse_exc('Key cannot be empty', self.lineno, line)
def error_unexpected_continuation(self, line):
raise self.parse_exc('Unexpected continuation line',
self.lineno, line)
def error_no_section_end_bracket(self, line):
raise self.parse_exc('Invalid section (must end with ])',
self.lineno, line)
def error_no_section_name(self, line):
raise self.parse_exc('Empty section name', self.lineno, line)

View File

@ -1,89 +0,0 @@
# Copyright 2015 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
from oslo_config import generator
def generate_sample(app):
if not app.config.config_generator_config_file:
app.warn("No config_generator_config_file is specified, "
"skipping sample config generation")
return
# Decided to update the existing config option
# config_generator_config_file to support a value that is a list of
# tuples, containing the file names as (input, output).
# We need to retain support for the option referring to a single string,
# and using the sample_config_basename for the output file in that case.
# After we release support for both forms of the option, we can update
# projects to always use the list of tuples, then remove
# sample_config_basename and the support for config_generator_config_file
# being a single string.
if isinstance(app.config.config_generator_config_file, list):
for config_file, base_name in app.config.config_generator_config_file:
if base_name is None:
base_name = _get_default_basename(config_file)
_generate_sample(app, config_file, base_name)
else:
_generate_sample(app,
app.config.config_generator_config_file,
app.config.sample_config_basename)
def _get_default_basename(config_file):
return os.path.splitext(os.path.basename(config_file))[0]
def _generate_sample(app, config_file, base_name):
def info(msg):
app.info('[%s] %s' % (__name__, msg))
# If we are given a file that isn't an absolute path, look for it
# in the source directory if it doesn't exist.
candidates = [
config_file,
os.path.join(app.srcdir, config_file,),
]
for c in candidates:
if os.path.isfile(c):
info('reading config generator instructions from %s' % c)
config_path = c
break
else:
raise ValueError(
"Could not find config_generator_config_file %r" %
app.config.config_generator_config_file)
if base_name:
out_file = os.path.join(app.srcdir, base_name) + '.conf.sample'
if not os.path.isdir(os.path.dirname(os.path.abspath(out_file))):
os.mkdir(os.path.dirname(os.path.abspath(out_file)))
else:
file_name = 'sample.config'
out_file = os.path.join(app.srcdir, file_name)
info('writing sample configuration to %s' % out_file)
generator.main(args=['--config-file', config_path,
'--output-file', out_file])
def setup(app):
app.add_config_value('config_generator_config_file', None, 'env')
app.add_config_value('sample_config_basename', None, 'env')
app.connect('builder-inited', generate_sample)

View File

@ -1,453 +0,0 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from docutils import nodes
from docutils.parsers import rst
from docutils.parsers.rst import directives
from docutils.statemachine import ViewList
from sphinx import addnodes
from sphinx.directives import ObjectDescription
from sphinx.domains import Domain
from sphinx.domains import ObjType
from sphinx.roles import XRefRole
from sphinx.util.nodes import make_refnode
from sphinx.util.nodes import nested_parse_with_titles
from oslo_config import cfg
from oslo_config import generator
import oslo_i18n
import six
def _list_table(headers, data, title='', columns=None):
"""Build a list-table directive.
:param add: Function to add one row to output.
:param headers: List of header values.
:param data: Iterable of row data, yielding lists or tuples with rows.
"""
yield '.. list-table:: %s' % title
yield ' :header-rows: 1'
if columns:
yield ' :widths: %s' % (','.join(str(c) for c in columns))
yield ''
yield ' - * %s' % headers[0]
for h in headers[1:]:
yield ' * %s' % h
for row in data:
yield ' - * %s' % row[0]
for r in row[1:]:
yield ' * %s' % r
yield ''
def _indent(text, n=2):
padding = ' ' * n
return '\n'.join(padding + l for l in text.splitlines())
def _make_anchor_target(group_name, option_name):
# We need to ensure this is unique across entire documentation
# http://www.sphinx-doc.org/en/stable/markup/inline.html#ref-role
target = '%s.%s' % (cfg._normalize_group_name(group_name),
option_name.lower())
return target
_TYPE_DESCRIPTIONS = {
cfg.StrOpt: 'string',
cfg.BoolOpt: 'boolean',
cfg.IntOpt: 'integer',
cfg.FloatOpt: 'floating point',
cfg.ListOpt: 'list',
cfg.DictOpt: 'dict',
cfg.MultiStrOpt: 'multi-valued',
cfg._ConfigFileOpt: 'list of filenames',
cfg._ConfigDirOpt: 'list of directory names',
}
def _get_choice_text(choice):
if choice is None:
return '<None>'
elif choice == '':
return "''"
return six.text_type(choice)
def _format_group(app, namespace, group_name, group_obj, opt_list):
group_name = group_name or 'DEFAULT'
app.info('[oslo.config] %s %s' % (namespace, group_name))
yield '.. oslo.config:group:: %s' % group_name
if namespace:
yield ' :namespace: %s' % namespace
yield ''
if group_obj and group_obj.help:
yield _indent(group_obj.help.rstrip())
yield ''
for opt in opt_list:
opt_type = _TYPE_DESCRIPTIONS.get(type(opt),
'unknown type')
yield '.. oslo.config:option:: %s' % opt.dest
yield ''
yield _indent(':Type: %s' % opt_type)
for default in generator._format_defaults(opt):
if default:
default = '``' + default + '``'
yield _indent(':Default: %s' % default)
if getattr(opt.type, 'min', None) is not None:
yield _indent(':Minimum Value: %s' % opt.type.min)
if getattr(opt.type, 'max', None) is not None:
yield _indent(':Maximum Value: %s' % opt.type.max)
if getattr(opt.type, 'choices', None):
choices_text = ', '.join([_get_choice_text(choice)
for choice in opt.type.choices])
yield _indent(':Valid Values: %s' % choices_text)
try:
if opt.mutable:
yield _indent(
':Mutable: This option can be changed without restarting.',
)
except AttributeError as err:
# NOTE(dhellmann): keystoneauth defines its own Opt class,
# and neutron (at least) returns instances of those
# classes instead of oslo_config Opt instances. The new
# mutable attribute is the first property where the API
# isn't supported in the external class, so we can use
# this failure to emit a warning. See
# https://bugs.launchpad.net/keystoneauth/+bug/1548433 for
# more details.
import warnings
if not isinstance(cfg.Opt, opt):
warnings.warn(
'Incompatible option class for %s (%r): %s' %
(opt.dest, opt.__class__, err),
)
else:
warnings.warn('Failed to fully format sample for %s: %s' %
(opt.dest, err))
if opt.advanced:
yield _indent(
':Advanced Option: intended for advanced users and not used',)
yield _indent(
':by the majority of users, and might have a significant',)
yield _indent(
':effect on stability and/or performance.',)
yield ''
try:
help_text = opt.help % {'default': 'the value above'}
except (TypeError, KeyError):
# There is no mention of the default in the help string,
# or the string had some unknown key
help_text = opt.help
if help_text:
yield _indent(help_text)
yield ''
if opt.deprecated_opts:
for line in _list_table(
['Group', 'Name'],
((d.group or group_name,
d.name or opt.dest or 'UNSET')
for d in opt.deprecated_opts),
title='Deprecated Variations'):
yield _indent(line)
if opt.deprecated_for_removal:
yield _indent('.. warning::')
if opt.deprecated_since:
yield _indent(' This option is deprecated for removal '
'since %s.' % opt.deprecated_since)
else:
yield _indent(' This option is deprecated for removal.')
yield _indent(' Its value may be silently ignored ')
yield _indent(' in the future.')
yield ''
if opt.deprecated_reason:
yield _indent(' :Reason: ' + opt.deprecated_reason)
yield ''
yield ''
def _format_option_help(app, namespaces, split_namespaces):
"""Generate a series of lines of restructuredtext.
Format the option help as restructuredtext and return it as a list
of lines.
"""
opts = generator._list_opts(namespaces)
if split_namespaces:
for namespace, opt_list in opts:
for group, opts in opt_list:
if isinstance(group, cfg.OptGroup):
group_name = group.name
else:
group_name = group
group = None
lines = _format_group(
app=app,
namespace=namespace,
group_name=group_name,
group_obj=group,
opt_list=opts,
)
for line in lines:
yield line
else:
# Merge the options from different namespaces that belong to
# the same group together and format them without the
# namespace.
by_section = {}
group_objs = {}
for ignore, opt_list in opts:
for group, group_opts in opt_list:
if isinstance(group, cfg.OptGroup):
group_name = group.name
else:
group_name = group
group = None
group_objs.setdefault(group_name, group)
by_section.setdefault(group_name, []).extend(group_opts)
for group_name, group_opts in sorted(by_section.items()):
lines = _format_group(
app=app,
namespace=None,
group_name=group_name,
group_obj=group_objs.get(group_name),
opt_list=group_opts,
)
for line in lines:
yield line
class ShowOptionsDirective(rst.Directive):
option_spec = {
'split-namespaces': directives.flag,
'config-file': directives.unchanged,
}
has_content = True
def run(self):
env = self.state.document.settings.env
app = env.app
split_namespaces = 'split-namespaces' in self.options
config_file = self.options.get('config-file')
if config_file:
app.info('loading config file %s' % config_file)
conf = cfg.ConfigOpts()
conf.register_opts(generator._generator_opts)
conf(
args=['--config-file', config_file],
project='oslo.config.sphinxext',
)
namespaces = conf.namespace[:]
else:
namespaces = [
c.strip()
for c in self.content
if c.strip()
]
result = ViewList()
source_name = '<' + __name__ + '>'
for line in _format_option_help(app, namespaces, split_namespaces):
result.append(line, source_name)
node = nodes.section()
node.document = self.state.document
nested_parse_with_titles(self.state, result, node)
return node.children
class ConfigGroupXRefRole(XRefRole):
"Handles :oslo.config:group: roles pointing to configuration groups."
def __init__(self):
super(ConfigGroupXRefRole, self).__init__(
warn_dangling=True,
)
def process_link(self, env, refnode, has_explicit_title, title, target):
# The anchor for the group link is the group name.
return target, target
class ConfigOptXRefRole(XRefRole):
"Handles :oslo.config:option: roles pointing to configuration options."
def __init__(self):
super(ConfigOptXRefRole, self).__init__(
warn_dangling=True,
)
def process_link(self, env, refnode, has_explicit_title, title, target):
if not has_explicit_title:
title = target
if '.' in target:
group, opt_name = target.split('.')
else:
group = 'DEFAULT'
opt_name = target
anchor = _make_anchor_target(group, opt_name)
return title, anchor
class ConfigGroup(rst.Directive):
has_content = True
option_spec = {
'namespace': directives.unchanged,
}
def run(self):
env = self.state.document.settings.env
app = env.app
group_name = ' '.join(self.content)
namespace = self.options.get('namespace')
cached_groups = env.domaindata['oslo.config']['groups']
# Store the current group for use later in option directives
env.temp_data['oslo.config:group'] = group_name
app.info('oslo.config group %r' % group_name)
# Store the location where this group is being defined
# for use when resolving cross-references later.
# FIXME: This should take the source namespace into account, too
cached_groups[group_name] = env.docname
result = ViewList()
source_name = '<' + __name__ + '>'
def _add(text):
"Append some text to the output result view to be parsed."
result.append(text, source_name)
if namespace:
title = '%s: %s' % (namespace, group_name)
else:
title = group_name
_add(title)
_add('-' * len(title))
node = nodes.section()
node.document = self.state.document
nested_parse_with_titles(self.state, result, node)
first_child = node.children[0]
# Compute the normalized target and set the node to have that
# as an id
target_name = cfg._normalize_group_name(group_name)
first_child['ids'].append(target_name)
indexnode = addnodes.index(entries=[])
return [indexnode] + node.children
class ConfigOption(ObjectDescription):
"Description of a configuration option (.. option)."
def handle_signature(self, sig, signode):
"""Transform an option description into RST nodes."""
optname = sig
self.env.app.info('oslo.config option %s' % optname)
# Insert a node into the output showing the option name
signode += addnodes.desc_name(optname, optname)
signode['allnames'] = [optname]
return optname
def add_target_and_index(self, firstname, sig, signode):
cached_options = self.env.domaindata['oslo.config']['options']
# Look up the current group name from the processing context
currgroup = self.env.temp_data.get('oslo.config:group')
# Compute the normalized target name for the option and give
# that to the node as an id
target_name = _make_anchor_target(currgroup, sig)
signode['ids'].append(target_name)
self.state.document.note_explicit_target(signode)
# Store the location of the option definition for later use in
# resolving cross-references
# FIXME: This should take the source namespace into account, too
cached_options[target_name] = self.env.docname
class ConfigDomain(Domain):
"""oslo.config domain."""
name = 'oslo.config'
label = 'oslo.config'
object_types = {
'configoption': ObjType('configuration option', 'option'),
}
directives = {
'group': ConfigGroup,
'option': ConfigOption,
}
roles = {
'option': ConfigOptXRefRole(),
'group': ConfigGroupXRefRole(),
}
initial_data = {
'options': {},
'groups': {},
}
def resolve_xref(self, env, fromdocname, builder,
typ, target, node, contnode):
if typ == 'option':
group_name, option_name = target.split('.', 1)
return make_refnode(
builder,
fromdocname,
env.domaindata['oslo.config']['options'][target],
target,
contnode,
option_name,
)
if typ == 'group':
return make_refnode(
builder,
fromdocname,
env.domaindata['oslo.config']['groups'][target],
target,
contnode,
target,
)
return None
def setup(app):
# NOTE(dhellmann): Try to turn off lazy translation from oslo_i18n
# so any translated help text or deprecation messages associated
# with configuration options are treated as regular strings
# instead of Message objects. Unfortunately this is a bit
# order-dependent, and so it's still possible that importing code
# from another module such as through the autodoc features, or
# even through the plugin scanner, will turn lazy evaluation back
# on.
oslo_i18n.enable_lazy(False)
app.add_directive('show-options', ShowOptionsDirective)
app.add_domain(ConfigDomain)

File diff suppressed because it is too large Load Diff

View File

@ -1,362 +0,0 @@
# Copyright 2014 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslotest import base as test_base
import testtools
from oslo_config import cfg
from oslo_config import cfgfilter
class BaseTestCase(test_base.BaseTestCase):
def setUp(self, conf=None):
super(BaseTestCase, self).setUp()
if conf is None:
self.conf = cfg.ConfigOpts()
else:
self.conf = conf
self.fconf = cfgfilter.ConfigFilter(self.conf)
class RegisterTestCase(BaseTestCase):
def test_register_opt_default(self):
self.fconf.register_opt(cfg.StrOpt('foo', default='bar'))
self.assertEqual('bar', self.fconf.foo)
self.assertEqual('bar', self.fconf['foo'])
self.assertIn('foo', self.fconf)
self.assertEqual(['foo'], list(self.fconf))
self.assertEqual(1, len(self.fconf))
self.assertNotIn('foo', self.conf)
self.assertEqual(0, len(self.conf))
self.assertRaises(cfg.NoSuchOptError, getattr, self.conf, 'foo')
def test_register_opt_none_default(self):
self.fconf.register_opt(cfg.StrOpt('foo'))
self.assertIsNone(self.fconf.foo)
self.assertIsNone(self.fconf['foo'])
self.assertIn('foo', self.fconf)
self.assertEqual(['foo'], list(self.fconf))
self.assertEqual(1, len(self.fconf))
self.assertNotIn('foo', self.conf)
self.assertEqual(0, len(self.conf))
self.assertRaises(cfg.NoSuchOptError, getattr, self.conf, 'foo')
def test_register_grouped_opt_default(self):
self.fconf.register_opt(cfg.StrOpt('foo', default='bar'),
group='blaa')
self.assertEqual('bar', self.fconf.blaa.foo)
self.assertEqual('bar', self.fconf['blaa']['foo'])
self.assertIn('blaa', self.fconf)
self.assertIn('foo', self.fconf.blaa)
self.assertEqual(['blaa'], list(self.fconf))
self.assertEqual(['foo'], list(self.fconf.blaa))
self.assertEqual(1, len(self.fconf))
self.assertEqual(1, len(self.fconf.blaa))
self.assertNotIn('blaa', self.conf)
self.assertEqual(0, len(self.conf))
self.assertRaises(cfg.NoSuchOptError, getattr, self.conf, 'blaa')
def test_register_grouped_opt_none_default(self):
self.fconf.register_opt(cfg.StrOpt('foo'), group='blaa')
self.assertIsNone(self.fconf.blaa.foo)
self.assertIsNone(self.fconf['blaa']['foo'])
self.assertIn('blaa', self.fconf)
self.assertIn('foo', self.fconf.blaa)
self.assertEqual(['blaa'], list(self.fconf))
self.assertEqual(['foo'], list(self.fconf.blaa))
self.assertEqual(1, len(self.fconf))
self.assertEqual(1, len(self.fconf.blaa))
self.assertNotIn('blaa', self.conf)
self.assertEqual(0, len(self.conf))
self.assertRaises(cfg.NoSuchOptError, getattr, self.conf, 'blaa')
def test_register_group(self):
group = cfg.OptGroup('blaa')
self.fconf.register_group(group)
self.fconf.register_opt(cfg.StrOpt('foo'), group=group)
self.assertIsNone(self.fconf.blaa.foo)
self.assertIsNone(self.fconf['blaa']['foo'])
self.assertIn('blaa', self.fconf)
self.assertIn('foo', self.fconf.blaa)
self.assertEqual(['blaa'], list(self.fconf))
self.assertEqual(['foo'], list(self.fconf.blaa))
self.assertEqual(1, len(self.fconf))
self.assertEqual(1, len(self.fconf.blaa))
self.assertNotIn('blaa', self.conf)
self.assertEqual(0, len(self.conf))
self.assertRaises(cfg.NoSuchOptError, getattr, self.conf, 'blaa')
def test_register_opts(self):
self.fconf.register_opts([cfg.StrOpt('foo'),
cfg.StrOpt('bar')])
self.assertIn('foo', self.fconf)
self.assertIn('bar', self.fconf)
self.assertNotIn('foo', self.conf)
self.assertNotIn('bar', self.conf)
def test_register_known_cli_opt(self):
self.conf.register_opt(cfg.StrOpt('foo'))
self.fconf.register_cli_opt(cfg.StrOpt('foo'))
self.assertIn('foo', self.fconf)
self.assertIn('foo', self.conf)
def test_register_unknown_cli_opt(self):
with testtools.ExpectedException(cfgfilter.CliOptRegisteredError):
self.fconf.register_cli_opt(cfg.StrOpt('foo'))
def test_register_known_cli_opts(self):
self.conf.register_cli_opts([cfg.StrOpt('foo'), cfg.StrOpt('bar')])
self.fconf.register_cli_opts([cfg.StrOpt('foo'), cfg.StrOpt('bar')])
self.assertIn('foo', self.fconf)
self.assertIn('bar', self.fconf)
self.assertIn('foo', self.conf)
self.assertIn('bar', self.conf)
def test_register_unknown_cli_opts(self):
self.conf.register_cli_opt(cfg.StrOpt('foo'))
with testtools.ExpectedException(cfgfilter.CliOptRegisteredError):
self.fconf.register_cli_opts([
cfg.StrOpt('foo'),
cfg.StrOpt('bar')
])
def test_register_opts_grouped(self):
self.fconf.register_opts([cfg.StrOpt('foo'), cfg.StrOpt('bar')],
group='blaa')
self.assertIn('foo', self.fconf.blaa)
self.assertIn('bar', self.fconf.blaa)
self.assertNotIn('blaa', self.conf)
def test_register_known_cli_opt_grouped(self):
self.conf.register_cli_opt(cfg.StrOpt('foo'), group='blaa')
self.fconf.register_cli_opt(cfg.StrOpt('foo'), group='blaa')
self.assertIn('foo', self.fconf.blaa)
self.assertIn('blaa', self.fconf)
self.assertIn('blaa', self.conf)
def test_register_unknown_cli_opt_grouped(self):
with testtools.ExpectedException(cfgfilter.CliOptRegisteredError):
self.fconf.register_cli_opt(cfg.StrOpt('foo'), group='blaa')
def test_register_known_cli_opts_grouped(self):
self.conf.register_cli_opts([cfg.StrOpt('foo'), cfg.StrOpt('bar')],
group='blaa')
self.fconf.register_cli_opts([cfg.StrOpt('foo'), cfg.StrOpt('bar')],
group='blaa')
self.assertIn('foo', self.fconf.blaa)
self.assertIn('bar', self.fconf.blaa)
self.assertIn('blaa', self.fconf)
self.assertIn('blaa', self.conf)
def test_register_unknown_opts_grouped(self):
self.conf.register_cli_opts([cfg.StrOpt('bar')], group='blaa')
with testtools.ExpectedException(cfgfilter.CliOptRegisteredError):
self.fconf.register_cli_opts([
cfg.StrOpt('foo'),
cfg.StrOpt('bar')
], group='blaa')
def test_unknown_opt(self):
self.assertNotIn('foo', self.fconf)
self.assertEqual(0, len(self.fconf))
self.assertRaises(cfg.NoSuchOptError, getattr, self.fconf, 'foo')
self.assertNotIn('blaa', self.conf)
def test_blocked_opt(self):
self.conf.register_opt(cfg.StrOpt('foo'))
self.assertIn('foo', self.conf)
self.assertEqual(1, len(self.conf))
self.assertIsNone(self.conf.foo)
self.assertNotIn('foo', self.fconf)
self.assertEqual(0, len(self.fconf))
self.assertRaises(cfg.NoSuchOptError, getattr, self.fconf, 'foo')
def test_already_registered_opt(self):
self.conf.register_opt(cfg.StrOpt('foo'))
self.fconf.register_opt(cfg.StrOpt('foo'))
self.assertIn('foo', self.conf)
self.assertEqual(1, len(self.conf))
self.assertIsNone(self.conf.foo)
self.assertIn('foo', self.fconf)
self.assertEqual(1, len(self.fconf))
self.assertIsNone(self.fconf.foo)
self.conf.set_override('foo', 'bar')
self.assertEqual('bar', self.conf.foo)
self.assertEqual('bar', self.fconf.foo)
def test_already_registered_opts(self):
self.conf.register_opts([cfg.StrOpt('foo'),
cfg.StrOpt('fu')])
self.fconf.register_opts([cfg.StrOpt('foo'),
cfg.StrOpt('bu')])
self.assertIn('foo', self.conf)
self.assertIn('fu', self.conf)
self.assertNotIn('bu', self.conf)
self.assertEqual(2, len(self.conf))
self.assertIsNone(self.conf.foo)
self.assertIsNone(self.conf.fu)
self.assertIn('foo', self.fconf)
self.assertIn('bu', self.fconf)
self.assertNotIn('fu', self.fconf)
self.assertEqual(2, len(self.fconf))
self.assertIsNone(self.fconf.foo)
self.assertIsNone(self.fconf.bu)
self.conf.set_override('foo', 'bar')
self.assertEqual('bar', self.conf.foo)
self.assertEqual('bar', self.fconf.foo)
def test_already_registered_cli_opt(self):
self.conf.register_cli_opt(cfg.StrOpt('foo'))
self.fconf.register_cli_opt(cfg.StrOpt('foo'))
self.assertIn('foo', self.conf)
self.assertEqual(1, len(self.conf))
self.assertIsNone(self.conf.foo)
self.assertIn('foo', self.fconf)
self.assertEqual(1, len(self.fconf))
self.assertIsNone(self.fconf.foo)
self.conf.set_override('foo', 'bar')
self.assertEqual('bar', self.conf.foo)
self.assertEqual('bar', self.fconf.foo)
def test_already_registered_cli_opts(self):
self.conf.register_cli_opts([cfg.StrOpt('foo'),
cfg.StrOpt('fu')])
self.fconf.register_cli_opts([cfg.StrOpt('foo'),
cfg.StrOpt('fu')])
self.assertIn('foo', self.conf)
self.assertIn('fu', self.conf)
self.assertEqual(2, len(self.conf))
self.assertIsNone(self.conf.foo)
self.assertIsNone(self.conf.fu)
self.assertIn('foo', self.fconf)
self.assertIn('fu', self.fconf)
self.assertEqual(2, len(self.fconf))
self.assertIsNone(self.fconf.foo)
self.assertIsNone(self.fconf.fu)
self.conf.set_override('foo', 'bar')
self.assertEqual('bar', self.conf.foo)
self.assertEqual('bar', self.fconf.foo)
class ImportTestCase(BaseTestCase):
def setUp(self):
super(ImportTestCase, self).setUp(cfg.CONF)
def test_import_opt(self):
self.assertFalse(hasattr(self.conf, 'fblaa'))
self.conf.import_opt('fblaa', 'oslo_config.tests.testmods.fblaa_opt')
self.assertTrue(hasattr(self.conf, 'fblaa'))
self.assertFalse(hasattr(self.fconf, 'fblaa'))
self.fconf.import_opt('fblaa', 'oslo_config.tests.testmods.fblaa_opt')
self.assertTrue(hasattr(self.fconf, 'fblaa'))
def test_import_opt_in_group(self):
self.assertFalse(hasattr(self.conf, 'fbar'))
self.conf.import_opt('foo', 'oslo_config.tests.testmods.fbar_foo_opt',
group='fbar')
self.assertTrue(hasattr(self.conf, 'fbar'))
self.assertTrue(hasattr(self.conf.fbar, 'foo'))
self.assertFalse(hasattr(self.fconf, 'fbar'))
self.fconf.import_opt('foo', 'oslo_config.tests.testmods.fbar_foo_opt',
group='fbar')
self.assertTrue(hasattr(self.fconf, 'fbar'))
self.assertTrue(hasattr(self.fconf.fbar, 'foo'))
def test_import_group(self):
self.assertFalse(hasattr(self.conf, 'fbaar'))
self.conf.import_group('fbaar',
'oslo_config.tests.testmods.fbaar_baa_opt')
self.assertTrue(hasattr(self.conf, 'fbaar'))
self.assertTrue(hasattr(self.conf.fbaar, 'baa'))
self.assertFalse(hasattr(self.fconf, 'fbaar'))
self.fconf.import_group('fbaar',
'oslo_config.tests.testmods.fbaar_baa_opt')
self.assertTrue(hasattr(self.fconf, 'fbaar'))
self.assertTrue(hasattr(self.fconf.fbaar, 'baa'))
class ExposeTestCase(BaseTestCase):
def test_expose_opt(self):
self.assertFalse(hasattr(self.conf, 'foo'))
self.assertFalse(hasattr(self.fconf, 'foo'))
self.conf.register_opt(cfg.StrOpt('foo'))
self.conf.set_override('foo', 'bar')
self.assertTrue(hasattr(self.conf, 'foo'))
self.assertEqual('bar', self.conf.foo)
self.assertFalse(hasattr(self.fconf, 'foo'))
self.fconf.expose_opt('foo')
self.assertTrue(hasattr(self.conf, 'foo'))
self.assertTrue(hasattr(self.fconf, 'foo'))
self.assertEqual('bar', self.fconf.foo)
def test_expose_opt_with_group(self):
self.assertFalse(hasattr(self.conf, 'foo'))
self.assertFalse(hasattr(self.fconf, 'foo'))
self.conf.register_opt(cfg.StrOpt('foo'), group='group')
self.conf.set_override('foo', 'bar', group='group')
self.assertTrue(hasattr(self.conf.group, 'foo'))
self.assertEqual('bar', self.conf.group.foo)
self.assertFalse(hasattr(self.fconf, 'group'))
self.fconf.expose_opt('foo', group='group')
self.assertTrue(hasattr(self.conf.group, 'foo'))
self.assertTrue(hasattr(self.fconf.group, 'foo'))
self.assertEqual('bar', self.fconf.group.foo)
def test_expose_group(self):
self.conf.register_opts([cfg.StrOpt('foo'),
cfg.StrOpt('bar')], group='group')
self.conf.register_opts([cfg.StrOpt('foo'),
cfg.StrOpt('bar')], group='another')
self.conf.set_override('foo', 'a', group='group')
self.conf.set_override('bar', 'b', group='group')
self.fconf.expose_group('group')
self.assertEqual('a', self.fconf.group.foo)
self.assertEqual('b', self.fconf.group.bar)
self.assertFalse(hasattr(self.fconf, 'another'))
self.assertTrue(hasattr(self.conf, 'another'))

View File

@ -1,3 +0,0 @@
[DEFAULT]
first_test_opt=loaded_value_1
second_test_opt=loaded_value_2

View File

@ -1,200 +0,0 @@
#
# Copyright 2013 Mirantis, Inc.
# Copyright 2013 OpenStack Foundation
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslotest import base
from oslo_config import cfg
from oslo_config import fixture as config
class ConfigTestCase(base.BaseTestCase):
def _make_fixture(self):
conf = cfg.ConfigOpts()
config_fixture = config.Config(conf)
config_fixture.setUp()
config_fixture.register_opt(cfg.StrOpt(
'testing_option', default='initial_value'))
config_fixture.register_opt(cfg.IntOpt(
'test2', min=0, default=5))
config_fixture.register_opt(cfg.StrOpt(
'test3', choices=['a', 'b'], default='a'))
return config_fixture
def test_overridden_value(self):
f = self._make_fixture()
self.assertEqual(f.conf.get('testing_option'), 'initial_value')
f.config(testing_option='changed_value')
self.assertEqual('changed_value',
f.conf.get('testing_option'))
def test_overridden_value_with_enforce_type(self):
f = self._make_fixture()
self.assertEqual(5, f.conf.get('test2'))
self.assertEqual('a', f.conf.get('test3'))
# with enforce_type=False
f.config(test2=-1)
self.assertEqual(-1, f.conf.get('test2'))
f.config(test3='c')
self.assertEqual('c', f.conf.get('test3'))
# with enforce_type=True
self.assertRaises(ValueError, f.config, test2=-1, enforce_type=True)
self.assertRaises(ValueError, f.config, test3='c', enforce_type=True)
def test_cleanup(self):
f = self._make_fixture()
f.config(testing_option='changed_value')
self.assertEqual(f.conf.get('testing_option'),
'changed_value')
f.conf.reset()
self.assertEqual(f.conf.get('testing_option'), 'initial_value')
def test_register_option(self):
f = self._make_fixture()
opt = cfg.StrOpt('new_test_opt', default='initial_value')
f.register_opt(opt)
self.assertEqual(f.conf.get('new_test_opt'),
opt.default)
def test_register_options(self):
f = self._make_fixture()
opt1 = cfg.StrOpt('first_test_opt', default='initial_value_1')
opt2 = cfg.StrOpt('second_test_opt', default='initial_value_2')
f.register_opts([opt1, opt2])
self.assertEqual(f.conf.get('first_test_opt'), opt1.default)
self.assertEqual(f.conf.get('second_test_opt'), opt2.default)
def test_cleanup_unregister_option(self):
f = self._make_fixture()
opt = cfg.StrOpt('new_test_opt', default='initial_value')
f.register_opt(opt)
self.assertEqual(f.conf.get('new_test_opt'),
opt.default)
f.cleanUp()
self.assertRaises(cfg.NoSuchOptError, f.conf.get, 'new_test_opt')
def test_register_cli_option(self):
f = self._make_fixture()
opt = cfg.StrOpt('new_test_opt', default='initial_value')
f.register_cli_opt(opt)
self.assertEqual(f.conf.get('new_test_opt'),
opt.default)
def test_register_cli_options(self):
f = self._make_fixture()
opt1 = cfg.StrOpt('first_test_opt', default='initial_value_1')
opt2 = cfg.StrOpt('second_test_opt', default='initial_value_2')
f.register_cli_opts([opt1, opt2])
self.assertEqual(f.conf.get('first_test_opt'), opt1.default)
self.assertEqual(f.conf.get('second_test_opt'), opt2.default)
def test_cleanup_unregister_cli_option(self):
f = self._make_fixture()
opt = cfg.StrOpt('new_test_opt', default='initial_value')
f.register_cli_opt(opt)
self.assertEqual(f.conf.get('new_test_opt'),
opt.default)
f.cleanUp()
self.assertRaises(cfg.NoSuchOptError, f.conf.get, 'new_test_opt')
def test_load_raw_values(self):
f = self._make_fixture()
f.load_raw_values(first_test_opt='loaded_value_1',
second_test_opt='loaded_value_2')
# Must not be registered.
self.assertRaises(cfg.NoSuchOptError, f.conf.get, 'first_test_opt')
self.assertRaises(cfg.NoSuchOptError, f.conf.get, 'second_test_opt')
opt1 = cfg.StrOpt('first_test_opt', default='initial_value_1')
opt2 = cfg.StrOpt('second_test_opt', default='initial_value_2')
f.register_opt(opt1)
f.register_opt(opt2)
self.assertEqual(f.conf.first_test_opt, 'loaded_value_1')
self.assertEqual(f.conf.second_test_opt, 'loaded_value_2')
# Cleanup.
f.cleanUp()
# Must no longer be registered.
self.assertRaises(cfg.NoSuchOptError, f.conf.get, 'first_test_opt')
self.assertRaises(cfg.NoSuchOptError, f.conf.get, 'second_test_opt')
# Even when registered, must be default.
f.register_opt(opt1)
f.register_opt(opt2)
self.assertEqual(f.conf.first_test_opt, 'initial_value_1')
self.assertEqual(f.conf.second_test_opt, 'initial_value_2')
def test_assert_default_files_cleanup(self):
"""Assert that using the fixture forces a clean list."""
f = self._make_fixture()
self.assertNotIn('default_config_files', f.conf)
config_files = ['./test_fixture.conf']
f.set_config_files(config_files)
self.assertEqual(f.conf.default_config_files, config_files)
f.cleanUp()
self.assertNotIn('default_config_files', f.conf)
def test_load_custom_files(self):
f = self._make_fixture()
self.assertNotIn('default_config_files', f.conf)
config_files = ['./oslo_config/tests/test_fixture.conf']
f.set_config_files(config_files)
opt1 = cfg.StrOpt('first_test_opt', default='initial_value_1')
opt2 = cfg.StrOpt('second_test_opt', default='initial_value_2')
f.register_opt(opt1)
f.register_opt(opt2)
self.assertEqual('loaded_value_1', f.conf.get('first_test_opt'))
self.assertEqual('loaded_value_2', f.conf.get('second_test_opt'))
def test_set_default(self):
f = self._make_fixture()
opt = cfg.StrOpt('new_test_opt', default='initial_value')
# Register the option directly so it is not cleaned up by the
# fixture.
f.conf.register_opt(opt)
f.set_default(
name='new_test_opt',
default='alternate_value',
)
self.assertEqual('alternate_value', f.conf.new_test_opt)
f.cleanUp()
self.assertEqual('initial_value', f.conf.new_test_opt)
def test_set_default_group(self):
f = self._make_fixture()
opt = cfg.StrOpt('new_test_opt', default='initial_value')
# Register the option directly so it is not cleaned up by the
# fixture.
f.conf.register_opt(opt, group='foo')
f.set_default(
name='new_test_opt',
default='alternate_value',
group='foo',
)
self.assertEqual('alternate_value', f.conf.foo.new_test_opt)
f.cleanUp()
self.assertEqual('initial_value', f.conf.foo.new_test_opt)

File diff suppressed because it is too large Load Diff

View File

@ -1,124 +0,0 @@
# Copyright 2012 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import unittest
from oslo_config import iniparser
class TestParser(iniparser.BaseParser):
comment_called = False
values = None
section = ''
def __init__(self):
self.values = {}
def assignment(self, key, value):
self.values.setdefault(self.section, {})
self.values[self.section][key] = value
def new_section(self, section):
self.section = section
def comment(self, section):
self.comment_called = True
class BaseParserTestCase(unittest.TestCase):
def setUp(self):
self.parser = iniparser.BaseParser()
def _assertParseError(self, *lines):
self.assertRaises(iniparser.ParseError, self.parser.parse, lines)
def test_invalid_assignment(self):
self._assertParseError("foo - bar")
def test_empty_key(self):
self._assertParseError(": bar")
def test_unexpected_continuation(self):
self._assertParseError(" baz")
def test_invalid_section(self):
self._assertParseError("[section")
def test_no_section_name(self):
self._assertParseError("[]")
class ParserTestCase(unittest.TestCase):
def setUp(self):
self.parser = TestParser()
def test_blank_line(self):
lines = [""]
self.parser.parse(lines)
self.assertEqual({}, self.parser.values)
def test_assignment_equal(self):
lines = ["foo = bar"]
self.parser.parse(lines)
self.assertEqual({'': {'foo': ['bar']}}, self.parser.values)
def test_assignment_colon(self):
lines = ["foo: bar"]
self.parser.parse(lines)
self.assertEqual({'': {'foo': ['bar']}}, self.parser.values)
def test_assignment_multiline(self):
lines = ["foo = bar0", " bar1"]
self.parser.parse(lines)
self.assertEqual({'': {'foo': ['bar0', 'bar1']}}, self.parser.values)
def test_assignment_multline_empty(self):
lines = ["foo = bar0", "", " bar1"]
self.assertRaises(iniparser.ParseError, self.parser.parse, lines)
def test_section_assignment(self):
lines = ["[test]", "foo = bar"]
self.parser.parse(lines)
self.assertEqual({'test': {'foo': ['bar']}}, self.parser.values)
def test_new_section(self):
lines = ["[foo]"]
self.parser.parse(lines)
self.assertEqual('foo', self.parser.section)
def test_comment(self):
lines = ["# foobar"]
self.parser.parse(lines)
self.assertTrue(self.parser.comment_called)
def test_empty_assignment(self):
lines = ["foo = "]
self.parser.parse(lines)
self.assertEqual({'': {'foo': ['']}}, self.parser.values)
def test_assignment_space_single_quote(self):
lines = ["foo = ' bar '"]
self.parser.parse(lines)
self.assertEqual({'': {'foo': [' bar ']}}, self.parser.values)
def test_assignment_space_double_quote(self):
lines = ["foo = \" bar \""]
self.parser.parse(lines)
self.assertEqual({'': {'foo': [' bar ']}}, self.parser.values)
class ExceptionTestCase(unittest.TestCase):
def test_parseerror(self):
exc = iniparser.ParseError('test', 42, 'example')
self.assertEqual(str(exc), "at line 42, test: 'example'")

View File

@ -1,128 +0,0 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
from oslotest import base
from oslo_config import sphinxconfiggen
class SingleSampleGenerationTest(base.BaseTestCase):
@mock.patch('os.path.isdir')
@mock.patch('os.path.isfile')
@mock.patch('oslo_config.generator.main')
def test_sample_gen_with_single_config_file(self, main, isfile, isdir):
isfile.side_effect = [False, True]
isdir.return_value = True
config = mock.Mock(config_generator_config_file='nova-gen.conf',
sample_config_basename='nova')
app = mock.Mock(srcdir='/opt/nova', config=config)
sphinxconfiggen.generate_sample(app)
main.assert_called_once_with(args=['--config-file',
'/opt/nova/nova-gen.conf',
'--output-file',
'/opt/nova/nova.conf.sample'
])
@mock.patch('os.path.isdir')
@mock.patch('os.path.isfile')
@mock.patch('oslo_config.generator.main')
def test_sample_gen_with_single_config_file_no_base(self, main, isfile,
isdir):
isfile.side_effect = [False, True]
isdir.return_value = True
config = mock.Mock(config_generator_config_file='nova-gen.conf',
sample_config_basename=None)
app = mock.Mock(srcdir='/opt/nova', config=config)
sphinxconfiggen.generate_sample(app)
main.assert_called_once_with(args=['--config-file',
'/opt/nova/nova-gen.conf',
'--output-file',
'/opt/nova/sample.config'])
class MultipleSampleGenerationTest(base.BaseTestCase):
@mock.patch('os.path.isdir')
@mock.patch('os.path.isfile')
@mock.patch('oslo_config.generator.main')
def test_multi_sample_gen(self, main, isfile, isdir):
isfile.side_effect = [False, True, False, True]
isdir.return_value = True
multiple_configs = [('glance-api-gen.conf', 'glance-api'),
('glance-reg-gen.conf', 'glance-reg')]
config = mock.Mock(config_generator_config_file=multiple_configs)
app = mock.Mock(srcdir='/opt/glance', config=config)
sphinxconfiggen.generate_sample(app)
self.assertEqual(main.call_count, 2)
main.assert_any_call(args=['--config-file',
'/opt/glance/glance-api-gen.conf',
'--output-file',
'/opt/glance/glance-api.conf.sample'])
main.assert_any_call(args=['--config-file',
'/opt/glance/glance-reg-gen.conf',
'--output-file',
'/opt/glance/glance-reg.conf.sample'])
@mock.patch('os.path.isdir')
@mock.patch('os.path.isfile')
@mock.patch('oslo_config.generator.main')
def test_multi_sample_gen_with_without_one_base(self, main, isfile, isdir):
isfile.side_effect = [False, True, False, True]
isdir.return_value = True
multiple_configs = [('glance-api-gen.conf', 'glance-api'),
('glance-reg-gen.conf', None)]
config = mock.Mock(config_generator_config_file=multiple_configs)
app = mock.Mock(srcdir='/opt/glance', config=config)
sphinxconfiggen.generate_sample(app)
self.assertEqual(main.call_count, 2)
main.assert_any_call(args=['--config-file',
'/opt/glance/glance-api-gen.conf',
'--output-file',
'/opt/glance/glance-api.conf.sample'])
main.assert_any_call(args=['--config-file',
'/opt/glance/glance-reg-gen.conf',
'--output-file',
'/opt/glance/glance-reg-gen.conf.sample'])
@mock.patch('os.path.isdir')
@mock.patch('os.path.isfile')
@mock.patch('oslo_config.generator.main')
def test_multi_sample_gen_with_without_any_base(self, main, isfile, isdir):
isfile.side_effect = [False, True, False, True]
isdir.return_value = True
multiple_configs = [('glance-api-gen.conf', None),
('glance-reg-gen.conf', None)]
config = mock.Mock(config_generator_config_file=multiple_configs)
app = mock.Mock(srcdir='/opt/glance', config=config)
sphinxconfiggen.generate_sample(app)
self.assertEqual(main.call_count, 2)
main.assert_any_call(args=['--config-file',
'/opt/glance/glance-api-gen.conf',
'--output-file',
'/opt/glance/glance-api-gen.conf.sample'])
main.assert_any_call(args=['--config-file',
'/opt/glance/glance-reg-gen.conf',
'--output-file',
'/opt/glance/glance-reg-gen.conf.sample'])

View File

@ -1,483 +0,0 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import textwrap
import mock
from oslotest import base
from oslo_config import cfg
from oslo_config import sphinxext
class FormatGroupTest(base.BaseTestCase):
def test_none_in_default(self):
# option with None group placed in DEFAULT
results = '\n'.join(list(sphinxext._format_group(
app=mock.Mock(),
namespace=None,
group_name=None,
group_obj=None,
opt_list=[
cfg.StrOpt('opt_name',
help='this appears in the default group'),
],
)))
self.assertEqual(textwrap.dedent('''
.. oslo.config:group:: DEFAULT
.. oslo.config:option:: opt_name
:Type: string
:Default: ``<None>``
this appears in the default group
''').lstrip(), results)
def test_with_default_value(self):
results = '\n'.join(list(sphinxext._format_group(
app=mock.Mock(),
namespace=None,
group_name=None,
group_obj=None,
opt_list=[
cfg.StrOpt('opt_name',
default='this is the default',
help='this appears in the default group'),
],
)))
self.assertEqual(textwrap.dedent('''
.. oslo.config:group:: DEFAULT
.. oslo.config:option:: opt_name
:Type: string
:Default: ``this is the default``
this appears in the default group
''').lstrip(), results)
def test_with_min(self):
results = '\n'.join(list(sphinxext._format_group(
app=mock.Mock(),
namespace=None,
group_name=None,
group_obj=None,
opt_list=[
cfg.IntOpt('opt_name',
min=1),
],
)))
self.assertEqual(textwrap.dedent('''
.. oslo.config:group:: DEFAULT
.. oslo.config:option:: opt_name
:Type: integer
:Default: ``<None>``
:Minimum Value: 1
''').lstrip(), results)
def test_with_min_0(self):
results = '\n'.join(list(sphinxext._format_group(
app=mock.Mock(),
namespace=None,
group_name=None,
group_obj=None,
opt_list=[
cfg.IntOpt('opt_name',
min=0),
],
)))
self.assertEqual(textwrap.dedent('''
.. oslo.config:group:: DEFAULT
.. oslo.config:option:: opt_name
:Type: integer
:Default: ``<None>``
:Minimum Value: 0
''').lstrip(), results)
def test_with_max(self):
results = '\n'.join(list(sphinxext._format_group(
app=mock.Mock(),
namespace=None,
group_name=None,
group_obj=None,
opt_list=[
cfg.IntOpt('opt_name',
max=1),
],
)))
self.assertEqual(textwrap.dedent('''
.. oslo.config:group:: DEFAULT
.. oslo.config:option:: opt_name
:Type: integer
:Default: ``<None>``
:Maximum Value: 1
''').lstrip(), results)
def test_with_max_0(self):
results = '\n'.join(list(sphinxext._format_group(
app=mock.Mock(),
namespace=None,
group_name=None,
group_obj=None,
opt_list=[
cfg.IntOpt('opt_name',
max=0),
],
)))
self.assertEqual(textwrap.dedent('''
.. oslo.config:group:: DEFAULT
.. oslo.config:option:: opt_name
:Type: integer
:Default: ``<None>``
:Maximum Value: 0
''').lstrip(), results)
def test_with_choices(self):
results = '\n'.join(list(sphinxext._format_group(
app=mock.Mock(),
namespace=None,
group_name=None,
group_obj=None,
opt_list=[
cfg.StrOpt('opt_name',
choices=['a', 'b', 'c', None, '']),
],
)))
self.assertEqual(textwrap.dedent('''
.. oslo.config:group:: DEFAULT
.. oslo.config:option:: opt_name
:Type: string
:Default: ``<None>``
:Valid Values: a, b, c, <None>, ''
''').lstrip(), results)
def test_group_obj_without_help(self):
# option with None group placed in DEFAULT
results = '\n'.join(list(sphinxext._format_group(
app=mock.Mock(),
namespace=None,
group_name='group',
group_obj=cfg.OptGroup('group'),
opt_list=[cfg.StrOpt('opt_name')],
)))
self.assertEqual(textwrap.dedent('''
.. oslo.config:group:: group
.. oslo.config:option:: opt_name
:Type: string
:Default: ``<None>``
''').lstrip(), results)
def test_group_obj_with_help(self):
# option with None group placed in DEFAULT
results = '\n'.join(list(sphinxext._format_group(
app=mock.Mock(),
namespace=None,
group_name='group',
group_obj=cfg.OptGroup('group', help='group help'),
opt_list=[cfg.StrOpt('opt_name')],
)))
self.assertEqual(textwrap.dedent('''
.. oslo.config:group:: group
group help
.. oslo.config:option:: opt_name
:Type: string
:Default: ``<None>``
''').lstrip(), results)
def test_deprecated_opts_without_deprecated_group(self):
results = '\n'.join(list(sphinxext._format_group(
app=mock.Mock(),
namespace=None,
group_name=None,
group_obj=None,
opt_list=[
cfg.StrOpt('opt_name',
deprecated_name='deprecated_name',
)
],
)))
self.assertEqual(textwrap.dedent('''
.. oslo.config:group:: DEFAULT
.. oslo.config:option:: opt_name
:Type: string
:Default: ``<None>``
.. list-table:: Deprecated Variations
:header-rows: 1
- * Group
* Name
- * DEFAULT
* deprecated_name
''').lstrip(), results)
def test_deprecated_opts_with_deprecated_group(self):
results = '\n'.join(list(sphinxext._format_group(
app=mock.Mock(),
namespace=None,
group_name=None,
group_obj=None,
opt_list=[
cfg.StrOpt('opt_name',
deprecated_name='deprecated_name',
deprecated_group='deprecated_group',
)
],
)))
self.assertEqual(textwrap.dedent('''
.. oslo.config:group:: DEFAULT
.. oslo.config:option:: opt_name
:Type: string
:Default: ``<None>``
.. list-table:: Deprecated Variations
:header-rows: 1
- * Group
* Name
- * deprecated_group
* deprecated_name
''').lstrip(), results)
def test_deprecated_for_removal(self):
results = '\n'.join(list(sphinxext._format_group(
app=mock.Mock(),
namespace=None,
group_name=None,
group_obj=None,
opt_list=[
cfg.StrOpt('opt_name',
deprecated_for_removal=True,
deprecated_reason='because I said so',
deprecated_since='13.0',
)
],
)))
self.assertIn('.. warning::', results)
self.assertIn('because I said so', results)
self.assertIn('since 13.0', results)
def test_mutable(self):
results = '\n'.join(list(sphinxext._format_group(
app=mock.Mock(),
namespace=None,
group_name=None,
group_obj=None,
opt_list=[
cfg.IntOpt('opt_name',
mutable=True),
],
)))
self.assertEqual(textwrap.dedent('''
.. oslo.config:group:: DEFAULT
.. oslo.config:option:: opt_name
:Type: integer
:Default: ``<None>``
:Mutable: This option can be changed without restarting.
''').lstrip(), results)
def test_not_mutable(self):
results = '\n'.join(list(sphinxext._format_group(
app=mock.Mock(),
namespace=None,
group_name=None,
group_obj=None,
opt_list=[
cfg.IntOpt('opt_name',
mutable=False),
],
)))
self.assertEqual(textwrap.dedent('''
.. oslo.config:group:: DEFAULT
.. oslo.config:option:: opt_name
:Type: integer
:Default: ``<None>``
''').lstrip(), results)
def test_advanced(self):
results = '\n'.join(list(sphinxext._format_group(
app=mock.Mock(),
namespace=None,
group_name=None,
group_obj=None,
opt_list=[
cfg.StrOpt('opt_name',
advanced=True),
],
)))
self.assertEqual(textwrap.dedent('''
.. oslo.config:group:: DEFAULT
.. oslo.config:option:: opt_name
:Type: string
:Default: ``<None>``
:Advanced Option: intended for advanced users and not used
:by the majority of users, and might have a significant
:effect on stability and/or performance.
''').lstrip(), results)
def test_not_advanced(self):
results = '\n'.join(list(sphinxext._format_group(
app=mock.Mock(),
namespace=None,
group_name=None,
group_obj=None,
opt_list=[
cfg.StrOpt('opt_name',
advanced=False),
],
)))
self.assertEqual(textwrap.dedent('''
.. oslo.config:group:: DEFAULT
.. oslo.config:option:: opt_name
:Type: string
:Default: ``<None>``
''').lstrip(), results)
class FormatOptionHelpTest(base.BaseTestCase):
@mock.patch('oslo_config.generator._list_opts')
@mock.patch('oslo_config.sphinxext._format_group')
def test_split_namespaces(self, _format_group, _list_opts):
_list_opts.return_value = [
('namespace1', [(None, ['opt1'])]),
('namespace2', [(None, ['opt2'])]),
]
list(sphinxext._format_option_help(
app=None,
namespaces=['namespace1', 'namespace2'],
split_namespaces=True))
_format_group.assert_any_call(
app=None,
namespace='namespace1',
group_name=None,
group_obj=None,
opt_list=['opt1'],
)
_format_group.assert_any_call(
app=None,
namespace='namespace2',
group_name=None,
group_obj=None,
opt_list=['opt2'],
)
@mock.patch('oslo_config.generator._list_opts')
@mock.patch('oslo_config.sphinxext._format_group')
def test_dont_split_namespaces(self, _format_group, _list_opts):
_list_opts.return_value = [
('namespace1', [(None, ['opt1'])]),
('namespace2', [(None, ['opt2'])]),
]
list(sphinxext._format_option_help(
app=None,
namespaces=['namespace1', 'namespace2'],
split_namespaces=False))
_format_group.assert_called_once_with(
app=None,
namespace=None,
group_name=None,
group_obj=None,
opt_list=['opt1', 'opt2'],
)
@mock.patch('oslo_config.generator._list_opts')
@mock.patch('oslo_config.sphinxext._format_group')
def test_dont_split_namespaces_with_group(self, _format_group, _list_opts):
grp_obj = cfg.OptGroup('grp1')
_list_opts.return_value = [
('namespace1', [(grp_obj, ['opt1'])]),
('namespace2', [('grp1', ['opt2'])]),
]
list(sphinxext._format_option_help(
app=None,
namespaces=['namespace1', 'namespace2'],
split_namespaces=False))
_format_group.assert_any_call(
app=None,
namespace=None,
group_name='grp1',
group_obj=grp_obj,
opt_list=['opt1', 'opt2'],
)
@mock.patch('oslo_config.generator._list_opts')
@mock.patch('oslo_config.sphinxext._format_group')
def test_split_namespaces_with_group(self, _format_group, _list_opts):
grp_obj = cfg.OptGroup('grp1')
_list_opts.return_value = [
('namespace1', [(grp_obj, ['opt1'])]),
('namespace2', [('grp1', ['opt2'])]),
]
list(sphinxext._format_option_help(
app=None,
namespaces=['namespace1', 'namespace2'],
split_namespaces=True))
print(_format_group.call_args_list)
_format_group.assert_any_call(
app=None,
namespace='namespace1',
group_name='grp1',
group_obj=grp_obj,
opt_list=['opt1'],
)
_format_group.assert_any_call(
app=None,
namespace='namespace2',
group_name='grp1',
group_obj=None,
opt_list=['opt2'],
)

View File

@ -1,884 +0,0 @@
# Copyright 2013 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import re
import unittest
from oslo_config import types
class ConfigTypeTests(unittest.TestCase):
def test_none_concrete_class(self):
class MyString(types.ConfigType):
def __init__(self, type_name='mystring value'):
super(MyString, self).__init__(type_name=type_name)
self.assertRaises(TypeError, MyString)
def test_concrete_class(self):
class MyString(types.ConfigType):
def __init__(self, type_name='mystring value'):
super(MyString, self).__init__(type_name=type_name)
def _formatter(self, value):
return value
MyString()
class TypeTestHelper(object):
def setUp(self):
super(TypeTestHelper, self).setUp()
self.type_instance = self.type
def assertConvertedValue(self, s, expected):
self.assertEqual(expected, self.type_instance(s))
def assertInvalid(self, value):
self.assertRaises(ValueError, self.type_instance, value)
class StringTypeTests(TypeTestHelper, unittest.TestCase):
type = types.String()
def test_empty_string_passes(self):
self.assertConvertedValue('', '')
def test_should_return_same_string_if_valid(self):
self.assertConvertedValue('foo bar', 'foo bar')
def test_listed_value(self):
self.type_instance = types.String(choices=['foo', 'bar'])
self.assertConvertedValue('foo', 'foo')
def test_unlisted_value(self):
self.type_instance = types.String(choices=['foo', 'bar'])
self.assertInvalid('baz')
def test_with_no_values_returns_error(self):
self.type_instance = types.String(choices=[])
self.assertInvalid('foo')
def test_string_with_non_closed_quote_is_invalid(self):
self.type_instance = types.String(quotes=True)
self.assertInvalid('"foo bar')
self.assertInvalid("'bar baz")
def test_quotes_are_stripped(self):
self.type_instance = types.String(quotes=True)
self.assertConvertedValue('"foo bar"', 'foo bar')
def test_trailing_quote_is_ok(self):
self.type_instance = types.String(quotes=True)
self.assertConvertedValue('foo bar"', 'foo bar"')
def test_repr(self):
t = types.String()
self.assertEqual('String', repr(t))
def test_repr_with_choices(self):
t = types.String(choices=['foo', 'bar'])
self.assertEqual('String(choices=[\'foo\', \'bar\'])', repr(t))
def test_equal(self):
self.assertTrue(types.String() == types.String())
def test_equal_with_same_choices(self):
t1 = types.String(choices=['foo', 'bar'])
t2 = types.String(choices=['foo', 'bar'])
t3 = types.String(choices=('foo', 'bar'))
t4 = types.String(choices=['bar', 'foo'])
self.assertTrue(t1 == t2)
self.assertTrue(t1 == t3)
self.assertTrue(t1 == t4)
def test_not_equal_with_different_choices(self):
t1 = types.String(choices=['foo', 'bar'])
t2 = types.String(choices=['foo', 'baz'])
self.assertFalse(t1 == t2)
def test_equal_with_equal_quote_falgs(self):
t1 = types.String(quotes=True)
t2 = types.String(quotes=True)
self.assertTrue(t1 == t2)
def test_not_equal_with_different_quote_falgs(self):
t1 = types.String(quotes=False)
t2 = types.String(quotes=True)
self.assertFalse(t1 == t2)
def test_not_equal_to_other_class(self):
self.assertFalse(types.String() == types.Integer())
def test_regex_matches(self):
self.type_instance = types.String(regex=re.compile("^[A-Z]"))
self.assertConvertedValue("Foo", "Foo")
def test_regex_matches_uncompiled(self):
self.type_instance = types.String(regex="^[A-Z]")
self.assertConvertedValue("Foo", "Foo")
def test_regex_fails(self):
self.type_instance = types.String(regex=re.compile("^[A-Z]"))
self.assertInvalid("foo")
def test_regex_and_choices_raises(self):
self.assertRaises(ValueError,
types.String,
regex=re.compile("^[A-Z]"),
choices=["Foo", "Bar", "baz"])
def test_equal_with_same_regex(self):
t1 = types.String(regex=re.compile("^[A-Z]"))
t2 = types.String(regex=re.compile("^[A-Z]"))
self.assertTrue(t1 == t2)
def test_not_equal_with_different_regex(self):
t1 = types.String(regex=re.compile("^[A-Z]"))
t2 = types.String(regex=re.compile("^[a-z]"))
self.assertFalse(t1 == t2)
def test_ignore_case(self):
self.type_instance = types.String(choices=['foo', 'bar'],
ignore_case=True)
self.assertConvertedValue('Foo', 'Foo')
self.assertConvertedValue('bAr', 'bAr')
def test_ignore_case_raises(self):
self.type_instance = types.String(choices=['foo', 'bar'],
ignore_case=False)
self.assertRaises(ValueError, self.assertConvertedValue, 'Foo', 'Foo')
def test_regex_and_ignore_case(self):
self.type_instance = types.String(regex=re.compile("^[A-Z]"),
ignore_case=True)
self.assertConvertedValue("foo", "foo")
def test_regex_and_ignore_case_str(self):
self.type_instance = types.String(regex="^[A-Z]", ignore_case=True)
self.assertConvertedValue("foo", "foo")
def test_regex_preserve_flags(self):
self.type_instance = types.String(regex=re.compile("^[A-Z]", re.I),
ignore_case=False)
self.assertConvertedValue("foo", "foo")
def test_max_length(self):
self.type_instance = types.String(max_length=5)
self.assertInvalid('123456')
self.assertConvertedValue('12345', '12345')
class BooleanTypeTests(TypeTestHelper, unittest.TestCase):
type = types.Boolean()
def test_True(self):
self.assertConvertedValue('True', True)
def test_yes(self):
self.assertConvertedValue('yes', True)
def test_on(self):
self.assertConvertedValue('on', True)
def test_1(self):
self.assertConvertedValue('1', True)
def test_False(self):
self.assertConvertedValue('False', False)
def test_no(self):
self.assertConvertedValue('no', False)
def test_off(self):
self.assertConvertedValue('off', False)
def test_0(self):
self.assertConvertedValue('0', False)
def test_other_values_produce_error(self):
self.assertInvalid('foo')
def test_repr(self):
self.assertEqual('Boolean', repr(types.Boolean()))
def test_equal(self):
self.assertEqual(types.Boolean(), types.Boolean())
def test_not_equal_to_other_class(self):
self.assertFalse(types.Boolean() == types.String())
class IntegerTypeTests(TypeTestHelper, unittest.TestCase):
type = types.Integer()
def test_empty_string(self):
self.assertConvertedValue('', None)
def test_whitespace_string(self):
self.assertConvertedValue(" \t\t\t\t", None)
def test_positive_values_are_valid(self):
self.assertConvertedValue('123', 123)
def test_zero_is_valid(self):
self.assertConvertedValue('0', 0)
def test_negative_values_are_valid(self):
self.assertConvertedValue('-123', -123)
def test_leading_whitespace_is_ignored(self):
self.assertConvertedValue(' 5', 5)
def test_trailing_whitespace_is_ignored(self):
self.assertConvertedValue('7 ', 7)
def test_non_digits_are_invalid(self):
self.assertInvalid('12a45')
def test_repr(self):
t = types.Integer()
self.assertEqual('Integer', repr(t))
def test_repr_with_min(self):
t = types.Integer(min=123)
self.assertEqual('Integer(min=123)', repr(t))
def test_repr_with_max(self):
t = types.Integer(max=456)
self.assertEqual('Integer(max=456)', repr(t))
def test_repr_with_min_and_max(self):
t = types.Integer(min=123, max=456)
self.assertEqual('Integer(min=123, max=456)', repr(t))
t = types.Integer(min=0, max=0)
self.assertEqual('Integer(min=0, max=0)', repr(t))
def test_repr_with_choices(self):
t = types.Integer(choices=[80, 457])
self.assertEqual('Integer(choices=[80, 457])', repr(t))
def test_equal(self):
self.assertTrue(types.Integer() == types.Integer())
def test_equal_with_same_min_and_no_max(self):
self.assertTrue(types.Integer(min=123) == types.Integer(min=123))
def test_equal_with_same_max_and_no_min(self):
self.assertTrue(types.Integer(max=123) == types.Integer(max=123))
def test_equal_with_same_min_and_max(self):
t1 = types.Integer(min=1, max=123)
t2 = types.Integer(min=1, max=123)
self.assertTrue(t1 == t2)
def test_equal_with_same_choices(self):
t1 = types.Integer(choices=[80, 457])
t2 = types.Integer(choices=[457, 80])
self.assertTrue(t1 == t2)
def test_not_equal(self):
self.assertFalse(types.Integer(min=123) == types.Integer(min=456))
self.assertFalse(types.Integer(choices=[80, 457]) ==
types.Integer(choices=[80, 40]))
self.assertFalse(types.Integer(choices=[80, 457]) ==
types.Integer())
def test_not_equal_to_other_class(self):
self.assertFalse(types.Integer() == types.String())
def test_choices_with_min_max(self):
self.assertRaises(ValueError,
types.Integer,
min=100,
choices=[50, 60])
self.assertRaises(ValueError,
types.Integer,
max=10,
choices=[50, 60])
types.Integer(min=10, max=100, choices=[50, 60])
def test_min_greater_max(self):
self.assertRaises(ValueError,
types.Integer,
min=100, max=50)
self.assertRaises(ValueError,
types.Integer,
min=-50, max=-100)
self.assertRaises(ValueError,
types.Integer,
min=0, max=-50)
self.assertRaises(ValueError,
types.Integer,
min=50, max=0)
def test_with_max_and_min(self):
t = types.Integer(min=123, max=456)
self.assertRaises(ValueError, t, 122)
t(123)
t(300)
t(456)
self.assertRaises(ValueError, t, 0)
self.assertRaises(ValueError, t, 457)
def test_with_min_zero(self):
t = types.Integer(min=0, max=456)
self.assertRaises(ValueError, t, -1)
t(0)
t(123)
t(300)
t(456)
self.assertRaises(ValueError, t, -201)
self.assertRaises(ValueError, t, 457)
def test_with_max_zero(self):
t = types.Integer(min=-456, max=0)
self.assertRaises(ValueError, t, 1)
t(0)
t(-123)
t(-300)
t(-456)
self.assertRaises(ValueError, t, 201)
self.assertRaises(ValueError, t, -457)
def test_with_choices_list(self):
t = types.Integer(choices=[80, 457])
self.assertRaises(ValueError, t, 1)
self.assertRaises(ValueError, t, 200)
self.assertRaises(ValueError, t, -457)
t(80)
t(457)
def test_with_choices_tuple(self):
t = types.Integer(choices=(80, 457))
self.assertRaises(ValueError, t, 1)
self.assertRaises(ValueError, t, 200)
self.assertRaises(ValueError, t, -457)
t(80)
t(457)
class FloatTypeTests(TypeTestHelper, unittest.TestCase):
type = types.Float()
def test_decimal_format(self):
v = self.type_instance('123.456')
self.assertAlmostEqual(v, 123.456)
def test_decimal_format_negative_float(self):
v = self.type_instance('-123.456')
self.assertAlmostEqual(v, -123.456)
def test_exponential_format(self):
v = self.type_instance('123e-2')
self.assertAlmostEqual(v, 1.23)
def test_non_float_is_invalid(self):
self.assertInvalid('123,345')
self.assertInvalid('foo')
def test_repr(self):
self.assertEqual('Float', repr(types.Float()))
def test_repr_with_min(self):
t = types.Float(min=1.1)
self.assertEqual('Float(min=1.1)', repr(t))
def test_repr_with_max(self):
t = types.Float(max=2.2)
self.assertEqual('Float(max=2.2)', repr(t))
def test_repr_with_min_and_max(self):
t = types.Float(min=1.1, max=2.2)
self.assertEqual('Float(min=1.1, max=2.2)', repr(t))
t = types.Float(min=1.0, max=2)
self.assertEqual('Float(min=1, max=2)', repr(t))
t = types.Float(min=0, max=0)
self.assertEqual('Float(min=0, max=0)', repr(t))
def test_equal(self):
self.assertTrue(types.Float() == types.Float())
def test_equal_with_same_min_and_no_max(self):
self.assertTrue(types.Float(min=123.1) == types.Float(min=123.1))
def test_equal_with_same_max_and_no_min(self):
self.assertTrue(types.Float(max=123.1) == types.Float(max=123.1))
def test_not_equal(self):
self.assertFalse(types.Float(min=123.1) == types.Float(min=456.1))
self.assertFalse(types.Float(max=123.1) == types.Float(max=456.1))
self.assertFalse(types.Float(min=123.1) == types.Float(max=123.1))
self.assertFalse(types.Float(min=123.1, max=456.1) ==
types.Float(min=123.1, max=456.2))
def test_not_equal_to_other_class(self):
self.assertFalse(types.Float() == types.Integer())
def test_equal_with_same_min_and_max(self):
t1 = types.Float(min=1.1, max=2.2)
t2 = types.Float(min=1.1, max=2.2)
self.assertTrue(t1 == t2)
def test_min_greater_max(self):
self.assertRaises(ValueError,
types.Float,
min=100.1, max=50)
self.assertRaises(ValueError,
types.Float,
min=-50, max=-100.1)
self.assertRaises(ValueError,
types.Float,
min=0.1, max=-50.0)
self.assertRaises(ValueError,
types.Float,
min=50.0, max=0.0)
def test_with_max_and_min(self):
t = types.Float(min=123.45, max=678.9)
self.assertRaises(ValueError, t, 123)
self.assertRaises(ValueError, t, 123.1)
t(124.1)
t(300)
t(456.0)
self.assertRaises(ValueError, t, 0)
self.assertRaises(ValueError, t, 800.5)
def test_with_min_zero(self):
t = types.Float(min=0, max=456.1)
self.assertRaises(ValueError, t, -1)
t(0.0)
t(123.1)
t(300.2)
t(456.1)
self.assertRaises(ValueError, t, -201.0)
self.assertRaises(ValueError, t, 457.0)
def test_with_max_zero(self):
t = types.Float(min=-456.1, max=0)
self.assertRaises(ValueError, t, 1)
t(0.0)
t(-123.0)
t(-300.0)
t(-456.0)
self.assertRaises(ValueError, t, 201.0)
self.assertRaises(ValueError, t, -457.0)
class ListTypeTests(TypeTestHelper, unittest.TestCase):
type = types.List()
def test_empty_value(self):
self.assertConvertedValue('', [])
def test_single_value(self):
self.assertConvertedValue(' foo bar ',
['foo bar'])
def test_list_of_values(self):
self.assertConvertedValue(' foo bar, baz ',
['foo bar',
'baz'])
def test_list_of_values_containing_commas(self):
self.type_instance = types.List(types.String(quotes=True))
self.assertConvertedValue('foo,"bar, baz",bam',
['foo',
'bar, baz',
'bam'])
def test_list_of_lists(self):
self.type_instance = types.List(
types.List(types.String(), bounds=True)
)
self.assertConvertedValue('[foo],[bar, baz],[bam]',
[['foo'], ['bar', 'baz'], ['bam']])
def test_list_of_custom_type(self):
self.type_instance = types.List(types.Integer())
self.assertConvertedValue('1,2,3,5',
[1, 2, 3, 5])
def test_bounds_parsing(self):
self.type_instance = types.List(types.Integer(), bounds=True)
self.assertConvertedValue('[1,2,3]', [1, 2, 3])
def test_bounds_required(self):
self.type_instance = types.List(types.Integer(), bounds=True)
self.assertInvalid('1,2,3')
self.assertInvalid('[1,2,3')
self.assertInvalid('1,2,3]')
def test_repr(self):
t = types.List(types.Integer())
self.assertEqual('List of Integer', repr(t))
def test_equal(self):
self.assertTrue(types.List() == types.List())
def test_equal_with_equal_custom_item_types(self):
it1 = types.Integer()
it2 = types.Integer()
self.assertTrue(types.List(it1) == types.List(it2))
def test_not_equal_with_non_equal_custom_item_types(self):
it1 = types.Integer()
it2 = types.String()
self.assertFalse(it1 == it2)
self.assertFalse(types.List(it1) == types.List(it2))
def test_not_equal_to_other_class(self):
self.assertFalse(types.List() == types.Integer())
class DictTypeTests(TypeTestHelper, unittest.TestCase):
type = types.Dict()
def test_empty_value(self):
self.assertConvertedValue('', {})
def test_single_value(self):
self.assertConvertedValue(' foo: bar ',
{'foo': 'bar'})
def test_dict_of_values(self):
self.assertConvertedValue(' foo: bar, baz: 123 ',
{'foo': 'bar',
'baz': '123'})
def test_custom_value_type(self):
self.type_instance = types.Dict(types.Integer())
self.assertConvertedValue('foo:123, bar: 456',
{'foo': 123,
'bar': 456})
def test_dict_of_values_containing_commas(self):
self.type_instance = types.Dict(types.String(quotes=True))
self.assertConvertedValue('foo:"bar, baz",bam:quux',
{'foo': 'bar, baz',
'bam': 'quux'})
def test_dict_of_dicts(self):
self.type_instance = types.Dict(
types.Dict(types.String(), bounds=True)
)
self.assertConvertedValue('k1:{k1:v1,k2:v2},k2:{k3:v3}',
{'k1': {'k1': 'v1', 'k2': 'v2'},
'k2': {'k3': 'v3'}})
def test_bounds_parsing(self):
self.type_instance = types.Dict(types.String(), bounds=True)
self.assertConvertedValue('{foo:bar,baz:123}',
{'foo': 'bar',
'baz': '123'})
def test_bounds_required(self):
self.type_instance = types.Dict(types.String(), bounds=True)
self.assertInvalid('foo:bar,baz:123')
self.assertInvalid('{foo:bar,baz:123')
self.assertInvalid('foo:bar,baz:123}')
def test_no_mapping_produces_error(self):
self.assertInvalid('foo,bar')
def test_repr(self):
t = types.Dict(types.Integer())
self.assertEqual('Dict of Integer', repr(t))
def test_equal(self):
self.assertTrue(types.Dict() == types.Dict())
def test_equal_with_equal_custom_item_types(self):
it1 = types.Integer()
it2 = types.Integer()
self.assertTrue(types.Dict(it1) == types.Dict(it2))
def test_not_equal_with_non_equal_custom_item_types(self):
it1 = types.Integer()
it2 = types.String()
self.assertFalse(it1 == it2)
self.assertFalse(types.Dict(it1) == types.Dict(it2))
def test_not_equal_to_other_class(self):
self.assertFalse(types.Dict() == types.Integer())
class IPAddressTypeTests(TypeTestHelper, unittest.TestCase):
type = types.IPAddress()
def test_ipv4_address(self):
self.assertConvertedValue('192.168.0.1', '192.168.0.1')
def test_ipv6_address(self):
self.assertConvertedValue('abcd:ef::1', 'abcd:ef::1')
def test_strings(self):
self.assertInvalid('')
self.assertInvalid('foo')
def test_numbers(self):
self.assertInvalid(1)
self.assertInvalid(-1)
self.assertInvalid(3.14)
class IPv4AddressTypeTests(IPAddressTypeTests):
type = types.IPAddress(4)
def test_ipv6_address(self):
self.assertInvalid('abcd:ef::1')
class IPv6AddressTypeTests(IPAddressTypeTests):
type = types.IPAddress(6)
def test_ipv4_address(self):
self.assertInvalid('192.168.0.1')
class HostnameTypeTests(TypeTestHelper, unittest.TestCase):
type = types.Hostname()
def assertConvertedEqual(self, value):
self.assertConvertedValue(value, value)
def test_empty_hostname_fails(self):
self.assertInvalid('')
def test_should_return_same_hostname_if_valid(self):
self.assertConvertedEqual('foo.bar')
def test_trailing_quote_is_invalid(self):
self.assertInvalid('foo.bar"')
def test_repr(self):
self.assertEqual('Hostname', repr(types.Hostname()))
def test_equal(self):
self.assertEqual(types.Hostname(), types.Hostname())
def test_not_equal_to_other_class(self):
self.assertNotEqual(types.Hostname(), types.Integer())
self.assertNotEqual(types.Hostname(), types.String())
def test_invalid_characters(self):
self.assertInvalid('"host"')
self.assertInvalid("h'ost'")
self.assertInvalid("h'ost")
self.assertInvalid("h$ost")
self.assertInvalid("h%ost")
self.assertInvalid("host_01.co.uk")
self.assertInvalid("host;name=99")
self.assertInvalid('___site0.1001')
self.assertInvalid('_site01001')
self.assertInvalid("host..name")
self.assertInvalid(".host.name.com")
self.assertInvalid("no spaces")
def test_no_start_end_hyphens(self):
self.assertInvalid("-host.com")
self.assertInvalid("-hostname.com-")
self.assertInvalid("hostname.co.uk-")
def test_strip_trailing_dot(self):
self.assertConvertedValue('cell1.nova.site1.', 'cell1.nova.site1')
self.assertConvertedValue('cell1.', 'cell1')
def test_valid_hostname(self):
self.assertConvertedEqual('cell1.nova.site1')
self.assertConvertedEqual('site01001')
self.assertConvertedEqual('home-site-here.org.com')
self.assertConvertedEqual('192.168.0.1')
self.assertConvertedEqual('1.1.1')
self.assertConvertedEqual('localhost')
def test_max_segment_size(self):
self.assertConvertedEqual('host.%s.com' % ('x' * 63))
self.assertInvalid('host.%s.com' % ('x' * 64))
def test_max_hostname_size(self):
test_str = '.'.join('x'*31 for x in range(8))
self.assertEqual(255, len(test_str))
self.assertInvalid(test_str)
self.assertConvertedEqual(test_str[:-2])
class URITypeTests(TypeTestHelper, unittest.TestCase):
type = types.URI()
def test_uri(self):
self.assertConvertedValue('http://example.com', 'http://example.com')
self.assertInvalid('invalid') # it doesn't include a scheme
self.assertInvalid('http://') # it doesn't include an authority
def test_repr(self):
self.assertEqual('URI', repr(types.URI()))
def test_max_length(self):
self.type_instance = types.String(max_length=30)
self.assertInvalid('http://www.example.com/versions')
self.assertConvertedValue('http://www.example.com',
'http://www.example.com')
class PortTypeTests(TypeTestHelper, unittest.TestCase):
type = types.Port()
def test_port(self):
self.assertInvalid(-1)
self.assertInvalid(65536)
self.assertConvertedValue('80', 80)
self.assertConvertedValue('65535', 65535)
def test_repr(self):
self.assertEqual('Port(min=0, max=65535)', repr(types.Port()))
def test_repr_with_min(self):
t = types.Port(min=123)
self.assertEqual('Port(min=123, max=65535)', repr(t))
def test_repr_with_max(self):
t = types.Port(max=456)
self.assertEqual('Port(min=0, max=456)', repr(t))
def test_repr_with_min_and_max(self):
t = types.Port(min=123, max=456)
self.assertEqual('Port(min=123, max=456)', repr(t))
t = types.Port(min=0, max=0)
self.assertEqual('Port(min=0, max=0)', repr(t))
def test_repr_with_choices(self):
t = types.Port(choices=[80, 457])
self.assertEqual('Port(choices=[80, 457])', repr(t))
def test_choices(self):
t = types.Port(choices=[80, 457])
self.assertRaises(ValueError, t, 1)
self.assertRaises(ValueError, t, 200)
t(80)
t(457)
def test_invalid_choices(self):
self.assertRaises(ValueError, types.Port, choices=[-1, 457])
self.assertRaises(ValueError, types.Port, choices=[1, 2, 3, 65536])
def test_equal(self):
self.assertTrue(types.Port() == types.Port())
def test_equal_with_same_min_and_no_max(self):
self.assertTrue(types.Port(min=123) == types.Port(min=123))
def test_equal_with_same_max_and_no_min(self):
self.assertTrue(types.Port(max=123) == types.Port(max=123))
def test_equal_with_same_min_and_max(self):
t1 = types.Port(min=1, max=123)
t2 = types.Port(min=1, max=123)
self.assertTrue(t1 == t2)
def test_equal_with_same_choices(self):
t1 = types.Port(choices=[80, 457])
t2 = types.Port(choices=[457, 80])
self.assertTrue(t1 == t2)
def test_not_equal(self):
self.assertFalse(types.Port(min=123) == types.Port(min=456))
self.assertFalse(types.Port(choices=[80, 457]) ==
types.Port(choices=[80, 40]))
self.assertFalse(types.Port(choices=[80, 457]) ==
types.Port())
def test_not_equal_to_other_class(self):
self.assertFalse(types.Port() == types.Integer())
def test_choices_with_min_max(self):
self.assertRaises(ValueError,
types.Port,
min=100,
choices=[50, 60])
self.assertRaises(ValueError,
types.Port,
max=10,
choices=[50, 60])
types.Port(min=10, max=100, choices=[50, 60])
def test_min_greater_max(self):
self.assertRaises(ValueError,
types.Port,
min=100, max=50)
self.assertRaises(ValueError,
types.Port,
min=-50, max=-100)
self.assertRaises(ValueError,
types.Port,
min=0, max=-50)
self.assertRaises(ValueError,
types.Port,
min=50, max=0)
def test_illegal_min(self):
self.assertRaises(ValueError,
types.Port,
min=-1, max=50)
self.assertRaises(ValueError,
types.Port,
min=-50)
def test_illegal_max(self):
self.assertRaises(ValueError,
types.Port,
min=100, max=65537)
self.assertRaises(ValueError,
types.Port,
max=100000)
def test_with_max_and_min(self):
t = types.Port(min=123, max=456)
self.assertRaises(ValueError, t, 122)
t(123)
t(300)
t(456)
self.assertRaises(ValueError, t, 0)
self.assertRaises(ValueError, t, 457)
def test_with_min_zero(self):
t = types.Port(min=0, max=456)
self.assertRaises(ValueError, t, -1)
t(0)
t(123)
t(300)
t(456)
self.assertRaises(ValueError, t, -201)
self.assertRaises(ValueError, t, 457)
def test_with_max_zero(self):
t = types.Port(max=0)
self.assertRaises(ValueError, t, 1)
t(0)
def test_with_choices_list(self):
t = types.Port(choices=[80, 457])
self.assertRaises(ValueError, t, 1)
self.assertRaises(ValueError, t, 200)
self.assertRaises(ValueError, t, -457)
t(80)
t(457)
def test_with_choices_tuple(self):
t = types.Port(choices=(80, 457))
self.assertRaises(ValueError, t, 1)
self.assertRaises(ValueError, t, 200)
self.assertRaises(ValueError, t, -457)
t(80)
t(457)

View File

@ -1,19 +0,0 @@
# Copyright 2012 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_config import cfg
CONF = cfg.CONF
CONF.register_opt(cfg.StrOpt('foo'), group='bar')

View File

@ -1,19 +0,0 @@
# Copyright (c) 2013 Intel Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_config import cfg
CONF = cfg.CONF
CONF.register_opt(cfg.StrOpt('baz'), group='qux')

View File

@ -1,19 +0,0 @@
# Copyright 2012 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_config import cfg
CONF = cfg.CONF
CONF.register_opt(cfg.StrOpt('blaa'))

View File

@ -1,21 +0,0 @@
# Copyright 2012 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_config import cfg
CONF = cfg.CONF
opt = cfg.StrOpt('baa')
CONF.register_opt(opt, group='fbaar')

View File

@ -1,21 +0,0 @@
# Copyright 2013 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_config import cfg
CONF = cfg.CONF
opt = cfg.StrOpt('foo')
CONF.register_opt(opt, group='fbar')

View File

@ -1,21 +0,0 @@
# Copyright 2012 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_config import cfg
CONF = cfg.CONF
opt = cfg.StrOpt('fblaa')
CONF.register_opt(opt)

View File

@ -1,738 +0,0 @@
# Copyright 2013 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Type conversion and validation classes for configuration options.
Use these classes as values for the `type` argument to
:class:`oslo_config.cfg.Opt` and its subclasses.
.. versionadded:: 1.3
"""
import operator
import re
import warnings
import abc
import netaddr
import rfc3986
import six
@six.add_metaclass(abc.ABCMeta)
class ConfigType(object):
def __init__(self, type_name='unknown type'):
self.type_name = type_name
NONE_DEFAULT = '<None>'
def format_defaults(self, default, sample_default=None):
"""Return a list of formatted default values.
"""
if sample_default is not None:
default_str = sample_default
elif default is None:
default_str = self.NONE_DEFAULT
else:
default_str = self._formatter(default)
return [default_str]
def quote_trailing_and_leading_space(self, str_val):
if not isinstance(str_val, six.string_types):
warnings.warn('converting \'%s\' to a string' % str_val)
str_val = six.text_type(str_val)
if str_val.strip() != str_val:
return '"%s"' % str_val
return str_val
@abc.abstractmethod
def _formatter(self, value):
pass
class String(ConfigType):
"""String type.
String values do not get transformed and are returned as str objects.
:param choices: Optional sequence of valid values. Mutually
exclusive with 'regex'.
:param quotes: If True and string is enclosed with single or double
quotes, will strip those quotes. Will signal error if
string have quote at the beginning and no quote at
the end. Turned off by default. Useful if used with
container types like List.
:param regex: Optional regular expression (string or compiled
regex) that the value must match on an unanchored
search. Mutually exclusive with 'choices'.
:param ignore_case: If True case differences (uppercase vs. lowercase)
between 'choices' or 'regex' will be ignored;
defaults to False.
:param max_length: Optional integer. If a positive value is specified,
a maximum length of an option value must be less than
or equal to this parameter. Otherwise no length check
will be done.
:param type_name: Type name to be used in the sample config file.
.. versionchanged:: 2.1
Added *regex* parameter.
.. versionchanged:: 2.5
Added *ignore_case* parameter.
.. versionchanged:: 2.7
Added *max_length* parameter.
Added *type_name* parameter.
"""
def __init__(self, choices=None, quotes=False, regex=None,
ignore_case=False, max_length=None,
type_name='string value'):
super(String, self).__init__(type_name=type_name)
if choices and regex:
raise ValueError("'choices' and 'regex' cannot both be specified")
self.ignore_case = ignore_case
self.quotes = quotes
self.max_length = max_length or 0
self.choices = choices
self.lower_case_choices = None
if self.choices is not None and self.ignore_case:
self.lower_case_choices = [c.lower() for c in choices]
self.regex = regex
if self.regex is not None:
re_flags = re.IGNORECASE if self.ignore_case else 0
# Check if regex is a string or an already compiled regex
if isinstance(regex, six.string_types):
self.regex = re.compile(regex, re_flags)
else:
self.regex = re.compile(regex.pattern, re_flags | regex.flags)
def __call__(self, value):
value = str(value)
if self.quotes and value:
if value[0] in "\"'":
if value[-1] != value[0]:
raise ValueError('Non-closed quote: %s' % value)
value = value[1:-1]
if self.max_length > 0 and len(value) > self.max_length:
raise ValueError("Value '%s' exceeds maximum length %d" %
(value, self.max_length))
if self.regex and not self.regex.search(value):
raise ValueError("Value %r doesn't match regex %r" %
(value, self.regex.pattern))
if self.choices is None:
return value
# Check for case insensitive
processed_value, choices = ((value.lower(), self.lower_case_choices)
if self.ignore_case else
(value, self.choices))
if processed_value in choices:
return value
raise ValueError(
'Valid values are [%s], but found %s' % (
', '.join([str(v) for v in self.choices]),
repr(value)))
def __repr__(self):
details = []
if self.choices:
details.append("choices=%r" % self.choices)
if self.regex:
details.append("regex=%r" % self.regex.pattern)
if details:
return "String(%s)" % ",".join(details)
return 'String'
def __eq__(self, other):
return (
(self.__class__ == other.__class__) and
(set(self.choices) == set(other.choices) if
self.choices and other.choices else
self.choices == other.choices) and
(self.quotes == other.quotes) and
(self.regex == other.regex)
)
def _formatter(self, value):
return self.quote_trailing_and_leading_space(value)
class MultiString(String):
"""Multi-valued string."""
def __init__(self, type_name='multi valued'):
super(MultiString, self).__init__(type_name=type_name)
NONE_DEFAULT = ['']
def format_defaults(self, default, sample_default=None):
"""Return a list of formatted default values.
"""
if sample_default is not None:
default_list = self._formatter(sample_default)
elif not default:
default_list = self.NONE_DEFAULT
else:
default_list = self._formatter(default)
return default_list
def _formatter(self, value):
return [self.quote_trailing_and_leading_space(v) for v in value]
class Boolean(ConfigType):
"""Boolean type.
Values are case insensitive and can be set using
1/0, yes/no, true/false or on/off.
:param type_name: Type name to be used in the sample config file.
.. versionchanged:: 2.7
Added *type_name* parameter.
"""
TRUE_VALUES = ['true', '1', 'on', 'yes']
FALSE_VALUES = ['false', '0', 'off', 'no']
def __init__(self, type_name='boolean value'):
super(Boolean, self).__init__(type_name=type_name)
def __call__(self, value):
if isinstance(value, bool):
return value
s = value.lower()
if s in self.TRUE_VALUES:
return True
elif s in self.FALSE_VALUES:
return False
else:
raise ValueError('Unexpected boolean value %r' % value)
def __repr__(self):
return 'Boolean'
def __eq__(self, other):
return self.__class__ == other.__class__
def _formatter(self, value):
return str(value).lower()
class Number(ConfigType):
"""Number class, base for Integer and Float.
:param min: Optional check that value is greater than or equal to min.
:param max: Optional check that value is less than or equal to max.
:param type_name: Type name to be used in the sample config file.
:param choices: Optional sequence of valid values.
:param num_type: the type of number used for casting (i.e int, float)
.. versionadded:: 3.14
"""
def __init__(self, num_type, type_name,
min=None, max=None, choices=None):
super(Number, self).__init__(type_name=type_name)
if min is not None and max is not None and max < min:
raise ValueError('Max value is less than min value')
invalid_choices = [c for c in choices or []
if (min is not None and min > c)
or (max is not None and max < c)]
if invalid_choices:
raise ValueError("Choices %s are out of bounds [%s..%s]"
% (invalid_choices, min, max))
self.min = min
self.max = max
self.choices = choices
self.num_type = num_type
def __call__(self, value):
if not isinstance(value, self.num_type):
s = str(value).strip()
if s == '':
return None
value = self.num_type(value)
if self.choices is None:
if self.min is not None and value < self.min:
raise ValueError('Should be greater than or equal to %g' %
self.min)
if self.max is not None and value > self.max:
raise ValueError('Should be less than or equal to %g' %
self.max)
else:
if value not in self.choices:
raise ValueError('Valid values are %r, but found %g' % (
self.choices, value))
return value
def __repr__(self):
props = []
if self.choices is not None:
props.append("choices=%r" % (self.choices,))
else:
if self.min is not None:
props.append('min=%g' % self.min)
if self.max is not None:
props.append('max=%g' % self.max)
if props:
return self.__class__.__name__ + '(%s)' % ', '.join(props)
return self.__class__.__name__
def __eq__(self, other):
return (
(self.__class__ == other.__class__) and
(self.min == other.min) and
(self.max == other.max) and
(set(self.choices) == set(other.choices) if
self.choices and other.choices else
self.choices == other.choices)
)
def _formatter(self, value):
return str(value)
class Integer(Number):
"""Integer type.
Converts value to an integer optionally doing range checking.
If value is whitespace or empty string will return None.
:param min: Optional check that value is greater than or equal to min.
:param max: Optional check that value is less than or equal to max.
:param type_name: Type name to be used in the sample config file.
:param choices: Optional sequence of valid values.
.. versionchanged:: 2.4
The class now honors zero for *min* and *max* parameters.
.. versionchanged:: 2.7
Added *type_name* parameter.
.. versionchanged:: 3.2
Added *choices* parameter.
.. versionchanged:: 3.16
*choices* is no longer mutually exclusive with *min*/*max*. If those are
supplied, all choices are verified to be within the range.
"""
def __init__(self, min=None, max=None, type_name='integer value',
choices=None):
super(Integer, self).__init__(int, type_name, min=min, max=max,
choices=choices)
class Float(Number):
"""Float type.
:param type_name: Type name to be used in the sample config file.
:param min: Optional check that value is greater than or equal to min.
:param max: Optional check that value is less than or equal to max.
.. versionchanged:: 2.7
Added *type_name* parameter.
.. versionchanged:: 3.14
Added *min* and *max* parameters. If *choices* are also supplied, they
must be within the range.
"""
def __init__(self, min=None, max=None, type_name='floating point value'):
super(Float, self).__init__(float, type_name, min=min, max=max)
class Port(Integer):
"""Port type
Represents a L4 Port.
:param type_name: Type name to be used in the sample config file.
:param choices: Optional sequence of valid values.
:param min: Optional check that value is greater than or equal to min.
:param max: Optional check that value is less than or equal to max.
.. versionadded:: 3.16
"""
PORT_MIN = 0
PORT_MAX = 65535
def __init__(self, min=None, max=None, type_name='port', choices=None):
min = self.PORT_MIN if min is None else min
max = self.PORT_MAX if max is None else max
if min < self.PORT_MIN:
raise ValueError('Min value cannot be less than %(min)d',
{'min': self.PORT_MIN})
if max > self.PORT_MAX:
raise ValueError('Max value cannot be more than %(max)d',
{'max': self.PORT_MAX})
super(Port, self).__init__(min=min, max=max, type_name=type_name,
choices=choices)
class List(ConfigType):
"""List type.
Represent values of other (item) type, separated by commas.
The resulting value is a list containing those values.
List doesn't know if item type can also contain commas. To workaround this
it tries the following: if the next part fails item validation, it appends
comma and next item until validation succeeds or there is no parts left.
In the later case it will signal validation error.
:param item_type: type of list items
:param bounds: if True, value should be inside "[" and "]" pair
:param type_name: Type name to be used in the sample config file.
.. versionchanged:: 2.7
Added *type_name* parameter.
"""
def __init__(self, item_type=None, bounds=False, type_name='list value'):
super(List, self).__init__(type_name=type_name)
if item_type is None:
item_type = String()
if not callable(item_type):
raise TypeError('item_type must be callable')
self.item_type = item_type
self.bounds = bounds
def __call__(self, value):
if isinstance(value, list):
return value
result = []
s = value.strip()
if self.bounds:
if not s.startswith('['):
raise ValueError('Value should start with "["')
if not s.endswith(']'):
raise ValueError('Value should end with "]"')
s = s[1:-1]
if s == '':
return result
values = s.split(',')
while values:
value = values.pop(0)
while True:
first_error = None
try:
validated_value = self.item_type(value.strip())
break
except ValueError as e:
if not first_error:
first_error = e
if len(values) == 0:
raise first_error
value += ',' + values.pop(0)
result.append(validated_value)
return result
def __repr__(self):
return 'List of %s' % repr(self.item_type)
def __eq__(self, other):
return (
(self.__class__ == other.__class__) and
(self.item_type == other.item_type)
)
def _formatter(self, value):
if isinstance(value, six.string_types):
return value
return ','.join(value)
class Dict(ConfigType):
"""Dictionary type.
Dictionary type values are key:value pairs separated by commas.
The resulting value is a dictionary of these key/value pairs.
Type of dictionary key is always string, but dictionary value
type can be customized.
:param value_type: type of values in dictionary
:param bounds: if True, value should be inside "{" and "}" pair
:param type_name: Type name to be used in the sample config file.
.. versionchanged:: 2.7
Added *type_name* parameter.
"""
def __init__(self, value_type=None, bounds=False, type_name='dict value'):
super(Dict, self).__init__(type_name=type_name)
if value_type is None:
value_type = String()
if not callable(value_type):
raise TypeError('value_type must be callable')
self.value_type = value_type
self.bounds = bounds
def __call__(self, value):
if isinstance(value, dict):
return value
result = {}
s = value.strip()
if self.bounds:
if not s.startswith('{'):
raise ValueError('Value should start with "{"')
if not s.endswith('}'):
raise ValueError('Value should end with "}"')
s = s[1:-1]
if s == '':
return result
pairs = s.split(',')
while pairs:
pair = pairs.pop(0)
while True:
first_error = None
try:
key_value = pair.split(':', 1)
if len(key_value) < 2:
raise ValueError('Value should be NAME:VALUE pairs '
'separated by ","')
key, value = key_value
key = key.strip()
value = value.strip()
value = self.value_type(value)
break
except ValueError as e:
if not first_error:
first_error = e
if not pairs:
raise first_error
pair += ',' + pairs.pop(0)
if key == '':
raise ValueError('Key name should not be empty')
if key in result:
raise ValueError('Duplicate key %s' % key)
result[key] = value
return result
def __repr__(self):
return 'Dict of %s' % repr(self.value_type)
def __eq__(self, other):
return (
(self.__class__ == other.__class__) and
(self.value_type == other.value_type)
)
def _formatter(self, value):
sorted_items = sorted(value.items(),
key=operator.itemgetter(0))
return ','.join(['%s:%s' % i for i in sorted_items])
class IPAddress(ConfigType):
"""IP address type
Represents either ipv4 or ipv6. Without specifying version parameter both
versions are checked
:param version: defines which version should be explicitly checked (4 or 6)
:param type_name: Type name to be used in the sample config file.
.. versionchanged:: 2.7
Added *type_name* parameter.
"""
def __init__(self, version=None, type_name='IP address value'):
super(IPAddress, self).__init__(type_name=type_name)
version_checkers = {
None: self._check_both_versions,
4: self._check_ipv4,
6: self._check_ipv6
}
self.version_checker = version_checkers.get(version)
if self.version_checker is None:
raise TypeError("%s is not a valid IP version." % version)
def __call__(self, value):
value = str(value)
if not value:
raise ValueError("IP address cannot be an empty string")
self.version_checker(value)
return value
def __repr__(self):
return "IPAddress"
def __eq__(self, other):
return self.__class__ == other.__class__
def _check_ipv4(self, address):
if not netaddr.valid_ipv4(address, netaddr.core.INET_PTON):
raise ValueError("%s is not an IPv4 address" % address)
def _check_ipv6(self, address):
if not netaddr.valid_ipv6(address, netaddr.core.INET_PTON):
raise ValueError("%s is not an IPv6 address" % address)
def _check_both_versions(self, address):
if not (netaddr.valid_ipv4(address, netaddr.core.INET_PTON) or
netaddr.valid_ipv6(address, netaddr.core.INET_PTON)):
raise ValueError("%s is not IPv4 or IPv6 address" % address)
def _formatter(self, value):
return value
class Hostname(ConfigType):
"""Hostname type.
A hostname refers to a valid DNS or hostname. It must not be longer than
253 characters, have a segment greater than 63 characters, nor start or
end with a hyphen.
:param type_name: Type name to be used in the sample config file.
"""
def __init__(self, type_name='hostname value'):
super(Hostname, self).__init__(type_name=type_name)
def __call__(self, value):
"""Check hostname is valid.
Ensures that each segment
- Contains at least one character and a maximum of 63 characters
- Consists only of allowed characters: letters (A-Z and a-z),
digits (0-9), and hyphen (-)
- Does not begin or end with a hyphen
- maximum total length of 253 characters
For more details , please see: http://tools.ietf.org/html/rfc1035
"""
if len(value) == 0:
raise ValueError("Cannot have an empty hostname")
if len(value) > 253:
raise ValueError("hostname is greater than 253 characters: %s"
% value)
if value.endswith("."):
value = value[:-1]
allowed = re.compile("(?!-)[A-Z0-9-]{1,63}(?<!-)$", re.IGNORECASE)
if any((not allowed.match(x)) for x in value.split(".")):
raise ValueError("%s is an invalid hostname" % value)
return value
def __repr__(self):
return 'Hostname'
def __eq__(self, other):
return self.__class__ == other.__class__
def _formatter(self, value):
return value
class URI(ConfigType):
"""URI type
Represents URI. Value will be validated as RFC 3986.
:param type_name: Type name to be used in the sample config file.
:param max_length: Optional integer. If a positive value is specified,
a maximum length of an option value must be less than
or equal to this parameter. Otherwise no length check
will be done.
.. versionchanged:: 3.14
Added *max_length* parameter.
"""
def __init__(self, max_length=None, type_name='uri value'):
super(URI, self).__init__(type_name=type_name)
self.max_length = max_length
def __call__(self, value):
if not rfc3986.is_valid_uri(value, require_scheme=True,
require_authority=True):
raise ValueError('invalid URI: %r' % value)
if self.max_length is not None and len(value) > self.max_length:
raise ValueError("Value '%s' exceeds maximum length %d" %
(value, self.max_length))
self.value = value
return value
def __repr__(self):
return 'URI'
def __eq__(self, other):
return (
(self.__class__ == other.__class__) and
(self.value == other.value)
)
def _formatter(self, value):
return value

View File

@ -1,18 +0,0 @@
# Copyright 2016 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
import pbr.version
version_info = pbr.version.VersionInfo('oslo_config')

View File

@ -1,3 +0,0 @@
---
features:
- Added minimum and maximum value limits to FloatOpt.

View File

@ -1,5 +0,0 @@
---
features:
- Integer and Float now support *min*, *max* and *choices*. Choices must
respect *min* and *max* (if provided).
- Added Port type as an Integer in the closed interval [0, 65535].

View File

@ -1,3 +0,0 @@
---
other:
- Start using reno for managing release notes.

View File

@ -1,277 +0,0 @@
# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# oslo.config Release Notes documentation build configuration file, created by
# sphinx-quickstart on Tue Nov 3 17:40:50 2015.
#
# This file is execfile()d with the current directory set to its
# containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
# sys.path.insert(0, os.path.abspath('.'))
# -- General configuration ------------------------------------------------
# If your documentation needs a minimal Sphinx version, state it here.
# needs_sphinx = '1.0'
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'oslosphinx',
'reno.sphinxext',
]
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# The suffix of source filenames.
source_suffix = '.rst'
# The encoding of source files.
# source_encoding = 'utf-8-sig'
# The master toctree document.
master_doc = 'index'
# General information about the project.
project = u'oslo.config Release Notes'
copyright = u'2016, oslo.config Developers'
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
from oslo_config.version import version_info as oslo_config_version
# The full version, including alpha/beta/rc tags.
release = oslo_config_version.version_string_with_vcs()
# The short X.Y version.
version = oslo_config_version.canonical_version_string()
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
# language = None
# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
# today = ''
# Else, today_fmt is used as the format for a strftime call.
# today_fmt = '%B %d, %Y'
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = []
# The reST default role (used for this markup: `text`) to use for all
# documents.
# default_role = None
# If true, '()' will be appended to :func: etc. cross-reference text.
# add_function_parentheses = True
# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
# add_module_names = True
# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
# show_authors = False
# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'
# A list of ignored prefixes for module index sorting.
# modindex_common_prefix = []
# If true, keep warnings as "system message" paragraphs in the built documents.
# keep_warnings = False
# -- Options for HTML output ----------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
html_theme = 'default'
# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
# html_theme_options = {}
# Add any paths that contain custom themes here, relative to this directory.
# html_theme_path = []
# The name for this set of Sphinx documents. If None, it defaults to
# "<project> v<release> documentation".
# html_title = None
# A shorter title for the navigation bar. Default is the same as html_title.
# html_short_title = None
# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
# html_logo = None
# The name of an image file (within the static path) to use as favicon of the
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
# html_favicon = None
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
# Add any extra paths that contain custom files (such as robots.txt or
# .htaccess) here, relative to this directory. These files are copied
# directly to the root of the documentation.
# html_extra_path = []
# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
# html_last_updated_fmt = '%b %d, %Y'
# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
# html_use_smartypants = True
# Custom sidebar templates, maps document names to template names.
# html_sidebars = {}
# Additional templates that should be rendered to pages, maps page names to
# template names.
# html_additional_pages = {}
# If false, no module index is generated.
# html_domain_indices = True
# If false, no index is generated.
# html_use_index = True
# If true, the index is split into individual pages for each letter.
# html_split_index = False
# If true, links to the reST sources are added to the pages.
# html_show_sourcelink = True
# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
# html_show_sphinx = True
# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
# html_show_copyright = True
# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it. The value of this option must be the
# base URL from which the finished HTML is served.
# html_use_opensearch = ''
# This is the file name suffix for HTML files (e.g. ".xhtml").
# html_file_suffix = None
# Output file base name for HTML help builder.
htmlhelp_basename = 'oslo.configReleaseNotesdoc'
# -- Options for LaTeX output ---------------------------------------------
latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
# 'papersize': 'letterpaper',
# The font size ('10pt', '11pt' or '12pt').
# 'pointsize': '10pt',
# Additional stuff for the LaTeX preamble.
# 'preamble': '',
}
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
('index', 'oslo.configReleaseNotes.tex',
u'oslo.config Release Notes Documentation',
u'oslo.config Developers', 'manual'),
]
# The name of an image file (relative to this directory) to place at the top of
# the title page.
# latex_logo = None
# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
# latex_use_parts = False
# If true, show page references after internal links.
# latex_show_pagerefs = False
# If true, show URL addresses after external links.
# latex_show_urls = False
# Documents to append as an appendix to all manuals.
# latex_appendices = []
# If false, no module index is generated.
# latex_domain_indices = True
# -- Options for manual page output ---------------------------------------
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
('index', 'oslo.configreleasenotes',
u'oslo.config Release Notes Documentation',
[u'oslo.config Developers'], 1)
]
# If true, show URL addresses after external links.
# man_show_urls = False
# -- Options for Texinfo output -------------------------------------------
# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
('index', 'oslo.configReleaseNotes',
u'oslo.config Release Notes Documentation',
u'oslo.config Developers', 'oslo.configReleaseNotes',
'An OpenStack library for parsing configuration options from the command'
' line and configuration files.',
'Miscellaneous'),
]
# Documents to append as an appendix to all manuals.
# texinfo_appendices = []
# If false, no module index is generated.
# texinfo_domain_indices = True
# How to display URL addresses: 'footnote', 'no', or 'inline'.
# texinfo_show_urls = 'footnote'
# If true, do not generate a @detailmenu in the "Top" node's menu.
# texinfo_no_detailmenu = False

View File

@ -1,10 +0,0 @@
===========================
oslo.config Release Notes
===========================
.. toctree::
:maxdepth: 1
unreleased
liberty
mitaka

View File

@ -1,6 +0,0 @@
============================
Liberty Series Release Notes
============================
.. release-notes::
:branch: origin/stable/liberty

View File

@ -1,6 +0,0 @@
===================================
Mitaka Series Release Notes
===================================
.. release-notes::
:branch: origin/stable/mitaka

View File

@ -1,5 +0,0 @@
==========================
Unreleased Release Notes
==========================
.. release-notes::

View File

@ -1,10 +0,0 @@
# The order of packages is significant, because pip processes them in the order
# of appearance. Changing the order has an impact on the overall integration
# process, which may cause wedges in the gate later.
debtcollector>=1.2.0 # Apache-2.0
netaddr!=0.7.16,>=0.7.12 # BSD
six>=1.9.0 # MIT
stevedore>=1.16.0 # Apache-2.0
oslo.i18n>=2.1.0 # Apache-2.0
rfc3986>=0.2.0 # Apache-2.0

View File

@ -1,49 +0,0 @@
[metadata]
name = oslo.config
author = OpenStack
author-email = openstack-dev@lists.openstack.org
summary = Oslo Configuration API
description-file =
README.rst
home-page = https://wiki.openstack.org/wiki/Oslo#oslo.config
classifier =
Development Status :: 5 - Production/Stable
Environment :: OpenStack
Intended Audience :: Developers
Intended Audience :: Information Technology
License :: OSI Approved :: Apache Software License
Operating System :: OS Independent
Programming Language :: Python
Programming Language :: Python :: 2
Programming Language :: Python :: 2.7
Programming Language :: Python :: 3
Programming Language :: Python :: 3.4
Programming Language :: Python :: 3.5
[files]
packages =
oslo_config
[global]
setup-hooks =
pbr.hooks.setup_hook
[entry_points]
console_scripts =
oslo-config-generator = oslo_config.generator:main
oslo.config.opts =
oslo.config = oslo_config._list_opts:list_opts
[build_sphinx]
source-dir = doc/source
build-dir = doc/build
all_files = 1
[upload_sphinx]
upload-dir = doc/build/html
[pbr]
warnerrors = True
[wheel]
universal = 1

View File

@ -1,29 +0,0 @@
# Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# THIS FILE IS MANAGED BY THE GLOBAL REQUIREMENTS REPO - DO NOT EDIT
import setuptools
# In python < 2.7.4, a lazy loading of package `pbr` will break
# setuptools if some other modules registered functions in `atexit`.
# solution from: http://bugs.python.org/issue15881#msg170215
try:
import multiprocessing # noqa
except ImportError:
pass
setuptools.setup(
setup_requires=['pbr>=1.8'],
pbr=True)

View File

@ -1,31 +0,0 @@
# The order of packages is significant, because pip processes them in the order
# of appearance. Changing the order has an impact on the overall integration
# process, which may cause wedges in the gate later.
hacking<0.11,>=0.10.0
fixtures>=3.0.0 # Apache-2.0/BSD
python-subunit>=0.0.18 # Apache-2.0/BSD
testrepository>=0.0.18 # Apache-2.0/BSD
testscenarios>=0.4 # Apache-2.0/BSD
testtools>=1.4.0 # MIT
oslotest>=1.10.0 # Apache-2.0
# when we can require tox>= 1.4, this can go into tox.ini:
# [testenv:cover]
# deps = {[testenv]deps} coverage
coverage>=3.6 # Apache-2.0
# this is required for the docs build jobs
sphinx!=1.3b1,<1.3,>=1.2.1 # BSD
oslosphinx!=3.4.0,>=2.5.0 # Apache-2.0
reno>=1.8.0 # Apache2
# Required only for tests
oslo.i18n>=2.1.0 # Apache-2.0
# mocking framework
mock>=2.0 # BSD
# Bandit security code scanner
bandit>=1.0.1 # Apache-2.0

49
tox.ini
View File

@ -1,49 +0,0 @@
[tox]
distribute = False
envlist = py35,py34,py27,pep8
[testenv]
deps = -r{toxinidir}/test-requirements.txt
commands =
python setup.py test --coverage --coverage-package-name=oslo_config --slowest --testr-args='{posargs}'
coverage report --show-missing
[testenv:pep8]
commands =
flake8
# Run security linter
bandit -r oslo_config -x tests -n5
[testenv:cover]
setenv = VIRTUAL_ENV={envdir}
commands =
python setup.py test --coverage --coverage-package-name=oslo_config --testr-args='{posargs}'
coverage report --show-missing
[testenv:venv]
commands = {posargs}
[testenv:docs]
commands = python setup.py build_sphinx
[testenv:bandit]
deps = -r{toxinidir}/test-requirements.txt
commands = bandit -r oslo_config -x tests -n5
[flake8]
show-source = True
exclude = .tox,dist,doc,*.egg,build
[testenv:pip-missing-reqs]
# do not install test-requirements as that will pollute the virtualenv for
# determining missing packages
# this also means that pip-missing-reqs must be installed separately, outside
# of the requirements.txt files
deps = pip_missing_reqs
commands = pip-missing-reqs -d --ignore-module=oslo_config* --ignore-module=pkg_resources --ignore-file=oslo_config/tests/* --ignore-file=tests/ oslo_config
[testenv:releasenotes]
commands = sphinx-build -a -E -W -d releasenotes/build/doctrees -b html releasenotes/source releasenotes/build/html
[hacking]
import_exceptions = oslo_config._i18n