././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1724318217.2029986 oslo.config-9.6.0/0000775000175000017500000000000000000000000013771 5ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/.coveragerc0000664000175000017500000000016100000000000016110 0ustar00zuulzuul00000000000000[run] branch = True source = oslo_config omit = oslo_config/tests/* [report] ignore_errors = True precision = 2 ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/.pre-commit-config.yaml0000664000175000017500000000252300000000000020254 0ustar00zuulzuul00000000000000# We from the Oslo project decided to pin repos based on the # commit hash instead of the version tag to prevend arbitrary # code from running in developer's machines. To update to a # newer version, run `pre-commit autoupdate` and then replace # the newer versions with their commit hash. default_language_version: python: python3 repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: 9136088a246768144165fcc3ecc3d31bb686920a # v3.3.0 hooks: - id: trailing-whitespace # Replaces or checks mixed line ending - id: mixed-line-ending args: ['--fix', 'lf'] exclude: '.*\.(svg)$' # Forbid files which have a UTF-8 byte-order marker - id: check-byte-order-marker # Checks that non-binary executables have a proper shebang - id: check-executables-have-shebangs # Check for files that contain merge conflict strings. - id: check-merge-conflict # Check for debugger imports and py37+ breakpoint() # calls in python source - id: debug-statements - id: check-yaml files: .*\.(yaml|yml)$ - repo: local hooks: - id: flake8 name: flake8 additional_dependencies: - hacking>=6.1.0,<6.2.0 language: python entry: flake8 files: '^.*\.py$' exclude: '^(doc|releasenotes|tools)/.*$' ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/.stestr.conf0000664000175000017500000000004100000000000016235 0ustar00zuulzuul00000000000000[DEFAULT] test_path=. top_dir=./ ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/.zuul.yaml0000664000175000017500000000033300000000000015731 0ustar00zuulzuul00000000000000- project: templates: - check-requirements - lib-forward-testing-python3 - openstack-python3-jobs - periodic-stable-jobs - publish-openstack-docs-pti - release-notes-jobs-python3 ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318217.0 oslo.config-9.6.0/AUTHORS0000664000175000017500000001761700000000000015055 0ustar00zuulzuul00000000000000Adam Harwell Adit Sarfaty Akihiro Motoki Akihiro Motoki Alex Gaynor Alexis Lee Alexis Lee Alfredo Moralejo Andreas Jaeger Andreas Jaeger Andrey Volkov Andrii Ostapenko Anthony Young Atsushi SAKAI BaoLiang Cui Ben Nemec Brant Knudson Brian Rosmaita Brian Stajkowski Brian Waldon Chandan Kumar ChangBo Guo(gcb) Chaozhe.Chen Chen Chris Dent Chris Hoge Chris Hoge Chris St. Pierre Christian Berendt Chuck Short Chuck Short Chung Chih, Hung Corey Bryant Cyril Roelandt Cyril Roelandt Daisuke Fujita Dan Prince Daniel Bengtsson Davanum Srinivas Davanum Srinivas David Ripton David Stanek David Vallee Delisle Dharini Chandrasekar Dirk Mueller Dolph Mathews Doug Chivers Doug Hellmann Doug Hellmann Edan David Einst Crazy Elena Ezhova Eoghan Glynn Eric Brown Eric Fried Eric Fried Eric Guo Eric Harney Fengqian.Gao Flaper Fesp Flavio Percoco Gary Kotton Gevorg Davoian Ghanshyam Ghanshyam Mann Giampaolo Lauria Hemanth Makkapati Hervé Beraud Ian Wienand Ihar Hrachyshka Ilya Shakhat Ilya Shakhat Jakub Libosvar James Carey James E. Blair James Page Jamie Lennox Jamie Lennox Jason Kölker Javeme Javier Pena Jay S. Bryant Jeremy Stanley Joe Gordon Joe Heck Johannes Erdfelt John L. Villalovos Jonathan Herlin Jonathan LaCour Joshua Harlow Joshua Harlow Joshua Harlow Julien Danjou Kenneth Giusti Kevin L. Mitchell Kiseok Kim Lance Bragstad Lars Butler Laurence Miao Lianhao Lu Lin Tan LingxianKong Lucas Alvares Gomes Luis A. Garcia Mark McLoughlin Mark Vanderwiel Markus Zoeller Masaki Matsushita Matt Riedemann Matt Riedemann Matthew Treinish Maxim Kulkin Mehdi Abaakouk Mehdi Abaakouk Mehdi Abaakouk Michael Basnight Michael Krotscheck Michael Still Michał Górny Miguel Grinberg Mike Fedosin Moises Guimaraes de Medeiros Moisés Guimarães de Medeiros Monty Taylor Natal Ngétal OpenStack Release Bot Paul Belanger Petr Blaho Philippe SERAPHIN Pierre Riteau Piyush Raman Srivastava Quan Tian Radomir Dopieralski Raildo Mascena Rick Harris Ronald Bradford Roxana Gherle Rui Yuan Dou Rushi Agrawal Russell Bryant Ryan Moore Sabari Kumar Murugesan Samuel Pilla Sascha Peilicke Sascha Peilicke Sean Dague Sean McGinnis Sean McGinnis Sean Pryor Sergey Lukjanov Shane Wang Stephen Finucane Stephen Finucane Stephen Finucane Steven Deaton Swapnil Kulkarni (coolsvap) Takashi Kajinami Takashi NATSUME Thomas Bechtold Thomas Herve Thomas Morin Tim Miller Tom Cammann Tong Damon Da Tony Breeds Trevor McCasland Victor Morales Victor Stinner Vincent Untz Vishvananda Ishaya Vu Cong Tuan Xi Yang YAMAMOTO Takashi Yaguo Zhou Yibo Cai Yuriy Taraday Yuval Brik Zane Bitter ZhiQiang Fan Zhihai Song ZhijunWei ZhongShengping Zhongyue Luo Zhongyue Luo avnish bhavani.cr blue55 caoyuan caoyuan chenxing dengzhaosen gengchc2 hnyang howardlee huang.xiangdong jacky06 likui liu-sheng liyingjun llg8212 loooosy lzyeval melissaml pengyuesheng ricolin shangxiaobj skudriashev songwenping sonu.kumar ting.wang venkatamahesh vponomaryov xuanyandong yan.haifeng yatinkarel zhangboye zhangyanxian zhouxinyong ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/CONTRIBUTING.rst0000664000175000017500000000135100000000000016432 0ustar00zuulzuul00000000000000If you would like to contribute to the development of oslo's libraries, first you must take a look to this page: https://specs.openstack.org/openstack/oslo-specs/specs/policy/contributing.html If you would like to contribute to the development of OpenStack, start by following the steps in this page: https://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: https://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 ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318216.0 oslo.config-9.6.0/ChangeLog0000664000175000017500000011255400000000000015553 0ustar00zuulzuul00000000000000CHANGES ======= 9.6.0 ----- * Provide an exit code when raising SystemExit 9.5.0 ----- * Fix tests for Python 3.13 * reno: Update master for unmaintained/zed * Remove old excludes * Add option for generate shell completion script * Dict: Support customizing key-value separator * Update master for stable/2024.1 * reno: Update master for unmaintained/xena * reno: Update master for unmaintained/wallaby * reno: Update master for unmaintained/victoria * doc: Drop oslo.db.concurrency namespace 9.4.0 ----- * reno: Update master for unmaintained/yoga * Bump hacking (again) * Bump hacking * Update python classifier in setup.cfg * Add choices parameter for IntOpt class 9.3.0 ----- * Remove compatibility with Python < 3.8 * Update master for stable/2023.2 9.2.0 ----- * Bump bandit and make oslo.config compatible with latest rules * Revert "Moves supported python runtimes from version 3.8 to 3.10" * Moves supported python runtimes from version 3.8 to 3.10 * Update master for stable/2023.1 9.1.1 ----- * Fix issues related to tox4 9.1.0 ----- * Make tests backward compat with cliff' older versions * Add Python3 antelope unit tests * Update master for stable/zed * Disable a config test if ran under the root user * fix CLI mismatch in unit tests 9.0.0 ----- * Drop python3.6/3.7 support in testing runtime * Add Python3 zed unit tests * Update master for stable/yoga 8.8.0 ----- * Update python testing classifier * Add Python3 yoga unit tests * Update master for stable/xena * setup.cfg: Replace dashes with underscores * Fix tests for Python3 * Replace deprecated UPPER\_CONSTRAINTS\_FILE variable 8.7.1 ----- * config-generator yaml format doesn't work with i18n fields 8.7.0 ----- * Ussuri+ is python3 only * Add the version parameter * setup.cfg: Replace dashes with underscores 8.6.0 ----- * Remove references to 'sys.version\_info' * Adding the missing HostDomain config option * Fix formatting of release list * remove unicode from code * tests: Add test for config file priority order * Move flake8 as a pre-commit local target * Add Python3 xena unit tests * Update master for stable/wallaby 8.5.0 ----- * Bump minimum version for PyYAML to 5.1 * remove lower-constraints.txt * inherit from object is not required for py3 * Move zuul config file to root folder * Dropping lower constraints testing 8.4.0 ----- * Adding --check-defaults to validator * Use py3 as the default runtime for tox * update doc url * Add a new type HostDomain * Adding pre-commit * Add Python3 wallaby unit tests * Update master for stable/victoria * tox: Enable parallel docs builds * sphinxext: Add 'merge\_domaindata' * Remove Babel from lower-constraints.txt 8.3.2 ----- * Bump bandit version * Convert rst to plaintext for oslo.config output 8.3.1 ----- * Fix unit tests to work with stevedore > 2.0.1 8.3.0 ----- * drop use of pkg\_resources 8.2.0 ----- * Update lower-constraints versions * Fix pygments style * generator: Handle 'cfg.Opt'-type options 8.1.0 ----- * Add py38 package metadata * Raise hacking minimum to 3.0.1 * Remove the unused coding style modules * Cap flake8 to be < 3.8.0 * Align contributing doc with oslo's policy * Remove six from lower-constraints.txt * docs: Mention other ways of escaping '$' * Resolve UnboundLocalError * Add release notes links to doc index * Mark sphinx extensions thread safe * Add Python3 victoria unit tests * Update master for stable/ussuri * Remove redundant class docstring info * Use unittest.mock instead of third party mock 8.0.2 ----- 8.0.1 ----- * Update the minversion parameter * mypy: Add boilerplate configuration * Switch to hacking 2.x * Remove six * trivial: Cleanup tox.ini * remove outdated header * Remove universal wheel configuration * reword releasenote for py27 support dropping 8.0.0 ----- * [ussuri][goal] Drop python 2.7 support and testing * tox: Trivial cleanup 7.0.0 ----- * Use stevedore to list the source drivers in documentation * Remove ConfigFilter * Ensure option groups don't change during logging 6.12.0 ------ * Stop configuring install\_command in tox * fix: fix float opt min and max value format errror * Switch to Ussuri jobs * tox: Keeping going with docs * update source link in readme * Update the constraints url * Update master for stable/train 6.11.1 ------ * Improve removing quotes logic * Bump the openstackdocstheme extension to 1.20 * Blacklist sphinx 2.1.0 (autodoc bug) * Clean up the unnecessary sphinx from test-requirements.txt * Add Python 3 Train unit tests 6.11.0 ------ * Remove '.testr.conf' * Switch to sphinxcontrib-apidoc * Deprecate ConfigFilter * Add unit tests for validator 6.10.0 ------ * Fix coverage tox venv * Add special handling for keystone\_authtoken group * Cap Bandit below 1.6.0 and update Sphinx requirement * Add a Quick Start tutorial * Revert "Replace git.openstack.org URLs with opendev.org URLs" * Document precedence of config sources * Replace git.openstack.org URLs with opendev.org URLs * Move user docs to top of index 6.9.0 ----- * OpenDev Migration Patch * Dropping the py35 testing * tox: Suppress output * Clarify help on config-file and config-dir options * Handle collections.abc deprecations * Add exclude-groups option to config validator * Update master for stable/stein * Add note to docs about CLI opts and config files * tox: Whitelist 'find' 6.8.1 ----- * add python 3.7 unit test job * [Configuration] Clean up .gitignore files 6.8.0 ----- * Cleaning testrepository occurrences * Migrating oslo.config to use stestr * fix formatting of sample defaults * Update hacking version * Resolve deprecation warning for rfc3986 uri validation * Use template for lower-constraints * Fix get\_location for opts in groups * Update mailinglist from dev to discuss * Revert "Optimizing the safety of the http link site in faq.rst." * Optimizing the safety of the http link site in faq.rst 6.7.0 ----- * Clean up grammar and spelling in driver docs * Add support for looking in environment for config 6.6.2 ----- * Update sphinx extension logging * Clean up .gitignore references to personal tools 6.6.1 ----- * Close a previously opened file in config generator * avoid trailing space in sphinxext log output 6.6.0 ----- * Unit tests to illustrate positional argument bug * Always build universal wheels * python3: Ensure ConfigOpts \_\_iter\_\_ uses list(d.keys()) * Document when sample\_default is in use * Add release note and documentation for config validator * Add support for machine-readable files to validator * Simple config validator 6.5.1 ----- * Replace assertRaisesRegexp with assertRaisesRegex * sphinxext: Handle multi-line group descriptions * sphinxext: Print output to logs, not to file * add lib-forward-testing-python3 test job * add python 3.6 unit test job * import zuul job settings from project-config * sphinxext: Stop emitting lines multiple times * Update reno for stable/rocky * Write sphinxext debug tempfile with linebreaks * move some documentation out of the source files 6.4.0 ----- * New cache layer for external sources * generator: Pass conf to \_OptFormatter * ensure we do not modify private data from drivers * User guide documentation for backend drivers for oslo.config * add detail to driver options in config generator 6.3.0 ----- * move configuration option list to the configuration guide * Add example group for the URI driver * Add config\_source option * Optionally use oslo.log for deprecated opt logging 6.2.2 ----- * Add release notes link to README * tox: Fix typo * sphinxext: Reporting correct lines for errors * sphinxext: Address nits from Ic6c2dcbe7823dd3fdc71db8dc5afab2d604559e3 * improve the documentation for option location information * fix tox python3 overrides * Create INI file ConfigurationSourceDriver * ConfigurationSource base class * Base class for a configuration driver * fix lower-constraints 6.2.1 ----- * Properly initialize HostAddress * Remove stale pip-missing-reqs tox test * Remove boolean from StrOpt example 6.2.0 ----- * Trivial: Update pypi url to new url * fix URI type equality comparison * Fix HostAddress superclass * use environment variable to control file location probing * sphinxext: Use 'sphinx.util.logging' 6.1.0 ----- * set default python to python3 * Switch pep8 job to python 3 * Make List option format bounds-sensitive 6.0.2 ----- * Fix typo 'importopt' to 'import\_opt' adding the missing \_ 6.0.1 ----- * disable stack inspection when setting option values * ignore location when comparing options for duplicate registration 6.0.0 ----- * Support trailing commas in values of list type * add a job to run unit tests against the lower bounds of our dependencies * Clean old output before new doc builds * Follow the new PTI for document build * Remove MultiConfigParser * sphinxext: Start parsing 'Opt.help' as rST * sphinxext: Separate parsing of group and opts * Add bindep.txt file to prevent fallback to generic list * add user documentation for get\_location() * track config file and command line locations for user settings * Updated from global requirements * Update links in README * trivial change to contributing instructions * add source filename to option locations set in code * report the correct location for an option updated with set\_defaults() * track the location where configuration options are set * Update reno for stable/queens * Updated from global requirements * Updated from global requirements * Updated from global requirements * Updated from global requirements 5.2.0 ----- * Fix the invalid links for doc file in oslo.config * sphinxext: Don't sometimes emit trailing newlines * Provide descriptions for choices * Remove -U from pip install * Avoid tox\_install.sh for constraints support * Clean up enforce\_type related test method's name 5.1.0 ----- * Remove setting of version/release from releasenotes * Updated from global requirements * Make help message include choices information for StrOpt 5.0.0 ----- * Remove the parameter enforce\_type from set\_override and set\_default * Correct documentation error in DeprecatedOpt 4.13.2 ------ * Prefer SNAP\_COMMON config files 4.13.1 ------ * Implement OptGroup.\_\_str\_\_ for log messages * Cleanup test-requirements 4.13.0 ------ * Updated from global requirements * Use boolean where expected * Fix sphinx option generation when advanced=True * Updated from global requirements 4.12.0 ------ * Remove redundant colon in doc * Updated from global requirements * Update reno for stable/pike * Updated from global requirements * Empty groupname will be 'DEFAULT' by default * Added exception when namespace option is missing * Fix the incorrect file path 4.11.0 ------ * add rst output mode for config generator * fix formatting of literal default values with spaces 4.10.0 ------ * fix an issue with looking up deprecated option names in code * handle option defaults that are not strings * Fix python 3.6 escape char warnings in strings 4.9.0 ----- * Update URLs in documents according to document migration 4.8.0 ----- * doc: Specify openstackdocs theme * Updated from global requirements * Remove log translations * sphinxext: Use lower logging level * sphinxext: Add arguments for oslo.config:group directive * switch from oslosphinx to openstackdocstheme * import content from the configuration reference guide * update the documentation URL in the readme 4.7.0 ----- * rearrange docs based on new standard layout * Updated from global requirements * Enable some off-by-default checks 4.6.0 ----- * add to group data model to for generator * Add snap package paths to default config dirs * Updated from global requirements * Fix html\_last\_updated\_fmt for Python3 4.5.0 ----- * Don't set enforce\_type if we are using the default 4.4.0 ----- * Rework the 'oslo-config-generator' documentation * sphinxext: Log with 'str', not 'repr' * Fix html\_last\_updated\_fmt for Python3 * Cast generator\_options to dict * Add PyYAML as a requirement 4.3.0 ----- * Updated from global requirements * Machine Readable Sample Config 4.2.0 ----- * Remove dependency oslo.i18n from test-requirements.txt * Updated from global requirements * Updated from global requirements 4.1.1 ----- * Updated from global requirements * Handle multi-line default values in olso-config-generator * Clarify info on a comment 4.1.0 ----- * add deprecation\_reason to log message 4.0.0 ----- * Fix string interpolation in ValueError * Remove usage of enforce\_type=True * Adjust removal\_version for argument enforce\_type * Optimize the link address * Make set\_override/set\_default with enforce\_type=True by default * Support hyphens in positional argument names * Assume positional arguments are required * Refactor unit tests for CLI usage 3.24.0 ------ * sphinxext: Handle unescaped percentage signs * Remove log translations * Add HACKING.rst * Fix conversion to item\_type on \_\_call\_\_ in List type(again) * Use Sphinx 1.5 warning-is-error 3.23.0 ------ * Updated from global requirements * [Fix gate]Update test requirement * Fix up the option docs * Handle both - and \_ forms of deprecated opts * Handle getattrs to ConfigOpts for deprecated opts * Updated from global requirements * pbr.version.VersionInfo needs package name (oslo.xyz and not oslo\_xyz) * Update reno for stable/ocata * Fix typo in generator.rst 3.22.0 ------ * Fix broken links * Updated from global requirements * Remove references to Python 3.4 * fix interpolation of None value * Normalize group sections * config: Pass description and epilog through * Add Constraints support * Replace six.iteritems() with .items() * Fixing HostName and adding support for HostAddress 3.21.0 ------ 3.20.0 ------ * List comprehensions * Add defaults for config-dir * Fix filename in comment * Show team and repo badges on README * Add 'summarize' option * Revert "Fix conversion to item\_type on \_\_call\_\_ in List type" * Fix conversion to item\_type on \_\_call\_\_ in List type * doc: Fix Range type version * Updated from global requirements 3.19.0 ------ * Fixup list types handling tuples * Updated from global requirements * [TrivialFix] Replace 'assertTrue(a in b)' with 'assertIn(a, b)' 3.18.0 ------ * Fix wrong usage of DuplicateOptError * Enable release notes translation * Updated from global requirements * Disable warning for missing oslo.config.opts.defaults * Updated from global requirements * Updated from global requirements * Add doc about config option name and comment in config file * modify the home-page info with the developer documentation * Correct nits in Iedf808 * Updated from global requirements * Fix missing option types to config doc * Add 'schemes' argument to URIOpt * Fix repr for tuples in choices * Add some documentation about option deprecation * test: add enforce\_type test * standardize release note page ordering * Warn user about enforce\_type default change * Ensure test\_config\_dir\_doesnt\_exist() dir doesn't exist * Update reno for stable/newton * Add HostnameOpt and URIOpt types to sphinxext * Add missing exceptions to the documentation * Add IPOpt and PortOpt names for sphinxext * Updated from global requirements * Add Range type 3.17.0 ------ * Updated from global requirements 3.16.0 ------ * Fix: default value of prog should remove extension * Add Port type to allow configuration of a list of tcp/ip ports 3.15.0 ------ * Advanced Option * Add set\_override method test with ListOpt 3.14.0 ------ * disable lazy translation in sphinx extension * Trivial: adjust import order to fit the import order guideline * Make error message more clear * Add min and max values to Float type and Opt * Fix parameters of assertEqual are misplaced * Updated from global requirements * Add max\_length to URIOpt * Remove discover from test-requirements * update docs for sphinxconfiggen * Add URIOpt to doced option types 3.13.0 ------ * Add Python 3.5 classifier and venv * Enabling your project for mutable-config * Add namespace to \_list\_opts() in test * decode subprocess output so doc build works on python3 * Updated from global requirements * Updated from global requirements 3.12.0 ------ * Add URIOpt * Add support for a deprecated version * Updated from global requirements * Support template substitution of DictOpt * fixture: Provide parameter enforce\_type for method config 3.11.0 ------ * Fix typo: 'olso' to 'oslo' * Return [] for .config\_dirs when config files are not parsed * generator: format string default value for List type properly * Updated from global requirements * Required Option * Updated from global requirements * Make sure ConfigType is an abstract class * Added i18n formatting to log messages * Remove duplicated code in method test\_equal of HostnameTypeTests * Incorrect group name when deprecated\_group is not specified * Handle some native python types in config generation 3.10.0 ------ * sphinxconfiggen to generate multiple sample files * Fix typo in sphinxconfiggen docs * Updated from global requirements * fix wrong module name sphinxgenconfig in doc * Update Beta development status from classifiers * Add 'DEPRECATED: ' prefix for deprecated opts * Avoid duplicate sections in generator output * Updated from global requirements * Updated from global requirements * Add \* to .coverage in .gitignore * Add enfore\_type parameter for set\_default, * Don't set html\_last\_updated\_fmt without git * Updated from global requirements * Updated from global requirements * cfg: do not add --version if not version provided * Disallow config option name as same as attribute of ConfigOpts * doc:Log warning when can't get information from git * Explicitly exclude tests from bandit scan * Fix typo error in docstrings of oslo\_config.cfg.py * improve documentation about registering entry points for config generator * Fix isinstance call * Replace deprecated LOG.warn with LOG.warning * Update reno for stable/mitaka * Fallback if git is absent * Move bandit into pep8 * Cleanup open file handles in test cases 3.9.0 ----- * remove redundant call to set explicit target * clear the cache before mutating the config files * Updated from global requirements * Add None-check to find\_file * add support for mutable options in the config generator * add unreleased page to release notes build * add a release note mentioning our use of reno * Add reno for release notes management 3.8.0 ----- * Hooks around mutate\_config\_files * Add hostname config type * Add config\_dirs property with a list of directories * Fix wrong check with non-None value when format group 3.7.0 ----- * add generator hook for apps to update option defaults * Updated from global requirements * refactor generator.\_list\_opts for further enhancement 3.6.0 ----- * Add debtcollector as requirement * always show coverage output from tests * handle group objects in sphinxext * refactor sphinxext and add unit tests * have show-options load the generator config file * support multiple namespaces in sphinxext * refactor sphinxext to make reformatting easier * refactor generator closures to private methods * Updated from global requirements * remove specially attribute handling from \_Namespace 3.5.0 ----- * Fix unrecognized subparser args * Add test for BoolOpt with no default * Fix bandit entry in tox.ini * Removal of bandit.yaml * Log mutated options at INFO * Fix version 'mutable' was added * Fix PortOpt versionchanged * treat option group names as section headers when documenting them * Fix MultiConfigParser removal version * Retain correct order of defined config options 3.4.0 ----- * Do not fail on certain config option help strings * Make list\_all\_sections return a list * Report mutated options * Warn if immutable options change * Add 'mutable' param to Opt * Fake attrs on \_Namespace * Make \_Namespace the value-holder * Updated from global requirements * enhance sphinx integration to allow links to option definitions 3.3.0 ----- * Remove argparse from requirements * Updated from global requirements * replace reference to default value in option help text in docs * add built-in options to auto-generated documentation * register the built-in options for the config generator to find * Add reST work label for configuration options * Allow port number with 0 * Replace deprecated library function os.popen() with subprocess * Fix can't generate config sample with non-None value 3.2.1 ----- * Fixed deprecated\_for\_removal warning generation * Module index grouping * Docs class links * generator: ignore doubles * Numerous corrections to the docstrings * Make oslo-config-generator fail gracefully when no arguments * Replace assertEqual(\*, None) with assertIsNone in tests * Python 3 deprecated the logger.warn method in favor of warning * Replace assertEqual(None,\*) with assertIsNone(\*) * Cleanly handle an undefined attribute reference * Replace assertEqual with assertTrue and assertFalse * assertIsNone(val) instead of assertEqual(None,val) 3.2.0 ----- * Capitalize IP * Add support for a deprecation reason * Clarify option types documentation * Correct warnings during doc build * Remove warning for option type not a ConfigType * Removes MANIFEST.in as it is not needed explicitely by PBR * Correct the docstring parameters for PortOpt * Make PortOpt accept choices parameter * Test equality of types.String choices as a set * Add new parameter \`choices\` to Integer type 3.1.0 ----- * [fix-compat] Tolerate non-string defaults in String type * [fix-compat] Support older use case for Opt type * Handle None section name in MultiOptConfigParser#get * Update the list\_opts documentation for new api * Remove duplicated profiles section from bandit.yaml * Revert "Add 'mutable' param to Opt" * Drop python 2.6 support * Update docstrings in cfg.py * Add 'mutable' param to Opt * Allow method set\_override with parameter override=None for all types 3.0.0 ----- * Move type formatting from generator to type * Add a bandit target to tox.ini * Updated from global requirements * Allow naming of custom types * Remove python 2.6 classifier * improve wording of comment in previous commit * Remove 'raise' in \_get * Remove python 2.6 and cleanup tox.ini * Add DEFAULT group in NoSuchOptError when group=None * Adjust docstring of module cfg 2.7.0 ----- * Add help text generation for OptGroups * Add max length check to StrOpt * Remove "Kept for backward-compatibility" comment * Fix StrOpt to allow setting quotes and regex * Improve default type checking * Use versionadded and versionchanged in doc * update doc to use new type PortOpt * Fix versionchanged * Raise exception when wrong type is used as default 2.6.0 ----- * Add PortOpt for integer with range 1 to 65535 2.5.0 ----- * Fix typos in cfg.py * Case insensitiveness when comparing choices values * Add missing tests and generator code for IPOpt * Fix coverage configuration and execution * Add item\_type and bounds to ListOpt * Fix bad exception for StrOpt with invalid choice value * fix literal rendering in dictionary options docs * Fix home-page value in setup.cfg with Oslo wiki * docs - Set pbr 'warnerrors' option for doc build * Include changelog/history in docs * Document DictOpt class * Correct the comment of \_\_call\_\_ method * Updated from global requirements * Demonstrate that MultiStr values from multiple sources are merged 2.4.0 ----- * Updated from global requirements * Make Integer type class honor zero for min/max 2.3.0 ----- * Add sphinx extension to embed pretty descriptions of options * Add sphinx extension to build sample config * Updated from global requirements 2.2.0 ----- * Updated from global requirements * preserve preformatted help text 2.1.0 ----- * Updated from global requirements * Add regex validation to String type 2.0.0 ----- * Add enforce\_type option when setting an override * Updated from global requirements * Add \*.egg\* to .gitignore * Updated from global requirements * Remove oslo namespace package 1.15.0 ------ * Fix use of mock for 1.1.0 * Updated from global requirements * Expose min and max to IntOpt * Don't convert ValueError into NoSuchOptError in ConfigOpts 1.14.0 ------ * Updated from global requirements * Add FAQ entry for why we do not treat config options as API * Add set\_default() method to fixture * fix fixture tests for fixtures 1.3.x * Add tox target to find missing requirements 1.13.0 ------ * Do not use oslo namespace for oslo.i18n lib * Fixes example for cfg * Updated from global requirements * Fix sorting of optional arguments * Switch badges from 'pypip.in' to 'shields.io' * Updated from global requirements 1.12.1 ------ * Fix sorting issue in python 3 * Fix typo in Opt class doc string 1.12.0 ------ * Remove pbr requirement * Document properties as properties * make registering a cli opt on a filter object work * Remove outdated release notes * cfg: support interpolation of config option from other groups * Advertise support for Python3.4 / Remove support for 3.3 * Do not sync run\_cross\_tests.sh * Updated from global requirements * Fix issue when generating code with cfg.OptGroup * Option name missing from ConfigFileValueError * Remove run\_cross\_tests.sh * Enforce alphabetic loading order for --config-dir contents 1.11.0 ------ * Uncap library requirements for liberty * Allow loading custom config files into Config Fixture 1.10.0 ------ * Added Raw Value Loading to Test Fixture * Add pypi download + version badges * Update hacking to kilo level * Fix logging of deprecated opts with dest override * Add deprecated\_for\_removal support to the config generator * Generate docs for constructor * Fix doc build warning * Updated from global requirements 1.9.3 ----- * Switch to non-namespaced module imports 1.9.2 ----- * print better message when choices has an empty string * None in config choices breaks oslo-config-generator 1.9.1 ----- * Generate help text indicating possible values * Expand and clean up documentation for the config generator * Add FAQ section to documentation 1.9.0 ----- * fix bug link in readme * Add ability to deprecate opts for removal * Typo in StrOpt docstring: Integer to String 1.8.0 ----- * Add exception handling for entry points 1.7.0 ----- * Updated from global requirements * Add expose\_opt to CfgFilter * Log a warning when deprecated opts are used * Do not import our namespace package * Fixes deprecation warning for oslo.config in cfg.py 1.6.1 ----- * Clear up MultiStrOpt and related documentation * Add a list\_all\_sections method to ConfigOpts * Better check for integer range to account for 0 * Reword DeprecatedOpt docstring * Support i18n messages in config generator * Fix of wrong cli opts unregistration 1.6.0 ----- * Set the version string * Stop sorting options on output * Move files out of the namespace package * Workflow documentation is now in infra-manual * Fix wrong order of positional args in cli * add tests coverage for an oslo.messaging use case 1.5.0 ----- * Refactored help string generation * Updated from global requirements * Updated from global requirements * Fix class constant indentation * Updated from global requirements * Activate pep8 check that \_ is imported * Updated from global requirements * Updated from global requirements * Add pbr to installation requirements * Updated from global requirements * Add some guidance for group names * delay formatting debug log message * Check config default value is correct type * Report permission denied when parsing config * Fix docs example using generator config files * Updated from global requirements * do not use colons in section titles * Stop using intersphinx 1.4.0 ----- * Looks for variable subtitution in the same group * Fixed typo in docstring for \_get\_config\_dirs * Update contributing instructions 1.4.0.0a4 --------- * Updated from global requirements * Work toward Python 3.4 support and testing * warn against sorting requirements * Explain groups for DeprecatedOpts * Add missing newline to stderr output when argument value is wrong * Log a fixed length string of asterisks for obfuscation * Added link to bug tracker and documentation in oslo.config readme * Bump hacking to version 0.9.2 1.4.0.0a3 --------- * Update 1.4.0.0a2 release notes 1.4.0.0a2 --------- * Changes imports order to pass H305, enables check * generator: tweak how MultiStrOpt defaults are handled * Add release notes for 1.3.0 and 1.4.0.0a1/2 * Hook up config fixture docs * Hook IPOpt class into the docs * Replaced 'e.g.' with 'for example' * Add cfgfilter.ConfigFilter * generator: remove the default sanitizer feature * cfg,generator: add Opt.sample\_default attribute * generator: refactor MultiStrOpt handling * Fix flaws in methods' docstring * Introduce Opts for IP addresses * Refactors \_Namespace to make the code clearer * Add oslo-config-generator * Add CLI option support to config fixture 1.4.0.0a1 --------- * Updated from global requirements * Remove print statement from types.Dict * Add warning about interpolating values from groups * Add more tests for positional CLI opts * Add test case for hyphenated option names * Fixes an issue validating max integer values * Bump hacking to 0.9.x series * Updated from global requirements * Add a doc sample for how to use the required field * log: string format arguments changed to function parameters * Fix deprecated\_opts for cli options * Reject option names prefixed with '\_' * Graduate config fixture * Updated from global requirements * Fix test\_version on Python 3.4 * Add section titles and fix markup of docstring * Avoid using too generic names in \_Namespace * Import run\_cross\_tests.sh from oslo-incubator * Move py33 env before py2x 1.3.0 ----- * Include the 'meta' trove classifiers for python versions * Do substitution on overrides and defaults too * Follow style guide for help strings * Updated from global requirements * Fix docstring for \_Namespace.\_get\_cli\_value * Convert to oslo.test * Fix a whitespace in a comment * Fix docstring of parsing order * Switch over to oslosphinx * Remove extraneous vim configuration comments * Fix misspellings in oslo.config * Add py33 trove classifier * Support building wheels (PEP-427) * Add Style Guide for help of config options * Add docs for types * Throw exception if --config-dir doesn't exist * Remove copyright from empty files * Implemented support for custom opt types * Fix for parsing error with Dollar Sign ($) in values * Utilizes assertIsNone and assertIsNotNone * Fix spelling errors in docstrings and comments * Added support of operator '==' * Updated from global requirements * Updated from global requirements * Fix to make ConfigOpts no longer obscure IOErrors * Replace assertEquals with assertEqual * Updated from global requirements * Add the ability to validate default options value 1.3.0a0 ------- * Updated from global requirements 1.2.1 ----- * Fix subparsers add\_parser() regression * Expand DeprecatedOpt documentation 1.2.0 ----- * Release notes for 1.2.0 * Raises error if duplicate keys found in DictOpt * Fix DictOpt to split only the first colon * Update requirements * Release notes for 1.2.0a4 * Remove explicit version from setup.cfg 1.2.0a4 ------- * Fix first param in \_\_eq\_\_ impl to be self * Bump hacking to 0.7.0 * Add auto-create support for OptGroup instances * NoSuchOptError should be used not AttributeError * Fixes six imports * Fix python 3.3 test configuration * Allow use of hacking 0.6.0 and fix min version * Add eclipse project files to .gitignore * Add Python 3 support * Raise an exception when \_oparser is not initialized * Hook docstrings into the published docs * Use oslo.sphinx and remove local copy of doc theme 1.2.0a3 ------- * Add release notes for 1.2.0a3 * Ensure namespace package is installed * Add reload\_config\_files function * Modify \_\_clear\_cache function * Use assertEqual() rather than assertEquals() * python3: Fix traceback while running tests * Tar up .testr.conf as well * Rename tools/(pip|test)-requires to \*requirements.txt * python3: Fix traceback while running tests * python3: Fix traceback while running tests * python3: More python3 compat updates * python3: Remove mox support from oslo.config * Fix config help order to be alphabetical * Enable pep8 E125 check (w/o any changes) * python3: Introduce py33 to tox.ini * Fix the priority of CLI args vs config file values * Parse config files in an argparse callback * Revert additions to ConfigParser public APIs * Refactor type conversion * Adds group name normalization tests * Simplify help string for --config-file * Add release notes for 1.2.0a2 * Add release notes for 1.1.1 1.2.0a2 ------- * Restrict StrOpt to just a few allowed values * Fix MultiConfigParser API breakage * Add release notes for 1.2.0a1 1.2.0a1 ------- * Allow Multiple sets of deprecated name/group for options * Add CLI vs config file priority test * Add test cases for default\_config\_files * Move --config-dir tests into their own test case * Remove some redundant code for bool opt actions * Normalizes non lowercase option group names in conf files * Switch to flake8/hacking instead of pep8+pyflakes * Made a couple minor textual documentation / typo fixes * Re-work test\_cli to use testscenarios * Upgrade testing to use testr * Update build to use pbr * Add python3 compatability support * Use builtin startswith and endswith methods in iniparser * Fixes OptGroup title set typo * Remove debug print statements from tests * Python 3.x compatibility * Support for Dictionary Options * set\_defaults does not work for more than one kwarg * Adds pylint section in tox.ini in oslo.config to check for unused imports * Removes unused imports in the oslo.config.tests module * Improve test cases for boolean values * Rename oslo-config to oslo.config * Fix copyright - from LLC to Foundation * Add Environment::OpenStack classifier * Open 1.2.x development 1.1.0 ----- * Copyright rename LLC to Foundation * Correct some typos in docstrings in cfg.py 1.1.0b1 ------- * Change the name of the project to oslo.config * Switch to semantic versioning * Explicitly set ConfigOpts.\_args to sys.argv[1:] 2013.1b5 -------- * Add deprecated\_group Opt kwarg * Add AUTHORS and ChangeLog to .gitignore * Add missing files to the tarball 2013.1b4 -------- * Make sure to install the oslo package as well * Add LICENSE file 2013.1b3 -------- * Fix setup.py to install the correct package * Allow MultiConfigParser get from mutliple sections * Refactor \_cparser\_get\_with\_deprecated() 2013.1b2 -------- * sort options to make --help output prettier 2013.1b1 -------- * Implements import\_group * Fixes "is not", "not in" syntax usage * Add sphinx documentation * Fix version to 2013.1 * Start using fixtures and testtools * Sync latest setup code from oslo-incubator * Move logging config options into the log module * Add setuptools magic * Add oslo-config project infrastructure * Move files to new locations for oslo-config * Fixes import order errors * Make tox run doctests * Verbose should not enable debug level logging * Fix pep8 E125 errors * Revert "Support lookup of value using "group.key"" * Support lookup of value using "group.key" * Add deprecated --logdir common opt * Add deprecated --logfile common opt * Allow nova and others to override some logging defaults * Fixing the trim for ListOp when reading from config file * Fix set\_default() with boolean CLI options * Improve cfg's argparse sub-parsers support * Fix regression with cfg CLI arguments * Fix ListOpt to trim whitespace * Add another duplicate opt test case * Hide the GroupAttr conf and group attributes * Fix broken --help with CommonConfigOpts * updating sphinx documentation * Don't reference argparse.\_StoreAction * Fix minor coding style issue * Remove ConfigCliParser class * Add support for positional arguments * Use stock argparse behaviour for optional args * Use stock argparse --usage behaviour * Use stock argparse --version behaviour * Remove add\_option() method * Completely remove cfg's disable\_interspersed\_args() * argparse support for cfg * Add a missing comma in a docstring * cfg: fix required if option has a dash * cfg: clean up None value handling * Allow set\_default and set\_override to use None * Tilde expansion for --config-file and --config-dir * Add import\_opt() method to ConfigOpts * Modifies \_is\_opt\_registered fcn to check for duplicate opts * cfg: allow empty config values * cfg: Fix typo in documentation * Update common code to support pep 1.3. bug 1014216 * Use 'is not None' instead of '!= None' * Fix a pep8 error * Adds support for bol and eol spaces to ini files * Add support to include config aliases * Fix pep8 errors * cfg: add a global CONF object * cfg: add generators for iterating over all options * cfg: move constructor args to \_\_call\_\_() args * Run pep8 on tests * Alphabetize imports in openstack/common/cfg.py * cfg: make reset() clear defaults and overrides * cfg: automatically create option groups * cfg: allow options to be marked as required * cfg: use a list comprehension instead of map() * New ConfigOpts.find\_file() for locating conf files * Support for directory source of config files * Provide file extension when when looking for files * Some refactoring of the cfg cache * Add caching to openstack.common.cfg * Typofix, OptionGroup should be OptGroup * Use absolute import for iniparser * Finish implementing MultiStrOpt * Avoid leaking secrets into config logging * Fix bug 954488 * fix restructuredtext formatting in docstrings * Add ConfigOpts.print\_help() * cfg: fix a small comment typo * cfg: unneeded multiple inheritance * PEP8 cleanup (openstack-common) * Backslash continuations (misc.) * Disable ConfigParser interpolation (lp#930270) * Implements blueprint separate-nova-volumeapi * Makes common/cfg.py raise AttributeError * PEP8 type comparison cleanup * Add the Mapping interface to cfg.ConfigOpts * Add cfg test case for recursive substitution * Add support to cfg for disabling interspersed args * Get cfg test cases to 100% * Add cfg test case for exceptions' \_\_str\_\_ methods * Fix some cfg test case naming conflicts * Add new cfg module ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/HACKING.rst0000664000175000017500000000017000000000000015565 0ustar00zuulzuul00000000000000Style Commandments ================== Read the OpenStack Style Commandments https://docs.openstack.org/hacking/latest/ ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/LICENSE0000664000175000017500000002665200000000000015011 0ustar00zuulzuul00000000000000 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. ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1724318217.2029986 oslo.config-9.6.0/PKG-INFO0000664000175000017500000000435600000000000015076 0ustar00zuulzuul00000000000000Metadata-Version: 2.1 Name: oslo.config Version: 9.6.0 Summary: Oslo Configuration API Home-page: https://docs.openstack.org/oslo.config/latest/ Author: OpenStack Author-email: openstack-discuss@lists.openstack.org License: UNKNOWN Description: ======================== Team and repository tags ======================== .. image:: https://governance.openstack.org/tc/badges/oslo.config.svg :target: https://governance.openstack.org/tc/reference/tags/index.html .. Change things from this point on ========================== Oslo Configuration Library ========================== .. image:: https://img.shields.io/pypi/v/oslo.config.svg :target: https://pypi.org/project/oslo.config/ :alt: Latest Version .. image:: https://img.shields.io/pypi/dm/oslo.config.svg :target: https://pypi.org/project/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: https://docs.openstack.org/oslo.config/latest/ * Source: https://opendev.org/openstack/oslo.config/ * Bugs: https://bugs.launchpad.net/oslo.config * Release notes: https://docs.openstack.org/releasenotes/oslo.config/ Platform: UNKNOWN Classifier: Development Status :: 5 - Production/Stable Classifier: Environment :: OpenStack Classifier: Intended Audience :: Developers Classifier: Intended Audience :: Information Technology Classifier: License :: OSI Approved :: Apache Software License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.8 Classifier: Programming Language :: Python :: 3.9 Classifier: Programming Language :: Python :: 3.10 Classifier: Programming Language :: Python :: 3.11 Classifier: Programming Language :: Python :: 3 :: Only Classifier: Programming Language :: Python :: Implementation :: CPython Requires-Python: >=3.8 Provides-Extra: rst_generator Provides-Extra: test ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/README.rst0000664000175000017500000000172700000000000015467 0ustar00zuulzuul00000000000000======================== Team and repository tags ======================== .. image:: https://governance.openstack.org/tc/badges/oslo.config.svg :target: https://governance.openstack.org/tc/reference/tags/index.html .. Change things from this point on ========================== Oslo Configuration Library ========================== .. image:: https://img.shields.io/pypi/v/oslo.config.svg :target: https://pypi.org/project/oslo.config/ :alt: Latest Version .. image:: https://img.shields.io/pypi/dm/oslo.config.svg :target: https://pypi.org/project/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: https://docs.openstack.org/oslo.config/latest/ * Source: https://opendev.org/openstack/oslo.config/ * Bugs: https://bugs.launchpad.net/oslo.config * Release notes: https://docs.openstack.org/releasenotes/oslo.config/ ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/bindep.txt0000664000175000017500000000045400000000000015776 0ustar00zuulzuul00000000000000# This is a cross-platform list tracking distribution packages needed by tests; # see http://docs.openstack.org/infra/bindep/ for additional information. locales [platform:debian] python3-all-dev [platform:ubuntu !platform:ubuntu-precise] python3-dev [platform:dpkg] python3-devel [platform:fedora] ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1724318217.1829967 oslo.config-9.6.0/doc/0000775000175000017500000000000000000000000014536 5ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/requirements.txt0000664000175000017500000000026200000000000020022 0ustar00zuulzuul00000000000000sphinx>=1.8.0 # BSD sphinxcontrib-apidoc>=0.2.0 # BSD openstackdocstheme>=1.20.0 # Apache-2.0 reno>=2.5.0 # Apache-2.0 fixtures>=3.0.0 # Apache-2.0/BSD doc8>=0.6.0 # Apache-2.0 ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1724318217.1829967 oslo.config-9.6.0/doc/source/0000775000175000017500000000000000000000000016036 5ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1724318217.1829967 oslo.config-9.6.0/doc/source/cli/0000775000175000017500000000000000000000000016605 5ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/cli/generator.rst0000664000175000017500000002747000000000000021337 0ustar00zuulzuul00000000000000======================= oslo-config-generator ======================= `oslo-config-generator` is a utility for generating sample config files in a variety of formats. Sample config files list all of the available options, along with their help string, type, deprecated aliases and defaults. These sample files can be used as config files for `oslo.config` itself (``ini``) or by configuration management tools (``json``, ``yaml``). .. versionadded:: 1.4.0 .. versionchanged:: 4.3.0 The :option:`oslo-config-generator --format` parameter was added, which allows outputting in additional formats. Usage ----- .. program:: oslo-config-generator .. code-block:: shell oslo-config-generator --namespace [--namespace ...] [--output-file ] [--wrap-width ] [--format ] [--minimal] [--summarize] .. option:: --namespace Option namespace under ``oslo.config.opts`` in which to query for options. .. option:: --output-file Path of the file to write to. :Default: stdout .. option:: --wrap-width The maximum length of help lines. :Default: 70 .. option:: --format Desired format for the output. ``ini`` is the only format that can be used directly with `oslo.config`. ``json`` and ``yaml`` are intended for third-party tools that want to write config files based on the sample config data. For more information, refer to :ref:`machine-readable-configs`. :Choices: ini, json, yaml .. option:: --minimal Generate a minimal required configuration. .. option:: --summarize Only output summaries of help text to config files. Retain longer help text for Sphinx documents. For example, to generate a sample config file for `oslo.messaging` you would run: .. code-block:: shell $ oslo-config-generator --namespace oslo.messaging > oslo.messaging.conf To generate a sample config file for an application ``myapp`` that has its own options and uses `oslo.messaging`, you would list both namespaces: .. code-block:: shell $ oslo-config-generator --namespace myapp \ --namespace oslo.messaging > myapp.conf To generate a sample config file for `oslo.messaging` in `JSON` format, you would run: .. code-block:: shell $ oslo-config-generator --namespace oslo.messaging \ --format json > oslo.messaging.conf Defining Option Discovery Entry Points -------------------------------------- The :option:`oslo-config-generator --namespace` option specifies an entry point name registered under the ``oslo.config.opts`` entry point namespace. For example, in the `oslo.messaging` ``setup.cfg`` we have: .. code-block:: ini [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: .. code-block:: python from oslo_config import cfg 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. .. code-block:: ini [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 multiple 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. .. code-block:: python from oslo_log import log def update_opt_defaults(): log.set_defaults( default_log_levels=log.get_default_log_levels() + ['noisy=WARN'], ) .. _machine-readable-configs: Generating Machine Readable Configs ----------------------------------- All deployment tools have to solve a similar problem: how to generate the config files for each service at deployment time. To help with this problem, `oslo-config-generator` can generate machine-readable sample config files that output the same data as the INI files used by `oslo.config` itself, but in a YAML or JSON format that can be more easily consumed by deployment tools. .. important:: The YAML and JSON-formatted files generated by `oslo-config-generator` cannot be used by `oslo.config` itself - they are only for use by other tools. For example, some YAML-formatted output might look like so: .. code-block:: yaml generator_options: config_dir: [] config_file: [] format_: yaml minimal: false namespace: - keystone output_file: null summarize: false wrap_width: 70 options: DEFAULT: help: '' opts: - advanced: false choices: [] default: null deprecated_for_removal: false deprecated_opts: [] deprecated_reason: null deprecated_since: null dest: admin_token help: Using this feature is *NOT* recommended. Instead, use the `keystone-manage bootstrap` command. The value of this option is treated as a "shared secret" that can be used to bootstrap Keystone through the API. This "token" does not represent a user (it has no identity), and carries no explicit authorization (it effectively bypasses most authorization checks). If set to `None`, the value is ignored and the `admin_token` middleware is effectively disabled. However, to completely disable `admin_token` in production (highly recommended, as it presents a security risk), remove `AdminTokenAuthMiddleware` (the `admin_token_auth` filter) from your paste application pipelines (for example, in `keystone-paste.ini`). max: null metavar: null min: null mutable: false name: admin_token namespace: keystone positional: false required: false sample_default: null secret: true short: null type: string value - ... ... deprecated_options: DEFAULT: - name: bind_host replacement_group: eventlet_server replacement_name: public_bind_host where the top-level keys are: ``generator_options`` The options passed to the :program:`oslo-config-generator` tool itself ``options`` All options registered in the provided namespace(s). These are grouped under the ``OptGroup`` they are assigned to which defaults to ``DEFAULT`` if unset. For information on the various attributes of each option, refer to :class:`oslo_config.cfg.Opt` and its subclasses. ``deprecated_options`` All **deprecated** options registered in the provided namespace(s). Like ``options``, these options are grouped by ``OptGroup``. 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: .. code-block:: ini [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: .. code-block:: shell $ 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: .. code-block:: shell $ cat > config-generator/api.conf < config-generator/compute.conf <`_. Whitespace ~~~~~~~~~~ To include whitespace in a configuration value, use a quoted string. For example: .. code-block:: ini ldap_dns_password='a password with spaces' Define an alternate location for a config file ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Most services and the ``*-manage`` command-line clients load the configuration file. To define an alternate location for the configuration file, pass the ``--config-file CONFIG_FILE`` parameter when you start a service or call a ``*-manage`` command. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/configuration/index.rst0000664000175000017500000000023100000000000022542 0ustar00zuulzuul00000000000000===================== Configuration Guide ===================== .. toctree:: :maxdepth: 2 quickstart format mutable options drivers ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/configuration/mutable.rst0000664000175000017500000000165300000000000023075 0ustar00zuulzuul00000000000000========================== Changing config at runtime ========================== OpenStack Newton introduces the ability to reload (or 'mutate') certain configuration options at runtime without a service restart. The following projects support this: * Compute (nova) Check individual options to discover if they are mutable. In practice ~~~~~~~~~~~ A common use case is to enable debug logging after a failure. Use the mutable config option called 'debug' to do this (providing ``log_config_append`` has not been set). An admin user may perform the following steps: #. Log onto the compute node. #. Edit the config file (EG ``nova.conf``) and change 'debug' to ``True``. #. Send a SIGHUP signal to the nova process (For example, ``pkill -HUP nova``). A log message will be written out confirming that the option has been changed. If you use a CMS like Ansible, Chef, or Puppet, we recommend scripting these steps through your CMS. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/configuration/options.rst0000664000175000017500000000141700000000000023135 0ustar00zuulzuul00000000000000======================================== Configuration Options from oslo.config ======================================== When loading values from the sources defined by the following options, the precedence is as follows: #. Command Line #. Environment Variables #. Config Files from ``--config-dir`` [1]_ #. Config Files from ``--config-file`` #. Pluggable Config Sources If a value is specified in multiple locations, the location used will be the one higher in the list. For example, if a value is specified both on the command line and in an environment variable, the value from the command line will be the one returned. .. [1] Files in a config dir are parsed in alphabetical order. Later files take precedence over earlier ones. .. show-options:: oslo.config ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/configuration/quickstart.rst0000664000175000017500000000373700000000000023643 0ustar00zuulzuul00000000000000========================== oslo.config Quick Start! ========================== Are you brand new to oslo.config? This brief tutorial will get you started understanding some of the fundamentals. Prerequisites ------------- * A plain text editor or Python-enabled IDE * A Python interpreter * A command shell from which the interpreter can be invoked * The oslo_config library in your Python path. Test Script ----------- Put this in a file called ``oslocfgtest.py``. .. code:: python # The sys module lets you get at the command line arguments. import sys # Load up the cfg module, which contains all the classes and methods # you'll need. from oslo_config import cfg # Define an option group grp = cfg.OptGroup('mygroup') # Define a couple of options opts = [cfg.StrOpt('option1'), cfg.IntOpt('option2', default=42)] # Register your config group cfg.CONF.register_group(grp) # Register your options within the config group cfg.CONF.register_opts(opts, group=grp) # Process command line arguments. The arguments tell CONF where to # find your config file, which it loads and parses to populate itself. cfg.CONF(sys.argv[1:]) # Now you can access the values from the config file as # CONF.. print("The value of option1 is %s" % cfg.CONF.mygroup.option1) print("The value of option2 is %d" % cfg.CONF.mygroup.option2) Conf File --------- Put this in a file called ``oslocfgtest.conf`` in the same directory as ``oslocfgtest.py``. .. code:: ini [mygroup] option1 = foo # Comment out option2 to test the default value # option2 = 123 Run It! ------- From your command shell, in the same directory as your script and conf, invoke: .. code:: shell python oslocfgtest.py --config-file oslocfgtest.conf Revel in the output being exactly as expected. If you've done everything right, you should see: .. code:: shell The value of option1 is foo The value of option2 is 42 Now go play with some more advanced option settings! ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1724318217.1829967 oslo.config-9.6.0/doc/source/contributor/0000775000175000017500000000000000000000000020410 5ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/contributor/index.rst0000664000175000017500000000012400000000000022246 0ustar00zuulzuul00000000000000============== Contributing ============== .. include:: ../../../CONTRIBUTING.rst ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/index.rst0000664000175000017500000000075500000000000017706 0ustar00zuulzuul00000000000000============= oslo.config ============= An OpenStack library for parsing configuration options from the command line and configuration files. Contents ======== .. toctree:: :maxdepth: 2 configuration/index reference/index cli/index contributor/index Release Notes ============= Read also the `oslo.config Release Notes `_. Indices and tables ================== * :ref:`genindex` * :ref:`modindex` * :ref:`search` ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1724318217.1869972 oslo.config-9.6.0/doc/source/reference/0000775000175000017500000000000000000000000017774 5ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/reference/accessing.rst0000664000175000017500000000105200000000000022463 0ustar00zuulzuul00000000000000====================================== Accessing Option Values In Your Code ====================================== Option values in the default group are referenced as attributes/properties on the config manager; groups are also attributes on the config manager, with attributes for each of the options associated with the group: .. code-block:: python server.start(app, conf.bind_port, conf.bind_host, conf) self.connection = kombu.connection.BrokerConnection( hostname=conf.rabbit.host, port=conf.rabbit.port, ...) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/reference/command-line.rst0000664000175000017500000000213700000000000023074 0ustar00zuulzuul00000000000000====================== Command Line Options ====================== Positional Command Line Arguments --------------------------------- Positional command line arguments are supported via a 'positional' Opt constructor argument: .. code-block:: console >>> conf = cfg.ConfigOpts() >>> conf.register_cli_opt(cfg.MultiStrOpt('bar', positional=True)) True >>> conf(['a', 'b']) >>> conf.bar ['a', 'b'] By default, positional arguments are also required. You may opt-out of this behavior by setting ``required=False``, to have an optional positional argument. Sub-Parsers ----------- It is also possible to use argparse "sub-parsers" to parse additional command line arguments using the SubCommandOpt class: .. code-block:: console >>> def add_parsers(subparsers): ... list_action = subparsers.add_parser('list') ... list_action.add_argument('id') ... >>> conf = cfg.ConfigOpts() >>> conf.register_cli_opt(cfg.SubCommandOpt('action', handler=add_parsers)) True >>> conf(args=['list', '10']) >>> conf.action.name, conf.action.id ('list', '10') ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/reference/configuration-files.rst0000664000175000017500000000510500000000000024476 0ustar00zuulzuul00000000000000============================= Loading Configuration Files ============================= The config manager has two CLI options defined by default, ``--config-file`` and ``--config-dir``: .. code-block:: python class ConfigOpts: def __call__(self, ...): opts = [ MultiStrOpt('config-file', ...), StrOpt('config-dir', ...), ] self.register_cli_opts(opts) Option values are parsed from any supplied config files using oslo_config.iniparser. If none are specified, a default set is used for example glance-api.conf and glance-common.conf: .. code-block:: text glance-api.conf: [DEFAULT] bind_port = 9292 glance-common.conf: [DEFAULT] bind_host = 0.0.0.0 Lines in a configuration file should not start with whitespace. A configuration file also supports comments, which must start with '#' or ';'. Option values in config files and those on the command line are parsed in order. The same option (includes deprecated option name and current option name) can appear many times, in config files or on the command line. Later values always override earlier ones. The order of configuration files inside the same configuration directory is defined by the alphabetic sorting order of their file names. Files in a configuration directory are parsed after any individual configuration files, so values that appear in both a configuration file and configuration directory will use the value from the directory. The parsing of CLI args and config files is initiated by invoking the config manager for example: .. code-block:: python conf = cfg.ConfigOpts() conf.register_opt(cfg.BoolOpt('verbose', ...)) conf(sys.argv[1:]) if conf.verbose: ... Option Value Interpolation -------------------------- Option values may reference other values using PEP 292 string substitution: .. code-block:: python opts = [ cfg.StrOpt('state_path', default=os.path.join(os.path.dirname(__file__), '../'), help='Top-level directory for maintaining nova state.'), cfg.StrOpt('sqlite_db', default='nova.sqlite', help='File name for SQLite.'), cfg.StrOpt('sql_connection', default='sqlite:///$state_path/$sqlite_db', help='Connection string for SQL database.'), ] .. note:: Interpolation can be avoided by using `$$`. .. note:: You can use `.` to delimit option from other groups, e.g. ${mygroup.myoption}. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/reference/defining.rst0000664000175000017500000002432300000000000022315 0ustar00zuulzuul00000000000000================== Defining Options ================== Configuration options may be set on the command line, in the :mod:`environment `, or in config files. Options are processed in that order. The schema for each option is defined using the :class:`Opt` class or its sub-classes, for example: .. code-block:: python from oslo_config import cfg from oslo_config import types PortType = types.Integer(1, 65535) common_opts = [ cfg.StrOpt('bind_host', default='0.0.0.0', help='IP address to listen on.'), cfg.Opt('bind_port', type=PortType, default=9292, help='Port number to listen on.') ] Option Types ------------ Options can have arbitrary types via the `type` parameter to the :class:`Opt` constructor. The `type` parameter is a callable object that takes a string and either returns a value of that particular type or raises :class:`ValueError` if the value can not be converted. For convenience, there are predefined option subclasses in :mod:`oslo_config.cfg` that set the option `type` as in the following table: ====================================== ====== Type Option ====================================== ====== :class:`oslo_config.types.String` :class:`oslo_config.cfg.StrOpt` :class:`oslo_config.types.String` :class:`oslo_config.cfg.SubCommandOpt` :class:`oslo_config.types.Boolean` :class:`oslo_config.cfg.BoolOpt` :class:`oslo_config.types.Integer` :class:`oslo_config.cfg.IntOpt` :class:`oslo_config.types.Float` :class:`oslo_config.cfg.FloatOpt` :class:`oslo_config.types.Port` :class:`oslo_config.cfg.PortOpt` :class:`oslo_config.types.List` :class:`oslo_config.cfg.ListOpt` :class:`oslo_config.types.Dict` :class:`oslo_config.cfg.DictOpt` :class:`oslo_config.types.IPAddress` :class:`oslo_config.cfg.IPOpt` :class:`oslo_config.types.Hostname` :class:`oslo_config.cfg.HostnameOpt` :class:`oslo_config.types.HostAddress` :class:`oslo_config.cfg.HostAddressOpt` :class:`oslo_config.types.URI` :class:`oslo_config.cfg.URIOpt` ====================================== ====== For :class:`oslo_config.cfg.MultiOpt` the `item_type` parameter defines the type of the values. For convenience, :class:`oslo_config.cfg.MultiStrOpt` is :class:`~oslo_config.cfg.MultiOpt` with the `item_type` parameter set to :class:`oslo_config.types.MultiString`. The following example defines options using the convenience classes: .. code-block:: python enabled_apis_opt = cfg.ListOpt('enabled_apis', default=['ec2', 'osapi_compute'], help='List of APIs to enable by default.') DEFAULT_EXTENSIONS = [ 'nova.api.openstack.compute.contrib.standard_extensions' ] osapi_compute_extension_opt = cfg.MultiStrOpt('osapi_compute_extension', default=DEFAULT_EXTENSIONS) Registering Options ------------------- Option schemas are registered with the config manager at runtime, but before the option is referenced: .. code-block:: python class ExtensionManager: enabled_apis_opt = cfg.ListOpt(...) def __init__(self, conf): self.conf = conf self.conf.register_opt(enabled_apis_opt) ... def _load_extensions(self): for ext_factory in self.conf.osapi_compute_extension: .... A common usage pattern is for each option schema to be defined in the module or class which uses the option: .. code-block:: python opts = ... def add_common_opts(conf): conf.register_opts(opts) def get_bind_host(conf): return conf.bind_host def get_bind_port(conf): return conf.bind_port An option may optionally be made available via the command line. Such options must be registered with the config manager before the command line is parsed (for the purposes of --help and CLI arg validation). Note that options registered for CLI use do not need to be registered again for use from other config sources, such as files. CLI options can be read from either the CLI or from the other enabled config sources. .. code-block:: python cli_opts = [ cfg.BoolOpt('verbose', short='v', default=False, help='Print more verbose output.'), cfg.BoolOpt('debug', short='d', default=False, help='Print debugging output.'), ] def add_common_opts(conf): conf.register_cli_opts(cli_opts) Option Groups ------------- Options can be registered as belonging to a group: .. code-block:: python rabbit_group = cfg.OptGroup(name='rabbit', title='RabbitMQ options') rabbit_host_opt = cfg.StrOpt('host', default='localhost', help='IP/hostname to listen on.'), rabbit_port_opt = cfg.PortOpt('port', default=5672, help='Port number to listen on.') def register_rabbit_opts(conf): conf.register_group(rabbit_group) # options can be registered under a group in either of these ways: conf.register_opt(rabbit_host_opt, group=rabbit_group) conf.register_opt(rabbit_port_opt, group='rabbit') If no group attributes are required other than the group name, the group need not be explicitly registered for example: .. code-block:: python def register_rabbit_opts(conf): # The group will automatically be created, equivalent calling: # conf.register_group(OptGroup(name='rabbit')) conf.register_opt(rabbit_port_opt, group='rabbit') If no group is specified, options belong to the 'DEFAULT' section of config files: .. code-block:: text glance-api.conf: [DEFAULT] bind_port = 9292 ... [rabbit] host = localhost port = 5672 use_ssl = False userid = guest password = guest virtual_host = / Command-line options in a group are automatically prefixed with the group name: .. code-block:: console --rabbit-host localhost --rabbit-port 9999 Dynamic Groups -------------- Groups can be registered dynamically by application code. This introduces a challenge for the sample generator, discovery mechanisms, and validation tools, since they do not know in advance the names of all of the groups. The ``dynamic_group_owner`` parameter to the constructor specifies the full name of an option registered in another group that controls repeated instances of a dynamic group. This option is usually a MultiStrOpt. For example, Cinder supports multiple storage backend devices and services. To configure Cinder to communicate with multiple backends, the ``enabled_backends`` option is set to the list of names of backends. Each backend group includes the options for communicating with that device or service. Driver Groups ------------- Groups can have dynamic sets of options, usually based on a driver that has unique requirements. This works at runtime because the code registers options before it uses them, but it introduces a challenge for the sample generator, discovery mechanisms, and validation tools because they do not know in advance the correct options for a group. To address this issue, the driver option for a group can be named using the ``driver_option`` parameter. Each driver option should define its own discovery entry point namespace to return the set of options for that driver, named using the prefix ``"oslo.config.opts."`` followed by the driver option name. In the Cinder case described above, a ``volume_backend_name`` option is part of the static definition of the group, so ``driver_option`` should be set to ``"volume_backend_name"``. And plugins should be registered under ``"oslo.config.opts.volume_backend_name"`` using the same names as the main plugin registered with ``"oslo.config.opts"``. The drivers residing within the Cinder code base have an entry point named ``"cinder"`` registered. Special Handling Instructions ----------------------------- Options may be declared as required so that an error is raised if the user does not supply a value for the option: .. code-block:: python opts = [ cfg.StrOpt('service_name', required=True), cfg.StrOpt('image_id', required=True), ... ] Options may be declared as secret so that their values are not leaked into log files: .. code-block:: python opts = [ cfg.StrOpt('s3_store_access_key', secret=True), cfg.StrOpt('s3_store_secret_key', secret=True), ... ] Dictionary Options ------------------ If you need end users to specify a dictionary of key/value pairs, then you can use the DictOpt: .. code-block:: python opts = [ cfg.DictOpt('foo', default={}) ] The end users can then specify the option foo in their configuration file as shown below: .. code-block:: ini [DEFAULT] foo = k1:v1,k2:v2 Advanced Option --------------- Use if you need to label an option as advanced in sample files, indicating the option is not normally used by the majority of users and might have a significant effect on stability and/or performance: .. code-block:: python from oslo_config import cfg opts = [ cfg.StrOpt('option1', default='default_value', advanced=True, help='This is help ' 'text.'), cfg.PortOpt('option2', default='default_value', help='This is help text.'), ] CONF = cfg.CONF CONF.register_opts(opts) This will result in the option being pushed to the bottom of the namespace and labeled as advanced in the sample files, with a notation about possible effects: .. code-block:: ini [DEFAULT] ... # This is help text. (string value) # option2 = default_value ... ... # This is help text. (string value) # 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. # option1 = default_value ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/reference/deprecating.rst0000664000175000017500000000250600000000000023016 0ustar00zuulzuul00000000000000==================== Option Deprecation ==================== If you want to rename some options, move them to another group or remove completely, you may change their declarations using `deprecated_name`, `deprecated_group` and `deprecated_for_removal` parameters to the :class:`Opt` constructor: .. code-block:: python from oslo_config import cfg conf = cfg.ConfigOpts() opt_1 = cfg.StrOpt('opt_1', default='foo', deprecated_name='opt1') opt_2 = cfg.StrOpt('opt_2', default='spam', deprecated_group='DEFAULT') opt_3 = cfg.BoolOpt('opt_3', default=False, deprecated_for_removal=True) conf.register_opt(opt_1, group='group_1') conf.register_opt(opt_2, group='group_2') conf.register_opt(opt_3) conf(['--config-file', 'config.conf']) assert conf.group_1.opt_1 == 'bar' assert conf.group_2.opt_2 == 'eggs' assert conf.opt_3 Assuming that the file config.conf has the following content: .. code-block:: ini [group_1] opt1 = bar [DEFAULT] opt_2 = eggs opt_3 = True the script will succeed, but will log three respective warnings about the given deprecated options. There are also `deprecated_reason` and `deprecated_since` parameters for specifying some additional information about a deprecation. All the mentioned parameters can be mixed together in any combinations. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/reference/drivers.rst0000664000175000017500000000047100000000000022206 0ustar00zuulzuul00000000000000 --------------- Backend Drivers --------------- Refer to :py:mod:`oslo_config.sources` Known Backend Drivers --------------------- .. NOTE(bnemec): These are private modules, so we need to explicitly document them .. automodule:: oslo_config.sources._uri .. automodule:: oslo_config.sources._environment ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/reference/faq.rst0000664000175000017500000001036300000000000021300 0ustar00zuulzuul00000000000000============================ 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 `__. 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 `__ 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. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/reference/globals.rst0000664000175000017500000000076300000000000022157 0ustar00zuulzuul00000000000000=================== Global ConfigOpts =================== This module also contains a global instance of the ConfigOpts class in order to support a common usage pattern in OpenStack: .. code-block:: python from oslo_config import cfg opts = [ cfg.StrOpt('bind_host', default='0.0.0.0'), cfg.PortOpt('bind_port', default=9292), ] CONF = cfg.CONF CONF.register_opts(opts) def start(server, app): server.start(app, CONF.bind_port, CONF.bind_host) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/reference/helpers.rst0000664000175000017500000000146200000000000022173 0ustar00zuulzuul00000000000000---------------- Helper Functions ---------------- Showing detailed locations for configuration settings ----------------------------------------------------- ``oslo.config`` can track the location in application and library code where an option is defined, defaults are set, or values are overridden. This feature is disabled by default because it is expensive and incurs a significant performance penalty, but it can be useful for developers tracing down issues with configuration option definitions. To turn on detailed location tracking, set the environment variable ``OSLO_CONFIG_SHOW_CODE_LOCATIONS`` to any non-empty value (for example, ``"1"`` or ``"yes, please"``) before starting the application, test suite, or script. Then use :func:`ConfigOpts.get_location` to access the location data for the option. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/reference/index.rst0000664000175000017500000000052400000000000021636 0ustar00zuulzuul00000000000000============================= oslo.config Reference Guide ============================= .. toctree:: :maxdepth: 2 API defining naming accessing configuration-files command-line deprecating globals helpers styleguide mutable locations sphinxext sphinxconfiggen drivers faq ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/reference/locations.rst0000664000175000017500000000443600000000000022530 0ustar00zuulzuul00000000000000========================== Option Setting Locations ========================== .. currentmodule:: oslo_config.cfg The :func:`~ConfigOpts.get_location` method of :class:`ConfigOpts` can be used to determine where the value for an option was set, either by the user or by the application code. The return value is a :class:`LocationInfo` instance, which includes 2 fields: ``location`` and ``detail``. The ``location`` value is a member of the :class:`Locations` enum, which has 5 possible values. The ``detail`` value is a string describing the location. Its value depends on the ``location``. .. list-table:: :header-rows: 1 :widths: 15 15 35 35 * - Value - ``is_user_controlled`` - Description - ``detail`` * - ``opt_default`` - ``False`` - The original default set when the option was defined. - The source file name where the option is defined. * - ``set_default`` - ``False`` - A default value set by the application as an override of the original default. This usually only applies to options defined in libraries. - The source file name where :func:`~ConfigOpts.set_default` or :func:`set_defaults` was called. * - ``set_override`` - ``False`` - A forced value set by the application. - The source file name where :func:`~ConfigOpts.set_override` was called. * - ``user`` - ``True`` - A value set by the user through a configuration backend such as a file. - The configuration file where the option is set. * - ``command_line`` - ``True`` - A value set by the user on the command line. - Empty string. * - ``environment`` - ``True`` - A value set by the user in the process environment. - The name of the environment variable. Did a user set a configuration option? ====================================== Each :class:`Locations` enum value has a boolean property indicating whether that type of location is managed by the user. This eliminates the need for application code to track which types of locations are user-controlled separately. .. code-block:: python loc = CONF.get_location('normal_opt').location if loc.is_user_controlled: print('normal_opt was set by the user') else: print('normal_opt was set by the application') ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/reference/mutable.rst0000664000175000017500000001056500000000000022166 0ustar00zuulzuul00000000000000Enabling 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/ ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/reference/naming.rst0000664000175000017500000000101300000000000021772 0ustar00zuulzuul00000000000000---------------------------------------------- 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. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/reference/sphinxconfiggen.rst0000664000175000017500000000375400000000000023730 0ustar00zuulzuul00000000000000==================================== 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. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/reference/sphinxext.rst0000664000175000017500000000225700000000000022566 0ustar00zuulzuul00000000000000==================== 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` ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/doc/source/reference/styleguide.rst0000664000175000017500000000613100000000000022705 0ustar00zuulzuul00000000000000---------------------------- 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 `_. 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 `_. 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') ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1724318217.1869972 oslo.config-9.6.0/oslo.config.egg-info/0000775000175000017500000000000000000000000017703 5ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318217.0 oslo.config-9.6.0/oslo.config.egg-info/PKG-INFO0000664000175000017500000000435600000000000021010 0ustar00zuulzuul00000000000000Metadata-Version: 2.1 Name: oslo.config Version: 9.6.0 Summary: Oslo Configuration API Home-page: https://docs.openstack.org/oslo.config/latest/ Author: OpenStack Author-email: openstack-discuss@lists.openstack.org License: UNKNOWN Description: ======================== Team and repository tags ======================== .. image:: https://governance.openstack.org/tc/badges/oslo.config.svg :target: https://governance.openstack.org/tc/reference/tags/index.html .. Change things from this point on ========================== Oslo Configuration Library ========================== .. image:: https://img.shields.io/pypi/v/oslo.config.svg :target: https://pypi.org/project/oslo.config/ :alt: Latest Version .. image:: https://img.shields.io/pypi/dm/oslo.config.svg :target: https://pypi.org/project/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: https://docs.openstack.org/oslo.config/latest/ * Source: https://opendev.org/openstack/oslo.config/ * Bugs: https://bugs.launchpad.net/oslo.config * Release notes: https://docs.openstack.org/releasenotes/oslo.config/ Platform: UNKNOWN Classifier: Development Status :: 5 - Production/Stable Classifier: Environment :: OpenStack Classifier: Intended Audience :: Developers Classifier: Intended Audience :: Information Technology Classifier: License :: OSI Approved :: Apache Software License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.8 Classifier: Programming Language :: Python :: 3.9 Classifier: Programming Language :: Python :: 3.10 Classifier: Programming Language :: Python :: 3.11 Classifier: Programming Language :: Python :: 3 :: Only Classifier: Programming Language :: Python :: Implementation :: CPython Requires-Python: >=3.8 Provides-Extra: rst_generator Provides-Extra: test ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318217.0 oslo.config-9.6.0/oslo.config.egg-info/SOURCES.txt0000664000175000017500000001121300000000000021565 0ustar00zuulzuul00000000000000.coveragerc .pre-commit-config.yaml .stestr.conf .zuul.yaml AUTHORS CONTRIBUTING.rst ChangeLog HACKING.rst LICENSE README.rst bindep.txt requirements.txt setup.cfg setup.py test-requirements.txt tox.ini doc/requirements.txt doc/source/conf.py doc/source/config-generator.conf doc/source/index.rst doc/source/cli/generator.rst doc/source/cli/index.rst doc/source/cli/validator.rst doc/source/configuration/drivers.rst doc/source/configuration/format.rst doc/source/configuration/index.rst doc/source/configuration/mutable.rst doc/source/configuration/options.rst doc/source/configuration/quickstart.rst doc/source/contributor/index.rst doc/source/reference/accessing.rst doc/source/reference/command-line.rst doc/source/reference/configuration-files.rst doc/source/reference/defining.rst doc/source/reference/deprecating.rst doc/source/reference/drivers.rst doc/source/reference/faq.rst doc/source/reference/globals.rst doc/source/reference/helpers.rst doc/source/reference/index.rst doc/source/reference/locations.rst doc/source/reference/mutable.rst doc/source/reference/naming.rst doc/source/reference/sphinxconfiggen.rst doc/source/reference/sphinxext.rst doc/source/reference/styleguide.rst oslo.config.egg-info/PKG-INFO oslo.config.egg-info/SOURCES.txt oslo.config.egg-info/dependency_links.txt oslo.config.egg-info/entry_points.txt oslo.config.egg-info/not-zip-safe oslo.config.egg-info/pbr.json oslo.config.egg-info/requires.txt oslo.config.egg-info/top_level.txt oslo_config/__init__.py oslo_config/_i18n.py oslo_config/_list_opts.py oslo_config/cfg.py oslo_config/fixture.py oslo_config/generator.py oslo_config/iniparser.py oslo_config/sphinxconfiggen.py oslo_config/sphinxext.py oslo_config/types.py oslo_config/validator.py oslo_config/version.py oslo_config/sources/__init__.py oslo_config/sources/_environment.py oslo_config/sources/_uri.py oslo_config/sources/templates/bash-completion.template oslo_config/sources/templates/zsh-completion.template oslo_config/tests/__init__.py oslo_config/tests/test_cfg.py oslo_config/tests/test_fixture.conf oslo_config/tests/test_fixture.py oslo_config/tests/test_generator.py oslo_config/tests/test_get_location.py oslo_config/tests/test_iniparser.py oslo_config/tests/test_sources.py oslo_config/tests/test_sphinxconfiggen.py oslo_config/tests/test_sphinxext.py oslo_config/tests/test_types.py oslo_config/tests/test_validator.py oslo_config/tests/testmods/__init__.py oslo_config/tests/testmods/bar_foo_opt.py oslo_config/tests/testmods/baz_qux_opt.py oslo_config/tests/testmods/blaa_opt.py oslo_config/tests/testmods/fbaar_baa_opt.py oslo_config/tests/testmods/fbar_foo_opt.py oslo_config/tests/testmods/fblaa_opt.py releasenotes/notes/.placeholder releasenotes/notes/add-HostAddressOpt-6e7e2afe7c7863cb.yaml releasenotes/notes/add-HostDomain-implement-rfc1033-c985a3054f824e9d.yaml releasenotes/notes/add-default-config-dirs-03340ff6689afe94.yaml releasenotes/notes/add-float-min-max-b1a2e16301c8435c.yaml releasenotes/notes/add-missing-config-option-9ee1992eea750200.yaml releasenotes/notes/add-port_type-8704295c6a56265d.yaml releasenotes/notes/add-reno-71dc832ce29b962f.yaml releasenotes/notes/add_choices_parameter_for_IntOpt_class-1b5e42c475c08f08.yaml releasenotes/notes/add_option_for_shell_completion-47b1b47d41a490e8.yaml releasenotes/notes/config-from-environment-3feba7b4cc747d2b.yaml releasenotes/notes/config-validator-256817f2183994fd.yaml releasenotes/notes/dict-key-value-separator-127ce36ba4d9245f.yaml releasenotes/notes/drop-python27-support-87f1b4089d4cc78b.yaml releasenotes/notes/machine-readable-sample-config-e8f8ba43ababcf99.yaml releasenotes/notes/positional-arguments-are-required-22ddca72e6f523bf.yaml releasenotes/notes/show-deprecated-reason-361a8eb31e05c97e.yaml releasenotes/notes/support-choice-descriptions-8b2d0c14fbd16b2a.yaml releasenotes/notes/support-fatal-deprecations-ea0513aa58a395ca.yaml releasenotes/notes/validator-check-defaults-e7b596a2fde781a8.yaml releasenotes/notes/validator-exclude-groups-ad2f046522a3407e.yaml releasenotes/source/2023.1.rst releasenotes/source/2023.2.rst releasenotes/source/2024.1.rst releasenotes/source/conf.py releasenotes/source/index.rst releasenotes/source/liberty.rst releasenotes/source/mitaka.rst releasenotes/source/newton.rst releasenotes/source/ocata.rst releasenotes/source/pike.rst releasenotes/source/queens.rst releasenotes/source/rocky.rst releasenotes/source/stein.rst releasenotes/source/train.rst releasenotes/source/unreleased.rst releasenotes/source/ussuri.rst releasenotes/source/victoria.rst releasenotes/source/wallaby.rst releasenotes/source/xena.rst releasenotes/source/yoga.rst releasenotes/source/zed.rst releasenotes/source/_static/.placeholder releasenotes/source/_templates/.placeholder././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318217.0 oslo.config-9.6.0/oslo.config.egg-info/dependency_links.txt0000664000175000017500000000000100000000000023751 0ustar00zuulzuul00000000000000 ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318217.0 oslo.config-9.6.0/oslo.config.egg-info/entry_points.txt0000664000175000017500000000042600000000000023203 0ustar00zuulzuul00000000000000[console_scripts] oslo-config-generator = oslo_config.generator:main oslo-config-validator = oslo_config.validator:main [oslo.config.driver] remote_file = oslo_config.sources._uri:URIConfigurationSourceDriver [oslo.config.opts] oslo.config = oslo_config._list_opts:list_opts ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318217.0 oslo.config-9.6.0/oslo.config.egg-info/not-zip-safe0000664000175000017500000000000100000000000022131 0ustar00zuulzuul00000000000000 ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318217.0 oslo.config-9.6.0/oslo.config.egg-info/pbr.json0000664000175000017500000000005600000000000021362 0ustar00zuulzuul00000000000000{"git_version": "beb7b6c", "is_release": true}././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318217.0 oslo.config-9.6.0/oslo.config.egg-info/requires.txt0000664000175000017500000000057200000000000022307 0ustar00zuulzuul00000000000000PyYAML>=5.1 debtcollector>=1.2.0 netaddr>=0.7.18 oslo.i18n>=3.15.3 requests>=2.18.0 rfc3986>=1.2.0 stevedore>=1.20.0 [rst_generator] rst2txt>=1.1.0 sphinx>=1.8.0 [test] bandit<1.8.0,>=1.7.0 coverage>=4.0 fixtures>=3.0.0 hacking<6.2.0,>=6.1.0 mypy>=0.720 oslo.log>=3.36.0 oslotest>=3.2.0 pre-commit>=2.6.0 requests_mock>=1.5.0 stestr>=2.1.0 testscenarios>=0.4 testtools>=2.2.0 ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318217.0 oslo.config-9.6.0/oslo.config.egg-info/top_level.txt0000664000175000017500000000001400000000000022430 0ustar00zuulzuul00000000000000oslo_config ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1724318217.1909974 oslo.config-9.6.0/oslo_config/0000775000175000017500000000000000000000000016272 5ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/__init__.py0000664000175000017500000000000000000000000020371 0ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/_i18n.py0000664000175000017500000000226400000000000017566 0ustar00zuulzuul00000000000000# 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 https://docs.openstack.org/oslo.i18n/latest/user/index.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 def get_available_languages(): return oslo_i18n.get_available_languages(DOMAIN) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/_list_opts.py0000664000175000017500000000433600000000000021031 0ustar00zuulzuul00000000000000# 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 copy import inspect from oslo_config import cfg import stevedore def list_opts(): default_config_files = [ '~/.project/project.conf', '~/project.conf', '/etc/project/project.conf', '/etc/project.conf', ] default_config_dirs = [ '~/.project/project.conf.d/', '~/project.conf.d/', '/etc/project/project.conf.d/', '/etc/project.conf.d/', ] options = [(None, cfg.ConfigOpts._list_options_for_discovery( default_config_files, default_config_dirs, ))] ext_mgr = stevedore.ExtensionManager( "oslo.config.driver", invoke_on_load=True) source_names = ext_mgr.names() for source_name in source_names: source = ext_mgr[source_name].obj source_options = copy.deepcopy(source.list_options_for_discovery()) source_description = inspect.getdoc(source) source_options.insert( 0, cfg.StrOpt( name='driver', sample_default=source_name, help=cfg._SOURCE_DRIVER_OPTION_HELP, ) ) group_name = 'sample_{}_source'.format(source_name) group_help = 'Example of using a {} source'.format(source_name) if source_description: group_help = '{}\n\n{}: {}'.format( group_help, source_name, source_description, ) group = cfg.OptGroup( name=group_name, help=group_help, driver_option='driver', dynamic_group_owner='config_source', ) options.append((group, source_options)) return options ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/cfg.py0000664000175000017500000037120600000000000017414 0ustar00zuulzuul00000000000000# 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. """Primary module in oslo_config. """ import argparse import collections from collections import abc import copy import enum import errno import functools import glob import inspect import itertools import logging import os import string import sys # NOTE(bnemec): oslo.log depends on oslo.config, so we can't # have a hard dependency on oslo.log. However, in most cases # oslo.log will be installed so we can use it. try: import oslo_log except ImportError: oslo_log = None from oslo_config import iniparser from oslo_config import sources # Absolute import to avoid circular import in Python 2.7 import oslo_config.sources._environment as _environment from oslo_config import types import stevedore LOG = logging.getLogger(__name__) _SOURCE_DRIVER_OPTION_HELP = ( 'The name of the driver that can load this ' 'configuration source.' ) class Locations(enum.Enum): opt_default = (1, False) set_default = (2, False) set_override = (3, False) user = (4, True) command_line = (5, True) environment = (6, True) def __init__(self, num, is_user_controlled): self.num = num self.is_user_controlled = is_user_controlled LocationInfo = collections.namedtuple('LocationInfo', ['location', 'detail']) class Error(Exception): """Base class for cfg exceptions.""" def __init__(self, msg=None): self.msg = msg def __str__(self): return self.msg class NotInitializedError(Error): """Raised if parser is not initialized yet.""" def __str__(self): return "call expression on parser has not been invoked" class ArgsAlreadyParsedError(Error): """Raised if a CLI opt is registered after parsing.""" def __str__(self): ret = "arguments already parsed" if self.msg: ret += ": " + self.msg return ret class NoSuchOptError(Error, AttributeError): """Raised if an opt which doesn't exist is referenced.""" def __init__(self, opt_name, group=None): self.opt_name = opt_name self.group = group def __str__(self): group_name = 'DEFAULT' if self.group is None else self.group.name return "no such option %s in group [%s]" % (self.opt_name, group_name) class NoSuchGroupError(Error): """Raised if a group which doesn't exist is referenced.""" def __init__(self, group_name): self.group_name = group_name def __str__(self): return "no such group [%s]" % self.group_name class DuplicateOptError(Error): """Raised if multiple opts with the same name are registered.""" def __init__(self, opt_name): self.opt_name = opt_name def __str__(self): return "duplicate option: %s" % self.opt_name class RequiredOptError(Error): """Raised if an option is required but no value is supplied by the user.""" def __init__(self, opt_name, group=None): self.opt_name = opt_name self.group = group def __str__(self): group_name = 'DEFAULT' if self.group is None else self.group.name return "value required for option %s in group [%s]" % (self.opt_name, group_name) class TemplateSubstitutionError(Error): """Raised if an error occurs substituting a variable in an opt value.""" def __str__(self): return "template substitution error: %s" % self.msg class ConfigFilesNotFoundError(Error): """Raised if one or more config files are not found.""" def __init__(self, config_files): self.config_files = config_files def __str__(self): return ('Failed to find some config files: %s' % ",".join(self.config_files)) class ConfigFilesPermissionDeniedError(Error): """Raised if one or more config files are not readable.""" def __init__(self, config_files): self.config_files = config_files def __str__(self): return ('Failed to open some config files: %s' % ",".join(self.config_files)) class ConfigDirNotFoundError(Error): """Raised if the requested config-dir is not found.""" def __init__(self, config_dir): self.config_dir = config_dir def __str__(self): return ('Failed to read config file directory: %s' % self.config_dir) class ConfigFileParseError(Error): """Raised if there is an error parsing a config file.""" def __init__(self, config_file, msg): self.config_file = config_file self.msg = msg def __str__(self): return 'Failed to parse %s: %s' % (self.config_file, self.msg) class ConfigSourceValueError(Error, ValueError): """Raised if a config source value does not match its opt type.""" pass class ConfigFileValueError(ConfigSourceValueError): """Raised if a config file value does not match its opt type.""" pass class DefaultValueError(Error, ValueError): """Raised if a default config type does not fit the opt type.""" def _fixpath(p): """Apply tilde expansion and absolutization to a path.""" return os.path.abspath(os.path.expanduser(p)) def _get_config_dirs(project=None): """Return a list of directories where config files may be located. :param project: an optional project name If a project is specified, following directories are returned:: ~/.${project}/ ~/ /etc/${project}/ /etc/ If a project is specified and installed from a snap package, following directories are also returned: ${SNAP_COMMON}/etc/${project} ${SNAP}/etc/${project} Otherwise, if project is not specified, these directories are returned: ~/ /etc/ """ snap = os.environ.get('SNAP') snap_c = os.environ.get('SNAP_COMMON') cfg_dirs = [ _fixpath(os.path.join('~', '.' + project)) if project else None, _fixpath('~'), os.path.join('/etc', project) if project else None, '/etc', os.path.join(snap_c, "etc", project) if snap_c and project else None, os.path.join(snap, "etc", project) if snap and project else None, ] return [x for x in cfg_dirs if x] def _search_dirs(dirs, basename, extension=""): """Search a list of directories for a given filename or directory name. Iterator over the supplied directories, returning the first file found with the supplied name and extension. :param dirs: a list of directories :param basename: the filename or directory name, for example 'glance-api' :param extension: the file extension, for example '.conf' :returns: the path to a matching file or directory, or None """ for d in dirs: path = os.path.join(d, '%s%s' % (basename, extension)) if os.path.exists(path): return path def _find_config_files(project, prog, extension): if prog is None: prog = os.path.basename(sys.argv[0]) if prog.endswith(".py"): prog = prog[:-3] cfg_dirs = _get_config_dirs(project) config_files = (_search_dirs(cfg_dirs, p, extension) for p in [project, prog] if p) return [x for x in config_files if x] def find_config_files(project=None, prog=None, extension='.conf'): """Return a list of default configuration files. :param project: an optional project name :param prog: the program name, defaulting to the basename of sys.argv[0], without extension .py :param extension: the type of the config file We default to two config files: [${project}.conf, ${prog}.conf] And we look for those config files in the following directories:: ~/.${project}/ ~/ /etc/${project}/ /etc/ ${SNAP_COMMON}/etc/${project} ${SNAP}/etc/${project} We return an absolute path for (at most) one of each the default config files, for the topmost directory it exists in. For example, if project=foo, prog=bar and /etc/foo/foo.conf, /etc/bar.conf and ~/.foo/bar.conf all exist, then we return ['/etc/foo/foo.conf', '~/.foo/bar.conf'] If no project name is supplied, we only look for ${prog}.conf. """ return _find_config_files(project, prog, extension) def find_config_dirs(project=None, prog=None, extension='.conf.d'): """Return a list of default configuration dirs. :param project: an optional project name :param prog: the program name, defaulting to the basename of sys.argv[0], without extension .py :param extension: the type of the config directory. Defaults to '.conf.d' We default to two config dirs: [${project}.conf.d/, ${prog}.conf.d/]. If no project name is supplied, we only look for ${prog.conf.d/}. And we look for those config dirs in the following directories:: ~/.${project}/ ~/ /etc/${project}/ /etc/ ${SNAP_COMMON}/etc/${project} ${SNAP}/etc/${project} We return an absolute path for each of the two config dirs, in the first place we find it (iff we find it). For example, if project=foo, prog=bar and /etc/foo/foo.conf.d/, /etc/bar.conf.d/ and ~/.foo/bar.conf.d/ all exist, then we return ['/etc/foo/foo.conf.d/', '~/.foo/bar.conf.d/'] """ return _find_config_files(project, prog, extension) def _is_opt_registered(opts, opt): """Check whether an opt with the same name is already registered. The same opt may be registered multiple times, with only the first registration having any effect. However, it is an error to attempt to register a different opt with the same name. :param opts: the set of opts already registered :param opt: the opt to be registered :returns: True if the opt was previously registered, False otherwise :raises: DuplicateOptError if a naming conflict is detected """ if opt.dest in opts: if opts[opt.dest]['opt'] != opt: raise DuplicateOptError(opt.name) return True else: return False _show_caller_details = bool(os.environ.get( 'OSLO_CONFIG_SHOW_CODE_LOCATIONS')) def _get_caller_detail(n=2): """Return a string describing where this is being called from. :param n: Number of steps up the stack to look. Defaults to ``2``. :type n: int :returns: str """ if not _show_caller_details: return None s = inspect.stack()[:n + 1] try: frame = s[n] try: return frame[1] # WARNING(dhellmann): Using frame.lineno to include the # line number in the return value causes some sort of # memory or stack corruption that manifests in values not # being cleaned up in the cfgfilter tests. # return '%s:%s' % (frame[1], frame[2]) finally: del frame finally: del s def set_defaults(opts, **kwargs): for opt in opts: if opt.dest in kwargs: opt.default = kwargs[opt.dest] opt._set_location = LocationInfo(Locations.set_default, _get_caller_detail()) def _normalize_group_name(group_name): if group_name == 'DEFAULT': return group_name return group_name.lower() def _report_deprecation(format_str, format_dict): """Report use of a deprecated option Uses versionutils from oslo.log if it is available. If not, logs a simple warning message. :param format_str: The message to use for the report :param format_dict: A dict containing keys for any parameters in format_str """ if oslo_log: # We can't import versionutils at the module level because of circular # imports. Importing just oslo_log at the module level and # versionutils locally allows us to unit test this and still avoid the # circular problem. from oslo_log import versionutils versionutils.report_deprecated_feature(LOG, format_str, format_dict) else: LOG.warning(format_str, format_dict) @functools.total_ordering class Opt: """Base class for all configuration options. The only required parameter is the option's name. However, it is common to also supply a default and help string for all options. :param name: the option's name :param type: the option's type. Must be a callable object that takes string and returns converted and validated value :param dest: the name of the corresponding :class:`.ConfigOpts` property :param short: a single character CLI option name :param default: the default value of the option :param positional: True if the option is a positional CLI argument :param metavar: the option argument to show in --help :param help: an explanation of how the option is used :param secret: true if the value should be obfuscated in log output :param required: true if a value must be supplied for this option :param deprecated_name: deprecated name option. Acts like an alias :param deprecated_group: the group containing a deprecated alias :param deprecated_opts: list of :class:`.DeprecatedOpt` :param sample_default: a default string for sample config files :param deprecated_for_removal: indicates whether this opt is planned for removal in a future release :param deprecated_reason: indicates why this opt is planned for removal in a future release. Silently ignored if deprecated_for_removal is False :param deprecated_since: indicates which release this opt was deprecated in. Accepts any string, though valid version strings are encouraged. Silently ignored if deprecated_for_removal is False :param mutable: True if this option may be reloaded :param advanced: a bool True/False value if this option has advanced usage and is not normally used by the majority of users An Opt object has no public methods, but has a number of public properties: .. py:attribute:: name the name of the option, which may include hyphens .. py:attribute:: type a callable object that takes string and returns converted and validated value. Default types are available from :class:`oslo_config.types` .. py:attribute:: dest the (hyphen-less) :class:`.ConfigOpts` property which contains the option value .. py:attribute:: short a single character CLI option name .. py:attribute:: default the default value of the option .. py:attribute:: sample_default a sample default value string to include in sample config files .. py:attribute:: positional True if the option is a positional CLI argument .. py:attribute:: metavar the name shown as the argument to a CLI option in --help output .. py:attribute:: help a string explaining how the option's value is used .. py:attribute:: advanced in sample files, a bool value indicating the option is advanced .. versionchanged:: 1.2 Added *deprecated_opts* parameter. .. versionchanged:: 1.4 Added *sample_default* parameter. .. versionchanged:: 1.9 Added *deprecated_for_removal* parameter. .. versionchanged:: 2.7 An exception is now raised if the default value has the wrong type. .. versionchanged:: 3.2 Added *deprecated_reason* parameter. .. versionchanged:: 3.5 Added *mutable* parameter. .. versionchanged:: 3.12 Added *deprecated_since* parameter. .. versionchanged:: 3.15 Added *advanced* parameter and attribute. """ multi = False def __init__(self, name, type=None, dest=None, short=None, default=None, positional=False, metavar=None, help=None, secret=False, required=None, deprecated_name=None, deprecated_group=None, deprecated_opts=None, sample_default=None, deprecated_for_removal=False, deprecated_reason=None, deprecated_since=None, mutable=False, advanced=False): if name.startswith('_'): raise ValueError('illegal name %s with prefix _' % (name,)) self.name = name if type is None: type = types.String() if not callable(type): raise TypeError('type must be callable') self.type = type # By default, non-positional options are *optional*, and positional # options are *required*. if required is None: required = True if positional else False if dest is None: self.dest = self.name.replace('-', '_') else: self.dest = dest self.short = short self.default = default self.sample_default = sample_default self.positional = positional self.metavar = metavar self.help = help self.secret = secret self.required = required self.deprecated_for_removal = deprecated_for_removal self.deprecated_reason = deprecated_reason self.deprecated_since = deprecated_since self._logged_deprecation = False if self.__class__ is Opt: stack_depth = 2 # someone instantiated Opt directly else: stack_depth = 3 # skip the call to the child class constructor self._set_location = LocationInfo( Locations.opt_default, _get_caller_detail(stack_depth), ) self.deprecated_opts = copy.deepcopy(deprecated_opts) or [] for o in self.deprecated_opts: if '-' in o.name: self.deprecated_opts.append(DeprecatedOpt( o.name.replace('-', '_'), group=o.group)) if deprecated_name is not None or deprecated_group is not None: self.deprecated_opts.append(DeprecatedOpt(deprecated_name, group=deprecated_group)) if deprecated_name and '-' in deprecated_name: self.deprecated_opts.append(DeprecatedOpt( deprecated_name.replace('-', '_'), group=deprecated_group)) self._check_default() self.mutable = mutable self.advanced = advanced def _default_is_ref(self): """Check if default is a reference to another var.""" if isinstance(self.default, str): tmpl = self.default.replace(r'\$', '').replace('$$', '') return '$' in tmpl return False def _check_default(self): if (self.default is not None and not self._default_is_ref()): try: self.type(self.default) except Exception: raise DefaultValueError("Error processing default value " "%(default)s for Opt type of %(opt)s." % {'default': self.default, 'opt': self.type}) def _vars_for_cmp(self): # NOTE(dhellmann): Get the instance variables of this Opt and # then make a new dictionary so we can modify the contents # before returning it without removing any attributes of the # object. v = dict(vars(self)) # NOTE(dhellmann): Ignore the location where the option is # defined when comparing them. Ideally we could use this to # detect duplicate settings in code bases, but as long as the # options match otherwise they should be safe. if '_set_location' in v: del v['_set_location'] return v def __ne__(self, another): return self._vars_for_cmp() != another._vars_for_cmp() def __eq__(self, another): return self._vars_for_cmp() == another._vars_for_cmp() __hash__ = object.__hash__ def _get_from_namespace(self, namespace, group_name): """Retrieves the option value from a _Namespace object. :param namespace: a _Namespace object :param group_name: a group name """ names = [(group_name, self.dest)] current_name = (group_name, self.name) for opt in self.deprecated_opts: dname, dgroup = opt.name, opt.group if dname or dgroup: names.append((dgroup if dgroup else group_name, dname if dname else self.dest)) value, loc = namespace._get_value( names, multi=self.multi, positional=self.positional, current_name=current_name) # The previous line will raise a KeyError if no value is set in the # config file, so we'll only log deprecations for set options. if self.deprecated_for_removal and not self._logged_deprecation: self._logged_deprecation = True pretty_group = group_name or 'DEFAULT' if self.deprecated_reason: pretty_reason = ' ({})'.format(self.deprecated_reason) else: pretty_reason = '' format_str = ('Option "%(option)s" from group "%(group)s" is ' 'deprecated for removal%(reason)s. Its value may ' 'be silently ignored in the future.') format_dict = {'option': self.dest, 'group': pretty_group, 'reason': pretty_reason} _report_deprecation(format_str, format_dict) return (value, loc) def _add_to_cli(self, parser, group=None): """Makes the option available in the command line interface. This is the method ConfigOpts uses to add the opt to the CLI interface as appropriate for the opt type. Some opt types may extend this method, others may just extend the helper methods it uses. :param parser: the CLI option parser :param group: an optional OptGroup object """ container = self._get_argparse_container(parser, group) kwargs = self._get_argparse_kwargs(group) prefix = self._get_argparse_prefix('', group.name if group else None) deprecated_names = [] for opt in self.deprecated_opts: deprecated_name = self._get_deprecated_cli_name(opt.name, opt.group) if deprecated_name is not None: deprecated_names.append(deprecated_name) self._add_to_argparse(parser, container, self.name, self.short, kwargs, prefix, self.positional, deprecated_names) def _add_to_argparse(self, parser, container, name, short, kwargs, prefix='', positional=False, deprecated_names=None): """Add an option to an argparse parser or group. :param container: an argparse._ArgumentGroup object :param name: the opt name :param short: the short opt name :param kwargs: the keyword arguments for add_argument() :param prefix: an optional prefix to prepend to the opt name :param positional: whether the option is a positional CLI argument :param deprecated_names: list of deprecated option names """ def hyphen(arg): return arg if not positional else '' # Because we must omit the dest parameter when using a positional # argument, the name supplied for the positional argument must not # include hyphens. if positional: prefix = prefix.replace('-', '_') name = name.replace('-', '_') args = [hyphen('--') + prefix + name] if short: args.append(hyphen('-') + short) for deprecated_name in deprecated_names: args.append(hyphen('--') + deprecated_name) parser.add_parser_argument(container, *args, **kwargs) def _get_argparse_container(self, parser, group): """Returns an argparse._ArgumentGroup. :param parser: an argparse.ArgumentParser :param group: an (optional) OptGroup object :returns: an argparse._ArgumentGroup if group is given, else parser """ if group is not None: return group._get_argparse_group(parser) else: return parser def _get_argparse_kwargs(self, group, **kwargs): r"""Build a dict of keyword arguments for argparse's add_argument(). Most opt types extend this method to customize the behaviour of the options added to argparse. :param group: an optional group :param \*\*kwargs: optional keyword arguments to add to :returns: a dict of keyword arguments """ if not self.positional: dest = self.dest if group is not None: dest = group.name + '_' + dest kwargs['dest'] = dest elif not self.required: kwargs['nargs'] = '?' kwargs.update({'default': None, 'metavar': self.metavar, 'help': self.help, }) return kwargs def _get_argparse_prefix(self, prefix, group_name): """Build a prefix for the CLI option name, if required. CLI options in a group are prefixed with the group's name in order to avoid conflicts between similarly named options in different groups. :param prefix: an existing prefix to append to (for example 'no' or '') :param group_name: an optional group name :returns: a CLI option prefix including the group name, if appropriate """ if group_name is not None: return group_name + '-' + prefix else: return prefix def _get_deprecated_cli_name(self, dname, dgroup, prefix=''): """Build a CLi arg name for deprecated options. Either a deprecated name or a deprecated group or both or neither can be supplied: dname, dgroup -> dgroup + '-' + dname dname -> dname dgroup -> dgroup + '-' + self.name neither -> None :param dname: a deprecated name, which can be None :param dgroup: a deprecated group, which can be None :param prefix: an prefix to append to (for example 'no' or '') :returns: a CLI argument name """ if dgroup == 'DEFAULT': dgroup = None if dname is None and dgroup is None: return None if dname is None: dname = self.name return self._get_argparse_prefix(prefix, dgroup) + dname def __lt__(self, another): return hash(self) < hash(another) class DeprecatedOpt: """Represents a Deprecated option. Here's how you can use it:: oldopts = [cfg.DeprecatedOpt('oldopt1', group='group1'), cfg.DeprecatedOpt('oldopt2', group='group2')] cfg.CONF.register_group(cfg.OptGroup('group1')) cfg.CONF.register_opt(cfg.StrOpt('newopt', deprecated_opts=oldopts), group='group1') For options which have a single value (like in the example above), if the new option is present ("[group1]/newopt" above), it will override any deprecated options present ("[group1]/oldopt1" and "[group2]/oldopt2" above). If no group is specified for a DeprecatedOpt option (i.e. the group is None), lookup will happen within the same group the new option is in. For example, if no group was specified for the second option 'oldopt2' in oldopts list:: oldopts = [cfg.DeprecatedOpt('oldopt1', group='group1'), cfg.DeprecatedOpt('oldopt2')] cfg.CONF.register_group(cfg.OptGroup('group1')) cfg.CONF.register_opt(cfg.StrOpt('newopt', deprecated_opts=oldopts), group='group1') then lookup for that option will happen in group 'group1'. If the new option is not present and multiple deprecated options are present, the option corresponding to the first element of deprecated_opts will be chosen. Multi-value options will return all new and deprecated options. So if we have a multi-value option "[group1]/opt1" whose deprecated option is "[group2]/opt2", and the conf file has both these options specified like so:: [group1] opt1=val10,val11 [group2] opt2=val21,val22 Then the value of "[group1]/opt1" will be ['val10', 'val11', 'val21', 'val22']. .. versionadded:: 1.2 """ def __init__(self, name, group=None): """Constructs an DeprecatedOpt object. :param name: the name of the option :param group: the group of the option """ self.name = name self.group = group def __key(self): return (self.name, self.group) def __eq__(self, other): return self.__key() == other.__key() def __hash__(self): return hash(self.__key()) class StrOpt(Opt): r"""Option with String type Option with ``type`` :class:`oslo_config.types.String` :param name: the option's name :param choices: Optional sequence of either valid values or tuples of valid values with descriptions. :param quotes: If True and string is enclosed with single or double quotes, will strip those quotes. :param regex: Optional regular expression (string or compiled regex) that the value must match on an unanchored search. :param ignore_case: If True case differences (uppercase vs. lowercase) between 'choices' or 'regex' will be ignored. :param max_length: If positive integer, the value must be less than or equal to this parameter. :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` .. versionchanged:: 2.7 Added *quotes* parameter .. versionchanged:: 2.7 Added *regex* parameter .. versionchanged:: 2.7 Added *ignore_case* parameter .. versionchanged:: 2.7 Added *max_length* parameter .. versionchanged:: 5.2 The *choices* parameter will now accept a sequence of tuples, where each tuple is of form (*choice*, *description*) """ def __init__(self, name, choices=None, quotes=None, regex=None, ignore_case=False, max_length=None, **kwargs): super(StrOpt, self).__init__(name, type=types.String( choices=choices, quotes=quotes, regex=regex, ignore_case=ignore_case, max_length=max_length), **kwargs) def _get_choice_text(self, choice): if choice is None: return '' elif choice == '': return "''" return str(choice) def _get_argparse_kwargs(self, group, **kwargs): """Extends the base argparse keyword dict for the config dir option.""" kwargs = super(StrOpt, self)._get_argparse_kwargs(group) if getattr(self.type, 'choices', None): choices_text = ', '.join([self._get_choice_text(choice) for choice in self.type.choices]) if kwargs['help'] is None: kwargs['help'] = '' kwargs['help'].rstrip('\n') kwargs['help'] += '\n Allowed values: %s\n' % choices_text return kwargs class BoolOpt(Opt): r"""Boolean options. Bool opts are set to True or False on the command line using --optname or --nooptname respectively. In config files, boolean values are cast with Boolean type. :param name: the option's name :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` """ def __init__(self, name, **kwargs): if 'positional' in kwargs: raise ValueError('positional boolean args not supported') super(BoolOpt, self).__init__(name, type=types.Boolean(), **kwargs) def _add_to_cli(self, parser, group=None): """Extends the base class method to add the --nooptname option.""" super(BoolOpt, self)._add_to_cli(parser, group) self._add_inverse_to_argparse(parser, group) def _add_inverse_to_argparse(self, parser, group): """Add the --nooptname option to the option parser.""" container = self._get_argparse_container(parser, group) kwargs = self._get_argparse_kwargs(group, action='store_false') prefix = self._get_argparse_prefix('no', group.name if group else None) deprecated_names = [] for opt in self.deprecated_opts: deprecated_name = self._get_deprecated_cli_name(opt.name, opt.group, prefix='no') if deprecated_name is not None: deprecated_names.append(deprecated_name) kwargs["help"] = "The inverse of --" + self.name self._add_to_argparse(parser, container, self.name, None, kwargs, prefix, self.positional, deprecated_names) def _get_argparse_kwargs(self, group, action='store_true', **kwargs): """Extends the base argparse keyword dict for boolean options.""" kwargs = super(BoolOpt, self)._get_argparse_kwargs(group, **kwargs) # type has no effect for BoolOpt, it only matters for # values that came from config files if 'type' in kwargs: del kwargs['type'] # metavar has no effect for BoolOpt if 'metavar' in kwargs: del kwargs['metavar'] kwargs['action'] = action return kwargs class IntOpt(Opt): r"""Option with Integer type Option with ``type`` :class:`oslo_config.types.Integer` :param name: the option's name :param min: minimum value the integer can take :param max: maximum value the integer can take :param choices: Optional sequence of either valid values or tuples of valid values with descriptions. :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` .. versionchanged:: 1.15 Added *min* and *max* parameters. .. versionchanged:: 9.3.0 Added *choices* parameter. """ def __init__(self, name, min=None, max=None, choices=None, **kwargs): super(IntOpt, self).__init__(name, type=types.Integer( min=min, max=max, choices=choices), **kwargs) class FloatOpt(Opt): r"""Option with Float type Option with ``type`` :class:`oslo_config.types.Float` :param name: the option's name :param min: minimum value the float can take :param max: maximum value the float can take :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` .. versionchanged:: 3.14 Added *min* and *max* parameters. """ def __init__(self, name, min=None, max=None, **kwargs): super(FloatOpt, self).__init__(name, type=types.Float(min, max), **kwargs) class ListOpt(Opt): r"""Option with List(String) type Option with ``type`` :class:`oslo_config.types.List` :param name: the option's name :param item_type: type of items (see :class:`oslo_config.types`) :param bounds: if True the value should be inside "[" and "]" pair :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` .. versionchanged:: 2.5 Added *item_type* and *bounds* parameters. """ def __init__(self, name, item_type=None, bounds=None, **kwargs): super(ListOpt, self).__init__(name, type=types.List(item_type=item_type, bounds=bounds), **kwargs) class DictOpt(Opt): r"""Option with Dict(String) type Option with ``type`` :class:`oslo_config.types.Dict` :param name: the option's name :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` .. versionadded:: 1.2 """ def __init__(self, name, **kwargs): super(DictOpt, self).__init__(name, type=types.Dict(), **kwargs) class IPOpt(Opt): r"""Opt with IPAddress type Option with ``type`` :class:`oslo_config.types.IPAddress` :param name: the option's name :param version: one of either ``4``, ``6``, or ``None`` to specify either version. :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` .. versionadded:: 1.4 """ def __init__(self, name, version=None, **kwargs): super(IPOpt, self).__init__(name, type=types.IPAddress(version), **kwargs) class PortOpt(Opt): r"""Option for a TCP/IP port number. Ports can range from 0 to 65535. Option with ``type`` :class:`oslo_config.types.Integer` :param name: the option's name :param min: minimum value the port can take :param max: maximum value the port can take :param choices: Optional sequence of either valid values or tuples of valid values with descriptions. :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` .. versionadded:: 2.6 .. versionchanged:: 3.2 Added *choices* parameter. .. versionchanged:: 3.4 Allow port number with 0. .. versionchanged:: 3.16 Added *min* and *max* parameters. .. versionchanged:: 5.2 The *choices* parameter will now accept a sequence of tuples, where each tuple is of form (*choice*, *description*) """ def __init__(self, name, min=None, max=None, choices=None, **kwargs): type = types.Port(min=min, max=max, choices=choices, type_name='port value') super(PortOpt, self).__init__(name, type=type, **kwargs) class HostnameOpt(Opt): r"""Option for a hostname. Only accepts valid hostnames. Option with ``type`` :class:`oslo_config.types.Hostname` :param name: the option's name :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` .. versionadded:: 3.8 """ def __init__(self, name, **kwargs): super(HostnameOpt, self).__init__(name, type=types.Hostname(), **kwargs) class HostAddressOpt(Opt): r"""Option for either an IP or a hostname. Accepts valid hostnames and valid IP addresses. Option with ``type`` :class:`oslo_config.types.HostAddress` :param name: the option's name :param version: one of either ``4``, ``6``, or ``None`` to specify either version. :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` .. versionadded:: 3.22 """ def __init__(self, name, version=None, **kwargs): super(HostAddressOpt, self).__init__(name, type=types.HostAddress(version), **kwargs) class HostDomainOpt(Opt): r"""Option for either an IP or a hostname. Like HostAddress with the support of _ character. Option with ``type`` :class:`oslo_config.types.HostDomain` :param name: the option's name :param version: one of either ``4``, ``6``, or ``None`` to specify either version. :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` .. versionadded:: 8.6 """ def __init__(self, name, version=None, **kwargs): super(HostDomainOpt, self).__init__(name, type=types.HostDomain(version), **kwargs) class URIOpt(Opt): r"""Opt with URI type Option with ``type`` :class:`oslo_config.types.URI` :param name: the option's name :param max_length: If positive integer, the value must be less than or equal to this parameter. :param schemes: list of valid URI schemes, e.g. 'https', 'ftp', 'git' :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` .. versionadded:: 3.12 .. versionchanged:: 3.14 Added *max_length* parameter .. versionchanged:: 3.18 Added *schemes* parameter """ def __init__(self, name, max_length=None, schemes=None, **kwargs): type = types.URI(max_length=max_length, schemes=schemes) super(URIOpt, self).__init__(name, type=type, **kwargs) class MultiOpt(Opt): r"""Multi-value option. Multi opt values are typed opts which may be specified multiple times. The opt value is a list containing all the values specified. :param name: the option's name :param item_type: Type of items (see :class:`oslo_config.types`) :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` For example:: cfg.MultiOpt('foo', item_type=types.Integer(), default=None, help="Multiple foo option") The command line ``--foo=1 --foo=2`` would result in ``cfg.CONF.foo`` containing ``[1,2]`` .. versionadded:: 1.3 """ multi = True def __init__(self, name, item_type, **kwargs): super(MultiOpt, self).__init__(name, item_type, **kwargs) def _get_argparse_kwargs(self, group, **kwargs): """Extends the base argparse keyword dict for multi value options.""" kwargs = super(MultiOpt, self)._get_argparse_kwargs(group) if not self.positional: kwargs['action'] = 'append' else: kwargs['nargs'] = '*' return kwargs class MultiStrOpt(MultiOpt): r"""MultiOpt with a MultiString ``item_type``. MultiOpt with a default :class:`oslo_config.types.MultiString` item type. :param name: the option's name :param \*\*kwargs: arbitrary keyword arguments passed to :class:`MultiOpt` """ def __init__(self, name, **kwargs): super(MultiStrOpt, self).__init__(name, item_type=types.MultiString(), **kwargs) class SubCommandOpt(Opt): """Sub-command options. Sub-command options allow argparse sub-parsers to be used to parse additional command line arguments. The handler argument to the SubCommandOpt constructor is a callable which is supplied an argparse subparsers object. Use this handler callable to add sub-parsers. The opt value is SubCommandAttr object with the name of the chosen sub-parser stored in the 'name' attribute and the values of other sub-parser arguments available as additional attributes. :param name: the option's name :param dest: the name of the corresponding :class:`.ConfigOpts` property :param handler: callable which is supplied subparsers object when invoked :param title: title of the sub-commands group in help output :param description: description of the group in help output :param help: a help string giving an overview of available sub-commands """ def __init__(self, name, dest=None, handler=None, title=None, description=None, help=None): """Construct an sub-command parsing option. This behaves similarly to other Opt sub-classes but adds a 'handler' argument. The handler is a callable which is supplied an subparsers object when invoked. The add_parser() method on this subparsers object can be used to register parsers for sub-commands. """ super(SubCommandOpt, self).__init__(name, type=types.String(), dest=dest, help=help) self.handler = handler self.title = title self.description = description def _add_to_cli(self, parser, group=None): """Add argparse sub-parsers and invoke the handler method.""" dest = self.dest if group is not None: dest = group.name + '_' + dest subparsers = parser.add_subparsers(dest=dest, title=self.title, description=self.description, help=self.help) # NOTE(jd) Set explicitly to True for Python 3 # See http://bugs.python.org/issue9253 for context subparsers.required = True if self.handler is not None: self.handler(subparsers) class _ConfigFileOpt(Opt): """The --config-file option. This is an private option type which handles the special processing required for --config-file options. As each --config-file option is encountered on the command line, we parse the file and store the parsed values in the _Namespace object. This allows us to properly handle the precedence of --config-file options over previous command line arguments, but not over subsequent arguments. .. versionadded:: 1.2 """ class ConfigFileAction(argparse.Action): """An argparse action for --config-file. As each --config-file option is encountered, this action adds the value to the config_file attribute on the _Namespace object but also parses the configuration file and stores the values found also in the _Namespace object. """ def __call__(self, parser, namespace, values, option_string=None): """Handle a --config-file command line argument. :raises: ConfigFileParseError, ConfigFileValueError """ if getattr(namespace, self.dest, None) is None: setattr(namespace, self.dest, []) items = getattr(namespace, self.dest) items.append(values) ConfigParser._parse_file(values, namespace) def __init__(self, name, **kwargs): super(_ConfigFileOpt, self).__init__(name, lambda x: x, **kwargs) def _get_argparse_kwargs(self, group, **kwargs): """Extends the base argparse keyword dict for the config file opt.""" kwargs = super(_ConfigFileOpt, self)._get_argparse_kwargs(group) kwargs['action'] = self.ConfigFileAction return kwargs class _ConfigDirOpt(Opt): """The --config-dir option. This is an private option type which handles the special processing required for --config-dir options. As each --config-dir option is encountered on the command line, we parse the files in that directory and store the parsed values in the _Namespace object. This allows us to properly handle the precedence of --config-dir options over previous command line arguments, but not over subsequent arguments. .. versionadded:: 1.2 """ class ConfigDirAction(argparse.Action): """An argparse action for --config-dir. As each --config-dir option is encountered, this action sets the config_dir attribute on the _Namespace object but also parses the configuration files and stores the values found also in the _Namespace object. """ def __call__(self, parser, namespace, values, option_string=None): """Handle a --config-dir command line argument. :raises: ConfigFileParseError, ConfigFileValueError, ConfigDirNotFoundError """ namespace._config_dirs.append(values) setattr(namespace, self.dest, values) values = os.path.expanduser(values) if not os.path.exists(values): raise ConfigDirNotFoundError(values) config_dir_glob = os.path.join(values, '*.conf') for config_file in sorted(glob.glob(config_dir_glob)): ConfigParser._parse_file(config_file, namespace) def __init__(self, name, **kwargs): super(_ConfigDirOpt, self).__init__(name, type=types.List(), **kwargs) def _get_argparse_kwargs(self, group, **kwargs): """Extends the base argparse keyword dict for the config dir option.""" kwargs = super(_ConfigDirOpt, self)._get_argparse_kwargs(group) kwargs['action'] = self.ConfigDirAction return kwargs class OptGroup: """Represents a group of opts. CLI opts in the group are automatically prefixed with the group name. Each group corresponds to a section in config files. An OptGroup object has no public methods, but has a number of public string properties: .. py:attribute:: name the name of the group .. py:attribute:: title the group title as displayed in --help .. py:attribute:: help the group description as displayed in --help :param name: the group name :type name: str :param title: the group title for --help :type title: str :param help: the group description for --help :type help: str :param dynamic_group_owner: The name of the option that controls repeated instances of this group. :type dynamic_group_owner: str :param driver_option: The name of the option within the group that controls which driver will register options. :type driver_option: str """ def __init__(self, name, title=None, help=None, dynamic_group_owner='', driver_option=''): """Constructs an OptGroup object.""" self.name = name self.title = "%s options" % name if title is None else title self.help = help self.dynamic_group_owner = dynamic_group_owner self.driver_option = driver_option self._opts = {} # dict of dicts of (opt:, override:, default:) self._argparse_group = None self._driver_opts = {} # populated by the config generator def _save_driver_opts(self, opts): """Save known driver opts. :param opts: mapping between driver name and list of opts :type opts: dict """ self._driver_opts.update(opts) def _get_generator_data(self): "Return a dict with data for the sample generator." return { 'help': self.help or '', 'dynamic_group_owner': self.dynamic_group_owner, 'driver_option': self.driver_option, 'driver_opts': self._driver_opts, } def _register_opt(self, opt, cli=False): """Add an opt to this group. :param opt: an Opt object :param cli: whether this is a CLI option :returns: False if previously registered, True otherwise :raises: DuplicateOptError if a naming conflict is detected """ if _is_opt_registered(self._opts, opt): return False self._opts[opt.dest] = {'opt': opt, 'cli': cli} return True def _unregister_opt(self, opt): """Remove an opt from this group. :param opt: an Opt object """ if opt.dest in self._opts: del self._opts[opt.dest] def _get_argparse_group(self, parser): if self._argparse_group is None: """Build an argparse._ArgumentGroup for this group.""" self._argparse_group = parser.add_argument_group(self.title, self.help) return self._argparse_group def _clear(self): """Clear this group's option parsing state.""" self._argparse_group = None def __str__(self): return self.name class ParseError(iniparser.ParseError): def __init__(self, msg, lineno, line, filename): super(ParseError, self).__init__(msg, lineno, line) self.filename = filename def __str__(self): return 'at %s:%d, %s: %r' % (self.filename, self.lineno, self.msg, self.line) class ConfigParser(iniparser.BaseParser): """Parses a single config file, populating 'sections' to look like:: {'DEFAULT': {'key': [value, ...], ...}, ...} Also populates self._normalized which looks the same but with normalized section names. """ def __init__(self, filename, sections): super(ConfigParser, self).__init__() self.filename = filename self.sections = sections self._normalized = None self.section = None def _add_normalized(self, normalized): self._normalized = normalized def parse(self): with open(self.filename) as f: return super(ConfigParser, self).parse(f.readlines()) def new_section(self, section): self.section = section self.sections.setdefault(self.section, {}) if self._normalized is not None: self._normalized.setdefault(_normalize_group_name(self.section), {}) def assignment(self, key, value): if not self.section: raise self.error_no_section() value = '\n'.join(value) def append(sections, section): sections[section].setdefault(key, []) sections[section][key].append(value) append(self.sections, self.section) if self._normalized is not None: append(self._normalized, _normalize_group_name(self.section)) def parse_exc(self, msg, lineno, line=None): return ParseError(msg, lineno, line, self.filename) def error_no_section(self): return self.parse_exc('Section must be started before assignment', self.lineno) @classmethod def _parse_file(cls, config_file, namespace): """Parse a config file and store any values in the namespace. :raises: ConfigFileParseError, ConfigFileValueError """ config_file = _fixpath(config_file) sections = {} normalized = {} parser = cls(config_file, sections) parser._add_normalized(normalized) try: parser.parse() except iniparser.ParseError as pe: raise ConfigFileParseError(pe.filename, str(pe)) except IOError as err: if err.errno == errno.ENOENT: namespace._file_not_found(config_file) return if err.errno == errno.EACCES: namespace._file_permission_denied(config_file) return raise namespace._add_parsed_config_file(config_file, sections, normalized) namespace._parse_cli_opts_from_config_file( config_file, sections, normalized) class _Namespace(argparse.Namespace): """An argparse namespace which also stores config file values. As we parse command line arguments, the values get set as attributes on a namespace object. However, we also want to parse config files as they are specified on the command line and collect the values alongside the option values parsed from the command line. Note, we don't actually assign values from config files as attributes on the namespace because config file options be registered after the command line has been parsed, so we may not know how to properly parse or convert a config file value at this point. """ _deprecated_opt_message = ('Option "%(dep_option)s" from group ' '"%(dep_group)s" is deprecated. Use option ' '"%(option)s" from group "%(group)s".') def __init__(self, conf): self._conf = conf self._parsed = [] self._normalized = [] self._emitted_deprecations = set() self._files_not_found = [] self._files_permission_denied = [] self._config_dirs = [] self._sections_to_file = {} def _parse_cli_opts_from_config_file(self, config_file, sections, normalized): """Parse CLI options from a config file. CLI options are special - we require they be registered before the command line is parsed. This means that as we parse config files, we can go ahead and apply the appropriate option-type specific conversion to the values in config files for CLI options. We can't do this for non-CLI options, because the schema describing those options may not be registered until after the config files are parsed. This method relies on that invariant in order to enforce proper priority of option values - i.e. that the order in which an option value is parsed, whether the value comes from the CLI or a config file, determines which value specified for a given option wins. The way we implement this ordering is that as we parse each config file, we look for values in that config file for CLI options only. Any values for CLI options found in the config file are treated like they had appeared on the command line and set as attributes on the namespace objects. Values in later config files or on the command line will override values found in this file. """ namespace = _Namespace(self._conf) namespace._add_parsed_config_file(config_file, sections, normalized) for opt, group in self._conf._all_cli_opts(): group_name = group.name if group is not None else None try: value, loc = opt._get_from_namespace(namespace, group_name) except KeyError: continue except ValueError as ve: raise ConfigFileValueError( "Value for option %s is not valid: %s" % (opt.name, str(ve))) if group_name is None: dest = opt.dest else: dest = group_name + '_' + opt.dest if opt.multi: if getattr(self, dest, None) is None: setattr(self, dest, []) values = getattr(self, dest) values.extend(value) else: setattr(self, dest, value) def _add_parsed_config_file(self, filename, sections, normalized): """Add a parsed config file to the list of parsed files. :param filename: the full name of the file that was parsed :param sections: a mapping of section name to dicts of config values :param normalized: sections mapping with section names normalized :raises: ConfigFileValueError """ for s in sections: self._sections_to_file[s] = filename self._parsed.insert(0, sections) self._normalized.insert(0, normalized) def _file_not_found(self, config_file): """Record that we were unable to open a config file. :param config_file: the path to the failed file """ self._files_not_found.append(config_file) def _file_permission_denied(self, config_file): """Record that we have no permission to open a config file. :param config_file: the path to the failed file """ self._files_permission_denied.append(config_file) def _get_cli_value(self, names, positional=False): """Fetch a CLI option value. Look up the value of a CLI option. The value itself may have come from parsing the command line or parsing config files specified on the command line. Type conversion have already been performed for CLI options at this point. :param names: a list of (section, name) tuples :param positional: whether this is a positional option """ for group_name, name in names: name = name if group_name is None else group_name + '_' + name value = getattr(self, name, None) if value is not None: # argparse ignores default=None for nargs='*' and returns [] if positional and not value: continue return value raise KeyError def _get_file_value( self, names, multi=False, normalized=False, current_name=None): """Fetch a config file value from the parsed files. :param names: a list of (section, name) tuples :param multi: a boolean indicating whether to return multiple values :param normalized: whether to normalize group names to lowercase :param current_name: current name in tuple being checked """ rvalue = [] def normalize(name): if name is None: name = 'DEFAULT' return _normalize_group_name(name) if normalized else name names = [(normalize(section), name) for section, name in names] loc = None for sections in (self._normalized if normalized else self._parsed): for section, name in names: if section not in sections: continue if name in sections[section]: current_name = current_name or names[0] self._check_deprecated((section, name), current_name, names[1:]) val = sections[section][name] if loc is None: loc = LocationInfo( Locations.user, self._sections_to_file.get(section, ''), ) if multi: rvalue = val + rvalue else: return (val, loc) if multi and rvalue != []: return (rvalue, loc) raise KeyError def _check_deprecated(self, name, current, deprecated): """Check for usage of deprecated names. :param name: A tuple of the form (group, name) representing the group and name where an opt value was found. :param current: A tuple of the form (group, name) representing the current name for an option. :param deprecated: A list of tuples with the same format as the name param which represent any deprecated names for an option. If the name param matches any entries in this list a deprecation warning will be logged. """ if name in deprecated and name not in self._emitted_deprecations: self._emitted_deprecations.add(name) current = (current[0] or 'DEFAULT', current[1]) format_dict = {'dep_option': name[1], 'dep_group': name[0], 'option': current[1], 'group': current[0]} _report_deprecation(self._deprecated_opt_message, format_dict) def _get_value(self, names, multi=False, positional=False, current_name=None, normalized=True): """Fetch a value from config files. Multiple names for a given configuration option may be supplied so that we can transparently handle files containing deprecated option names or groups. :param names: a list of (section, name) tuples :param positional: whether this is a positional option :param multi: a boolean indicating whether to return multiple values :param normalized: whether to normalize group names to lowercase """ # NOTE(dhellmann): We don't have a way to track which options # that are registered as command line values show up on the # command line or in the configuration files. So we look up # the value in the file first to get the location, and then # try looking it up as a CLI value in case it was set there. # Set a default location indicating that the value came from # the command line. This will be overridden if we find a value # in a file. loc = LocationInfo(Locations.command_line, '') try: file_names = [(g if g is not None else 'DEFAULT', n) for g, n in names] values, loc = self._get_file_value( file_names, multi=multi, normalized=normalized, current_name=current_name) except KeyError: # If we receive a KeyError when looking for the CLI, just # go ahead and throw it because we know we don't have a # value. raise_later = True else: raise_later = False # Now try the CLI try: value = self._get_cli_value(names, positional) return (value, loc) except KeyError: if raise_later: # Re-raise to indicate that we haven't found the value # anywhere. raise # Return the value we found in the file. return (values if multi else values[-1], loc) def _sections(self): for sections in self._parsed: for section in sections: yield section class _CachedArgumentParser(argparse.ArgumentParser): """class for caching/collecting command line arguments. It also sorts the arguments before initializing the ArgumentParser. We need to do this since ArgumentParser by default does not sort the argument options and the only way to influence the order of arguments in '--help' is to ensure they are added in the sorted order. """ def __init__(self, prog=None, usage=None, **kwargs): super(_CachedArgumentParser, self).__init__(prog, usage, **kwargs) self._args_cache = {} def add_parser_argument(self, container, *args, **kwargs): values = [] if container in self._args_cache: values = self._args_cache[container] values.append({'args': args, 'kwargs': kwargs}) self._args_cache[container] = values def initialize_parser_arguments(self): # NOTE(mfedosin): The code below looks a little bit weird, but # it's done because we need to sort only optional opts and do # not touch positional. For the reason optional opts go first in # the values we only need to find an index of the first positional # option and then sort the values slice. for container, values in self._args_cache.items(): index = 0 has_positional = False for index, argument in enumerate(values): if not argument['args'][0].startswith('-'): has_positional = True break size = index if has_positional else len(values) values[:size] = sorted(values[:size], key=lambda x: x['args']) for argument in values: try: container.add_argument(*argument['args'], **argument['kwargs']) except argparse.ArgumentError: options = ','.join(argument['args']) raise DuplicateOptError(options) self._args_cache = {} def parse_args(self, args=None, namespace=None): self.initialize_parser_arguments() return super(_CachedArgumentParser, self).parse_args(args, namespace) def print_help(self, file=None): self.initialize_parser_arguments() super(_CachedArgumentParser, self).print_help(file) def print_usage(self, file=None): self.initialize_parser_arguments() super(_CachedArgumentParser, self).print_usage(file) class ConfigOpts(abc.Mapping): """Config options which may be set on the command line or in config files. ConfigOpts is a configuration option manager with APIs for registering option schemas, grouping options, parsing option values and retrieving the values of options. It has built-in support for :oslo.config:option:`config_file` and :oslo.config:option:`config_dir` options. .. versionchanged:: 9.5.0 Added shell-completion option for generate a shell completion script. """ disallow_names = ('project', 'prog', 'version', 'usage', 'default_config_files', 'default_config_dirs') supported_shell_completion = ['bash', 'zsh'] # NOTE(dhellmann): This instance is reused by list_opts(). _config_source_opt = ListOpt( 'config_source', metavar='SOURCE', default=[], help=('Lists configuration groups that provide more ' 'details for accessing configuration settings ' 'from locations other than local files.'), ) # Add option for generate a shell completion script _shell_completion_opt = StrOpt( 'shell_completion', choices=supported_shell_completion, help='Display a shell completion script' ) def __init__(self): """Construct a ConfigOpts object.""" self._opts = {} # dict of dicts of (opt:, override:, default:) self._groups = {} self._deprecated_opts = {} self._args = None self._oparser = None self._namespace = None self._mutable_ns = None self._mutate_hooks = set([]) self.__cache = {} self.__drivers_cache = {} self._config_opts = [] self._cli_opts = collections.deque() self._validate_default_values = False self._sources = [] self._ext_mgr = None # Though the env_driver is a Source, we load it by default. self._use_env = True self._env_driver = _environment.EnvironmentConfigurationSource() self.register_opt(self._config_source_opt) self.register_cli_opt(self._shell_completion_opt) def _pre_setup(self, project, prog, version, usage, description, epilog, default_config_files, default_config_dirs): """Initialize a ConfigCliParser object for option parsing.""" if prog is None: prog = os.path.basename(sys.argv[0]) if prog.endswith(".py"): prog = prog[:-3] if default_config_files is None: default_config_files = find_config_files(project, prog) if default_config_dirs is None: default_config_dirs = find_config_dirs(project, prog) self._oparser = _CachedArgumentParser( prog=prog, usage=usage, description=description, epilog=epilog) if version is not None: self._oparser.add_parser_argument(self._oparser, '--version', action='version', version=version) return prog, default_config_files, default_config_dirs @staticmethod def _make_config_options(default_config_files, default_config_dirs): return [ _ConfigFileOpt('config-file', default=default_config_files, metavar='PATH', help=('Path to a config file to use. Multiple ' 'config files can be specified, with values ' 'in later files taking precedence. Defaults ' 'to %(default)s. This option must be set ' 'from the command-line.')), _ConfigDirOpt('config-dir', metavar='DIR', default=default_config_dirs, help='Path to a config directory to pull `*.conf` ' 'files from. This file set is sorted, so as to ' 'provide a predictable parse order if ' 'individual options are over-ridden. The set ' 'is parsed after the file(s) specified via ' 'previous --config-file, arguments hence ' 'over-ridden options in the directory take ' 'precedence. This option must be set from ' 'the command-line.'), ] @classmethod def _list_options_for_discovery(cls, default_config_files, default_config_dirs): "Return options to be used by list_opts() for the sample generator." options = cls._make_config_options(default_config_files, default_config_dirs) options.append(cls._config_source_opt) return options def _setup(self, project, prog, version, usage, default_config_files, default_config_dirs, use_env): """Initialize a ConfigOpts object for option parsing.""" self._config_opts = self._make_config_options(default_config_files, default_config_dirs) self.register_cli_opts(self._config_opts) self.project = project self.prog = prog self.version = version self.usage = usage self.default_config_files = default_config_files self.default_config_dirs = default_config_dirs self._use_env = use_env def __clear_cache(f): @functools.wraps(f) def __inner(self, *args, **kwargs): if kwargs.pop('clear_cache', True): result = f(self, *args, **kwargs) self.__cache.clear() return result else: return f(self, *args, **kwargs) return __inner def __clear_drivers_cache(f): @functools.wraps(f) def __inner(self, *args, **kwargs): if kwargs.pop('clear_drivers_cache', True): result = f(self, *args, **kwargs) self.__drivers_cache.clear() return result else: return f(self, *args, **kwargs) return __inner def __call__(self, args=None, project=None, prog=None, version=None, usage=None, default_config_files=None, default_config_dirs=None, validate_default_values=False, description=None, epilog=None, use_env=True): """Parse command line arguments and config files. Calling a ConfigOpts object causes the supplied command line arguments and config files to be parsed, causing opt values to be made available as attributes of the object. The object may be called multiple times, each time causing the previous set of values to be overwritten. Automatically registers the --config-file option with either a supplied list of default config files, or a list from find_config_files(). If the --config-dir option is set, any *.conf files from this directory are pulled in, after all the file(s) specified by the --config-file option. :param args: command line arguments (defaults to sys.argv[1:]) :param project: the toplevel project name, used to locate config files :param prog: the name of the program (defaults to sys.argv[0] basename, without extension .py) :param version: the program version (for --version) :param usage: a usage string (%prog will be expanded) :param description: A description of what the program does :param epilog: Text following the argument descriptions :param default_config_files: config files to use by default :param default_config_dirs: config dirs to use by default :param validate_default_values: whether to validate the default values :param use_env: If True (the default) look in the environment as one source of option values. :raises: SystemExit, ConfigFilesNotFoundError, ConfigFileParseError, ConfigFilesPermissionDeniedError, RequiredOptError, DuplicateOptError .. versionchanged:: 9.5.0 Added shell-completion option for generate a shell completion script. """ self.clear() self._validate_default_values = validate_default_values prog, default_config_files, default_config_dirs = self._pre_setup( project, prog, version, usage, description, epilog, default_config_files, default_config_dirs) self._setup(project, prog, version, usage, default_config_files, default_config_dirs, use_env) # This is necessary to analyse first args now, # because if there are subcommands, # these are mandatory even if you only want to use # the --shell_completion option argv = args if args is not None else sys.argv[1:] if len(argv) > 1 and argv[0] == '--shell_completion' \ and args[1] in self.supported_shell_completion: shell = argv[1] self._print_shell_completion(shell) sys.exit(0) self._namespace = self._parse_cli_opts(argv) if self._namespace._files_not_found: raise ConfigFilesNotFoundError(self._namespace._files_not_found) if self._namespace._files_permission_denied: raise ConfigFilesPermissionDeniedError( self._namespace._files_permission_denied) self._load_alternative_sources() self._check_required_opts() def _print_shell_completion(self, shell): """Print shell completion Script :param shell: name of shell to generate script, actually bash or zsh """ maps, descr, opts, opts_sub, args = {}, {}, {}, {}, {} multi = [] template = os.path.join(os.path.dirname(os.path.abspath(__file__)), "templates", f"{shell}-completion.template") for opt, group in self._all_cli_opts(): if isinstance(opt, SubCommandOpt): # If a subcommand, call _add_to_cli, for getting the subparser opt._add_to_cli(self._oparser, group) else: name = f"{group}_{opt.dest}" \ if not (group is None and group != '') \ else f"{opt.dest}" if opt.multi: multi.append(name) opts.setdefault(name, []) opts[name] += [f"--{name}"] descr[name] = opt.help maps[f"--{name}"] = name if opt.short: opts[name] += [f"-{opt.short}"] maps[f"-{opt.short}"] = name if hasattr(opt.type, 'choices') and opt.type.choices: args[name] = ' '.join(opt.type.choices.keys()) else: args[name] = ' ' for op in self._oparser._actions: # Analyze parser for find subcommanf options and help option if isinstance(op, argparse._SubParsersAction): for k, v in op._name_parser_map.items(): descr[k] = v.description if v.description else '' opts_sub.setdefault(k, {}) for opt in v._actions: op_str = opt.option_strings descr[f"{k}_{opt.dest}"] = opt.help opts_sub[k][opt.dest] = op_str for op in op_str: maps[op] = opt.dest elif isinstance(op, argparse._HelpAction): opts.setdefault(op.dest, []) opts[op.dest] += [f"--{op.dest}", '-h'] descr[op.dest] = op.help if shell == 'bash': output = self._generate_bash_completion(template, maps=maps, opts=opts, descr=descr, opts_sub=opts_sub, multi=multi, args=args) elif shell == 'zsh': output = self._generate_zsh_completion(template, maps=maps, opts=opts, descr=descr, opts_sub=opts_sub, multi=multi, args=args) print(output) def _generate_bash_completion(self, template, maps=None, opts=None, descr=None, opts_sub=None, multi=None, args=None): """Generate a bash completaion script :param template: tamplate for generate script :param maps: a dict mapping short and long option name with destination variable :param opts: a dict of option (destination is key, short and long name is in a list in value) :param descr: dict of help message for option :param opts_sub: dict of subcommand :param multi: list of MultiOPt :param args: dict of options with arguments """ if maps is None: maps = {} if opts is None: opts = {} if descr is None: descr = {} if opts_sub is None: opts_sub = {} if multi is None: multi = [] if args is None: args = {} b_opts_sub = '' b_opts = ' '.join([f"[{k}]='{' '.join(v)}'" for k, v in opts.items()]) b_args = ' '.join([f"[{k}]='{v}'" for k, v in args.items()]) for k, v in opts_sub.items(): b_opts += f" [{k}]='{k}'" sub = '|'.join([f"{ik}=\"{' '.join(iv)}\"" for ik, iv in v.items() if len(iv) > 0]) b_opts_sub += f"[{k}]='{sub}' " b_map = ' '.join([f"[{k}]={v}" for k, v in maps.items()]) b_multi = ' '.join([f"[{k}]=true" for k in multi]) with open(template, "r") as input: output = input.read().format(scriptname=self.prog, opts=b_opts, opts_sub=b_opts_sub, args=b_args, multi=b_multi, map=b_map) return output def _generate_zsh_completion(self, template, maps=None, opts=None, descr=None, opts_sub=None, multi=None, args=None): """Generate a zsh completaion script :param template: tamplate for generate script :param maps: a dict mapping short and long option name with destination variable :param opts: a dict of option (destination is key, short and long name is in a list in value) :param descr: dict of help message for option :param opts_sub: dict of subcommand :param multi: list of MultiOPt :param args: dict of options with arguments """ if maps is None: maps = {} if opts is None: opts = {} if descr is None: descr = {} if opts_sub is None: opts_sub = {} if multi is None: multi = [] if args is None: args = {} p = self.prog t = ' ' z_opts = '' for k, v in opts.items(): repeat = '*' if k in multi else f"({' '.join(v)})" o = f"{{{','.join(v)}}}" if len(v) > 1 else v[0] d = descr[k] c = f":choice:({args[k]})" if k in args else '' z_opts += f"{t*2}'{repeat}'{o}'[{d}]{c}' \\\n" if opts_sub: z_opts += f"{t*2}'*::{p} command:_{p}_commands'\n" c_list = '' c_opts = '' for k, v in opts_sub.items(): desc = descr[k] if descr[k] else '' c_list += f"{t*2}'{k}:{desc}'\n" c_opts += f"{t*3}{k})\n" c_opts += f"{t*4}_arguments -s \\\n" for ik, iv in v.items(): if len(iv) > 1: c_opts += f"{t*4}'({' '.join(iv)})'{{{','.join(iv)}}}" elif len(iv) == 1: c_opts += f"{t*4}{iv[0]}" desc = descr[k+'_'+ik] if descr[k+'_'+ik] else '' c_opts += f"'[{desc}]' \\\n" c_opts += f"\n{t*4};;\n" with open(template, "r") as input: output = input.read().format(scriptname=p, opts=z_opts, commands_list=c_list, commands_opts=c_opts) return output def _load_alternative_sources(self): # Look for other sources of option data. for source_group_name in self.config_source: source = self._open_source_from_opt_group(source_group_name) if source is not None: self._sources.append(source) def _open_source_from_opt_group(self, group_name): if not self._ext_mgr: self._ext_mgr = stevedore.ExtensionManager( "oslo.config.driver", invoke_on_load=True) self.register_opt( StrOpt('driver', choices=self._ext_mgr.names(), help=_SOURCE_DRIVER_OPTION_HELP), group=group_name) try: driver_name = self[group_name].driver except ConfigFileValueError as err: LOG.error( "could not load configuration from %r. %s", group_name, err.msg) return None if driver_name is None: LOG.error( "could not load configuration from %r, no 'driver' is set.", group_name) return None LOG.info('loading configuration from %r using %r', group_name, driver_name) driver = self._ext_mgr[driver_name].obj try: return driver.open_source_from_opt_group(self, group_name) except Exception as err: LOG.error( "could not load configuration from %r using %s driver: %s", group_name, driver_name, err) return None def __getattr__(self, name): """Look up an option value and perform string substitution. :param name: the opt name (or 'dest', more precisely) :returns: the option value (after string substitution) or a GroupAttr :raises: ValueError or NoSuchOptError """ try: return self._get(name) except ValueError: raise except Exception: raise NoSuchOptError(name) def __getitem__(self, key): """Look up an option value and perform string substitution.""" return self.__getattr__(key) def __contains__(self, key): """Return True if key is the name of a registered opt or group.""" return key in self._opts or key in self._groups def __iter__(self): """Iterate over all registered opt and group names.""" for key in itertools.chain(list(self._opts.keys()), list(self._groups.keys())): yield key def __len__(self): """Return the number of options and option groups.""" return len(self._opts) + len(self._groups) def reset(self): """Clear the object state and unset overrides and defaults.""" self._unset_defaults_and_overrides() self.clear() @__clear_cache def clear(self): """Reset the state of the object to before options were registered. This method removes all registered options and discards the data from the command line and configuration files. Any subparsers added using the add_cli_subparsers() will also be removed as a side-effect of this method. """ self._args = None self._oparser = None self._namespace = None self._mutable_ns = None # Keep _mutate_hooks self._validate_default_values = False self.unregister_opts(self._config_opts) for group in self._groups.values(): group._clear() def _add_cli_opt(self, opt, group): if {'opt': opt, 'group': group} in self._cli_opts: return if opt.positional: self._cli_opts.append({'opt': opt, 'group': group}) else: self._cli_opts.appendleft({'opt': opt, 'group': group}) def _track_deprecated_opts(self, opt, group=None): if hasattr(opt, 'deprecated_opts'): for dep_opt in opt.deprecated_opts: dep_group = dep_opt.group or 'DEFAULT' dep_dest = dep_opt.name if dep_dest: dep_dest = dep_dest.replace('-', '_') if dep_group not in self._deprecated_opts: self._deprecated_opts[dep_group] = { dep_dest: { 'opt': opt, 'group': group } } else: self._deprecated_opts[dep_group][dep_dest] = { 'opt': opt, 'group': group } @__clear_cache def register_opt(self, opt, group=None, cli=False): """Register an option schema. Registering an option schema makes any option value which is previously or subsequently parsed from the command line or config files available as an attribute of this object. :param opt: an instance of an Opt sub-class :param group: an optional OptGroup object or group name :param cli: whether this is a CLI option :return: False if the opt was already registered, True otherwise :raises: DuplicateOptError """ if group is not None: group = self._get_group(group, autocreate=True) if cli: self._add_cli_opt(opt, group) self._track_deprecated_opts(opt, group=group) return group._register_opt(opt, cli) # NOTE(gcb) We can't use some names which are same with attributes of # Opts in default group. They includes project, prog, version, usage, # default_config_files and default_config_dirs. if group is None: if opt.name in self.disallow_names: raise ValueError('Name %s was reserved for oslo.config.' % opt.name) if cli: self._add_cli_opt(opt, None) if _is_opt_registered(self._opts, opt): return False self._opts[opt.dest] = {'opt': opt, 'cli': cli} self._track_deprecated_opts(opt) return True @__clear_cache def register_opts(self, opts, group=None): """Register multiple option schemas at once.""" for opt in opts: self.register_opt(opt, group, clear_cache=False) @__clear_cache def register_cli_opt(self, opt, group=None): """Register a CLI option schema. CLI option schemas must be registered before the command line and config files are parsed. This is to ensure that all CLI options are shown in --help and option validation works as expected. :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, ArgsAlreadyParsedError """ if self._args is not None: raise ArgsAlreadyParsedError("cannot register CLI option") return self.register_opt(opt, group, cli=True, clear_cache=False) @__clear_cache 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, clear_cache=False) def register_group(self, group): """Register an option group. An option group must be registered before options can be registered with the group. :param group: an OptGroup object """ if group.name in self._groups: return self._groups[group.name] = copy.copy(group) @__clear_cache def unregister_opt(self, opt, group=None): """Unregister an option. :param opt: an Opt object :param group: an optional OptGroup object or group name :raises: ArgsAlreadyParsedError, NoSuchGroupError """ if self._args is not None: raise ArgsAlreadyParsedError("reset before unregistering options") remitem = None for item in self._cli_opts: if (item['opt'].dest == opt.dest and (group is None or self._get_group(group).name == item['group'].name)): remitem = item break if remitem is not None: self._cli_opts.remove(remitem) if group is not None: self._get_group(group)._unregister_opt(opt) elif opt.dest in self._opts: del self._opts[opt.dest] @__clear_cache def unregister_opts(self, opts, group=None): """Unregister multiple CLI option schemas at once.""" for opt in opts: self.unregister_opt(opt, group, clear_cache=False) def import_opt(self, name, module_str, group=None): """Import an option definition from a module. Import a module and check that a given option is registered. This is intended for use with global configuration objects like cfg.CONF where modules commonly register options with CONF at module load time. If one module requires an option defined by another module it can use this method to explicitly declare the dependency. :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 """ __import__(module_str) self._get_opt_info(name, group) def import_group(self, group, module_str): """Import an option group from a module. Import a module and check that a given option group is registered. This is intended for use with global configuration objects like cfg.CONF where modules commonly register options with CONF at module load time. If one module requires an option group defined by another module it can use this method to explicitly declare the dependency. :param group: an option OptGroup object or group name :param module_str: the name of a module to import :raises: ImportError, NoSuchGroupError """ __import__(module_str) self._get_group(group) @__clear_cache def set_override(self, name, override, group=None): """Override an opt value. Override the command line, config file and default values of a given option. :param name: the name/dest of the opt :param override: the override value :param group: an option OptGroup object or group name :raises: NoSuchOptError, NoSuchGroupError """ opt_info = self._get_opt_info(name, group) opt_info['override'] = self._get_enforced_type_value( opt_info['opt'], override) opt_info['location'] = LocationInfo( Locations.set_override, _get_caller_detail(3), # this function has a decorator to skip ) @__clear_cache def set_default(self, name, default, group=None): """Override an opt's default value. Override the default value of given option. A command line or config file value will still take precedence over this default. :param name: the name/dest of the opt :param default: the default value :param group: an option OptGroup object or group name :raises: NoSuchOptError, NoSuchGroupError """ opt_info = self._get_opt_info(name, group) opt_info['default'] = self._get_enforced_type_value( opt_info['opt'], default) opt_info['location'] = LocationInfo( Locations.set_default, _get_caller_detail(3), # this function has a decorator to skip ) def _get_enforced_type_value(self, opt, value): if value is None: return None return self._convert_value(value, opt) @__clear_cache def clear_override(self, name, group=None): """Clear an override an opt value. Clear a previously set override of the command line, config file and default values of a given option. :param name: the name/dest of the opt :param group: an option OptGroup object or group name :raises: NoSuchOptError, NoSuchGroupError """ opt_info = self._get_opt_info(name, group) opt_info.pop('override', None) @__clear_cache def clear_default(self, name, group=None): """Clear an override an opt's default value. Clear a previously set override of the default value of given option. :param name: the name/dest of the opt :param group: an option OptGroup object or group name :raises: NoSuchOptError, NoSuchGroupError """ opt_info = self._get_opt_info(name, group) opt_info.pop('default', None) def _all_opt_infos(self): """A generator function for iteration opt infos.""" for info in self._opts.values(): yield info, None for group in self._groups.values(): for info in group._opts.values(): yield info, group def _all_cli_opts(self): """A generator function for iterating CLI opts.""" for item in self._cli_opts: yield item['opt'], item['group'] def _unset_defaults_and_overrides(self): """Unset any default or override on all options.""" for info, group in self._all_opt_infos(): info.pop('default', None) info.pop('override', None) @property def config_dirs(self): if self._namespace is None: return [] return self._namespace._config_dirs def find_file(self, name): """Locate a file located alongside the config files. Search for a file with the supplied basename in the directories which we have already loaded config files from and other known configuration directories. The directory, if any, supplied by the config_dir option is searched first. Then the config_file option is iterated over and each of the base directories of the config_files values are searched. Failing both of these, the standard directories searched by the module level find_config_files() function is used. The first matching file is returned. :param name: the filename, for example 'policy.json' :returns: the path to a matching file, or None """ if not self._namespace: raise NotInitializedError() dirs = [] if self._namespace._config_dirs: for directory in self._namespace._config_dirs: dirs.append(_fixpath(directory)) for cf in reversed(self.config_file): dirs.append(os.path.dirname(_fixpath(cf))) dirs.extend(_get_config_dirs(self.project)) return _search_dirs(dirs, name) def log_opt_values(self, logger, lvl): """Log the value of all registered opts. It's often useful for an app to log its configuration to a log file at startup for debugging. This method dumps to the entire config state to the supplied logger at a given log level. :param logger: a logging.Logger object :param lvl: the log level (for example logging.DEBUG) arg to logger.log() """ logger.log(lvl, "*" * 80) logger.log(lvl, "Configuration options gathered from:") logger.log(lvl, "command line args: %s", self._args) logger.log(lvl, "config files: %s", hasattr(self, 'config_file') and self.config_file or []) logger.log(lvl, "=" * 80) def _sanitize(opt, value): """Obfuscate values of options declared secret.""" return value if not opt.secret else '*' * 4 for opt_name in sorted(self._opts): opt = self._get_opt_info(opt_name)['opt'] logger.log(lvl, "%-30s = %s", opt_name, _sanitize(opt, getattr(self, opt_name))) for group_name in list(self._groups): group_attr = self.GroupAttr(self, self._get_group(group_name)) for opt_name in sorted(self._groups[group_name]._opts): opt = self._get_opt_info(opt_name, group_name)['opt'] logger.log(lvl, "%-30s = %s", "%s.%s" % (group_name, opt_name), _sanitize(opt, getattr(group_attr, opt_name))) logger.log(lvl, "*" * 80) def print_usage(self, file=None): """Print the usage message for the current program. This method is for use after all CLI options are known registered using __call__() method. If this method is called before the __call__() is invoked, it throws NotInitializedError :param file: the File object (if None, output is on sys.stdout) :raises: NotInitializedError """ if not self._oparser: raise NotInitializedError() self._oparser.print_usage(file) def print_help(self, file=None): """Print the help message for the current program. This method is for use after all CLI options are known registered using __call__() method. If this method is called before the __call__() is invoked, it throws NotInitializedError :param file: the File object (if None, output is on sys.stdout) :raises: NotInitializedError """ if not self._oparser: raise NotInitializedError() self._oparser.print_help(file) def _get(self, name, group=None, namespace=None): if isinstance(group, OptGroup): key = (group.name, name) else: key = (group, name) if namespace is None: try: return self.__cache[key] except KeyError: # nosec: Valid control flow instruction pass value, loc = self._do_get(name, group, namespace) self.__cache[key] = value return value def _do_get(self, name, group=None, namespace=None): """Look up an option value. :param name: the opt name (or 'dest', more precisely) :param group: an OptGroup :param namespace: the namespace object to get the option value from :returns: 2-tuple of the option value or a GroupAttr object and LocationInfo or None :raises: NoSuchOptError, NoSuchGroupError, ConfigFileValueError, TemplateSubstitutionError """ if group is None and name in self._groups: return (self.GroupAttr(self, self._get_group(name)), None) info = self._get_opt_info(name, group) opt = info['opt'] if 'location' in info: loc = info['location'] else: loc = opt._set_location if isinstance(opt, SubCommandOpt): return (self.SubCommandAttr(self, group, opt.dest), None) if 'override' in info: return (self._substitute(info['override']), loc) def convert(value): return self._convert_value( self._substitute(value, group, namespace), opt) group_name = group.name if group else None key = (group_name, name) # If use_env is true, get a value from the environment but don't use # it yet. We will look at the command line first, below. env_val = (sources._NoValue, None) if self._use_env: env_val = self._env_driver.get(group_name, name, opt) if opt.mutable and namespace is None: namespace = self._mutable_ns if namespace is None: namespace = self._namespace if namespace is not None: try: alt_loc = None try: val, alt_loc = opt._get_from_namespace(namespace, group_name) # Try command line first if (val != sources._NoValue and alt_loc.location == Locations.command_line): return (convert(val), alt_loc) # Environment source second if env_val[0] != sources._NoValue: return (convert(env_val[0]), env_val[1]) # Default file source third if val != sources._NoValue: return (convert(val), alt_loc) except KeyError: # nosec: Valid control flow instruction alt_loc = LocationInfo( Locations.environment, self._env_driver.get_name(group_name, name), ) # If there was a KeyError looking at config files or # command line, retry the env_val. if env_val[0] != sources._NoValue: return (convert(env_val[0]), env_val[1]) except ValueError as ve: message = "Value for option %s from %s is not valid: %s" % ( opt.name, alt_loc, str(ve)) # Preserve backwards compatibility for file-based value # errors. if alt_loc.location == Locations.user: raise ConfigFileValueError(message) raise ConfigSourceValueError(message) try: return self.__drivers_cache[key] except KeyError: # nosec: Valid control flow instruction pass for source in self._sources: val = source.get(group_name, name, opt) if val[0] != sources._NoValue: result = (convert(val[0]), val[1]) self.__drivers_cache[key] = result return result if 'default' in info: return (self._substitute(info['default']), loc) if self._validate_default_values: if opt.default is not None: try: convert(opt.default) except ValueError as e: raise ConfigFileValueError( "Default value for option %s is not valid: %s" % (opt.name, str(e))) if opt.default is not None: return (convert(opt.default), loc) return (None, None) def _substitute(self, value, group=None, namespace=None): """Perform string template substitution. Substitute any template variables (for example $foo, ${bar}) in the supplied string value(s) with opt values. :param value: the string value, or list of string values :param group: the group that retrieves the option value from :param namespace: the namespace object that retrieves the option value from :returns: the substituted string(s) """ if isinstance(value, list): return [self._substitute(i, group=group, namespace=namespace) for i in value] elif isinstance(value, str): # Treat a backslash followed by the dollar sign "\$" # the same as the string template escape "$$" as it is # a bit more natural for users if r'\$' in value: value = value.replace(r'\$', '$$') tmpl = self.Template(value) ret = tmpl.safe_substitute( self.StrSubWrapper(self, group=group, namespace=namespace)) return ret elif isinstance(value, dict): # Substitute template variables in both key and value return {self._substitute(key, group=group, namespace=namespace): self._substitute(val, group=group, namespace=namespace) for key, val in value.items()} else: return value class Template(string.Template): idpattern = r'[_a-z][\._a-z0-9]*' def _convert_value(self, value, opt): """Perform value type conversion. Converts values using option's type. Handles cases when value is actually a list of values (for example for multi opts). :param value: the string value, or list of string values :param opt: option definition (instance of Opt class or its subclasses) :returns: converted value """ if opt.multi: return [opt.type(v) for v in value] else: return opt.type(value) def _get_group(self, group_or_name, autocreate=False): """Looks up a OptGroup object. Helper function to return an OptGroup given a parameter which can either be the group's name or an OptGroup object. The OptGroup object returned is from the internal dict of OptGroup objects, which will be a copy of any OptGroup object that users of the API have access to. If autocreate is True, the group will be created if it's not found. If group is an instance of OptGroup, that same instance will be registered, otherwise a new instance of OptGroup will be created. :param group_or_name: the group's name or the OptGroup object itself :param autocreate: whether to auto-create the group if it's not found :raises: NoSuchGroupError """ group = group_or_name if isinstance(group_or_name, OptGroup) else None group_name = group.name if group else group_or_name if group_name not in self._groups: if not autocreate: raise NoSuchGroupError(group_name) self.register_group(group or OptGroup(name=group_name)) return self._groups[group_name] def _find_deprecated_opts(self, opt_name, group=None): real_opt_name = None real_group_name = None group_name = group or 'DEFAULT' if hasattr(group_name, 'name'): group_name = group_name.name dep_group = self._deprecated_opts.get(group_name) if dep_group: real_opt_dict = dep_group.get(opt_name) if real_opt_dict: real_opt_name = real_opt_dict['opt'].name if real_opt_dict['group']: real_group_name = real_opt_dict['group'].name return real_opt_name, real_group_name def _get_opt_info(self, opt_name, group=None): """Return the (opt, override, default) dict for an opt. :param opt_name: an opt name/dest :param group: an optional group name or OptGroup object :raises: NoSuchOptError, NoSuchGroupError """ if group is None: opts = self._opts else: group = self._get_group(group) opts = group._opts if opt_name not in opts: real_opt_name, real_group_name = self._find_deprecated_opts( opt_name, group=group) if not real_opt_name: raise NoSuchOptError(opt_name, group) log_real_group_name = real_group_name or 'DEFAULT' dep_message = ('Config option %(dep_group)s.%(dep_option)s ' ' is deprecated. Use option %(group)s.' '%(option)s instead.') LOG.warning(dep_message, {'dep_option': opt_name, 'dep_group': group, 'option': real_opt_name, 'group': log_real_group_name}) opt_name = real_opt_name if real_group_name: group = self._get_group(real_group_name) opts = group._opts return opts[opt_name] def _check_required_opts(self, namespace=None): """Check that all opts marked as required have values specified. :param namespace: the namespace object be checked the required options :raises: RequiredOptError """ for info, group in self._all_opt_infos(): opt = info['opt'] if opt.required: if 'default' in info or 'override' in info: continue if self._get(opt.dest, group, namespace) is None: raise RequiredOptError(opt.name, group) def _parse_cli_opts(self, args): """Parse command line options. Initializes the command line option parser and parses the supplied command line arguments. :param args: the command line arguments :returns: a _Namespace object containing the parsed option values :raises: SystemExit, DuplicateOptError ConfigFileParseError, ConfigFileValueError """ self._args = args for opt, group in self._all_cli_opts(): opt._add_to_cli(self._oparser, group) return self._parse_config_files() def _parse_config_files(self): """Parse configure files options. :raises: SystemExit, ConfigFilesNotFoundError, ConfigFileParseError, ConfigFilesPermissionDeniedError, RequiredOptError, DuplicateOptError """ namespace = _Namespace(self) # handle --config-file args or the default_config_files for arg in self._args: if arg == '--config-file' or arg.startswith('--config-file='): break else: for config_file in self.default_config_files: ConfigParser._parse_file(config_file, namespace) # handle --config-dir args or the default_config_dirs for arg in self._args: if arg == '--config-dir' or arg.startswith('--config-dir='): break else: for config_dir in self.default_config_dirs: # for the default config-dir directories we just continue # if the directories do not exist. This is different to the # case where --config-dir is given on the command line. if not os.path.exists(config_dir): continue config_dir_glob = os.path.join(config_dir, '*.conf') for config_file in sorted(glob.glob(config_dir_glob)): ConfigParser._parse_file(config_file, namespace) self._oparser.parse_args(self._args, namespace) self._validate_cli_options(namespace) return namespace def _validate_cli_options(self, namespace): for opt, group in sorted(self._all_cli_opts(), key=lambda x: x[0].name): group_name = group.name if group else None try: value, loc = opt._get_from_namespace(namespace, group_name) except KeyError: continue value = self._substitute(value, group=group, namespace=namespace) try: self._convert_value(value, opt) except ValueError: sys.stderr.write("argument --%s: Invalid %s value: %s\n" % ( opt.dest, repr(opt.type), value)) raise SystemExit(1) def _reload_config_files(self): namespace = self._parse_config_files() if namespace._files_not_found: raise ConfigFilesNotFoundError(namespace._files_not_found) if namespace._files_permission_denied: raise ConfigFilesPermissionDeniedError( namespace._files_permission_denied) self._check_required_opts(namespace) return namespace @__clear_cache @__clear_drivers_cache def reload_config_files(self): """Reload configure files and parse all options :return: False if reload configure files failed or else return True """ try: namespace = self._reload_config_files() except SystemExit as exc: LOG.warning("Caught SystemExit while reloading configure " "files with exit code: %d", exc.code) return False except Error as err: LOG.warning("Caught Error while reloading configure files: " "%s", err) return False else: self._namespace = namespace return True def register_mutate_hook(self, hook): """Registers a hook to be called by mutate_config_files. :param hook: a function accepting this ConfigOpts object and a dict of config mutations, as returned by mutate_config_files. :return: None """ self._mutate_hooks.add(hook) def mutate_config_files(self): """Reload configure files and parse all options. Only options marked as 'mutable' will appear to change. Hooks are called in a NON-DETERMINISTIC ORDER. Do not expect hooks to be called in the same order as they were added. :return: {(None or 'group', 'optname'): (old_value, new_value), ... } :raises: Error if reloading fails """ self.__cache.clear() old_mutate_ns = self._mutable_ns or self._namespace self._mutable_ns = self._reload_config_files() self._warn_immutability() fresh = self._diff_ns(old_mutate_ns, self._mutable_ns) def key_fn(item): # Py3 won't sort heterogeneous types. Sort None as TAB which has a # very low ASCII value. (groupname, optname) = item[0] return item[0] if groupname else ('\t', optname) sorted_fresh = sorted(fresh.items(), key=key_fn) for (groupname, optname), (old, new) in sorted_fresh: groupname = groupname if groupname else 'DEFAULT' LOG.info("Option %(group)s.%(option)s changed from " "[%(old_val)s] to [%(new_val)s]", {'group': groupname, 'option': optname, 'old_val': old, 'new_val': new}) for hook in self._mutate_hooks: hook(self, fresh) return fresh def _warn_immutability(self): """Check immutable opts have not changed. _do_get won't return the new values but presumably someone changed the config file expecting them to change so we should warn them they won't. """ for info, group in self._all_opt_infos(): opt = info['opt'] if opt.mutable: continue groupname = group.name if group else 'DEFAULT' try: old, _ = opt._get_from_namespace(self._namespace, groupname) except KeyError: old = None try: new, _ = opt._get_from_namespace(self._mutable_ns, groupname) except KeyError: new = None if old != new: LOG.warning("Ignoring change to immutable option " "%(group)s.%(option)s", {"group": groupname, "option": opt.name}) def _diff_ns(self, old_ns, new_ns): """Compare mutable option values between two namespaces. This can be used to only reconfigure stateful sessions when necessary. :return {(None or 'group', 'optname'): (old_value, new_value), ... } """ diff = {} for info, group in self._all_opt_infos(): opt = info['opt'] if not opt.mutable: continue groupname = group.name if group else None try: old, _ = opt._get_from_namespace(old_ns, groupname) except KeyError: old = None try: new, _ = opt._get_from_namespace(new_ns, groupname) except KeyError: new = None if old != new: diff[(groupname, opt.name)] = (old, new) return diff def list_all_sections(self): """List all sections from the configuration. Returns a sorted list of all section names found in the configuration files, whether declared beforehand or not. """ s = set([]) if self._mutable_ns: s |= set(self._mutable_ns._sections()) if self._namespace: s |= set(self._namespace._sections()) return sorted(s) def get_location(self, name, group=None): """Return the location where the option is being set. :param name: The name of the option. :type name: str :param group: The name of the group of the option. Defaults to ``'DEFAULT'``. :type group: str :return: LocationInfo .. seealso:: :doc:`/reference/locations` .. versionadded:: 5.3.0 """ opt_group = OptGroup(group) if group is not None else None value, loc = self._do_get(name, opt_group, None) return loc class GroupAttr(abc.Mapping): """Helper class. 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 def __getattr__(self, name): """Look up an option value and perform template substitution.""" return self._conf._get(name, self._group) def __getitem__(self, key): """Look up an option value and perform string substitution.""" return self.__getattr__(key) def __contains__(self, key): """Return True if key is the name of a registered opt or group.""" return key in self._group._opts def __iter__(self): """Iterate over all registered opt and group names.""" for key in self._group._opts.keys(): yield key def __len__(self): """Return the number of options and option groups.""" return len(self._group._opts) class SubCommandAttr: """Helper class. Represents the name and arguments of an argparse sub-parser. """ def __init__(self, conf, group, dest): """Construct a SubCommandAttr object. :param conf: a ConfigOpts object :param group: an OptGroup object :param dest: the name of the sub-parser """ self._conf = conf self._group = group self._dest = dest def __getattr__(self, name): """Look up a sub-parser name or argument value.""" if name == 'name': name = self._dest if self._group is not None: name = self._group.name + '_' + name return getattr(self._conf._namespace, name) if name in self._conf: raise DuplicateOptError(name) try: return getattr(self._conf._namespace, name) except AttributeError: raise NoSuchOptError(name) class StrSubWrapper: """Helper class. Exposes opt values as a dict for string substitution. """ def __init__(self, conf, group=None, namespace=None): """Construct a StrSubWrapper object. :param conf: a ConfigOpts object :param group: an OptGroup object :param namespace: the namespace object that retrieves the option value from """ self.conf = conf self.group = group self.namespace = namespace def __getitem__(self, key): """Look up an opt value from the ConfigOpts object. :param key: an opt name :returns: an opt value """ try: group_name, option = key.split(".", 1) except ValueError: group = self.group option = key else: group = OptGroup(name=group_name) try: value = self.conf._get(option, group=group, namespace=self.namespace) except NoSuchOptError: value = self.conf._get(key, namespace=self.namespace) if isinstance(value, self.conf.GroupAttr): raise TemplateSubstitutionError( 'substituting group %s not supported' % key) if value is None: return '' return value CONF = ConfigOpts() ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/fixture.py0000664000175000017500000002164200000000000020337 0ustar00zuulzuul00000000000000# # 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 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._reset_default_config_dirs) self.addCleanup(self._unregister_config_opts) self.addCleanup(self.conf.reset) self._registered_config_opts = {} # Grab an old copy of the default config files/dirs - 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 if hasattr(self.conf, 'default_config_dirs'): self._default_config_dirs = self.conf.default_config_dirs else: self._default_config_dirs = 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. """ group = kw.pop('group', None) for k, v in kw.items(): self.conf.set_override(k, v, group) 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 _reset_default_config_dirs(self): if not hasattr(self.conf, 'default_config_dirs'): return if self._default_config_dirs: self.conf.default_config_dirs = self._default_config_dirs else: # Delete, because we could conceivably begin with the property # being unset. self.conf.default_config_dirs = 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 kwargs.items(): # 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_config_dirs(self, config_dirs): """Specify a list of config dirs to read. This method allows you to predefine the list of configuration dirs 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_dirs, list): raise AttributeError("Please pass a list() to set_config_dirs()") # Make sure the namespace exists for our tests. if not self.conf._namespace: self.conf([]) self.conf.default_config_dirs = config_dirs 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) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/generator.py0000664000175000017500000007572200000000000020647 0ustar00zuulzuul00000000000000# 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/cli/generator.rst for details. .. versionadded:: 1.4 """ import collections import copy import importlib.metadata import json import logging import operator import sys try: # For oslo.config[rst-generator] from docutils import core as docutils_core from docutils.parsers.rst import nodes as docutils_nodes from docutils.parsers.rst import roles as docutils_roles import rst2txt from sphinx import roles as sphinx_roles except ImportError: import textwrap rst2txt = None import yaml from oslo_config import cfg from oslo_i18n import _message import stevedore.named # noqa LOG = logging.getLogger(__name__) UPPER_CASE_GROUP_NAMES = ['DEFAULT'] _generator_opts = [ cfg.StrOpt( 'output-file', help='Path of the file to write to. Defaults to stdout.'), cfg.IntOpt( 'wrap-width', default=70, min=0, 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.'), cfg.BoolOpt( 'summarize', default=False, help='Only output summaries of help text to config files. Retain ' 'longer help text for Sphinx documents.'), cfg.StrOpt( 'format', help='Desired format for the output.', default='ini', choices=[ ('ini', 'The only format that can be used directly with ' 'oslo.config.'), ('json', 'Intended for third-party tools that want to write ' 'config files based on the sample config data.'), ('yaml', 'Same as json'), ('rst', 'Can be used to dump the text given to Sphinx when ' 'building documentation using the Sphinx extension. ' 'Useful for debugging,') ], dest='format_'), ] 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 = '' elif isinstance(opt, (cfg.StrOpt, cfg.IntOpt, cfg.FloatOpt, cfg.IPOpt, cfg.PortOpt, cfg.HostnameOpt, cfg.HostAddressOpt, cfg.URIOpt, cfg.Opt)): default_str = str(opt.default) elif isinstance(opt, cfg.BoolOpt): default_str = str(opt.default).lower() elif isinstance(opt, (cfg.ListOpt, cfg._ConfigFileOpt, cfg._ConfigDirOpt)): default_str = ','.join(str(d) for d in 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('Unknown option type: %s', repr(opt)) default_str = str(opt.default) defaults = [default_str] results = [] for default_str in defaults: if not isinstance(default_str, str): default_str = str(default_str) 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: """Format configuration option descriptions to a file.""" def __init__(self, conf, output_file=None): """Construct an OptFormatter object. :param conf: The config object from _generator_opts :param output_file: a writeable file object """ self.output_file = output_file or sys.stdout self.wrap_width = conf.wrap_width or 998 # arbitrary line length self.minimal = conf.minimal self.summarize = conf.summarize if rst2txt: # register the default Sphinx roles for rolename, nodeclass in sphinx_roles.generic_docroles.items(): generic = docutils_roles.GenericRole(rolename, nodeclass) docutils_roles.register_local_role(rolename, generic) for rolename, func in sphinx_roles.specific_docroles.items(): docutils_roles.register_local_role(rolename, func) # plus our custom roles for rolename in ('oslo.config:option', 'oslo.config:group'): generic = docutils_roles.GenericRole(rolename, docutils_nodes.strong) docutils_roles.register_local_role(rolename, generic) 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 rst2txt: help_text = docutils_core.publish_string( source=help_text, writer=rst2txt.Writer(), settings_overrides={'wrap_width': self.wrap_width} ).decode() lines = '' for line in help_text.splitlines(): lines += '# ' + line + '\n' if line else '#\n' lines = [lines] elif 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 '' elif choice == '': return "''" return str(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 :returns: a formatted group description string """ 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): """Format a description of an option to the output file. :param opt: a cfg.Opt instance :param group_name: name of the group to which the opt is assigned :returns: a formatted opt description string """ if not opt.help: LOG.warning('"%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: # an empty line signifies a new paragraph. We only want the # summary line if self.summarize: _split = opt.help.split('\n\n') opt_help = _split[0].rstrip(':').rstrip('.') if len(_split) > 1: opt_help += '. For more information, refer to the ' opt_help += 'documentation.' else: opt_help = opt.help help_text = '%s%s (%s)' % (opt_prefix, opt_help, opt_type) else: help_text = '(%s)' % opt_type lines = self._format_help(help_text) if getattr(opt.type, 'min', None) is not None: lines.append('# Minimum value: {}\n'.format(opt.type.min)) if getattr(opt.type, 'max', None) is not None: lines.append('# Maximum value: {}\n'.format(opt.type.max)) if getattr(opt.type, 'choices', None): lines.append('# Possible values:\n') for choice in opt.type.choices: help_text = '%s - %s' % ( self._get_choice_text(choice), opt.type.choices[choice] or '') lines.extend(self._format_help(help_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: # NOTE(bnemec): opt names with a - are not valid in a config file, # but it is possible to add a DeprecatedOpt with a - name. We # want to ignore those as they won't work anyway. if d.name and '-' not in d.name: 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 opt.sample_default: lines.append( '#\n' '# This option has a sample default set, which means that\n' '# its actual default value may vary from the one documented\n' '# below.\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.replace('\n', '\n# ') if self.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, lines): """Write an arbitrary sequence of strings to the output file. :param lines: a list of arbitrary strings """ self.output_file.writelines(lines) 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: # NOTE: Normalize group names to lowe-case except those defined in # UPPER_CASE_GROUP_NAMES if group: group_name = getattr(group, 'name', str(group)) if group_name.upper() in UPPER_CASE_GROUP_NAMES: normalized_gn = group_name.upper() else: normalized_gn = group_name.lower() if normalized_gn != group_name: LOG.warning('normalizing group name %r to %r', group_name, normalized_gn) if hasattr(group, 'name'): group.name = normalized_gn else: group = normalized_gn 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, [(g, list(clean[namespace][g].values())) for g 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_driver_opts_loaders(namespaces, driver_option_name): mgr = stevedore.named.NamedExtensionManager( namespace='oslo.config.opts.' + driver_option_name, 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_driver_opts(driver_option_name, namespaces): """List the options available from plugins for drivers based on the option. :param driver_option_name: The name of the option controlling the driver options. :param namespaces: a list of namespaces registered under 'oslo.config.opts.' + driver_option_name :returns: a dict mapping driver name to option list """ all_opts = {} loaders = _get_driver_opts_loaders(namespaces, driver_option_name) for plugin_name, loader in loaders: for driver_name, option_list in loader().items(): all_opts.setdefault(driver_name, []).extend(option_list) return all_opts def _get_opt_default_updaters(namespaces): mgr = stevedore.named.NamedExtensionManager( 'oslo.config.opts.defaults', names=namespaces, warn_on_missing_entrypoint=False, 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. response = [] for namespace, loader in loaders: # The loaders return iterables for the group opts, and we need # to extend them, so build a list. namespace_values = [] # Look through the groups and find any that need drivers so we # can load those extra options. for group, group_opts in loader(): # group_opts is an iterable but we are going to extend it # so convert it to a list. group_opts = list(group_opts) if isinstance(group, cfg.OptGroup): if group.driver_option: # Load the options for all of the known drivers. driver_opts = _get_driver_opts( group.driver_option, namespaces, ) # Save the list of names of options for each # driver in the group for use later. Add the # options to the group_opts list so they are # processed along with the static options in that # group. driver_opt_names = {} for driver_name, opts in sorted(driver_opts.items()): # Multiple plugins may add values to the same # driver name, so combine the lists we do # find. driver_opt_names.setdefault(driver_name, []).extend( o.name for o in opts) group_opts.extend(opts) group._save_driver_opts(driver_opt_names) namespace_values.append((group, group_opts)) response.append((namespace, namespace_values)) return _cleanup_opts(response) def on_load_failure_callback(*args, **kwargs): raise def _output_opts(f, group, group_data): 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 f.minimal and not opt.required: pass else: f.write('\n') f.format(opt, group) 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, [(, [opt_1, opt_2])]) tuples, such as returned by _list_opts. :returns: {, {'object': , 'namespaces': [(, )]}} may be a string or a group object. is always a string. will only be set if 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 _build_entry(opt, group, namespace, conf): """Return a dict representing the passed in opt The dict will contain all public attributes of opt, as well as additional entries for namespace, choices, min, and max. Any DeprecatedOpts contained in the deprecated_opts member will be converted to a dict with the format: {'group': , 'name': } :param opt: The Opt object to represent as a dict. :param group: The name of the group containing opt. :param namespace: The name of the namespace containing opt. :param conf: The ConfigOpts object containing the options for the generator tool """ entry = {key: value for key, value in opt.__dict__.items() if not key.startswith('_')} entry['namespace'] = namespace # Where present, we store choices as an OrderedDict. The default repr for # this is not very machine readable, thus, it is switched to a list of # tuples here. In addition, in some types, choices is explicitly set to # None. Force these cases to [] so it is always an iterable type. if getattr(entry['type'], 'choices', None): entry['choices'] = list(entry['type'].choices.items()) else: entry['choices'] = [] entry['min'] = getattr(entry['type'], 'min', None) entry['max'] = getattr(entry['type'], 'max', None) entry['type'] = _format_type_name(entry['type']) deprecated_opts = [] for deprecated_opt in entry['deprecated_opts']: # NOTE(bnemec): opt names with a - are not valid in a config file, # but it is possible to add a DeprecatedOpt with a - name. We # want to ignore those as they won't work anyway. if not deprecated_opt.name or '-' not in deprecated_opt.name: deprecated_opts.append( {'group': deprecated_opt.group or group, 'name': deprecated_opt.name or entry['name'], }) entry['deprecated_opts'] = deprecated_opts return entry def _generate_machine_readable_data(groups, conf): """Create data structure for machine readable sample config Returns a dictionary with the top-level keys 'options', 'deprecated_options', and 'generator_options'. 'options' contains a dict mapping group names to a list of options in that group. Each option is represented by the result of a call to _build_entry. Only non-deprecated options are included in this list. 'deprecated_options' contains a dict mapping groups names to a list of opts from that group which were deprecated. 'generator_options' is a dict mapping the options for the sample config generator itself to their values. :param groups: A dict of groups as returned by _get_groups. :param conf: The ConfigOpts object containing the options for the generator tool """ output_data = {'options': {}, 'deprecated_options': {}, 'generator_options': {}} # See _get_groups for details on the structure of group_data for group_name, group_data in groups.items(): output_group = {'opts': [], 'help': ''} output_data['options'][group_name] = output_group for namespace in group_data['namespaces']: for opt in namespace[1]: if group_data['object']: output_group.update( group_data['object']._get_generator_data() ) else: output_group.update({ 'dynamic_group_owner': '', 'driver_option': '', 'driver_opts': {}, }) entry = _build_entry(opt, group_name, namespace[0], conf) output_group['opts'].append(entry) # Need copies of the opts because we modify them for deprecated_opt in copy.deepcopy(entry['deprecated_opts']): group = deprecated_opt.pop('group') deprecated_options = output_data['deprecated_options'] deprecated_options.setdefault(group, []) deprecated_opt['replacement_name'] = entry['name'] deprecated_opt['replacement_group'] = group_name deprecated_options[group].append(deprecated_opt) # Build the list of options in the group that are not tied to # a driver. non_driver_opt_names = [ o['name'] for o in output_group['opts'] if not any(o['name'] in output_group['driver_opts'][d] for d in output_group['driver_opts']) ] output_group['standard_opts'] = non_driver_opt_names output_data['generator_options'] = dict(conf) return output_data def i18n_representer(dumper, data): """oslo_i18n yaml representer Returns a translated to the default locale string for yaml.safe_dump :param dumper: a SafeDumper instance passed by yaml.safe_dump :param data: a oslo_i18n._message.Message instance """ serializedData = str(data.translation()) return dumper.represent_str(serializedData) def _output_machine_readable(groups, output_file, conf): """Write a machine readable sample config file Take the data returned by _generate_machine_readable_data and write it in the format specified by the format_ attribute of conf. :param groups: A dict of groups as returned by _get_groups. :param output_file: A file-like object to which the data should be written. :param conf: The ConfigOpts object containing the options for the generator tool """ output_data = _generate_machine_readable_data(groups, conf) if conf.format_ == 'yaml': yaml.SafeDumper.add_representer(_message.Message, i18n_representer) output_file.write(yaml.safe_dump(output_data, default_flow_style=False)) else: output_file.write(json.dumps(output_data, sort_keys=True)) output_file.write('\n') def _output_human_readable(namespaces, output_file): """Write an RST formated version of the docs for the options. :param groups: A list of the namespaces to use for discovery. :param output_file: A file-like object to which the data should be written. """ try: from oslo_config import sphinxext except ImportError: raise RuntimeError( 'Could not import sphinxext. ' 'Please install Sphinx and try again.', ) output_data = list(sphinxext._format_option_help( LOG, namespaces, False)) output_file.write('\n'.join(output_data)) def generate(conf, output_file=None): """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) own_file = False if output_file is None: if conf.output_file: output_file = open(conf.output_file, 'w') own_file = True else: output_file = sys.stdout groups = _get_groups(_list_opts(conf.namespace)) if conf.format_ == 'ini': formatter = _OptFormatter(conf, output_file=output_file) # Output the "DEFAULT" section as the very first section _output_opts(formatter, 'DEFAULT', groups.pop('DEFAULT')) # 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) elif conf.format_ == 'rst': _output_human_readable( conf.namespace, output_file=output_file, ) else: _output_machine_readable(groups, output_file=output_file, conf=conf) if own_file: output_file.close() def main(args=None): """The main function of oslo-config-generator.""" version = importlib.metadata.version('oslo.config') logging.basicConfig(level=logging.WARN) conf = cfg.ConfigOpts() register_cli_opts(conf) try: conf(args, version=version) except cfg.RequiredOptError: conf.print_help() if not sys.argv[1:]: raise SystemExit(1) raise generate(conf) if __name__ == '__main__': main() ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/iniparser.py0000664000175000017500000001014700000000000020643 0ustar00zuulzuul00000000000000# 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: 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) ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1724318217.1909974 oslo.config-9.6.0/oslo_config/sources/0000775000175000017500000000000000000000000017755 5ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/sources/__init__.py0000664000175000017500000001130200000000000022063 0ustar00zuulzuul00000000000000# 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""" Oslo.config's primary source of configuration data are plaintext, INI-like style, configuration files. With the addition of backend drivers support, it is possible to store configuration data in different places and with different formats, as long as there exists a proper driver to connect to those external sources and provide a way to fetch configuration values from them. A backend driver implementation is divided in two main classes, a driver class of type :class:`ConfigurationSourceDriver` and a configuration source class of type :class:`ConfigurationSource`. **IMPORTANT:** At this point, all backend drivers are only able to provide immutable values. This protects applications and services from having options in external sources mutated when they reload configuration files. """ import abc # We cannot use None as a sentinel indicating a missing value because it # may be a valid value or default, so we use a custom singleton instead. _NoValue = object() class ConfigurationSourceDriver(metaclass=abc.ABCMeta): """A backend driver option for oslo.config. For each group name listed in **config_source** in the **DEFAULT** group, a :class:`ConfigurationSourceDriver` is responsible for creating one new instance of a :class:`ConfigurationSource`. The proper driver class to be used is selected based on the **driver** option inside each one of the groups listed in config_source and loaded through entry points managed by stevedore using the namespace oslo.config.driver:: [DEFAULT] config_source = source1 config_source = source2 ... [source1] driver = remote_file ... [source2] driver = castellan ... Each specific driver knows all the available options to properly instatiate a ConfigurationSource using the values comming from the given group in the open_source_from_opt_group() method and is able to generate sample config through the list_options_for_discovery() method. """ @abc.abstractmethod def open_source_from_opt_group(self, conf, group_name): """Return an open configuration source. Uses group_name to find the configuration settings for the new source and then open the configuration source and return it. If a source cannot be open, raises an appropriate exception. :param conf: The active configuration option handler from which to seek configuration values. :type conf: ConfigOpts :param group_name: The configuration option group name where the options for the source are stored. :type group_name: str :returns: an instance of a subclass of ConfigurationSource """ @abc.abstractmethod def list_options_for_discovery(self): """Return the list of options available to configure a new source. Drivers should advertise all supported options in this method for the purpose of sample generation by oslo-config-generator. For an example on how to implement this method you can check the **remote_file** driver at oslo_config.sources._uri.URIConfigurationSourceDriver. :returns: a list of supported options of a ConfigurationSource. """ class ConfigurationSource(metaclass=abc.ABCMeta): """A configuration source option for oslo.config. A configuration source is able to fetch configuration values based on a (group, option) key from an external source that supports key-value mapping such as INI files, key-value stores, secret stores, and so on. """ @abc.abstractmethod def get(self, group_name, option_name, opt): """Return the value of the option from the group. :param group_name: Name of the group. :type group_name: str :param option_name: Name of the option. :type option_name: str :param opt: The option definition. :type opt: Opt :returns: A tuple (value, location) where value is the option value or oslo_config.sources._NoValue if the (group, option) is not present in the source, and location is a LocationInfo. """ ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/sources/_environment.py0000664000175000017500000000633400000000000023040 0ustar00zuulzuul00000000000000# 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""" Environment ----------- The **environment** backend driver provides a method of accessing configuration data in environment variables. It is enabled by default and requires no additional configuration to use. The environment is checked after command line options, but before configuration files. Environment variables are checked for any configuration data. The variable names take the form: * A prefix of ``OS_`` * The group name, uppercased * Separated from the option name by a `__` (double underscore) * Followed by the name For an option that looks like this in the usual INI format:: [placement_database] connection = sqlite:/// the corresponding environment variable would be ``OS_PLACEMENT_DATABASE__CONNECTION``. The Driver Class ================ .. autoclass:: EnvironmentConfigurationSourceDriver The Configuration Source Class ============================== .. autoclass:: EnvironmentConfigurationSource """ import os # Avoid circular import import oslo_config.cfg from oslo_config import sources # In current practice this class is not used because the # EnvironmentConfigurationSource is loaded by default, but we keep it # here in case we choose to change that behavior in the future. class EnvironmentConfigurationSourceDriver(sources.ConfigurationSourceDriver): """A backend driver for environment variables. This configuration source is available by default and does not need special configuration to use. The sample config is generated automatically but is not necessary. """ def list_options_for_discovery(self): """There are no options for this driver.""" return [] def open_source_from_opt_group(self, conf, group_name): return EnvironmentConfigurationSource() class EnvironmentConfigurationSource(sources.ConfigurationSource): """A configuration source for options in the environment.""" @staticmethod def get_name(group_name, option_name): """Return the expected environment variable name for the given option. :param group_name: The group name or None. Defaults to 'DEFAULT' if None. :param option_name: The option name. :returns: Th expected environment variable name. """ group_name = group_name or 'DEFAULT' return 'OS_{}__{}'.format(group_name.upper(), option_name.upper()) def get(self, group_name, option_name, opt): env_name = self.get_name(group_name, option_name) try: value = os.environ[env_name] loc = oslo_config.cfg.LocationInfo( oslo_config.cfg.Locations.environment, env_name) return (value, loc) except KeyError: return (sources._NoValue, None) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/sources/_uri.py0000664000175000017500000001411300000000000021265 0ustar00zuulzuul00000000000000# 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""" Remote File ----------- The **remote_file** backend driver is the first driver implemented by oslo.config. It extends the previous limit of only accessing local files to a new scenario where it is possible to access configuration data over the network. The **remote_file** driver is based on the **requests** module and is capable of accessing remote files through **HTTP** or **HTTPS**. To definition of a remote_file configuration data source can be as minimal as:: [DEFAULT] config_source = external_config_group [external_config_group] driver = remote_file uri = http://mydomain.com/path/to/config/data.conf Or as complete as:: [DEFAULT] config_source = external_config_group [external_config_group] driver = remote_file uri = https://mydomain.com/path/to/config/data.conf ca_path = /path/to/server/ca.pem client_key = /path/to/my/key.pem client_cert = /path/to/my/cert.pem On the following sessions, you can find more information about this driver's classes and its options. The Driver Class ================ .. autoclass:: URIConfigurationSourceDriver The Configuration Source Class ============================== .. autoclass:: URIConfigurationSource """ import requests import tempfile from oslo_config import cfg from oslo_config import sources class URIConfigurationSourceDriver(sources.ConfigurationSourceDriver): """A backend driver for remote files served through http[s]. Required options: - uri: URI containing the file location. Non-required options: - ca_path: The path to a CA_BUNDLE file or directory with certificates of trusted CAs. - client_cert: Client side certificate, as a single file path containing either the certificate only or the private key and the certificate. - client_key: Client side private key, in case client_cert is specified but does not includes the private key. """ _uri_driver_opts = [ cfg.URIOpt( 'uri', schemes=['http', 'https'], required=True, sample_default='https://example.com/my-configuration.ini', help=('Required option with the URI of the ' 'extra configuration file\'s location.'), ), cfg.StrOpt( 'ca_path', sample_default='/etc/ca-certificates', help=('The path to a CA_BUNDLE file or directory ' 'with certificates of trusted CAs.'), ), cfg.StrOpt( 'client_cert', sample_default='/etc/ca-certificates/service-client-keystore', help=('Client side certificate, as a single file path ' 'containing either the certificate only or the ' 'private key and the certificate.'), ), cfg.StrOpt( 'client_key', help=('Client side private key, in case client_cert is ' 'specified but does not includes the private key.'), ), cfg.StrOpt( 'timeout', default=60, help=('Timeout is the number of seconds the request will wait ' 'for your client to establish a connection to a remote ' 'machine call on the socket.'), ), ] def list_options_for_discovery(self): return self._uri_driver_opts def open_source_from_opt_group(self, conf, group_name): conf.register_opts(self._uri_driver_opts, group_name) return URIConfigurationSource( conf[group_name].uri, conf[group_name].ca_path, conf[group_name].client_cert, conf[group_name].client_key, conf[group_name].timeout) class URIConfigurationSource(sources.ConfigurationSource): """A configuration source for remote files served through http[s]. :param uri: The Uniform Resource Identifier of the configuration to be retrieved. :param ca_path: The path to a CA_BUNDLE file or directory with certificates of trusted CAs. :param client_cert: Client side certificate, as a single file path containing either the certificate only or the private key and the certificate. :param client_key: Client side private key, in case client_cert is specified but does not includes the private key. """ def __init__(self, uri, ca_path=None, client_cert=None, client_key=None, timeout=60): self._uri = uri self._namespace = cfg._Namespace(cfg.ConfigOpts()) data = self._fetch_uri(uri, ca_path, client_cert, client_key, timeout) with tempfile.NamedTemporaryFile() as tmpfile: tmpfile.write(data.encode("utf-8")) tmpfile.flush() cfg.ConfigParser._parse_file(tmpfile.name, self._namespace) def _fetch_uri(self, uri, ca_path, client_cert, client_key, timeout): verify = ca_path if ca_path else True cert = (client_cert, client_key) if client_cert and client_key else \ client_cert with requests.get(uri, verify=verify, cert=cert, timeout=timeout) as response: response.raise_for_status() # raises only in case of HTTPError return response.text def get(self, group_name, option_name, opt): try: return self._namespace._get_value( [(group_name, option_name)], multi=opt.multi) except KeyError: return (sources._NoValue, None) ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1724318217.1909974 oslo.config-9.6.0/oslo_config/sources/templates/0000775000175000017500000000000000000000000021753 5ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/sources/templates/bash-completion.template0000664000175000017500000000255100000000000026577 0ustar00zuulzuul00000000000000#!/bin/bash completion for {scriptname} _{scriptname}(){{ local cur prev local -A ARGS MAP FORCE OPTS OPTS_SUB MULTI COMPREPLY=() cur="${{COMP_WORDS[COMP_CWORD]}}" prev="${{COMP_WORDS[COMP_CWORD-1]}}" OPTS=({opts}) OPTS_SUB=({opts_sub}) ARGS=({args}) MAP=({map}) MULTI=({multi}) if [ ! -z "$prev" ]; then # if is an argument complete with list of choice if define prev_key=${{MAP[$prev]}} if [ ! -z $prev_key ] && [ ! -z "${{ARGS[$prev_key]}}" ]; then COMPREPLY=($(compgen -W "${{ARGS[$prev_key]}}" -- "${{cur}}")) return 0 fi fi for in_use in ${{COMP_WORDS[@]:1}}; do key=${{MAP[$in_use]}} IFS='|' if [[ -v OPTS_SUB[$key] ]];then # If is a subcommand redefine completion unset OPTS local -A OPTS for el in ${{OPTS_SUB[$key]}}; do IFS='=' read k v <<< ${{el}} IFS='|' OPTS+=( [${{k}}]="${{v}}" ) done fi unset IFS # Unset option that is already use if [[ -z "MULTI[$key]" ]]; then unset OPTS[$key] unset ARGS[$key] fi done compl="${{OPTS[@]}}" COMPREPLY=($(compgen -W "${{compl}}" -- "${{cur}}")) return 0 }} complete -F _{scriptname} {scriptname} ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/sources/templates/zsh-completion.template0000664000175000017500000000104500000000000026463 0ustar00zuulzuul00000000000000#compdef _{scriptname} {scriptname} _{scriptname}_commands(){{ #Script used only if subcommand local -a _{scriptname}_cmds # Add subcommands list _{scriptname}_cmds=( {commands_list} ) if (( CURRENT == 1 )); then _describe -t commands '{scriptname} command' _{scriptname}_cmds || compadd "$@" else local curcontext="$curcontext" #Check if subcommand and redefine completion case "$words[1]" in {commands_opts} esac fi }} _{scriptname}(){{ local curcontext="$curcontext" state line _arguments -s \ {opts} }} ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/sphinxconfiggen.py0000664000175000017500000000673100000000000022044 0ustar00zuulzuul00000000000000# 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 sphinx.util import logging from oslo_config import generator LOG = logging.getLogger(__name__) def generate_sample(app): if not app.config.config_generator_config_file: LOG.warning("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): LOG.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) return { 'parallel_read_safe': True, 'parallel_write_safe': True, } ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/sphinxext.py0000664000175000017500000004307100000000000020703 0ustar00zuulzuul00000000000000# 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 import oslo_i18n 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 import logging 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 LOG = logging.getLogger(__name__) 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 def _indent(text, n=2): padding = ' ' * n # we don't want to indent blank lines so just output them as-is return '\n'.join(padding + x if x else '' for x 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.IPOpt: 'ip address', cfg.PortOpt: 'port number', cfg.HostnameOpt: 'hostname', cfg.URIOpt: 'URI', cfg.HostAddressOpt: 'host address', cfg._ConfigFileOpt: 'list of filenames', cfg._ConfigDirOpt: 'list of directory names', } def _get_choice_text(choice): if choice is None: return '' elif choice == '': return "''" return str(choice) def _format_opt(opt, group_name): 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: yield _indent(':Default: ``%s``' % default) else: yield _indent(':Default: ``%r``' % 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', 6) yield _indent( 'effect on stability and/or performance.', 6) if opt.sample_default: yield _indent( '') yield _indent( 'This option has a sample default set, which means that') yield _indent( 'its actual default value may vary from the one documented') yield _indent( 'above.') try: help_text = opt.help % {'default': 'the value above'} except (TypeError, KeyError, ValueError): # There is no mention of the default in the help string, # the string had some unknown key, or the string contained # invalid formatting characters help_text = opt.help if help_text: yield '' for line in help_text.strip().splitlines(): yield _indent(line.rstrip()) # We don't bother outputting this if not using new-style choices with # inline descriptions if getattr(opt.type, 'choices', None) and not all( x is None for x in opt.type.choices.values()): yield '' yield _indent('.. rubric:: Possible values') for choice in opt.type.choices: yield '' yield _indent(_get_choice_text(choice)) yield _indent(_indent( opt.type.choices[choice] or '')) if opt.deprecated_opts: yield '' 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 '' 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.') if opt.deprecated_reason: reason = ' '.join([x.strip() for x in opt.deprecated_reason.splitlines()]) yield '' yield _indent(' :Reason: ' + reason) yield '' def _format_group(namespace, group_name, group_obj): yield '.. oslo.config:group:: %s' % group_name if namespace: yield ' :namespace: %s' % namespace yield '' if group_obj and group_obj.help: for line in group_obj.help.strip().splitlines(): yield _indent(line.rstrip()) yield '' def _format_group_opts(namespace, group_name, group_obj, opt_list): group_name = group_name or 'DEFAULT' LOG.debug('%s %s', namespace, group_name) for line in _format_group(namespace, group_name, group_obj): yield line for opt in opt_list: for line in _format_opt(opt, group_name): yield line def _format_option_help(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 if group_name is None: group_name = 'DEFAULT' lines = _format_group_opts( 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 if group_name is None: group_name = 'DEFAULT' 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_opts( 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): split_namespaces = 'split-namespaces' in self.options config_file = self.options.get('config-file') if config_file: LOG.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 = self.state.document.current_source for count, line in enumerate(_format_option_help( namespaces, split_namespaces)): result.append(line, source_name, count) LOG.debug('%5d%s%s', count, ' ' if line else '', line) node = nodes.section() node.document = self.state.document # With the resolution for bug #1755783, we now parse the 'Opt.help' # attribute as rST. Unfortunately, there are a lot of broken option # descriptions out there and we don't want to break peoples' builds # suddenly. As a result, we disable 'warning-is-error' temporarily. # Users will still see the warnings but the build will continue. with logging.skip_warningiserror(): 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): required_arguments = 1 optional_arguments = 0 has_content = True option_spec = { 'namespace': directives.unchanged, } def run(self): env = self.state.document.settings.env group_name = self.arguments[0] 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 LOG.debug('oslo.config group %s' % 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)) _add('') for line in self.content: _add(line) 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 LOG.debug('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): """Resolve cross-references""" 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 merge_domaindata(self, docnames, otherdata): for target, docname in otherdata['options'].items(): if docname in docnames: self.data['options'][target] = docname for target, docname in otherdata['groups'].items(): if docname in docnames: self.data['groups'][target] = docname 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) return { 'parallel_read_safe': True, 'parallel_write_safe': True, } ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1724318217.1949978 oslo.config-9.6.0/oslo_config/tests/0000775000175000017500000000000000000000000017434 5ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/tests/__init__.py0000664000175000017500000000000000000000000021533 0ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/tests/test_cfg.py0000664000175000017500000061107100000000000021612 0ustar00zuulzuul00000000000000# 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. import argparse import errno import functools import io import logging import os import shutil import sys import tempfile import unittest from unittest import mock import fixtures from oslotest import base import testscenarios from oslo_config import cfg from oslo_config import types load_tests = testscenarios.load_tests_apply_scenarios class ExceptionsTestCase(base.BaseTestCase): def test_error(self): msg = str(cfg.Error('foobar')) self.assertEqual('foobar', msg) def test_args_already_parsed_error(self): msg = str(cfg.ArgsAlreadyParsedError('foobar')) self.assertEqual('arguments already parsed: foobar', msg) def test_no_such_opt_error(self): msg = str(cfg.NoSuchOptError('foo')) self.assertEqual('no such option foo in group [DEFAULT]', msg) def test_no_such_opt_error_with_group(self): msg = str(cfg.NoSuchOptError('foo', cfg.OptGroup('bar'))) self.assertEqual('no such option foo in group [bar]', msg) def test_no_such_group_error(self): msg = str(cfg.NoSuchGroupError('bar')) self.assertEqual('no such group [bar]', msg) def test_duplicate_opt_error(self): msg = str(cfg.DuplicateOptError('foo')) self.assertEqual('duplicate option: foo', msg) def test_required_opt_error(self): msg = str(cfg.RequiredOptError('foo')) self.assertEqual('value required for option foo in group [DEFAULT]', msg) def test_required_opt_error_with_group(self): msg = str(cfg.RequiredOptError('foo', cfg.OptGroup('bar'))) self.assertEqual('value required for option foo in group [bar]', msg) def test_template_substitution_error(self): msg = str(cfg.TemplateSubstitutionError('foobar')) self.assertEqual('template substitution error: foobar', msg) def test_config_files_not_found_error(self): msg = str(cfg.ConfigFilesNotFoundError(['foo', 'bar'])) self.assertEqual('Failed to find some config files: foo,bar', msg) def test_config_files_permission_denied_error(self): msg = str(cfg.ConfigFilesPermissionDeniedError(['foo', 'bar'])) self.assertEqual('Failed to open some config files: foo,bar', msg) def test_config_dir_not_found_error(self): msg = str(cfg.ConfigDirNotFoundError('foobar')) self.assertEqual('Failed to read config file directory: foobar', msg) def test_config_file_parse_error(self): msg = str(cfg.ConfigFileParseError('foo', 'foobar')) self.assertEqual('Failed to parse foo: foobar', msg) class BaseTestCase(base.BaseTestCase): class TestConfigOpts(cfg.ConfigOpts): def __call__(self, args=None, default_config_files=[], default_config_dirs=[], usage=None): return cfg.ConfigOpts.__call__( self, args=args, prog='test', version='1.0', usage=usage, description='somedesc', epilog='tepilog', default_config_files=default_config_files, default_config_dirs=default_config_dirs, validate_default_values=True) def setUp(self): super(BaseTestCase, self).setUp() self.useFixture(fixtures.NestedTempfile()) self.conf = self.TestConfigOpts() self.tempdirs = [] def create_tempfiles(self, files, ext='.conf'): tempfiles = [] for (basename, contents) in files: if not os.path.isabs(basename): # create all the tempfiles in a tempdir tmpdir = tempfile.mkdtemp() path = os.path.join(tmpdir, basename + ext) # the path can start with a subdirectory so create # it if it doesn't exist yet if not os.path.exists(os.path.dirname(path)): os.makedirs(os.path.dirname(path)) else: path = basename + ext fd = os.open(path, os.O_CREAT | os.O_WRONLY) tempfiles.append(path) try: os.write(fd, contents.encode('utf-8')) finally: os.close(fd) return tempfiles class UsageTestCase(BaseTestCase): def test_print_usage(self): f = io.StringIO() self.conf([]) self.conf.print_usage(file=f) self.assertIn( 'usage: test [-h] [--config-dir DIR] [--config-file PATH]\n\ [--shell_completion SHELL_COMPLETION] [--version]', f.getvalue()) self.assertNotIn('somedesc', f.getvalue()) self.assertNotIn('tepilog', f.getvalue()) self.assertNotIn('optional:', f.getvalue()) def test_print_custom_usage(self): conf = self.TestConfigOpts() self.tempdirs = [] f = io.StringIO() conf([], usage='%(prog)s FOO BAR') conf.print_usage(file=f) self.assertIn('usage: test FOO BAR', f.getvalue()) self.assertNotIn('somedesc', f.getvalue()) self.assertNotIn('tepilog', f.getvalue()) self.assertNotIn('optional:', f.getvalue()) def test_print_help(self): f = io.StringIO() self.conf([]) self.conf.print_help(file=f) self.assertIn( 'usage: test [-h] [--config-dir DIR] [--config-file PATH]\n\ [--shell_completion SHELL_COMPLETION] [--version]', f.getvalue()) self.assertIn('somedesc', f.getvalue()) self.assertIn('tepilog', f.getvalue()) self.assertNotIn('optional:', f.getvalue()) class HelpTestCase(BaseTestCase): def test_print_help(self): f = io.StringIO() self.conf([]) self.conf.print_help(file=f) self.assertIn( 'usage: test [-h] [--config-dir DIR] [--config-file PATH]\n\ [--shell_completion SHELL_COMPLETION] [--version]', f.getvalue()) # argparse may generate two different help messages: # - In Python >=3.10: "options:\n --version" # - In Python <3.10: "optional arguments:" self.assertRegex(f.getvalue(), 'option(s|al arguments):') self.assertIn('-h, --help', f.getvalue()) def test_print_strOpt_with_choices_help(self): f = io.StringIO() cli_opts = [ cfg.StrOpt('aa', short='a', default='xx', choices=['xx', 'yy', 'zz'], help='StrOpt with choices.'), cfg.StrOpt('bb', short='b', default='yy', choices=[None, 'yy', 'zz'], help='StrOpt with choices.'), cfg.StrOpt('cc', short='c', default='zz', choices=['', 'yy', 'zz'], help='StrOpt with choices.'), ] self.conf.register_cli_opts(cli_opts) self.conf([]) self.conf.print_help(file=f) self.assertIn( 'usage: test [-h] [--aa AA] [--bb BB] [--cc CC] ' '[--config-dir DIR]', f.getvalue()) # argparse may generate two different help messages: # - In Python >=3.10: "options:\n --version" # - In Python <3.10: "optional arguments:" self.assertRegex(f.getvalue(), 'option(s|al arguments):') self.assertIn('-h, --help', f.getvalue()) self.assertIn('StrOpt with choices. Allowed values: xx, yy, zz', f.getvalue()) self.assertIn('StrOpt with choices. Allowed values: , yy, zz', f.getvalue()) self.assertIn("StrOpt with choices. Allowed values: '', yy, zz", f.getvalue()) def test_print_sorted_help(self): f = io.StringIO() self.conf.register_cli_opt(cfg.StrOpt('abc')) self.conf.register_cli_opt(cfg.StrOpt('zba')) self.conf.register_cli_opt(cfg.StrOpt('ghi')) self.conf.register_cli_opt(cfg.StrOpt('deb')) self.conf([]) self.conf.print_help(file=f) zba = f.getvalue().find('--zba') abc = f.getvalue().find('--abc') ghi = f.getvalue().find('--ghi') deb = f.getvalue().find('--deb') list = [abc, deb, ghi, zba] self.assertEqual(sorted(list), list) def test_print_sorted_help_with_positionals(self): f = io.StringIO() self.conf.register_cli_opt( cfg.StrOpt('pst', positional=True, required=False)) self.conf.register_cli_opt(cfg.StrOpt('abc')) self.conf.register_cli_opt(cfg.StrOpt('zba')) self.conf.register_cli_opt(cfg.StrOpt('ghi')) self.conf([]) self.conf.print_help(file=f) zba = f.getvalue().find('--zba') abc = f.getvalue().find('--abc') ghi = f.getvalue().find('--ghi') list = [abc, ghi, zba] self.assertEqual(sorted(list), list) def test_print_help_with_deprecated(self): f = io.StringIO() abc = cfg.StrOpt('a-bc', deprecated_opts=[cfg.DeprecatedOpt('d-ef')]) uvw = cfg.StrOpt('u-vw', deprecated_name='x-yz') self.conf.register_cli_opt(abc) self.conf.register_cli_opt(uvw) self.conf([]) self.conf.print_help(file=f) if sys.version_info >= (3, 13): self.assertIn('--a-bc, --d-ef, --d_ef A_BC', f.getvalue()) self.assertIn('--u-vw, --x-yz, --x_yz U_VW', f.getvalue()) else: self.assertIn('--a-bc A_BC, --d-ef A_BC, --d_ef A_BC', f.getvalue()) self.assertIn('--u-vw U_VW, --x-yz U_VW, --x_yz U_VW', f.getvalue()) class FindConfigFilesTestCase(BaseTestCase): def test_find_config_files(self): config_files = [ os.path.expanduser('~/.blaa/blaa.conf'), '/etc/foo.conf', ] self.useFixture(fixtures.MonkeyPatch('sys.argv', ['foo'])) self.useFixture(fixtures.MonkeyPatch('os.path.exists', lambda p: p in config_files)) self.assertEqual(cfg.find_config_files(project='blaa'), config_files) def test_find_config_files_overrides(self): """Ensure priority of directories is enforced. Ensure we will only ever return two files: $project.conf and $prog.conf. """ config_files = [ os.path.expanduser('~/.foo/foo.conf'), # no '~/.foo/bar.conf' to make sure we fall through os.path.expanduser('~/foo.conf'), os.path.expanduser('~/bar.conf'), '/etc/foo/foo.conf', '/etc/foo/bar.conf', '/etc/foo.conf', '/etc/bar.conf', ] self.useFixture(fixtures.MonkeyPatch( 'os.path.exists', lambda p: p in config_files)) expected = [ os.path.expanduser('~/.foo/foo.conf'), os.path.expanduser('~/bar.conf'), ] actual = cfg.find_config_files(project='foo', prog='bar') self.assertEqual(expected, actual) def test_find_config_files_snap(self): config_files = [ '/snap/nova/current/etc/blaa/blaa.conf', '/var/snap/nova/common/etc/blaa/blaa.conf', ] fake_env = { 'SNAP': '/snap/nova/current/', 'SNAP_COMMON': '/var/snap/nova/common/', } self.useFixture(fixtures.MonkeyPatch('sys.argv', ['foo'])) self.useFixture(fixtures.MonkeyPatch('os.path.exists', lambda p: p in config_files)) self.useFixture(fixtures.MonkeyPatch('os.environ', fake_env)) self.assertEqual(cfg.find_config_files(project='blaa'), ['/var/snap/nova/common/etc/blaa/blaa.conf']) def test_find_config_files_with_extension(self): config_files = ['/etc/foo.json'] self.useFixture(fixtures.MonkeyPatch('sys.argv', ['foo'])) self.useFixture(fixtures.MonkeyPatch('os.path.exists', lambda p: p in config_files)) self.assertEqual(cfg.find_config_files(project='blaa'), []) self.assertEqual(cfg.find_config_files(project='blaa', extension='.json'), config_files) class FindConfigDirsTestCase(BaseTestCase): def test_find_config_dirs(self): config_dirs = [os.path.expanduser('~/.blaa/blaa.conf.d'), '/etc/foo.conf.d'] self.useFixture(fixtures.MonkeyPatch('sys.argv', ['foo'])) self.useFixture(fixtures.MonkeyPatch('os.path.exists', lambda p: p in config_dirs)) self.assertEqual(cfg.find_config_dirs(project='blaa'), config_dirs) def test_find_config_dirs_snap(self): config_dirs = ['/var/snap/nova/common/etc/blaa/blaa.conf.d'] fake_env = {'SNAP': '/snap/nova/current/', 'SNAP_COMMON': '/var/snap/nova/common/'} self.useFixture(fixtures.MonkeyPatch('sys.argv', ['foo'])) self.useFixture(fixtures.MonkeyPatch('os.path.exists', lambda p: p in config_dirs)) self.useFixture(fixtures.MonkeyPatch('os.environ', fake_env)) self.assertEqual(cfg.find_config_dirs(project='blaa'), config_dirs) def test_find_config_dirs_non_exists(self): self.useFixture(fixtures.MonkeyPatch('sys.argv', ['foo'])) self.assertEqual(cfg.find_config_dirs(project='blaa'), []) def test_find_config_dirs_with_extension(self): config_dirs = ['/etc/foo.json.d'] self.useFixture(fixtures.MonkeyPatch('sys.argv', ['foo'])) self.useFixture(fixtures.MonkeyPatch('os.path.exists', lambda p: p in config_dirs)) self.assertEqual(cfg.find_config_dirs(project='blaa'), []) self.assertEqual(cfg.find_config_dirs(project='blaa', extension='.json.d'), config_dirs) class DefaultConfigFilesTestCase(BaseTestCase): def test_use_default(self): self.conf.register_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('foo-', '[DEFAULT]\n''foo = bar\n')]) self.conf.register_cli_opt(cfg.StrOpt('config-file-foo')) self.conf(args=['--config-file-foo', 'foo.conf'], default_config_files=[paths[0]]) self.assertEqual([paths[0]], self.conf.config_file) self.assertEqual('bar', self.conf.foo) def test_do_not_use_default_multi_arg(self): self.conf.register_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('foo-', '[DEFAULT]\n''foo = bar\n')]) self.conf(args=['--config-file', paths[0]], default_config_files=['bar.conf']) self.assertEqual([paths[0]], self.conf.config_file) self.assertEqual('bar', self.conf.foo) def test_do_not_use_default_single_arg(self): self.conf.register_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('foo-', '[DEFAULT]\n''foo = bar\n')]) self.conf(args=['--config-file=' + paths[0]], default_config_files=['bar.conf']) self.assertEqual([paths[0]], self.conf.config_file) self.assertEqual('bar', self.conf.foo) def test_no_default_config_file(self): self.conf(args=[]) self.assertEqual([], self.conf.config_file) def test_find_default_config_file(self): paths = self.create_tempfiles([('def', '[DEFAULT]')]) self.useFixture(fixtures.MonkeyPatch( 'oslo_config.cfg.find_config_files', lambda project, prog: paths)) self.conf(args=[], default_config_files=None) self.assertEqual(paths, self.conf.config_file) def test_default_config_file(self): paths = self.create_tempfiles([('def', '[DEFAULT]')]) self.conf(args=[], default_config_files=paths) self.assertEqual(paths, self.conf.config_file) def test_default_config_file_with_value(self): self.conf.register_cli_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('def', '[DEFAULT]\n''foo = bar\n')]) self.conf(args=[], default_config_files=paths) self.assertEqual(paths, self.conf.config_file) self.assertEqual('bar', self.conf.foo) def test_default_config_file_priority(self): self.conf.register_cli_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('def', '[DEFAULT]\n''foo = bar\n')]) self.conf(args=['--foo=blaa'], default_config_files=paths) self.assertEqual(paths, self.conf.config_file) self.assertEqual('blaa', self.conf.foo) class DefaultConfigDirsTestCase(BaseTestCase): def test_use_default(self): self.conf.register_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('foo.conf.d/foo', '[DEFAULT]\n''foo = bar\n')]) p = os.path.dirname(paths[0]) self.conf.register_cli_opt(cfg.StrOpt('config-dir-foo')) self.conf(args=['--config-dir-foo', 'foo.conf.d'], default_config_dirs=[p]) self.assertEqual([p], self.conf.config_dir) self.assertEqual('bar', self.conf.foo) def test_do_not_use_default_multi_arg(self): self.conf.register_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('foo.conf.d/foo', '[DEFAULT]\n''foo = bar\n')]) p = os.path.dirname(paths[0]) self.conf(args=['--config-dir', p], default_config_dirs=['bar.conf.d']) self.assertEqual([p], self.conf.config_dirs) self.assertEqual('bar', self.conf.foo) def test_do_not_use_default_single_arg(self): self.conf.register_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('foo.conf.d/foo', '[DEFAULT]\n''foo = bar\n')]) p = os.path.dirname(paths[0]) self.conf(args=['--config-dir=' + p], default_config_dirs=['bar.conf.d']) self.assertEqual([p], self.conf.config_dir) self.assertEqual('bar', self.conf.foo) def test_no_default_config_dir(self): self.conf(args=[]) self.assertEqual([], self.conf.config_dir) def test_find_default_config_dir(self): paths = self.create_tempfiles([('def.conf.d/def', '[DEFAULT]')]) p = os.path.dirname(paths[0]) self.useFixture(fixtures.MonkeyPatch( 'oslo_config.cfg.find_config_dirs', lambda project, prog: p)) self.conf(args=[], default_config_dirs=None) self.assertEqual([p], self.conf.config_dir) def test_default_config_dir(self): paths = self.create_tempfiles([('def.conf.d/def', '[DEFAULT]')]) p = os.path.dirname(paths[0]) self.conf(args=[], default_config_dirs=[p]) self.assertEqual([p], self.conf.config_dir) def test_default_config_dir_with_value(self): self.conf.register_cli_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('def.conf.d/def', '[DEFAULT]\n''foo = bar\n')]) p = os.path.dirname(paths[0]) self.conf(args=[], default_config_dirs=[p]) self.assertEqual([p], self.conf.config_dir) self.assertEqual('bar', self.conf.foo) def test_default_config_dir_priority(self): self.conf.register_cli_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('def.conf.d/def', '[DEFAULT]\n''foo = bar\n')]) p = os.path.dirname(paths[0]) self.conf(args=['--foo=blaa'], default_config_dirs=[p]) self.assertEqual([p], self.conf.config_dir) self.assertEqual('blaa', self.conf.foo) class CliOptsTestCase(BaseTestCase): """Test CLI Options. Each test scenario takes a name for the scenarios, as well as a dict: opt_class - class of the type of option that should be tested default - a default value for the option cli_args - a list containing a representation of an input command line value - the result value that is expected to be found deps - a tuple of deprecated name/group """ IPv4Opt = functools.partial(cfg.IPOpt, version=4) IPv6Opt = functools.partial(cfg.IPOpt, version=6) multi_int = functools.partial(cfg.MultiOpt, item_type=types.Integer()) multi_float = functools.partial(cfg.MultiOpt, item_type=types.Float()) multi_string = functools.partial(cfg.MultiOpt, item_type=types.String()) scenarios = [ ('str_default', dict(opt_class=cfg.StrOpt, default=None, cli_args=[], value=None, deps=(None, None))), ('str_arg', dict(opt_class=cfg.StrOpt, default=None, cli_args=['--foo', 'bar'], value='bar', deps=(None, None))), ('str_arg_deprecated_name', dict(opt_class=cfg.StrOpt, default=None, cli_args=['--oldfoo', 'bar'], value='bar', deps=('oldfoo', None))), ('str_arg_deprecated_group', dict(opt_class=cfg.StrOpt, default=None, cli_args=['--old-foo', 'bar'], value='bar', deps=(None, 'old'))), ('str_arg_deprecated_group_default', dict(opt_class=cfg.StrOpt, default=None, cli_args=['--foo', 'bar'], value='bar', deps=(None, 'DEFAULT'))), ('str_arg_deprecated_group_and_name', dict(opt_class=cfg.StrOpt, default=None, cli_args=['--old-oof', 'bar'], value='bar', deps=('oof', 'old'))), ('bool_default', dict(opt_class=cfg.BoolOpt, default=False, cli_args=[], value=False, deps=(None, None))), ('bool_arg', dict(opt_class=cfg.BoolOpt, default=None, cli_args=['--foo'], value=True, deps=(None, None))), ('bool_arg_deprecated_name', dict(opt_class=cfg.BoolOpt, default=None, cli_args=['--oldfoo'], value=True, deps=('oldfoo', None))), ('bool_arg_deprecated_group', dict(opt_class=cfg.BoolOpt, default=None, cli_args=['--old-foo'], value=True, deps=(None, 'old'))), ('bool_arg_deprecated_group_default', dict(opt_class=cfg.BoolOpt, default=None, cli_args=['--foo'], value=True, deps=(None, 'DEFAULT'))), ('bool_arg_deprecated_group_and_name', dict(opt_class=cfg.BoolOpt, default=None, cli_args=['--old-oof'], value=True, deps=('oof', 'old'))), ('bool_arg_inverse', dict(opt_class=cfg.BoolOpt, default=None, cli_args=['--foo', '--nofoo'], value=False, deps=(None, None))), ('bool_arg_inverse_deprecated_name', dict(opt_class=cfg.BoolOpt, default=None, cli_args=['--oldfoo', '--nooldfoo'], value=False, deps=('oldfoo', None))), ('bool_arg_inverse_deprecated_group', dict(opt_class=cfg.BoolOpt, default=None, cli_args=['--old-foo', '--old-nofoo'], value=False, deps=(None, 'old'))), ('bool_arg_inverse_deprecated_group_default', dict(opt_class=cfg.BoolOpt, default=None, cli_args=['--foo', '--nofoo'], value=False, deps=(None, 'DEFAULT'))), ('bool_arg_inverse_deprecated_group_and_name', dict(opt_class=cfg.BoolOpt, default=None, cli_args=['--old-oof', '--old-nooof'], value=False, deps=('oof', 'old'))), ('int_default', dict(opt_class=cfg.IntOpt, default=10, cli_args=[], value=10, deps=(None, None))), ('int_arg', dict(opt_class=cfg.IntOpt, default=None, cli_args=['--foo=20'], value=20, deps=(None, None))), ('int_arg_deprecated_name', dict(opt_class=cfg.IntOpt, default=None, cli_args=['--oldfoo=20'], value=20, deps=('oldfoo', None))), ('int_arg_deprecated_group', dict(opt_class=cfg.IntOpt, default=None, cli_args=['--old-foo=20'], value=20, deps=(None, 'old'))), ('int_arg_deprecated_group_default', dict(opt_class=cfg.IntOpt, default=None, cli_args=['--foo=20'], value=20, deps=(None, 'DEFAULT'))), ('int_arg_deprecated_group_and_name', dict(opt_class=cfg.IntOpt, default=None, cli_args=['--old-oof=20'], value=20, deps=('oof', 'old'))), ('float_default', dict(opt_class=cfg.FloatOpt, default=1.0, cli_args=[], value=1.0, deps=(None, None))), ('float_arg', dict(opt_class=cfg.FloatOpt, default=None, cli_args=['--foo', '2.0'], value=2.0, deps=(None, None))), ('float_arg_deprecated_name', dict(opt_class=cfg.FloatOpt, default=None, cli_args=['--oldfoo', '2.0'], value=2.0, deps=('oldfoo', None))), ('float_arg_deprecated_group', dict(opt_class=cfg.FloatOpt, default=None, cli_args=['--old-foo', '2.0'], value=2.0, deps=(None, 'old'))), ('float_arg_deprecated_group_default', dict(opt_class=cfg.FloatOpt, default=None, cli_args=['--foo', '2.0'], value=2.0, deps=(None, 'DEFAULT'))), ('float_arg_deprecated_group_and_name', dict(opt_class=cfg.FloatOpt, default=None, cli_args=['--old-oof', '2.0'], value=2.0, deps=('oof', 'old'))), ('float_default_as_integer', dict(opt_class=cfg.FloatOpt, default=2, cli_args=['--old-oof', '2.0'], value=2.0, deps=('oof', 'old'))), ('ipv4addr_arg', dict(opt_class=IPv4Opt, default=None, cli_args=['--foo', '192.168.0.1'], value='192.168.0.1', deps=(None, None))), ('ipaddr_arg_implicitv4', dict(opt_class=cfg.IPOpt, default=None, cli_args=['--foo', '192.168.0.1'], value='192.168.0.1', deps=(None, None))), ('ipaddr_arg_implicitv6', dict(opt_class=cfg.IPOpt, default=None, cli_args=['--foo', 'abcd:ef::1'], value='abcd:ef::1', deps=(None, None))), ('ipv6addr_arg', dict(opt_class=IPv6Opt, default=None, cli_args=['--foo', 'abcd:ef::1'], value='abcd:ef::1', deps=(None, None))), ('list_default', dict(opt_class=cfg.ListOpt, default=['bar'], cli_args=[], value=['bar'], deps=(None, None))), ('list_arg', dict(opt_class=cfg.ListOpt, default=None, cli_args=['--foo', 'blaa,bar'], value=['blaa', 'bar'], deps=(None, None))), ('list_arg_with_spaces', dict(opt_class=cfg.ListOpt, default=None, cli_args=['--foo', 'blaa ,bar'], value=['blaa', 'bar'], deps=(None, None))), ('list_arg_deprecated_name', dict(opt_class=cfg.ListOpt, default=None, cli_args=['--oldfoo', 'blaa,bar'], value=['blaa', 'bar'], deps=('oldfoo', None))), ('list_arg_deprecated_group', dict(opt_class=cfg.ListOpt, default=None, cli_args=['--old-foo', 'blaa,bar'], value=['blaa', 'bar'], deps=(None, 'old'))), ('list_arg_deprecated_group_default', dict(opt_class=cfg.ListOpt, default=None, cli_args=['--foo', 'blaa,bar'], value=['blaa', 'bar'], deps=(None, 'DEFAULT'))), ('list_arg_deprecated_group_and_name', dict(opt_class=cfg.ListOpt, default=None, cli_args=['--old-oof', 'blaa,bar'], value=['blaa', 'bar'], deps=('oof', 'old'))), ('dict_default', dict(opt_class=cfg.DictOpt, default={'foo': 'bar'}, cli_args=[], value={'foo': 'bar'}, deps=(None, None))), ('dict_arg', dict(opt_class=cfg.DictOpt, default=None, cli_args=['--foo', 'key1:blaa,key2:bar'], value={'key1': 'blaa', 'key2': 'bar'}, deps=(None, None))), ('dict_arg_multiple_keys_last_wins', dict(opt_class=cfg.DictOpt, default=None, cli_args=['--foo', 'key1:blaa', '--foo', 'key2:bar'], value={'key2': 'bar'}, deps=(None, None))), ('dict_arg_with_spaces', dict(opt_class=cfg.DictOpt, default=None, cli_args=['--foo', 'key1:blaa ,key2:bar'], value={'key1': 'blaa', 'key2': 'bar'}, deps=(None, None))), ('dict_arg_deprecated_name', dict(opt_class=cfg.DictOpt, default=None, cli_args=['--oldfoo', 'key1:blaa', '--oldfoo', 'key2:bar'], value={'key2': 'bar'}, deps=('oldfoo', None))), ('dict_arg_deprecated_group', dict(opt_class=cfg.DictOpt, default=None, cli_args=['--old-foo', 'key1:blaa,key2:bar'], value={'key1': 'blaa', 'key2': 'bar'}, deps=(None, 'old'))), ('dict_arg_deprecated_group2', dict(opt_class=cfg.DictOpt, default=None, cli_args=['--old-foo', 'key1:blaa', '--old-foo', 'key2:bar'], value={'key2': 'bar'}, deps=(None, 'old'))), ('dict_arg_deprecated_group_default', dict(opt_class=cfg.DictOpt, default=None, cli_args=['--foo', 'key1:blaa', '--foo', 'key2:bar'], value={'key2': 'bar'}, deps=(None, 'DEFAULT'))), ('dict_arg_deprecated_group_and_name', dict(opt_class=cfg.DictOpt, default=None, cli_args=['--old-oof', 'key1:blaa,key2:bar'], value={'key1': 'blaa', 'key2': 'bar'}, deps=('oof', 'old'))), ('dict_arg_deprecated_group_and_name2', dict(opt_class=cfg.DictOpt, default=None, cli_args=['--old-oof', 'key1:blaa', '--old-oof', 'key2:bar'], value={'key2': 'bar'}, deps=('oof', 'old'))), ('port_default', dict(opt_class=cfg.PortOpt, default=80, cli_args=[], value=80, deps=(None, None))), ('port_arg', dict(opt_class=cfg.PortOpt, default=None, cli_args=['--foo=80'], value=80, deps=(None, None))), ('port_arg_deprecated_name', dict(opt_class=cfg.PortOpt, default=None, cli_args=['--oldfoo=80'], value=80, deps=('oldfoo', None))), ('port_arg_deprecated_group', dict(opt_class=cfg.PortOpt, default=None, cli_args=['--old-foo=80'], value=80, deps=(None, 'old'))), ('port_arg_deprecated_group_default', dict(opt_class=cfg.PortOpt, default=None, cli_args=['--foo=80'], value=80, deps=(None, 'DEFAULT'))), ('port_arg_deprecated_group_and_name', dict(opt_class=cfg.PortOpt, default=None, cli_args=['--old-oof=80'], value=80, deps=('oof', 'old'))), ('uri_default', dict(opt_class=cfg.URIOpt, default='http://example.com', cli_args=[], value='http://example.com', deps=(None, None))), ('uri_arg', dict(opt_class=cfg.URIOpt, default=None, cli_args=['--foo', 'http://example.com'], value='http://example.com', deps=(None, None))), ('multistr_default', dict(opt_class=cfg.MultiStrOpt, default=['bar'], cli_args=[], value=['bar'], deps=(None, None))), ('multistr_arg', dict(opt_class=cfg.MultiStrOpt, default=None, cli_args=['--foo', 'blaa', '--foo', 'bar'], value=['blaa', 'bar'], deps=(None, None))), ('multistr_arg_deprecated_name', dict(opt_class=cfg.MultiStrOpt, default=None, cli_args=['--oldfoo', 'blaa', '--oldfoo', 'bar'], value=['blaa', 'bar'], deps=('oldfoo', None))), ('multistr_arg_deprecated_group', dict(opt_class=cfg.MultiStrOpt, default=None, cli_args=['--old-foo', 'blaa', '--old-foo', 'bar'], value=['blaa', 'bar'], deps=(None, 'old'))), ('multistr_arg_deprecated_group_default', dict(opt_class=cfg.MultiStrOpt, default=None, cli_args=['--foo', 'blaa', '--foo', 'bar'], value=['blaa', 'bar'], deps=(None, 'DEFAULT'))), ('multistr_arg_deprecated_group_and_name', dict(opt_class=cfg.MultiStrOpt, default=None, cli_args=['--old-oof', 'blaa', '--old-oof', 'bar'], value=['blaa', 'bar'], deps=('oof', 'old'))), ('multiopt_arg_int', dict(opt_class=multi_int, default=None, cli_args=['--foo', '1', '--foo', '2'], value=[1, 2], deps=(None, None))), ('multiopt_float_int', dict(opt_class=multi_float, default=None, cli_args=['--foo', '1.2', '--foo', '2.3'], value=[1.2, 2.3], deps=(None, None))), ('multiopt_string', dict(opt_class=multi_string, default=None, cli_args=['--foo', 'bar', '--foo', 'baz'], value=["bar", "baz"], deps=(None, None))), ] def test_cli(self): self.conf.register_cli_opt( self.opt_class('foo', default=self.default, deprecated_name=self.deps[0], deprecated_group=self.deps[1])) self.conf(self.cli_args) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(self.value, self.conf.foo) class CliSpecialOptsTestCase(BaseTestCase): def test_help(self): self.useFixture(fixtures.MonkeyPatch('sys.stdout', io.StringIO())) self.assertRaises(SystemExit, self.conf, ['--help']) self.assertIn('usage: test', sys.stdout.getvalue()) self.assertIn('[--version]', sys.stdout.getvalue()) self.assertIn('[-h]', sys.stdout.getvalue()) self.assertIn('--help', sys.stdout.getvalue()) self.assertIn('[--config-dir DIR]', sys.stdout.getvalue()) self.assertIn('[--config-file PATH]', sys.stdout.getvalue()) def test_version(self): stream_name = 'stdout' self.useFixture(fixtures.MonkeyPatch("sys.%s" % stream_name, io.StringIO())) self.assertRaises(SystemExit, self.conf, ['--version']) self.assertIn('1.0', getattr(sys, stream_name).getvalue()) def test_config_file(self): paths = self.create_tempfiles([('1', '[DEFAULT]'), ('2', '[DEFAULT]')]) self.conf(['--config-file', paths[0], '--config-file', paths[1]]) self.assertEqual(paths, self.conf.config_file) class PositionalTestCase(BaseTestCase): def _do_pos_test(self, opt_class, default, cli_args, value): self.conf.register_cli_opt(opt_class('foo', default=default, positional=True, required=False)) self.conf(cli_args) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(value, self.conf.foo) def test_positional_str_none_default(self): self._do_pos_test(cfg.StrOpt, None, [], None) def test_positional_str_default(self): self._do_pos_test(cfg.StrOpt, 'bar', [], 'bar') def test_positional_str_arg(self): self._do_pos_test(cfg.StrOpt, None, ['bar'], 'bar') def test_positional_int_none_default(self): self._do_pos_test(cfg.IntOpt, None, [], None) def test_positional_int_default(self): self._do_pos_test(cfg.IntOpt, 10, [], 10) def test_positional_int_arg(self): self._do_pos_test(cfg.IntOpt, None, ['20'], 20) def test_positional_float_none_default(self): self._do_pos_test(cfg.FloatOpt, None, [], None) def test_positional_float_default(self): self._do_pos_test(cfg.FloatOpt, 1.0, [], 1.0) def test_positional_float_arg(self): self._do_pos_test(cfg.FloatOpt, None, ['2.0'], 2.0) def test_positional_list_none_default(self): self._do_pos_test(cfg.ListOpt, None, [], None) def test_positional_list_empty_default(self): self._do_pos_test(cfg.ListOpt, [], [], []) def test_positional_list_default(self): self._do_pos_test(cfg.ListOpt, ['bar'], [], ['bar']) def test_positional_list_arg(self): self._do_pos_test(cfg.ListOpt, None, ['blaa,bar'], ['blaa', 'bar']) def test_positional_dict_none_default(self): self._do_pos_test(cfg.DictOpt, None, [], None) def test_positional_dict_empty_default(self): self._do_pos_test(cfg.DictOpt, {}, [], {}) def test_positional_dict_default(self): self._do_pos_test(cfg.DictOpt, {'key1': 'bar'}, [], {'key1': 'bar'}) def test_positional_dict_arg(self): self._do_pos_test(cfg.DictOpt, None, ['key1:blaa,key2:bar'], {'key1': 'blaa', 'key2': 'bar'}) def test_positional_ip_none_default(self): self._do_pos_test(cfg.IPOpt, None, [], None) def test_positional_ip_default(self): self._do_pos_test(cfg.IPOpt, '127.0.0.1', [], '127.0.0.1') def test_positional_ip_arg(self): self._do_pos_test(cfg.IPOpt, None, ['127.0.0.1'], '127.0.0.1') def test_positional_port_none_default(self): self._do_pos_test(cfg.PortOpt, None, [], None) def test_positional_port_default(self): self._do_pos_test(cfg.PortOpt, 80, [], 80) def test_positional_port_arg(self): self._do_pos_test(cfg.PortOpt, None, ['443'], 443) def test_positional_uri_default(self): self._do_pos_test(cfg.URIOpt, 'http://example.com', [], 'http://example.com') def test_positional_uri_none_default(self): self._do_pos_test(cfg.URIOpt, None, [], None) def test_positional_uri_arg(self): self._do_pos_test(cfg.URIOpt, None, ['http://example.com'], 'http://example.com') def test_positional_multistr_none_default(self): self._do_pos_test(cfg.MultiStrOpt, None, [], None) def test_positional_multistr_empty_default(self): self._do_pos_test(cfg.MultiStrOpt, [], [], []) def test_positional_multistr_default(self): self._do_pos_test(cfg.MultiStrOpt, ['bar'], [], ['bar']) def test_positional_multistr_arg(self): self._do_pos_test(cfg.MultiStrOpt, None, ['blaa', 'bar'], ['blaa', 'bar']) def test_positional_bool(self): self.assertRaises(ValueError, cfg.BoolOpt, 'foo', positional=True) def test_required_positional_opt_defined(self): self.conf.register_cli_opt( cfg.StrOpt('foo', required=True, positional=True)) self.useFixture(fixtures.MonkeyPatch('sys.stdout', io.StringIO())) self.assertRaises(SystemExit, self.conf, ['--help']) self.assertIn(' foo\n', sys.stdout.getvalue()) self.conf(['bar']) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) def test_required_positional_opt_undefined(self): self.conf.register_cli_opt( cfg.StrOpt('foo', required=True, positional=True)) self.useFixture(fixtures.MonkeyPatch('sys.stdout', io.StringIO())) self.assertRaises(SystemExit, self.conf, ['--help']) self.assertIn(' foo\n', sys.stdout.getvalue()) self.assertRaises(SystemExit, self.conf, []) def test_optional_positional_opt_defined(self): self.conf.register_cli_opt( cfg.StrOpt('foo', required=False, positional=True)) self.useFixture(fixtures.MonkeyPatch('sys.stdout', io.StringIO())) self.assertRaises(SystemExit, self.conf, ['--help']) self.assertIn(' [foo]\n', sys.stdout.getvalue()) self.conf(['bar']) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) def test_optional_positional_opt_undefined(self): self.conf.register_cli_opt( cfg.StrOpt('foo', required=False, positional=True)) self.useFixture(fixtures.MonkeyPatch('sys.stdout', io.StringIO())) self.assertRaises(SystemExit, self.conf, ['--help']) self.assertIn(' [foo]\n', sys.stdout.getvalue()) self.conf([]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertIsNone(self.conf.foo) def test_optional_positional_hyphenated_opt_defined(self): self.conf.register_cli_opt( cfg.StrOpt('foo-bar', required=False, positional=True)) self.useFixture(fixtures.MonkeyPatch('sys.stdout', io.StringIO())) self.assertRaises(SystemExit, self.conf, ['--help']) self.assertIn(' [foo_bar]\n', sys.stdout.getvalue()) self.conf(['baz']) self.assertTrue(hasattr(self.conf, 'foo_bar')) self.assertEqual('baz', self.conf.foo_bar) def test_optional_positional_hyphenated_opt_undefined(self): self.conf.register_cli_opt( cfg.StrOpt('foo-bar', required=False, positional=True)) self.useFixture(fixtures.MonkeyPatch('sys.stdout', io.StringIO())) self.assertRaises(SystemExit, self.conf, ['--help']) self.assertIn(' [foo_bar]\n', sys.stdout.getvalue()) self.conf([]) self.assertTrue(hasattr(self.conf, 'foo_bar')) self.assertIsNone(self.conf.foo_bar) def test_required_positional_hyphenated_opt_defined(self): self.conf.register_cli_opt( cfg.StrOpt('foo-bar', required=True, positional=True)) self.useFixture(fixtures.MonkeyPatch('sys.stdout', io.StringIO())) self.assertRaises(SystemExit, self.conf, ['--help']) self.assertIn(' foo_bar\n', sys.stdout.getvalue()) self.conf(['baz']) self.assertTrue(hasattr(self.conf, 'foo_bar')) self.assertEqual('baz', self.conf.foo_bar) def test_required_positional_hyphenated_opt_undefined(self): self.conf.register_cli_opt( cfg.StrOpt('foo-bar', required=True, positional=True)) self.useFixture(fixtures.MonkeyPatch('sys.stdout', io.StringIO())) self.assertRaises(SystemExit, self.conf, ['--help']) self.assertIn(' foo_bar\n', sys.stdout.getvalue()) self.assertRaises(SystemExit, self.conf, []) def test_missing_required_cli_opt(self): self.conf.register_cli_opt( cfg.StrOpt('foo', required=True, positional=True)) self.assertRaises(SystemExit, self.conf, []) def test_positional_opts_order(self): self.conf.register_cli_opts(( cfg.StrOpt('command', positional=True), cfg.StrOpt('arg1', positional=True), cfg.StrOpt('arg2', positional=True)) ) self.conf(['command', 'arg1', 'arg2']) self.assertEqual('command', self.conf.command) self.assertEqual('arg1', self.conf.arg1) self.assertEqual('arg2', self.conf.arg2) def test_positional_opt_order(self): self.conf.register_cli_opt( cfg.StrOpt('command', positional=True)) self.conf.register_cli_opt( cfg.StrOpt('arg1', positional=True)) self.conf.register_cli_opt( cfg.StrOpt('arg2', positional=True)) self.conf(['command', 'arg1', 'arg2']) self.assertEqual('command', self.conf.command) self.assertEqual('arg1', self.conf.arg1) self.assertEqual('arg2', self.conf.arg2) def test_positional_opt_unregister(self): command = cfg.StrOpt('command', positional=True) arg1 = cfg.StrOpt('arg1', positional=True) arg2 = cfg.StrOpt('arg2', positional=True) self.conf.register_cli_opt(command) self.conf.register_cli_opt(arg1) self.conf.register_cli_opt(arg2) self.conf(['command', 'arg1', 'arg2']) self.assertEqual('command', self.conf.command) self.assertEqual('arg1', self.conf.arg1) self.assertEqual('arg2', self.conf.arg2) self.conf.reset() self.conf.unregister_opt(arg1) self.conf.unregister_opt(arg2) arg0 = cfg.StrOpt('arg0', positional=True) self.conf.register_cli_opt(arg0) self.conf.register_cli_opt(arg1) self.conf(['command', 'arg0', 'arg1']) self.assertEqual('command', self.conf.command) self.assertEqual('arg0', self.conf.arg0) self.assertEqual('arg1', self.conf.arg1) # The real report_deprecated_feature has caching that causes races in our # unit tests. This replicates the relevant functionality. def _fake_deprecated_feature(logger, msg, *args, **kwargs): stdmsg = 'Deprecated: %s' % msg logger.warning(stdmsg, *args, **kwargs) @mock.patch('oslo_log.versionutils.report_deprecated_feature', _fake_deprecated_feature) class ConfigFileOptsTestCase(BaseTestCase): def setUp(self): super(ConfigFileOptsTestCase, self).setUp() self.logger = self.useFixture( fixtures.FakeLogger( format='%(message)s', level=logging.WARNING, nuke_handlers=True, ) ) def _do_deprecated_test(self, opt_class, value, result, key, section='DEFAULT', dname=None, dgroup=None): self.conf.register_opt(opt_class('newfoo', deprecated_name=dname, deprecated_group=dgroup)) paths = self.create_tempfiles([('test', '[' + section + ']\n' + key + ' = ' + value + '\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'newfoo')) self.assertEqual(result, self.conf.newfoo) def _do_dname_test_use(self, opt_class, value, result): self._do_deprecated_test(opt_class, value, result, 'oldfoo', dname='oldfoo') def _do_dgroup_test_use(self, opt_class, value, result): self._do_deprecated_test(opt_class, value, result, 'newfoo', section='old', dgroup='old') def _do_default_dgroup_test_use(self, opt_class, value, result): self._do_deprecated_test(opt_class, value, result, 'newfoo', section='DEFAULT', dgroup='DEFAULT') def _do_dgroup_and_dname_test_use(self, opt_class, value, result): self._do_deprecated_test(opt_class, value, result, 'oof', section='old', dgroup='old', dname='oof') def _do_dname_test_ignore(self, opt_class, value, result): self._do_deprecated_test(opt_class, value, result, 'newfoo', dname='oldfoo') def _do_dgroup_test_ignore(self, opt_class, value, result): self._do_deprecated_test(opt_class, value, result, 'newfoo', section='DEFAULT', dgroup='old') def _do_dgroup_and_dname_test_ignore(self, opt_class, value, result): self._do_deprecated_test(opt_class, value, result, 'oof', section='old', dgroup='old', dname='oof') def test_conf_file_str_default(self): self.conf.register_opt(cfg.StrOpt('foo', default='bar')) paths = self.create_tempfiles([('test', '[DEFAULT]\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) def test_conf_file_str_value(self): self.conf.register_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) def test_conf_file_str_value_override(self): self.conf.register_cli_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = baar\n'), ('2', '[DEFAULT]\n' 'foo = baaar\n')]) self.conf(['--foo', 'bar', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('baaar', self.conf.foo) def test_conf_file_str_value_override_use_deprecated(self): """last option should always win, even if last uses deprecated.""" self.conf.register_cli_opt( cfg.StrOpt('newfoo', deprecated_name='oldfoo')) paths = self.create_tempfiles([('0', '[DEFAULT]\n' 'newfoo = middle\n'), ('1', '[DEFAULT]\n' 'oldfoo = last\n')]) self.conf(['--newfoo', 'first', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'newfoo')) self.assertTrue(hasattr(self.conf, 'oldfoo')) self.assertEqual('last', self.conf.newfoo) log_out = self.logger.output self.assertIn('is deprecated', log_out) self.assertIn('Use option "newfoo"', log_out) def test_use_deprecated_for_removal_without_reason(self): self.conf.register_cli_opt( cfg.StrOpt('oldfoo', deprecated_for_removal=True)) paths = self.create_tempfiles([('0', '[DEFAULT]\n' 'oldfoo = middle\n')]) self.conf(['--oldfoo', 'first', '--config-file', paths[0]]) log_out = self.logger.output self.assertIn('deprecated for removal.', log_out) def test_use_deprecated_for_removal_with_reason(self): self.conf.register_cli_opt( cfg.StrOpt('oldfoo', deprecated_for_removal=True, deprecated_reason='a very good reason')) paths = self.create_tempfiles([('0', '[DEFAULT]\n' 'oldfoo = middle\n')]) self.conf(['--oldfoo', 'first', '--config-file', paths[0]]) log_out = self.logger.output self.assertIn('deprecated for removal (a very good reason).', log_out) def test_conf_file_str_use_dname(self): self._do_dname_test_use(cfg.StrOpt, 'value1', 'value1') def test_conf_file_str_use_dgroup(self): self._do_dgroup_test_use(cfg.StrOpt, 'value1', 'value1') def test_conf_file_str_use_default_dgroup(self): self._do_default_dgroup_test_use(cfg.StrOpt, 'value1', 'value1') def test_conf_file_str_use_dgroup_and_dname(self): self._do_dgroup_and_dname_test_use(cfg.StrOpt, 'value1', 'value1') def test_conf_file_str_ignore_dname(self): self._do_dname_test_ignore(cfg.StrOpt, 'value2', 'value2') def test_conf_file_str_ignore_dgroup(self): self._do_dgroup_test_ignore(cfg.StrOpt, 'value2', 'value2') def test_conf_file_str_ignore_dgroup_and_dname(self): self._do_dgroup_and_dname_test_ignore(cfg.StrOpt, 'value2', 'value2') def test_conf_file_str_value_with_good_choice_value(self): self.conf.register_opt(cfg.StrOpt('foo', choices=['bar', 'baz'])) paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) def test_conf_file_bool_default_none(self): self.conf.register_opt(cfg.BoolOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertIsNone(self.conf.foo) def test_conf_file_bool_default_false(self): self.conf.register_opt(cfg.BoolOpt('foo', default=False)) paths = self.create_tempfiles([('test', '[DEFAULT]\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertFalse(self.conf.foo) def test_conf_file_bool_value(self): self.conf.register_opt(cfg.BoolOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = true\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertTrue(self.conf.foo) def test_conf_file_bool_cli_value_override(self): self.conf.register_cli_opt(cfg.BoolOpt('foo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = 0\n')]) self.conf(['--foo', '--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertFalse(self.conf.foo) def test_conf_file_bool_cli_inverse_override(self): self.conf.register_cli_opt(cfg.BoolOpt('foo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = true\n')]) self.conf(['--nofoo', '--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertTrue(self.conf.foo) def test_conf_file_bool_cli_order_override(self): self.conf.register_cli_opt(cfg.BoolOpt('foo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = false\n')]) self.conf(['--config-file', paths[0], '--foo']) self.assertTrue(hasattr(self.conf, 'foo')) self.assertTrue(self.conf.foo) def test_conf_file_bool_file_value_override(self): self.conf.register_cli_opt(cfg.BoolOpt('foo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = 0\n'), ('2', '[DEFAULT]\n' 'foo = yes\n')]) self.conf(['--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertTrue(self.conf.foo) def test_conf_file_bool_use_dname(self): self._do_dname_test_use(cfg.BoolOpt, 'yes', True) def test_conf_file_bool_use_dgroup(self): self._do_dgroup_test_use(cfg.BoolOpt, 'yes', True) def test_conf_file_bool_use_default_dgroup(self): self._do_default_dgroup_test_use(cfg.BoolOpt, 'yes', True) def test_conf_file_bool_use_dgroup_and_dname(self): self._do_dgroup_and_dname_test_use(cfg.BoolOpt, 'yes', True) def test_conf_file_bool_ignore_dname(self): self._do_dname_test_ignore(cfg.BoolOpt, 'no', False) def test_conf_file_bool_ignore_dgroup(self): self._do_dgroup_test_ignore(cfg.BoolOpt, 'no', False) def test_conf_file_bool_ignore_group_and_dname(self): self._do_dgroup_and_dname_test_ignore(cfg.BoolOpt, 'no', False) def test_conf_file_int_default(self): self.conf.register_opt(cfg.IntOpt('foo', default=666)) paths = self.create_tempfiles([('test', '[DEFAULT]\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(666, self.conf.foo) def test_conf_file_int_string_default_type(self): self.conf.register_opt(cfg.IntOpt('foo', default='666')) self.conf([]) self.assertEqual(666, self.conf.foo) def test_conf_file_int_value(self): self.conf.register_opt(cfg.IntOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 666\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(666, self.conf.foo) def test_conf_file_int_value_override(self): self.conf.register_cli_opt(cfg.IntOpt('foo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = 66\n'), ('2', '[DEFAULT]\n' 'foo = 666\n')]) self.conf(['--foo', '6', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(666, self.conf.foo) def test_conf_file_int_min_max(self): self.conf.register_opt(cfg.IntOpt('foo', min=1, max=5)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 10\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_int_min_greater_max(self): self.assertRaises(ValueError, cfg.IntOpt, 'foo', min=5, max=1) def test_conf_file_int_use_dname(self): self._do_dname_test_use(cfg.IntOpt, '66', 66) def test_conf_file_int_use_dgroup(self): self._do_dgroup_test_use(cfg.IntOpt, '66', 66) def test_conf_file_int_use_default_dgroup(self): self._do_default_dgroup_test_use(cfg.IntOpt, '66', 66) def test_conf_file_int_use_dgroup_and_dname(self): self._do_dgroup_and_dname_test_use(cfg.IntOpt, '66', 66) def test_conf_file_int_ignore_dname(self): self._do_dname_test_ignore(cfg.IntOpt, '64', 64) def test_conf_file_int_ignore_dgroup(self): self._do_dgroup_test_ignore(cfg.IntOpt, '64', 64) def test_conf_file_int_ignore_dgroup_and_dname(self): self._do_dgroup_and_dname_test_ignore(cfg.IntOpt, '64', 64) def test_conf_file_float_default(self): self.conf.register_opt(cfg.FloatOpt('foo', default=6.66)) paths = self.create_tempfiles([('test', '[DEFAULT]\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(6.66, self.conf.foo) def test_conf_file_float_default_wrong_type(self): self.assertRaises(cfg.DefaultValueError, cfg.FloatOpt, 'foo', default='foobar6.66') def test_conf_file_float_value(self): self.conf.register_opt(cfg.FloatOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 6.66\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(6.66, self.conf.foo) def test_conf_file_float_value_override(self): self.conf.register_cli_opt(cfg.FloatOpt('foo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = 6.6\n'), ('2', '[DEFAULT]\n' 'foo = 6.66\n')]) self.conf(['--foo', '6', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(6.66, self.conf.foo) def test_conf_file_float_use_dname(self): self._do_dname_test_use(cfg.FloatOpt, '66.54', 66.54) def test_conf_file_float_use_dgroup(self): self._do_dgroup_test_use(cfg.FloatOpt, '66.54', 66.54) def test_conf_file_float_use_default_dgroup(self): self._do_default_dgroup_test_use(cfg.FloatOpt, '66.54', 66.54) def test_conf_file_float_use_dgroup_and_dname(self): self._do_dgroup_and_dname_test_use(cfg.FloatOpt, '66.54', 66.54) def test_conf_file_float_ignore_dname(self): self._do_dname_test_ignore(cfg.FloatOpt, '64.54', 64.54) def test_conf_file_float_ignore_dgroup(self): self._do_dgroup_test_ignore(cfg.FloatOpt, '64.54', 64.54) def test_conf_file_float_ignore_dgroup_and_dname(self): self._do_dgroup_and_dname_test_ignore(cfg.FloatOpt, '64.54', 64.54) def test_conf_file_float_min_max_above_max(self): self.conf.register_opt(cfg.FloatOpt('foo', min=1.1, max=5.5)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 10.5\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_float_only_max_above_max(self): self.conf.register_opt(cfg.FloatOpt('foo', max=5.5)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 10.5\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_float_min_max_below_min(self): self.conf.register_opt(cfg.FloatOpt('foo', min=1.1, max=5.5)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 0.5\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_float_only_min_below_min(self): self.conf.register_opt(cfg.FloatOpt('foo', min=1.1)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 0.5\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_float_min_max_in_range(self): self.conf.register_opt(cfg.FloatOpt('foo', min=1.1, max=5.5)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 4.5\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(4.5, self.conf.foo) def test_conf_file_float_only_max_in_range(self): self.conf.register_opt(cfg.FloatOpt('foo', max=5.5)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 4.5\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(4.5, self.conf.foo) def test_conf_file_float_only_min_in_range(self): self.conf.register_opt(cfg.FloatOpt('foo', min=3.5)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 4.5\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(4.5, self.conf.foo) def test_conf_file_float_min_greater_max(self): self.assertRaises(ValueError, cfg.FloatOpt, 'foo', min=5.5, max=1.5) def test_conf_file_list_default(self): self.conf.register_opt(cfg.ListOpt('foo', default=['bar'])) paths = self.create_tempfiles([('test', '[DEFAULT]\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(['bar'], self.conf.foo) def test_conf_file_list_default_wrong_type(self): self.assertRaises(cfg.DefaultValueError, cfg.ListOpt, 'foo', default=25) def test_conf_file_list_value(self): self.conf.register_opt(cfg.ListOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(['bar'], self.conf.foo) def test_conf_file_list_value_override(self): self.conf.register_cli_opt(cfg.ListOpt('foo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = bar,bar\n'), ('2', '[DEFAULT]\n' 'foo = b,a,r\n')]) self.conf(['--foo', 'bar', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(['b', 'a', 'r'], self.conf.foo) def test_conf_file_list_item_type(self): self.conf.register_cli_opt(cfg.ListOpt('foo', item_type=types.Integer())) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = 1,2\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual([1, 2], self.conf.foo) def test_conf_file_list_item_wrong_type(self): self.assertRaises(cfg.DefaultValueError, cfg.ListOpt, 'foo', default="bar", item_type=types.Integer()) def test_conf_file_list_bounds(self): self.conf.register_cli_opt(cfg.ListOpt('foo', item_type=types.Integer(), default="[1,2]", bounds=True)) self.conf([]) self.assertEqual([1, 2], self.conf.foo) def test_conf_file_list_use_dname(self): self._do_dname_test_use(cfg.ListOpt, 'a,b,c', ['a', 'b', 'c']) def test_conf_file_list_use_dgroup(self): self._do_dgroup_test_use(cfg.ListOpt, 'a,b,c', ['a', 'b', 'c']) def test_conf_file_list_use_default_dgroup(self): self._do_default_dgroup_test_use(cfg.ListOpt, 'a,b,c', ['a', 'b', 'c']) def test_conf_file_list_use_dgroup_and_dname(self): self._do_dgroup_and_dname_test_use(cfg.ListOpt, 'a,b,c', ['a', 'b', 'c']) def test_conf_file_list_ignore_dname(self): self._do_dname_test_ignore(cfg.ListOpt, 'd,e,f', ['d', 'e', 'f']) def test_conf_file_list_ignore_dgroup(self): self._do_dgroup_test_ignore(cfg.ListOpt, 'd,e,f', ['d', 'e', 'f']) def test_conf_file_list_ignore_dgroup_and_dname(self): self._do_dgroup_and_dname_test_ignore( cfg.ListOpt, 'd,e,f', ['d', 'e', 'f']) def test_conf_file_list_spaces_use_dname(self): self._do_dname_test_use(cfg.ListOpt, 'a, b, c', ['a', 'b', 'c']) def test_conf_file_list_spaces_use_dgroup(self): self._do_dgroup_test_use(cfg.ListOpt, 'a, b, c', ['a', 'b', 'c']) def test_conf_file_list_spaces_use_default_dgroup(self): self._do_default_dgroup_test_use( cfg.ListOpt, 'a, b, c', ['a', 'b', 'c']) def test_conf_file_list_spaces_use_dgroup_and_dname(self): self._do_dgroup_and_dname_test_use( cfg.ListOpt, 'a, b, c', ['a', 'b', 'c']) def test_conf_file_list_spaces_ignore_dname(self): self._do_dname_test_ignore(cfg.ListOpt, 'd, e, f', ['d', 'e', 'f']) def test_conf_file_list_spaces_ignore_dgroup(self): self._do_dgroup_test_ignore(cfg.ListOpt, 'd, e, f', ['d', 'e', 'f']) def test_conf_file_list_spaces_ignore_dgroup_and_dname(self): self._do_dgroup_and_dname_test_ignore(cfg.ListOpt, 'd, e, f', ['d', 'e', 'f']) def test_conf_file_dict_default(self): self.conf.register_opt(cfg.DictOpt('foo', default={'key': 'bar'})) paths = self.create_tempfiles([('test', '[DEFAULT]\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual({'key': 'bar'}, self.conf.foo) def test_conf_file_dict_value(self): self.conf.register_opt(cfg.DictOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = key:bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual({'key': 'bar'}, self.conf.foo) def test_conf_file_dict_colon_in_value(self): self.conf.register_opt(cfg.DictOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = key:bar:baz\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual({'key': 'bar:baz'}, self.conf.foo) def test_conf_file_dict_value_no_colon(self): self.conf.register_opt(cfg.DictOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = key:bar,baz\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') self.assertRaises(ValueError, getattr, self.conf, 'foo') def test_conf_file_dict_value_duplicate_key(self): self.conf.register_opt(cfg.DictOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = key:bar,key:baz\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') self.assertRaises(ValueError, getattr, self.conf, 'foo') def test_conf_file_dict_values_override_deprecated(self): self.conf.register_cli_opt(cfg.DictOpt('foo', deprecated_name='oldfoo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = key1:bar1\n'), ('2', '[DEFAULT]\n' 'oldfoo = key2:bar2\n' 'oldfoo = key3:bar3\n')]) self.conf(['--foo', 'key0:bar0', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual({'key3': 'bar3'}, self.conf.foo) def test_conf_file_dict_deprecated(self): self.conf.register_opt(cfg.DictOpt('newfoo', deprecated_name='oldfoo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'oldfoo= key1:bar1\n' 'oldfoo = key2:bar2\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'newfoo')) self.assertEqual({'key2': 'bar2'}, self.conf.newfoo) def test_conf_file_dict_value_override(self): self.conf.register_cli_opt(cfg.DictOpt('foo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = key:bar,key2:bar\n'), ('2', '[DEFAULT]\n' 'foo = k1:v1,k2:v2\n')]) self.conf(['--foo', 'x:y,x2:y2', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual({'k1': 'v1', 'k2': 'v2'}, self.conf.foo) def test_conf_file_dict_use_dname(self): self._do_dname_test_use(cfg.DictOpt, 'k1:a,k2:b,k3:c', {'k1': 'a', 'k2': 'b', 'k3': 'c'}) def test_conf_file_dict_use_dgroup(self): self._do_dgroup_test_use(cfg.DictOpt, 'k1:a,k2:b,k3:c', {'k1': 'a', 'k2': 'b', 'k3': 'c'}) def test_conf_file_dict_use_default_dgroup(self): self._do_default_dgroup_test_use(cfg.DictOpt, 'k1:a,k2:b,k3:c', {'k1': 'a', 'k2': 'b', 'k3': 'c'}) def test_conf_file_dict_use_dgroup_and_dname(self): self._do_dgroup_and_dname_test_use(cfg.DictOpt, 'k1:a,k2:b,k3:c', {'k1': 'a', 'k2': 'b', 'k3': 'c'}) def test_conf_file_dict_ignore_dname(self): self._do_dname_test_ignore(cfg.DictOpt, 'k1:d,k2:e,k3:f', {'k1': 'd', 'k2': 'e', 'k3': 'f'}) def test_conf_file_dict_ignore_dgroup(self): self._do_dgroup_test_ignore(cfg.DictOpt, 'k1:d,k2:e,k3:f', {'k1': 'd', 'k2': 'e', 'k3': 'f'}) def test_conf_file_dict_ignore_dgroup_and_dname(self): self._do_dgroup_and_dname_test_ignore(cfg.DictOpt, 'k1:d,k2:e,k3:f', {'k1': 'd', 'k2': 'e', 'k3': 'f'}) def test_conf_file_dict_spaces_use_dname(self): self._do_dname_test_use(cfg.DictOpt, 'k1:a,k2:b,k3:c', {'k1': 'a', 'k2': 'b', 'k3': 'c'}) def test_conf_file_dict_spaces_use_dgroup(self): self._do_dgroup_test_use(cfg.DictOpt, 'k1:a,k2:b,k3:c', {'k1': 'a', 'k2': 'b', 'k3': 'c'}) def test_conf_file_dict_spaces_use_default_dgroup(self): self._do_default_dgroup_test_use(cfg.DictOpt, 'k1:a,k2:b,k3:c', {'k1': 'a', 'k2': 'b', 'k3': 'c'}) def test_conf_file_dict_spaces_use_dgroup_and_dname(self): self._do_dgroup_and_dname_test_use(cfg.DictOpt, 'k1:a,k2:b,k3:c', {'k1': 'a', 'k2': 'b', 'k3': 'c'}) def test_conf_file_dict_spaces_ignore_dname(self): self._do_dname_test_ignore(cfg.DictOpt, 'k1:d,k2:e,k3:f', {'k1': 'd', 'k2': 'e', 'k3': 'f'}) def test_conf_file_dict_spaces_ignore_dgroup(self): self._do_dgroup_test_ignore(cfg.DictOpt, 'k1:d,k2:e,k3:f', {'k1': 'd', 'k2': 'e', 'k3': 'f'}) def test_conf_file_dict_spaces_ignore_dgroup_and_dname(self): self._do_dgroup_and_dname_test_ignore(cfg.DictOpt, 'k1:d,k2:e,k3:f', {'k1': 'd', 'k2': 'e', 'k3': 'f'}) def test_conf_file_port_outside_range(self): self.conf.register_opt(cfg.PortOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 65536\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_port_list(self): self.conf.register_opt(cfg.ListOpt('foo', item_type=types.Port())) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 22, 80\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual([22, 80], self.conf.foo) def test_conf_file_port_list_default(self): self.conf.register_opt(cfg.ListOpt('foo', item_type=types.Port(), default=[55, 77])) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 22, 80\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual([22, 80], self.conf.foo) def test_conf_file_port_list_only_default(self): self.conf.register_opt(cfg.ListOpt('foo', item_type=types.Port(), default=[55, 77])) paths = self.create_tempfiles([('test', '[DEFAULT]\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual([55, 77], self.conf.foo) def test_conf_file_port_list_outside_range(self): self.conf.register_opt(cfg.ListOpt('foo', item_type=types.Port())) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 1,65536\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_port_min_max_above_max(self): self.conf.register_opt(cfg.PortOpt('foo', min=1, max=5)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 10\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_port_only_max_above_max(self): self.conf.register_opt(cfg.PortOpt('foo', max=500)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 600\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_port_min_max_below_min(self): self.conf.register_opt(cfg.PortOpt('foo', min=100, max=500)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 99\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_port_only_min_below_min(self): self.conf.register_opt(cfg.PortOpt('foo', min=1025)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 1024\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_port_min_max_in_range(self): self.conf.register_opt(cfg.PortOpt('foo', min=1025, max=6000)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 2500\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(2500, self.conf.foo) def test_conf_file_port_only_max_in_range(self): self.conf.register_opt(cfg.PortOpt('foo', max=5000)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 45\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(45, self.conf.foo) def test_conf_file_port_only_min_in_range(self): self.conf.register_opt(cfg.PortOpt('foo', min=35)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 45\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(45, self.conf.foo) def test_conf_file_port_min_greater_max(self): self.assertRaises(ValueError, cfg.PortOpt, 'foo', min=55, max=15) def test_conf_file_multistr_default(self): self.conf.register_opt(cfg.MultiStrOpt('foo', default=['bar'])) paths = self.create_tempfiles([('test', '[DEFAULT]\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(['bar'], self.conf.foo) def test_conf_file_multistr_value(self): self.conf.register_opt(cfg.MultiStrOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(['bar'], self.conf.foo) def test_conf_file_multistr_values_append_deprecated(self): self.conf.register_cli_opt(cfg.MultiStrOpt('foo', deprecated_name='oldfoo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = bar1\n'), ('2', '[DEFAULT]\n' 'oldfoo = bar2\n' 'oldfoo = bar3\n')]) self.conf(['--foo', 'bar0', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(['bar0', 'bar1', 'bar2', 'bar3'], self.conf.foo) def test_conf_file_multistr_values_append(self): self.conf.register_cli_opt(cfg.MultiStrOpt('foo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = bar1\n'), ('2', '[DEFAULT]\n' 'foo = bar2\n' 'foo = bar3\n')]) self.conf(['--foo', 'bar0', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(['bar0', 'bar1', 'bar2', 'bar3'], self.conf.foo) def test_conf_file_multistr_deprecated(self): self.conf.register_opt( cfg.MultiStrOpt('newfoo', deprecated_name='oldfoo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'oldfoo= bar1\n' 'oldfoo = bar2\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'newfoo')) self.assertEqual(['bar1', 'bar2'], self.conf.newfoo) def test_conf_file_multiple_opts(self): self.conf.register_opts([cfg.StrOpt('foo'), cfg.StrOpt('bar')]) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bar\n' 'bar = foo\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) self.assertTrue(hasattr(self.conf, 'bar')) self.assertEqual('foo', self.conf.bar) def test_conf_file_raw_value(self): self.conf.register_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bar-%08x\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar-%08x', self.conf.foo) def test_conf_file_sorted_group(self): # Create enough groups for the sorted problem to appear for i in range(10): group = cfg.OptGroup('group%s' % i, 'options') self.conf.register_group(group) self.conf.register_cli_opt(cfg.StrOpt('opt1'), group=group) paths = self.create_tempfiles( [('test', '[group1]\nopt1 = foo\n[group2]\nopt2 = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('foo', self.conf.group1.opt1) class ConfigFileReloadTestCase(BaseTestCase): def test_conf_files_reload(self): self.conf.register_cli_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = baar\n'), ('2', '[DEFAULT]\n' 'foo = baaar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('baar', self.conf.foo) shutil.copy(paths[1], paths[0]) self.conf.reload_config_files() self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('baaar', self.conf.foo) def test_conf_files_reload_default(self): self.conf.register_cli_opt(cfg.StrOpt('foo1')) self.conf.register_cli_opt(cfg.StrOpt('foo2')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo1 = default1\n'), ('2', '[DEFAULT]\n' 'foo2 = default2\n')]) paths_change = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo1 = change_default1\n'), ('2', '[DEFAULT]\n' 'foo2 = change_default2\n')]) self.conf(args=[], default_config_files=paths) self.assertTrue(hasattr(self.conf, 'foo1')) self.assertEqual('default1', self.conf.foo1) self.assertTrue(hasattr(self.conf, 'foo2')) self.assertEqual('default2', self.conf.foo2) shutil.copy(paths_change[0], paths[0]) shutil.copy(paths_change[1], paths[1]) self.conf.reload_config_files() self.assertTrue(hasattr(self.conf, 'foo1')) self.assertEqual('change_default1', self.conf.foo1) self.assertTrue(hasattr(self.conf, 'foo2')) self.assertEqual('change_default2', self.conf.foo2) def test_conf_files_reload_file_not_found(self): self.conf.register_cli_opt(cfg.StrOpt('foo', required=True)) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = baar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('baar', self.conf.foo) os.remove(paths[0]) self.conf.reload_config_files() self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('baar', self.conf.foo) def test_conf_files_reload_error(self): self.conf.register_cli_opt(cfg.StrOpt('foo', required=True)) self.conf.register_cli_opt(cfg.StrOpt('foo1', required=True)) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = test1\n' 'foo1 = test11\n'), ('2', '[DEFAULT]\n' 'foo2 = test2\n' 'foo3 = test22\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('test1', self.conf.foo) self.assertTrue(hasattr(self.conf, 'foo1')) self.assertEqual('test11', self.conf.foo1) shutil.copy(paths[1], paths[0]) self.conf.reload_config_files() self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('test1', self.conf.foo) self.assertTrue(hasattr(self.conf, 'foo1')) self.assertEqual('test11', self.conf.foo1) class ConfigFileMutateTestCase(BaseTestCase): def setUp(self): super(ConfigFileMutateTestCase, self).setUp() self.my_group = cfg.OptGroup('group', 'group options') self.conf.register_group(self.my_group) def _test_conf_files_mutate(self): paths = self.create_tempfiles([ ('1', '[DEFAULT]\n' 'foo = old_foo\n' '[group]\n' 'boo = old_boo\n'), ('2', '[DEFAULT]\n' 'foo = new_foo\n' '[group]\n' 'boo = new_boo\n')]) self.conf(['--config-file', paths[0]]) shutil.copy(paths[1], paths[0]) return self.conf.mutate_config_files() def test_conf_files_mutate_none(self): """Test that immutable opts are not reloaded""" self.conf.register_cli_opt(cfg.StrOpt('foo')) self._test_conf_files_mutate() self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('old_foo', self.conf.foo) def test_conf_files_mutate_foo(self): """Test that a mutable opt can be reloaded.""" self.conf.register_cli_opt(cfg.StrOpt('foo', mutable=True)) self._test_conf_files_mutate() self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('new_foo', self.conf.foo) def test_conf_files_mutate_group(self): """Test that mutable opts in groups can be reloaded.""" self.conf.register_cli_opt(cfg.StrOpt('boo', mutable=True), group=self.my_group) self._test_conf_files_mutate() self.assertTrue(hasattr(self.conf, 'group')) self.assertTrue(hasattr(self.conf.group, 'boo')) self.assertEqual('new_boo', self.conf.group.boo) def test_warn_immutability(self): self.log_fixture = self.useFixture(fixtures.FakeLogger()) self.conf.register_cli_opt(cfg.StrOpt('foo', mutable=True)) self.conf.register_cli_opt(cfg.StrOpt('boo'), group=self.my_group) self._test_conf_files_mutate() self.assertEqual( "Ignoring change to immutable option group.boo\n" "Option DEFAULT.foo changed from [old_foo] to [new_foo]\n", self.log_fixture.output) def test_diff(self): self.log_fixture = self.useFixture(fixtures.FakeLogger()) self.conf.register_cli_opt(cfg.StrOpt('imm')) self.conf.register_cli_opt(cfg.StrOpt('blank', mutable=True)) self.conf.register_cli_opt(cfg.StrOpt('foo', mutable=True)) self.conf.register_cli_opt(cfg.StrOpt('boo', mutable=True), group=self.my_group) diff = self._test_conf_files_mutate() self.assertEqual( {(None, 'foo'): ('old_foo', 'new_foo'), ('group', 'boo'): ('old_boo', 'new_boo')}, diff) expected = ("Option DEFAULT.foo changed from [old_foo] to [new_foo]\n" "Option group.boo changed from [old_boo] to [new_boo]\n") self.assertEqual(expected, self.log_fixture.output) def test_hooks_invoked_once(self): fresh = {} result = [0] def foo(conf, foo_fresh): self.assertEqual(conf, self.conf) self.assertEqual(fresh, foo_fresh) result[0] += 1 self.conf.register_mutate_hook(foo) self.conf.register_mutate_hook(foo) self._test_conf_files_mutate() self.assertEqual(1, result[0]) def test_hooks_see_new_values(self): def foo(conf, fresh): # Verify that we see the new value inside the mutate hook. self.assertEqual('new_foo', conf.foo) self.conf.register_cli_opt(cfg.StrOpt('foo', mutable=True)) self.conf.register_mutate_hook(foo) paths = self.create_tempfiles([ ('1', '[DEFAULT]\n' 'foo = old_foo\n' '[group]\n' 'boo = old_boo\n'), ('2', '[DEFAULT]\n' 'foo = new_foo\n' '[group]\n' 'boo = new_boo\n')]) self.conf(['--config-file', paths[0]]) # We access the value once before mutating it to ensure the # cache is populated. self.assertEqual('old_foo', self.conf.foo) shutil.copy(paths[1], paths[0]) self.conf.mutate_config_files() # Verify that we see the new value after mutation is complete. self.assertEqual('new_foo', self.conf.foo) def test_clear(self): """Show that #clear doesn't undeclare opts. This justifies not clearing mutate_hooks either. ResetAndClearTestCase shows that values are cleared. """ self.conf.register_cli_opt(cfg.StrOpt('cli')) self.conf.register_opt(cfg.StrOpt('foo')) dests = [info['opt'].dest for info, _ in self.conf._all_opt_infos()] self.assertIn('cli', dests) self.assertIn('foo', dests) self.conf.clear() dests = [info['opt'].dest for info, _ in self.conf._all_opt_infos()] self.assertIn('cli', dests) self.assertIn('foo', dests) class OptGroupsTestCase(BaseTestCase): def test_arg_group(self): blaa_group = cfg.OptGroup('blaa', 'blaa options') self.conf.register_group(blaa_group) self.conf.register_cli_opt(cfg.StrOpt('foo'), group=blaa_group) self.conf(['--blaa-foo', 'bar']) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) self.assertEqual('blaa', str(blaa_group)) def test_autocreate_group_by_name(self): self.conf.register_cli_opt(cfg.StrOpt('foo'), group='blaa') self.conf(['--blaa-foo', 'bar']) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_autocreate_group_by_group(self): group = cfg.OptGroup(name='blaa', title='Blaa options') self.conf.register_cli_opt(cfg.StrOpt('foo'), group=group) self.conf(['--blaa-foo', 'bar']) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_autocreate_title(self): blaa_group = cfg.OptGroup('blaa') self.assertEqual(blaa_group.title, 'blaa options') def test_arg_group_by_name(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_cli_opt(cfg.StrOpt('foo'), group='blaa') self.conf(['--blaa-foo', 'bar']) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_arg_group_with_default(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_cli_opt( cfg.StrOpt('foo', default='bar'), group='blaa') self.conf([]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_arg_group_with_conf_and_group_opts(self): self.conf.register_cli_opt(cfg.StrOpt('conf'), group='blaa') self.conf.register_cli_opt(cfg.StrOpt('group'), group='blaa') self.conf(['--blaa-conf', 'foo', '--blaa-group', 'bar']) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'conf')) self.assertEqual('foo', self.conf.blaa.conf) self.assertTrue(hasattr(self.conf.blaa, 'group')) self.assertEqual('bar', self.conf.blaa.group) def test_arg_group_in_config_file(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo'), group='blaa') paths = self.create_tempfiles([('test', '[blaa]\n' 'foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_arg_group_in_config_file_with_deprecated_name(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo', deprecated_name='oldfoo'), group='blaa') paths = self.create_tempfiles([('test', '[blaa]\n' 'oldfoo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_arg_group_in_config_file_with_deprecated_group(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo', deprecated_group='DEFAULT'), group='blaa') paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_arg_group_in_config_file_with_deprecated_group_and_name(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt( cfg.StrOpt('foo', deprecated_group='DEFAULT', deprecated_name='oldfoo'), group='blaa') paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'oldfoo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_arg_group_in_config_file_override_deprecated_name(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo', deprecated_name='oldfoo'), group='blaa') paths = self.create_tempfiles([('test', '[blaa]\n' 'foo = bar\n' 'oldfoo = blabla\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_arg_group_in_config_file_override_deprecated_group(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo', deprecated_group='DEFAULT'), group='blaa') paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = blabla\n' '[blaa]\n' 'foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_arg_group_in_config_file_override_deprecated_group_and_name(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt( cfg.StrOpt('foo', deprecated_group='DEFAULT', deprecated_name='oldfoo'), group='blaa') paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'oldfoo = blabla\n' '[blaa]\n' 'foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_arg_group_in_config_file_with_capital_name(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo'), group='blaa') paths = self.create_tempfiles([('test', '[BLAA]\n' 'foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertFalse(hasattr(self.conf, 'BLAA')) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_arg_group_in_config_file_with_capital_name_on_legacy_code(self): self.conf.register_group(cfg.OptGroup('BLAA')) self.conf.register_opt(cfg.StrOpt('foo'), group='BLAA') paths = self.create_tempfiles([('test', '[BLAA]\n' 'foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertFalse(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf, 'BLAA')) self.assertTrue(hasattr(self.conf.BLAA, 'foo')) self.assertEqual('bar', self.conf.BLAA.foo) class MappingInterfaceTestCase(BaseTestCase): def test_mapping_interface(self): self.conf.register_cli_opt(cfg.StrOpt('foo')) self.conf(['--foo', 'bar']) self.assertIn('foo', self.conf) self.assertIn('config_file', self.conf) self.assertEqual(len(self.conf), 5) self.assertEqual('bar', self.conf['foo']) self.assertEqual('bar', self.conf.get('foo')) self.assertIn('bar', list(self.conf.values())) def test_mapping_interface_with_group(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_cli_opt(cfg.StrOpt('foo'), group='blaa') self.conf(['--blaa-foo', 'bar']) self.assertIn('blaa', self.conf) self.assertIn('foo', list(self.conf['blaa'])) self.assertEqual(len(self.conf['blaa']), 1) self.assertEqual('bar', self.conf['blaa']['foo']) self.assertEqual('bar', self.conf['blaa'].get('foo')) self.assertIn('bar', self.conf['blaa'].values()) self.assertEqual(self.conf['blaa'], self.conf.blaa) class OptNameSeparatorTestCase(BaseTestCase): # NOTE(bnemec): The broken* values in these scenarios are opt dests or # config file names that are not actually valid, but can be added via a # DeprecatedOpt. The tests only verify that those values do not end up # in the final config object. scenarios = [ ('hyphen', dict(opt_name='foo-bar', opt_dest='foo_bar', broken_opt_dest='foo-bar', cf_name='foo_bar', broken_cf_name='foo-bar', cli_name='foo-bar', hyphen=True)), ('underscore', dict(opt_name='foo_bar', opt_dest='foo_bar', broken_opt_dest='foo-bar', cf_name='foo_bar', broken_cf_name='foo-bar', cli_name='foo_bar', hyphen=False)), ] def test_attribute_and_key_name(self): self.conf.register_opt(cfg.StrOpt(self.opt_name)) self.assertTrue(hasattr(self.conf, self.opt_dest)) self.assertFalse(hasattr(self.conf, self.broken_opt_dest)) self.assertIn(self.opt_dest, self.conf) self.assertNotIn(self.broken_opt_dest, self.conf) def test_cli_opt_name(self): self.conf.register_cli_opt(cfg.BoolOpt(self.opt_name)) self.conf(['--' + self.cli_name]) self.assertTrue(getattr(self.conf, self.opt_dest)) def test_config_file_opt_name(self): self.conf.register_opt(cfg.BoolOpt(self.opt_name)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' + self.cf_name + ' = True\n' + self.broken_cf_name + ' = False\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(getattr(self.conf, self.opt_dest)) def test_deprecated_name(self): self.conf.register_opt(cfg.StrOpt('foobar', deprecated_name=self.opt_name)) self.assertTrue(hasattr(self.conf, 'foobar')) # TODO(mtreinish): Add a check on the log message self.assertTrue(hasattr(self.conf, self.opt_dest)) self.assertFalse(hasattr(self.conf, self.broken_opt_dest)) self.assertIn('foobar', self.conf) self.assertNotIn(self.opt_dest, self.conf) self.assertNotIn(self.broken_opt_dest, self.conf) def test_deprecated_name_alternate_group(self): self.conf.register_opt( cfg.StrOpt('foobar', deprecated_name=self.opt_name, deprecated_group='testing'), group=cfg.OptGroup('testing'), ) self.assertTrue(hasattr(self.conf.testing, 'foobar')) # TODO(mtreinish): Add a check on the log message self.assertTrue(hasattr(self.conf.testing, self.opt_dest)) self.assertFalse(hasattr(self.conf.testing, self.broken_opt_dest)) self.assertIn('foobar', self.conf.testing) self.assertNotIn(self.opt_dest, self.conf.testing) self.assertNotIn(self.broken_opt_dest, self.conf.testing) def test_deprecated_name_cli(self): self.conf.register_cli_opt(cfg.BoolOpt('foobar', deprecated_name=self.opt_name)) self.conf(['--' + self.cli_name]) self.assertTrue(self.conf.foobar) def test_deprecated_name_config_file(self): self.conf.register_opt(cfg.BoolOpt('foobar', deprecated_name=self.opt_name)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' + self.cf_name + ' = True\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(self.conf.foobar) def test_deprecated_opts(self): oldopts = [cfg.DeprecatedOpt(self.opt_name)] self.conf.register_opt(cfg.StrOpt('foobar', deprecated_opts=oldopts)) self.assertTrue(hasattr(self.conf, 'foobar')) # TODO(mtreinish): Add check for the log message self.assertTrue(hasattr(self.conf, self.opt_dest)) self.assertFalse(hasattr(self.conf, self.broken_opt_dest)) self.assertIn('foobar', self.conf) self.assertNotIn(self.opt_dest, self.conf) self.assertNotIn(self.broken_opt_dest, self.conf) def test_deprecated_opts_cli(self): oldopts = [cfg.DeprecatedOpt(self.opt_name)] self.conf.register_cli_opt(cfg.BoolOpt('foobar', deprecated_opts=oldopts)) self.conf(['--' + self.cli_name]) self.assertTrue(self.conf.foobar) def test_deprecated_opts_config_file(self): oldopts = [cfg.DeprecatedOpt(self.opt_name)] self.conf.register_opt(cfg.BoolOpt('foobar', deprecated_opts=oldopts)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' + self.cf_name + ' = True\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(self.conf.foobar) class ReRegisterOptTestCase(BaseTestCase): def test_conf_file_re_register_opt(self): opt = cfg.StrOpt('foo') self.assertTrue(self.conf.register_opt(opt)) self.assertFalse(self.conf.register_opt(opt)) def test_conf_file_re_register_opt_in_group(self): group = cfg.OptGroup('blaa') self.conf.register_group(group) self.conf.register_group(group) # not an error opt = cfg.StrOpt('foo') self.assertTrue(self.conf.register_opt(opt, group=group)) self.assertFalse(self.conf.register_opt(opt, group='blaa')) class RegisterOptNameTestCase(BaseTestCase): def test_register_opt_with_disallow_name(self): for name in cfg.ConfigOpts.disallow_names: opt = cfg.StrOpt(name) self.assertRaises(ValueError, self.conf.register_opt, opt) class TemplateSubstitutionTestCase(BaseTestCase): def _prep_test_str_sub(self, foo_default=None, bar_default=None): self.conf.register_cli_opt(cfg.StrOpt('foo', default=foo_default)) self.conf.register_cli_opt(cfg.StrOpt('bar', default=bar_default)) def _assert_str_sub(self): self.assertTrue(hasattr(self.conf, 'bar')) self.assertEqual('blaa', self.conf.bar) def test_str_sub_default_from_default(self): self._prep_test_str_sub(foo_default='blaa', bar_default='$foo') self.conf([]) self._assert_str_sub() def test_str_sub_default_from_default_recurse(self): self.conf.register_cli_opt(cfg.StrOpt('blaa', default='blaa')) self._prep_test_str_sub(foo_default='$blaa', bar_default='$foo') self.conf([]) self._assert_str_sub() def test_str_sub_default_from_arg(self): self._prep_test_str_sub(bar_default='$foo') self.conf(['--foo', 'blaa']) self._assert_str_sub() def test_str_sub_default_from_config_file(self): self._prep_test_str_sub(bar_default='$foo') paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = blaa\n')]) self.conf(['--config-file', paths[0]]) self._assert_str_sub() def test_str_sub_arg_from_default(self): self._prep_test_str_sub(foo_default='blaa') self.conf(['--bar', '$foo']) self._assert_str_sub() def test_str_sub_arg_from_arg(self): self._prep_test_str_sub() self.conf(['--foo', 'blaa', '--bar', '$foo']) self._assert_str_sub() def test_str_sub_arg_from_config_file(self): self._prep_test_str_sub() paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = blaa\n')]) self.conf(['--config-file', paths[0], '--bar=$foo']) self._assert_str_sub() def test_str_sub_config_file_from_default(self): self._prep_test_str_sub(foo_default='blaa') paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'bar = $foo\n')]) self.conf(['--config-file', paths[0]]) self._assert_str_sub() def test_str_sub_config_file_from_arg(self): self._prep_test_str_sub() paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'bar = $foo\n')]) self.conf(['--config-file', paths[0], '--foo=blaa']) self._assert_str_sub() def test_str_sub_config_file_from_config_file(self): self._prep_test_str_sub() paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'bar = $foo\n' 'foo = blaa\n')]) self.conf(['--config-file', paths[0]]) self._assert_str_sub() def test_str_sub_with_dollar_escape_char(self): self._prep_test_str_sub() paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'bar=foo-somethin$$k2\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'bar')) self.assertEqual('foo-somethin$k2', self.conf.bar) def test_str_sub_with_backslash_escape_char(self): self._prep_test_str_sub() paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'bar=foo-somethin\\$k2\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'bar')) self.assertEqual('foo-somethin$k2', self.conf.bar) def test_str_sub_group_from_default(self): self.conf.register_cli_opt(cfg.StrOpt('foo', default='blaa')) self.conf.register_group(cfg.OptGroup('ba')) self.conf.register_cli_opt(cfg.StrOpt('r', default='$foo'), group='ba') self.conf([]) self.assertTrue(hasattr(self.conf, 'ba')) self.assertTrue(hasattr(self.conf.ba, 'r')) self.assertEqual('blaa', self.conf.ba.r) def test_str_sub_set_default(self): self._prep_test_str_sub() self.conf.set_default('bar', '$foo') self.conf.set_default('foo', 'blaa') self.conf([]) self._assert_str_sub() def test_str_sub_set_override(self): self._prep_test_str_sub() self.conf.set_override('bar', '$foo') self.conf.set_override('foo', 'blaa') self.conf([]) self._assert_str_sub() def _prep_test_str_int_sub(self, foo_default=None, bar_default=None): self.conf.register_cli_opt(cfg.StrOpt('foo', default=foo_default)) self.conf.register_cli_opt(cfg.IntOpt('bar', default=bar_default)) def _assert_int_sub(self): self.assertTrue(hasattr(self.conf, 'bar')) self.assertEqual(123, self.conf.bar) def test_sub_default_from_default(self): self._prep_test_str_int_sub(foo_default='123', bar_default='$foo') self.conf([]) self._assert_int_sub() def test_sub_default_from_default_recurse(self): self.conf.register_cli_opt(cfg.StrOpt('blaa', default='123')) self._prep_test_str_int_sub(foo_default='$blaa', bar_default='$foo') self.conf([]) self._assert_int_sub() def test_sub_default_from_arg(self): self._prep_test_str_int_sub(bar_default='$foo') self.conf(['--foo', '123']) self._assert_int_sub() def test_sub_default_from_config_file(self): self._prep_test_str_int_sub(bar_default='$foo') paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 123\n')]) self.conf(['--config-file', paths[0]]) self._assert_int_sub() def test_sub_arg_from_default(self): self._prep_test_str_int_sub(foo_default='123') self.conf(['--bar', '$foo']) self._assert_int_sub() def test_sub_arg_from_arg(self): self._prep_test_str_int_sub() self.conf(['--foo', '123', '--bar', '$foo']) self._assert_int_sub() def test_sub_arg_from_config_file(self): self._prep_test_str_int_sub() paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 123\n')]) self.conf(['--config-file', paths[0], '--bar=$foo']) self._assert_int_sub() def test_sub_config_file_from_default(self): self._prep_test_str_int_sub(foo_default='123') paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'bar = $foo\n')]) self.conf(['--config-file', paths[0]]) self._assert_int_sub() def test_sub_config_file_from_arg(self): self._prep_test_str_int_sub() paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'bar = $foo\n')]) self.conf(['--config-file', paths[0], '--foo=123']) self._assert_int_sub() def test_sub_config_file_from_config_file(self): self._prep_test_str_int_sub() paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'bar = $foo\n' 'foo = 123\n')]) self.conf(['--config-file', paths[0]]) self._assert_int_sub() def test_sub_group_from_default(self): self.conf.register_cli_opt(cfg.StrOpt('foo', default='123')) self.conf.register_group(cfg.OptGroup('ba')) self.conf.register_cli_opt(cfg.IntOpt('r', default='$foo'), group='ba') self.conf([]) self.assertTrue(hasattr(self.conf, 'ba')) self.assertTrue(hasattr(self.conf.ba, 'r')) self.assertEqual('123', self.conf.foo) self.assertEqual(123, self.conf.ba.r) def test_sub_group_from_default_deprecated(self): self.conf.register_group(cfg.OptGroup('ba')) self.conf.register_cli_opt(cfg.StrOpt( 'foo', default='123', deprecated_group='DEFAULT'), group='ba') self.conf.register_cli_opt(cfg.IntOpt('r', default='$foo'), group='ba') self.conf([]) self.assertTrue(hasattr(self.conf, 'ba')) self.assertTrue(hasattr(self.conf.ba, 'foo')) self.assertEqual('123', self.conf.ba.foo) self.assertTrue(hasattr(self.conf.ba, 'r')) self.assertEqual(123, self.conf.ba.r) def test_sub_group_from_args_deprecated(self): self.conf.register_group(cfg.OptGroup('ba')) self.conf.register_cli_opt(cfg.StrOpt( 'foo', default='123', deprecated_group='DEFAULT'), group='ba') self.conf.register_cli_opt(cfg.IntOpt('r', default='$foo'), group='ba') self.conf(['--ba-foo=4242']) self.assertTrue(hasattr(self.conf, 'ba')) self.assertTrue(hasattr(self.conf.ba, 'foo')) self.assertTrue(hasattr(self.conf.ba, 'r')) self.assertEqual('4242', self.conf.ba.foo) self.assertEqual(4242, self.conf.ba.r) def test_sub_group_from_configfile_deprecated(self): self.conf.register_group(cfg.OptGroup('ba')) self.conf.register_cli_opt(cfg.StrOpt( 'foo', default='123', deprecated_group='DEFAULT'), group='ba') self.conf.register_cli_opt(cfg.IntOpt('r', default='$foo'), group='ba') paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo=4242\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'ba')) self.assertTrue(hasattr(self.conf.ba, 'foo')) self.assertTrue(hasattr(self.conf.ba, 'r')) self.assertEqual('4242', self.conf.ba.foo) self.assertEqual(4242, self.conf.ba.r) def test_dict_sub_default_from_default(self): self.conf.register_cli_opt(cfg.StrOpt('foo', default='floo')) self.conf.register_cli_opt(cfg.StrOpt('bar', default='blaa')) self.conf.register_cli_opt(cfg.DictOpt('dt', default={'$foo': '$bar'})) self.conf([]) self.assertEqual('blaa', self.conf.dt['floo']) def test_dict_sub_default_from_default_multi(self): self.conf.register_cli_opt(cfg.StrOpt('foo', default='floo')) self.conf.register_cli_opt(cfg.StrOpt('bar', default='blaa')) self.conf.register_cli_opt(cfg.StrOpt('goo', default='gloo')) self.conf.register_cli_opt(cfg.StrOpt('har', default='hlaa')) self.conf.register_cli_opt(cfg.DictOpt('dt', default={'$foo': '$bar', '$goo': 'goo', 'har': '$har', 'key1': 'str', 'key2': 12345})) self.conf([]) self.assertEqual('blaa', self.conf.dt['floo']) self.assertEqual('goo', self.conf.dt['gloo']) self.assertEqual('hlaa', self.conf.dt['har']) self.assertEqual('str', self.conf.dt['key1']) self.assertEqual(12345, self.conf.dt['key2']) def test_dict_sub_default_from_default_recurse(self): self.conf.register_cli_opt(cfg.StrOpt('foo', default='$foo2')) self.conf.register_cli_opt(cfg.StrOpt('foo2', default='floo')) self.conf.register_cli_opt(cfg.StrOpt('bar', default='$bar2')) self.conf.register_cli_opt(cfg.StrOpt('bar2', default='blaa')) self.conf.register_cli_opt(cfg.DictOpt('dt', default={'$foo': '$bar'})) self.conf([]) self.assertEqual('blaa', self.conf.dt['floo']) def test_dict_sub_default_from_arg(self): self.conf.register_cli_opt(cfg.StrOpt('foo', default=None)) self.conf.register_cli_opt(cfg.StrOpt('bar', default=None)) self.conf.register_cli_opt(cfg.DictOpt('dt', default={'$foo': '$bar'})) self.conf(['--foo', 'floo', '--bar', 'blaa']) self.assertTrue(hasattr(self.conf, 'dt')) self.assertEqual('blaa', self.conf.dt['floo']) def test_dict_sub_default_from_config_file(self): self.conf.register_cli_opt(cfg.StrOpt('foo', default='floo')) self.conf.register_cli_opt(cfg.StrOpt('bar', default='blaa')) self.conf.register_cli_opt(cfg.DictOpt('dt', default={})) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'dt = $foo:$bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'dt')) self.assertEqual('blaa', self.conf.dt['floo']) class ConfigDirTestCase(BaseTestCase): def test_config_dir(self): snafu_group = cfg.OptGroup('snafu') self.conf.register_group(snafu_group) self.conf.register_cli_opt(cfg.StrOpt('foo')) self.conf.register_cli_opt(cfg.StrOpt('bell'), group=snafu_group) dir = tempfile.mkdtemp() self.tempdirs.append(dir) paths = self.create_tempfiles([(os.path.join(dir, '00-test'), '[DEFAULT]\n' 'foo = bar-00\n' '[snafu]\n' 'bell = whistle-00\n'), (os.path.join(dir, '02-test'), '[snafu]\n' 'bell = whistle-02\n' '[DEFAULT]\n' 'foo = bar-02\n'), (os.path.join(dir, '01-test'), '[DEFAULT]\n' 'foo = bar-01\n')]) self.conf(['--foo', 'bar', '--config-dir', os.path.dirname(paths[0])]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar-02', self.conf.foo) self.assertTrue(hasattr(self.conf, 'snafu')) self.assertTrue(hasattr(self.conf.snafu, 'bell')) self.assertEqual('whistle-02', self.conf.snafu.bell) def test_config_dir_multistr(self): # Demonstrate that values for multistr options found in # different sources are combined. self.conf.register_cli_opt(cfg.MultiStrOpt('foo')) dir = tempfile.mkdtemp() self.tempdirs.append(dir) paths = self.create_tempfiles([(os.path.join(dir, '00-test'), '[DEFAULT]\n' 'foo = bar-00\n'), (os.path.join(dir, '02-test'), '[DEFAULT]\n' 'foo = bar-02\n'), (os.path.join(dir, '01-test'), '[DEFAULT]\n' 'foo = bar-01\n')]) self.conf(['--foo', 'bar', '--config-dir', os.path.dirname(paths[0])]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(['bar', 'bar-00', 'bar-01', 'bar-02'], self.conf.foo) def test_config_dir_file_precedence(self): snafu_group = cfg.OptGroup('snafu') self.conf.register_group(snafu_group) self.conf.register_cli_opt(cfg.StrOpt('foo')) self.conf.register_cli_opt(cfg.StrOpt('bell'), group=snafu_group) dir = tempfile.mkdtemp() self.tempdirs.append(dir) paths = self.create_tempfiles([(os.path.join(dir, '00-test'), '[DEFAULT]\n' 'foo = bar-00\n'), ('01-test', '[snafu]\n' 'bell = whistle-01\n' '[DEFAULT]\n' 'foo = bar-01\n'), ('03-test', '[snafu]\n' 'bell = whistle-03\n' '[DEFAULT]\n' 'foo = bar-03\n'), (os.path.join(dir, '02-test'), '[DEFAULT]\n' 'foo = bar-02\n')]) self.conf(['--foo', 'bar', '--config-file', paths[1], '--config-dir', os.path.dirname(paths[0]), '--config-file', paths[2], ]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar-03', self.conf.foo) self.assertTrue(hasattr(self.conf, 'snafu')) self.assertTrue(hasattr(self.conf.snafu, 'bell')) self.assertEqual('whistle-03', self.conf.snafu.bell) def test_config_dir_default_file_precedence(self): snafu_group = cfg.OptGroup('snafu') self.conf.register_group(snafu_group) self.conf.register_cli_opt(cfg.StrOpt('foo')) self.conf.register_cli_opt(cfg.StrOpt('bell'), group=snafu_group) dir = tempfile.mkdtemp() self.tempdirs.append(dir) paths = self.create_tempfiles([(os.path.join(dir, '00-test'), '[DEFAULT]\n' 'foo = bar-00\n' '[snafu]\n' 'bell = whistle-11\n'), ('01-test', '[snafu]\n' 'bell = whistle-01\n' '[DEFAULT]\n' 'foo = bar-01\n'), ('03-test', '[snafu]\n' 'bell = whistle-03\n' '[DEFAULT]\n' 'foo = bar-03\n'), (os.path.join(dir, '02-test'), '[DEFAULT]\n' 'foo = bar-02\n')]) self.conf(['--foo', 'bar', '--config-dir', os.path.dirname(paths[0])], default_config_files=[paths[1], paths[2]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar-02', self.conf.foo) self.assertTrue(hasattr(self.conf, 'snafu')) self.assertTrue(hasattr(self.conf.snafu, 'bell')) self.assertEqual('whistle-11', self.conf.snafu.bell) def test_config_dir_doesnt_exist(self): tmpdir = tempfile.mkdtemp() os.rmdir(tmpdir) self.assertRaises(cfg.ConfigDirNotFoundError, self.conf, ['--config-dir', tmpdir] ) class ReparseTestCase(BaseTestCase): def test_reparse(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_cli_opt( cfg.StrOpt('foo', default='r'), group='blaa') paths = self.create_tempfiles([('test', '[blaa]\n' 'foo = b\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('b', self.conf.blaa.foo) self.conf(['--blaa-foo', 'a']) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('a', self.conf.blaa.foo) self.conf([]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('r', self.conf.blaa.foo) class OverridesTestCase(BaseTestCase): def test_default_none(self): self.conf.register_opt(cfg.StrOpt('foo', default='foo')) self.conf([]) self.assertEqual('foo', self.conf.foo) self.conf.set_default('foo', None) self.assertIsNone(self.conf.foo) self.conf.clear_default('foo') self.assertEqual('foo', self.conf.foo) def test_no_default_override(self): self.conf.register_opt(cfg.StrOpt('foo')) self.conf([]) self.assertIsNone(self.conf.foo) self.conf.set_default('foo', 'bar') self.assertEqual('bar', self.conf.foo) self.conf.clear_default('foo') self.assertIsNone(self.conf.foo) def test_default_override(self): self.conf.register_opt(cfg.StrOpt('foo', default='foo')) self.conf([]) self.assertEqual('foo', self.conf.foo) self.conf.set_default('foo', 'bar') self.assertEqual('bar', self.conf.foo) self.conf.clear_default('foo') self.assertEqual('foo', self.conf.foo) def test_set_default_not_in_choices(self): self.conf.register_group(cfg.OptGroup('f')) self.conf.register_cli_opt(cfg.StrOpt('oo', choices=('a', 'b')), group='f') self.assertRaises(ValueError, self.conf.set_default, 'oo', 'c', 'f') def test_wrong_type_default_override(self): self.conf.register_opt(cfg.IntOpt('foo', default=1)) self.conf([]) self.assertEqual(1, self.conf.foo) self.assertRaises(ValueError, self.conf.set_default, 'foo', 'not_really_a_int') def test_override(self): self.conf.register_opt(cfg.StrOpt('foo')) self.conf.set_override('foo', 'bar') self.conf([]) self.assertEqual('bar', self.conf.foo) self.conf.clear_override('foo') self.assertIsNone(self.conf.foo) def test_override_none(self): self.conf.register_opt(cfg.StrOpt('foo', default='foo')) self.conf([]) self.assertEqual('foo', self.conf.foo) self.conf.set_override('foo', None) self.assertIsNone(self.conf.foo) self.conf.clear_override('foo') self.assertEqual('foo', self.conf.foo) def test_group_no_default_override(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo'), group='blaa') self.conf([]) self.assertIsNone(self.conf.blaa.foo) self.conf.set_default('foo', 'bar', group='blaa') self.assertEqual('bar', self.conf.blaa.foo) self.conf.clear_default('foo', group='blaa') self.assertIsNone(self.conf.blaa.foo) def test_group_default_override(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo', default='foo'), group='blaa') self.conf([]) self.assertEqual('foo', self.conf.blaa.foo) self.conf.set_default('foo', 'bar', group='blaa') self.assertEqual('bar', self.conf.blaa.foo) self.conf.clear_default('foo', group='blaa') self.assertEqual('foo', self.conf.blaa.foo) def test_group_override(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo'), group='blaa') self.assertIsNone(self.conf.blaa.foo) self.conf.set_override('foo', 'bar', group='blaa') self.conf([]) self.assertEqual('bar', self.conf.blaa.foo) self.conf.clear_override('foo', group='blaa') self.assertIsNone(self.conf.blaa.foo) def test_cli_bool_default(self): self.conf.register_cli_opt(cfg.BoolOpt('foo')) self.conf.set_default('foo', True) self.assertTrue(self.conf.foo) self.conf([]) self.assertTrue(self.conf.foo) self.conf.set_default('foo', False) self.assertFalse(self.conf.foo) self.conf.clear_default('foo') self.assertIsNone(self.conf.foo) def test_cli_bool_override(self): self.conf.register_cli_opt(cfg.BoolOpt('foo')) self.conf.set_override('foo', True) self.assertTrue(self.conf.foo) self.conf([]) self.assertTrue(self.conf.foo) self.conf.set_override('foo', False) self.assertFalse(self.conf.foo) self.conf.clear_override('foo') self.assertIsNone(self.conf.foo) def test__str_override(self): self.conf.register_opt(cfg.StrOpt('foo')) self.conf.set_override('foo', True) self.conf([]) self.assertEqual('True', self.conf.foo) self.conf.clear_override('foo') self.assertIsNone(self.conf.foo) def test__wrong_type_override(self): self.conf.register_opt(cfg.IntOpt('foo')) self.assertRaises(ValueError, self.conf.set_override, 'foo', "not_really_a_int") def test_set_override_in_choices(self): self.conf.register_group(cfg.OptGroup('f')) self.conf.register_cli_opt(cfg.StrOpt('oo', choices=('a', 'b')), group='f') self.conf.set_override('oo', 'b', 'f') self.assertEqual('b', self.conf.f.oo) def test_set_override_not_in_choices(self): self.conf.register_group(cfg.OptGroup('f')) self.conf.register_cli_opt(cfg.StrOpt('oo', choices=('a', 'b')), group='f') self.assertRaises(ValueError, self.conf.set_override, 'oo', 'c', 'f') def test_bool_override(self): self.conf.register_opt(cfg.BoolOpt('foo')) self.conf.set_override('foo', 'True') self.conf([]) self.assertTrue(self.conf.foo) self.conf.clear_override('foo') self.assertIsNone(self.conf.foo) def test_int_override_with_None(self): self.conf.register_opt(cfg.IntOpt('foo')) self.conf.set_override('foo', None) self.conf([]) self.assertIsNone(self.conf.foo) self.conf.clear_override('foo') self.assertIsNone(self.conf.foo) def test_str_override_with_None(self): self.conf.register_opt(cfg.StrOpt('foo')) self.conf.set_override('foo', None) self.conf([]) self.assertIsNone(self.conf.foo) self.conf.clear_override('foo') self.assertIsNone(self.conf.foo) def test_List_override(self): self.conf.register_opt(cfg.ListOpt('foo')) self.conf.set_override('foo', ['aa', 'bb']) self.conf([]) self.assertEqual(['aa', 'bb'], self.conf.foo) self.conf.clear_override('foo') self.assertIsNone(self.conf.foo) class ResetAndClearTestCase(BaseTestCase): def test_clear(self): self.conf.register_cli_opt(cfg.StrOpt('foo')) self.conf.register_cli_opt(cfg.StrOpt('bar'), group='blaa') self.assertIsNone(self.conf.foo) self.assertIsNone(self.conf.blaa.bar) self.conf(['--foo', 'foo', '--blaa-bar', 'bar']) self.assertEqual('foo', self.conf.foo) self.assertEqual('bar', self.conf.blaa.bar) self.conf.clear() self.assertIsNone(self.conf.foo) self.assertIsNone(self.conf.blaa.bar) def test_reset_and_clear_with_defaults_and_overrides(self): self.conf.register_cli_opt(cfg.StrOpt('foo')) self.conf.register_cli_opt(cfg.StrOpt('bar'), group='blaa') self.conf.set_default('foo', 'foo') self.conf.set_override('bar', 'bar', group='blaa') self.conf(['--foo', 'foofoo']) self.assertEqual('foofoo', self.conf.foo) self.assertEqual('bar', self.conf.blaa.bar) self.conf.clear() self.assertEqual('foo', self.conf.foo) self.assertEqual('bar', self.conf.blaa.bar) self.conf.reset() self.assertIsNone(self.conf.foo) self.assertIsNone(self.conf.blaa.bar) class UnregisterOptTestCase(BaseTestCase): def test_unregister_opt(self): opts = [cfg.StrOpt('foo'), cfg.StrOpt('bar')] self.conf.register_opts(opts) self.assertTrue(hasattr(self.conf, 'foo')) self.assertTrue(hasattr(self.conf, 'bar')) self.conf.unregister_opt(opts[0]) self.assertFalse(hasattr(self.conf, 'foo')) self.assertTrue(hasattr(self.conf, 'bar')) self.conf([]) self.assertRaises(cfg.ArgsAlreadyParsedError, self.conf.unregister_opt, opts[1]) self.conf.clear() self.assertTrue(hasattr(self.conf, 'bar')) self.conf.unregister_opts(opts) def test_unregister_opt_from_group(self): opt = cfg.StrOpt('foo') self.conf.register_opt(opt, group='blaa') self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.conf.unregister_opt(opt, group='blaa') self.assertFalse(hasattr(self.conf.blaa, 'foo')) class ImportOptTestCase(BaseTestCase): def test_import_opt(self): self.assertFalse(hasattr(cfg.CONF, 'blaa')) cfg.CONF.import_opt('blaa', 'oslo_config.tests.testmods.blaa_opt') self.assertTrue(hasattr(cfg.CONF, 'blaa')) def test_import_opt_in_group(self): self.assertFalse(hasattr(cfg.CONF, 'bar')) cfg.CONF.import_opt('foo', 'oslo_config.tests.testmods.bar_foo_opt', group='bar') self.assertTrue(hasattr(cfg.CONF, 'bar')) self.assertTrue(hasattr(cfg.CONF.bar, 'foo')) def test_import_opt_import_errror(self): self.assertRaises(ImportError, cfg.CONF.import_opt, 'blaa', 'oslo_config.tests.testmods.blaablaa_opt') def test_import_opt_no_such_opt(self): self.assertRaises(cfg.NoSuchOptError, cfg.CONF.import_opt, 'blaablaa', 'oslo_config.tests.testmods.blaa_opt') def test_import_opt_no_such_group(self): self.assertRaises(cfg.NoSuchGroupError, cfg.CONF.import_opt, 'blaa', 'oslo_config.tests.testmods.blaa_opt', group='blaa') class ImportGroupTestCase(BaseTestCase): def test_import_group(self): self.assertFalse(hasattr(cfg.CONF, 'qux')) cfg.CONF.import_group('qux', 'oslo_config.tests.testmods.baz_qux_opt') self.assertTrue(hasattr(cfg.CONF, 'qux')) self.assertTrue(hasattr(cfg.CONF.qux, 'baz')) def test_import_group_import_error(self): self.assertRaises(ImportError, cfg.CONF.import_group, 'qux', 'oslo_config.tests.testmods.bazzz_quxxx_opt') def test_import_group_no_such_group(self): self.assertRaises(cfg.NoSuchGroupError, cfg.CONF.import_group, 'quxxx', 'oslo_config.tests.testmods.baz_qux_opt') class RequiredOptsTestCase(BaseTestCase): def setUp(self): BaseTestCase.setUp(self) self.conf.register_opt(cfg.StrOpt('boo', required=False)) def test_required_opt(self): self.conf.register_opt(cfg.StrOpt('foo', required=True)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bar')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) def test_required_cli_opt(self): self.conf.register_cli_opt(cfg.StrOpt('foo', required=True)) self.conf(['--foo', 'bar']) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) def test_required_cli_opt_with_dash(self): self.conf.register_cli_opt(cfg.StrOpt('foo-bar', required=True)) self.conf(['--foo-bar', 'baz']) self.assertTrue(hasattr(self.conf, 'foo_bar')) self.assertEqual('baz', self.conf.foo_bar) def test_missing_required_opt(self): self.conf.register_opt(cfg.StrOpt('foo', required=True)) self.assertRaises(cfg.RequiredOptError, self.conf, []) def test_missing_required_cli_opt(self): self.conf.register_cli_opt(cfg.StrOpt('foo', required=True)) self.assertRaises(cfg.RequiredOptError, self.conf, []) def test_required_group_opt(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo', required=True), group='blaa') paths = self.create_tempfiles([('test', '[blaa]\n' 'foo = bar')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_required_cli_group_opt(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_cli_opt( cfg.StrOpt('foo', required=True), group='blaa') self.conf(['--blaa-foo', 'bar']) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_missing_required_group_opt(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo', required=True), group='blaa') self.assertRaises(cfg.RequiredOptError, self.conf, []) def test_missing_required_cli_group_opt(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_cli_opt( cfg.StrOpt('foo', required=True), group='blaa') self.assertRaises(cfg.RequiredOptError, self.conf, []) def test_required_opt_with_default(self): self.conf.register_cli_opt(cfg.StrOpt('foo', required=True)) self.conf.set_default('foo', 'bar') self.conf([]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) def test_required_opt_with_override(self): self.conf.register_cli_opt(cfg.StrOpt('foo', required=True)) self.conf.set_override('foo', 'bar') self.conf([]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) class SadPathTestCase(BaseTestCase): def test_unknown_attr(self): self.conf([]) self.assertFalse(hasattr(self.conf, 'foo')) self.assertRaises(AttributeError, getattr, self.conf, 'foo') self.assertRaises(cfg.NoSuchOptError, self.conf._get, 'foo') self.assertRaises(cfg.NoSuchOptError, self.conf.__getattr__, 'foo') def test_unknown_attr_is_attr_error(self): self.conf([]) self.assertFalse(hasattr(self.conf, 'foo')) self.assertRaises(AttributeError, getattr, self.conf, 'foo') def test_unknown_group_attr(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf([]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertFalse(hasattr(self.conf.blaa, 'foo')) self.assertRaises(cfg.NoSuchOptError, getattr, self.conf.blaa, 'foo') def test_ok_duplicate(self): opt = cfg.StrOpt('foo') self.conf.register_cli_opt(opt) opt2 = cfg.StrOpt('foo') self.conf.register_cli_opt(opt2) self.conf([]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertIsNone(self.conf.foo) def test_error_duplicate(self): self.conf.register_cli_opt(cfg.StrOpt('foo', help='bar')) self.assertRaises(cfg.DuplicateOptError, self.conf.register_cli_opt, cfg.StrOpt('foo')) def test_error_duplicate_with_different_dest(self): self.conf.register_cli_opt(cfg.StrOpt('foo', dest='f')) self.conf.register_cli_opt(cfg.StrOpt('foo')) self.assertRaises(cfg.DuplicateOptError, self.conf, []) def test_error_duplicate_short(self): self.conf.register_cli_opt(cfg.StrOpt('foo', short='f')) self.conf.register_cli_opt(cfg.StrOpt('bar', short='f')) self.assertRaises(cfg.DuplicateOptError, self.conf, []) def test_already_parsed(self): self.conf([]) self.assertRaises(cfg.ArgsAlreadyParsedError, self.conf.register_cli_opt, cfg.StrOpt('foo')) def test_bad_cli_arg(self): self.conf.register_opt(cfg.BoolOpt('foo')) self.useFixture(fixtures.MonkeyPatch('sys.stderr', io.StringIO())) self.assertRaises(SystemExit, self.conf, ['--foo']) self.assertIn('error', sys.stderr.getvalue()) self.assertIn('--foo', sys.stderr.getvalue()) def _do_test_bad_cli_value(self, opt_class): self.conf.register_cli_opt(opt_class('foo')) self.useFixture(fixtures.MonkeyPatch('sys.stderr', io.StringIO())) self.assertRaises(SystemExit, self.conf, ['--foo', 'bar']) self.assertIn('foo', sys.stderr.getvalue()) self.assertIn('bar', sys.stderr.getvalue()) def test_bad_int_arg(self): self._do_test_bad_cli_value(cfg.IntOpt) def test_bad_float_arg(self): self._do_test_bad_cli_value(cfg.FloatOpt) def test_conf_file_not_found(self): (fd, path) = tempfile.mkstemp() os.remove(path) self.assertRaises(cfg.ConfigFilesNotFoundError, self.conf, ['--config-file', path]) @unittest.skipIf(os.getuid() == 0, "Not supported with the root privileges") def test_conf_file_permission_denied(self): (fd, path) = tempfile.mkstemp() os.chmod(path, 0x000) self.assertRaises(cfg.ConfigFilesPermissionDeniedError, self.conf, ['--config-file', path]) os.remove(path) def test_conf_file_broken(self): paths = self.create_tempfiles([('test', 'foo')]) self.assertRaises(cfg.ConfigFileParseError, self.conf, ['--config-file', paths[0]]) def _do_test_conf_file_bad_value(self, opt_class): self.conf.register_opt(opt_class('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(ValueError, getattr, self.conf, 'foo') self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_bad_bool(self): self._do_test_conf_file_bad_value(cfg.BoolOpt) def test_conf_file_bad_int(self): self._do_test_conf_file_bad_value(cfg.IntOpt) def test_conf_file_bad_float(self): self._do_test_conf_file_bad_value(cfg.FloatOpt) def test_str_sub_none_value(self): self.conf.register_cli_opt(cfg.StrOpt('oo')) self.conf.register_cli_opt(cfg.StrOpt('bar', default='$oo')) self.conf.register_cli_opt(cfg.StrOpt('barbar', default='foo $oo foo')) self.conf([]) self.assertTrue(hasattr(self.conf, 'bar')) self.assertEqual('', self.conf.bar) self.assertEqual("foo foo", self.conf.barbar) def test_str_sub_from_group(self): self.conf.register_group(cfg.OptGroup('f')) self.conf.register_cli_opt(cfg.StrOpt('oo', default='blaa'), group='f') self.conf.register_cli_opt(cfg.StrOpt('bar', default='$f.oo')) self.conf([]) self.assertTrue(hasattr(self.conf, 'bar')) self.assertEqual("blaa", self.conf.bar) def test_str_sub_from_group_with_brace(self): self.conf.register_group(cfg.OptGroup('f')) self.conf.register_cli_opt(cfg.StrOpt('oo', default='blaa'), group='f') self.conf.register_cli_opt(cfg.StrOpt('bar', default='${f.oo}')) self.conf([]) self.assertTrue(hasattr(self.conf, 'bar')) self.assertEqual("blaa", self.conf.bar) def test_set_default_unknown_attr(self): self.conf([]) self.assertRaises( cfg.NoSuchOptError, self.conf.set_default, 'foo', 'bar') def test_set_default_unknown_group(self): self.conf([]) self.assertRaises(cfg.NoSuchGroupError, self.conf.set_default, 'foo', 'bar', group='blaa') def test_set_override_unknown_attr(self): self.conf([]) self.assertRaises( cfg.NoSuchOptError, self.conf.set_override, 'foo', 'bar') def test_set_override_unknown_group(self): self.conf([]) self.assertRaises(cfg.NoSuchGroupError, self.conf.set_override, 'foo', 'bar', group='blaa') class FindFileTestCase(BaseTestCase): def test_find_file_without_init(self): self.assertRaises(cfg.NotInitializedError, self.conf.find_file, 'foo.json') def test_find_policy_file(self): policy_file = '/etc/policy.json' self.useFixture(fixtures.MonkeyPatch( 'os.path.exists', lambda p: p == policy_file)) self.conf([]) self.assertIsNone(self.conf.find_file('foo.json')) self.assertEqual(policy_file, self.conf.find_file('policy.json')) def test_find_policy_file_with_config_file(self): dir = tempfile.mkdtemp() self.tempdirs.append(dir) paths = self.create_tempfiles([(os.path.join(dir, 'test.conf'), '[DEFAULT]'), (os.path.join(dir, 'policy.json'), '{}')], ext='') self.conf(['--config-file', paths[0]]) self.assertEqual(paths[1], self.conf.find_file('policy.json')) def test_find_policy_file_with_multiple_config_dirs(self): dir1 = tempfile.mkdtemp() self.tempdirs.append(dir1) dir2 = tempfile.mkdtemp() self.tempdirs.append(dir2) self.conf(['--config-dir', dir1, '--config-dir', dir2]) self.assertEqual(2, len(self.conf.config_dirs)) self.assertEqual(dir1, self.conf.config_dirs[0]) self.assertEqual(dir2, self.conf.config_dirs[1]) def test_config_dirs_empty_list_when_nothing_parsed(self): self.assertEqual([], self.conf.config_dirs) def test_find_policy_file_with_config_dir(self): dir = tempfile.mkdtemp() self.tempdirs.append(dir) dir2 = tempfile.mkdtemp() self.tempdirs.append(dir2) path = self.create_tempfiles([(os.path.join(dir, 'policy.json'), '{}')], ext='')[0] self.conf(['--config-dir', dir, '--config-dir', dir2]) self.assertEqual(path, self.conf.find_file('policy.json')) class OptDumpingTestCase(BaseTestCase): class FakeLogger: def __init__(self, test_case, expected_lvl): self.test_case = test_case self.expected_lvl = expected_lvl self.logged = [] def log(self, lvl, fmt, *args): self.test_case.assertEqual(lvl, self.expected_lvl) self.logged.append(fmt % args) def setUp(self): super(OptDumpingTestCase, self).setUp() self._args = ['--foo', 'this', '--blaa-bar', 'that', '--blaa-key', 'admin', '--passwd', 'hush'] def _do_test_log_opt_values(self, args): self.conf.register_cli_opt(cfg.StrOpt('foo')) self.conf.register_cli_opt(cfg.StrOpt('passwd', secret=True)) self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_cli_opt(cfg.StrOpt('bar'), 'blaa') self.conf.register_cli_opt(cfg.StrOpt('key', secret=True), 'blaa') self.conf(args) logger = self.FakeLogger(self, 666) self.conf.log_opt_values(logger, 666) self.assertEqual([ "*" * 80, "Configuration options gathered from:", "command line args: ['--foo', 'this', '--blaa-bar', " "'that', '--blaa-key', 'admin', '--passwd', 'hush']", "config files: []", "=" * 80, "config_dir = []", "config_file = []", "config_source = []", "foo = this", "passwd = ****", "shell_completion = None", "blaa.bar = that", "blaa.key = ****", "*" * 80, ], logger.logged) def test_log_opt_values(self): self._do_test_log_opt_values(self._args) def test_log_opt_values_from_sys_argv(self): self.useFixture(fixtures.MonkeyPatch('sys.argv', ['foo'] + self._args)) self._do_test_log_opt_values(None) def test_log_opt_values_empty_config(self): empty_conf = cfg.ConfigOpts() logger = self.FakeLogger(self, 666) empty_conf.log_opt_values(logger, 666) self.assertEqual([ "*" * 80, "Configuration options gathered from:", "command line args: None", "config files: []", "=" * 80, "config_source = []", "shell_completion = None", "*" * 80, ], logger.logged) class ConfigParserTestCase(BaseTestCase): def test_parse_file(self): paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bar\n' '[BLAA]\n' 'bar = foo\n')]) sections = {} parser = cfg.ConfigParser(paths[0], sections) parser.parse() self.assertIn('DEFAULT', sections) self.assertIn('BLAA', sections) self.assertEqual(sections['DEFAULT']['foo'], ['bar']) self.assertEqual(sections['BLAA']['bar'], ['foo']) def test_parse_file_with_normalized(self): paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bar\n' '[BLAA]\n' 'bar = foo\n')]) sections = {} normalized = {} parser = cfg.ConfigParser(paths[0], sections) parser._add_normalized(normalized) parser.parse() self.assertIn('DEFAULT', sections) self.assertIn('DEFAULT', normalized) self.assertIn('BLAA', sections) self.assertIn('blaa', normalized) self.assertEqual(sections['DEFAULT']['foo'], ['bar']) self.assertEqual(normalized['DEFAULT']['foo'], ['bar']) self.assertEqual(sections['BLAA']['bar'], ['foo']) self.assertEqual(normalized['blaa']['bar'], ['foo']) def test_no_section(self): with tempfile.NamedTemporaryFile() as tmpfile: tmpfile.write(b'foo = bar') tmpfile.flush() parser = cfg.ConfigParser(tmpfile.name, {}) self.assertRaises(cfg.ParseError, parser.parse) def test__parse_file_ioerror(self): # Test that IOErrors (other than 'No such file or directory') # are propagated. filename = 'fake' namespace = mock.Mock() with mock.patch('oslo_config.cfg.ConfigParser.parse') as parse: parse.side_effect = IOError(errno.EMFILE, filename, 'Too many open files') self.assertRaises(IOError, cfg.ConfigParser._parse_file, filename, namespace) class NamespaceTestCase(BaseTestCase): def setUp(self): super(NamespaceTestCase, self).setUp() self.ns = cfg._Namespace(self.conf) def read(self, *texts): paths = ((str(i), t) for i, t in enumerate(texts)) for path in self.create_tempfiles(paths): cfg.ConfigParser._parse_file(path, self.ns) def assertAbsent(self, key, normalized=False): self.assertRaises(KeyError, self.ns._get_value, [key], normalized=normalized) def assertValue(self, key, expect, multi=False, normalized=False): actual, _ = self.ns._get_value([key], multi=multi, normalized=normalized) self.assertEqual(actual, expect) def test_cli(self): self.conf.register_cli_opt(cfg.StrOpt('foo')) key = (None, 'foo') self.assertAbsent(key) self.read('[DEFAULT]\n' 'foo = file0\n') self.assertValue(key, 'file0') self.read('[DEFAULT]\n' 'foo = file1\n') self.assertEqual('file1', self.ns._get_cli_value([key])) def test_single_file(self): self.read('[DEFAULT]\n' 'foo = bar\n' '[BLAA]\n' 'bar = foo\n') self.assertValue(('DEFAULT', 'foo'), 'bar') self.assertValue(('DEFAULT', 'foo'), ['bar'], multi=True) self.assertValue(('DEFAULT', 'foo'), ['bar'], multi=True) self.assertValue((None, 'foo'), ['bar'], multi=True) self.assertValue(('DEFAULT', 'foo'), ['bar'], multi=True, normalized=True) self.assertValue(('BLAA', 'bar'), 'foo') self.assertValue(('BLAA', 'bar'), ['foo'], multi=True) self.assertValue(('blaa', 'bar'), ['foo'], multi=True, normalized=True) def test_multiple_files(self): self.read('[DEFAULT]\n' 'foo = bar\n' '[BLAA]\n' 'bar = foo', '[DEFAULT]\n' 'foo = barbar\n' '[BLAA]\n' 'bar = foofoo\n' '[bLAa]\n' 'bar = foofoofoo\n') self.assertValue(('DEFAULT', 'foo'), 'barbar') self.assertValue(('DEFAULT', 'foo'), ['bar', 'barbar'], multi=True) self.assertValue(('BLAA', 'bar'), 'foofoo') self.assertValue(('bLAa', 'bar'), 'foofoofoo') self.assertValue(('BLAA', 'bar'), ['foo', 'foofoo'], multi=True) self.assertValue(('Blaa', 'bar'), ['foo', 'foofoo', 'foofoofoo'], multi=True, normalized=True) def test_attrs_subparser(self): CONF = cfg.ConfigOpts() CONF.register_cli_opt(cfg.SubCommandOpt( 'foo', handler=lambda sub: sub.add_parser('foo'))) CONF(['foo']) def test_attrs_subparser_failure(self): CONF = cfg.ConfigOpts() CONF.register_cli_opt(cfg.SubCommandOpt( 'foo', handler=lambda sub: sub.add_parser('foo'))) self.assertRaises(SystemExit, CONF, ['foo', 'bar']) class TildeExpansionTestCase(BaseTestCase): def test_config_file_tilde(self): homedir = os.path.expanduser('~') tmpfile = tempfile.mktemp(dir=homedir, prefix='cfg-', suffix='.conf') tmpbase = os.path.basename(tmpfile) try: self.conf(['--config-file', os.path.join('~', tmpbase)]) except cfg.ConfigFilesNotFoundError as cfnfe: self.assertIn(homedir, str(cfnfe)) self.useFixture(fixtures.MonkeyPatch( 'os.path.exists', lambda p: p == tmpfile)) self.assertEqual(tmpfile, self.conf.find_file(tmpbase)) def test_config_dir_tilde(self): homedir = os.path.expanduser('~') try: tmpdir = tempfile.mkdtemp(dir=homedir, prefix='cfg-', suffix='.d') tmpfile = os.path.join(tmpdir, 'foo.conf') self.useFixture(fixtures.MonkeyPatch( 'glob.glob', lambda p: [tmpfile])) e = self.assertRaises(cfg.ConfigFilesNotFoundError, self.conf, ['--config-dir', os.path.join('~', os.path.basename(tmpdir))] ) self.assertIn(tmpdir, str(e)) finally: try: shutil.rmtree(tmpdir) except OSError as exc: if exc.errno != 2: raise class SubCommandTestCase(BaseTestCase): def test_sub_command(self): def add_parsers(subparsers): sub = subparsers.add_parser('a') sub.add_argument('bar', type=int) self.conf.register_cli_opt( cfg.SubCommandOpt('cmd', handler=add_parsers)) self.assertTrue(hasattr(self.conf, 'cmd')) self.conf(['a', '10']) self.assertTrue(hasattr(self.conf.cmd, 'name')) self.assertTrue(hasattr(self.conf.cmd, 'bar')) self.assertEqual('a', self.conf.cmd.name) self.assertEqual(10, self.conf.cmd.bar) def test_sub_command_with_parent(self): def add_parsers(subparsers): parent = argparse.ArgumentParser(add_help=False) parent.add_argument('bar', type=int) subparsers.add_parser('a', parents=[parent]) self.conf.register_cli_opt( cfg.SubCommandOpt('cmd', handler=add_parsers)) self.assertTrue(hasattr(self.conf, 'cmd')) self.conf(['a', '10']) self.assertTrue(hasattr(self.conf.cmd, 'name')) self.assertTrue(hasattr(self.conf.cmd, 'bar')) self.assertEqual('a', self.conf.cmd.name) self.assertEqual(10, self.conf.cmd.bar) def test_sub_command_with_dest(self): def add_parsers(subparsers): subparsers.add_parser('a') self.conf.register_cli_opt( cfg.SubCommandOpt('cmd', dest='command', handler=add_parsers)) self.assertTrue(hasattr(self.conf, 'command')) self.conf(['a']) self.assertEqual('a', self.conf.command.name) def test_sub_command_with_group(self): def add_parsers(subparsers): sub = subparsers.add_parser('a') sub.add_argument('--bar', choices='XYZ') self.conf.register_cli_opt( cfg.SubCommandOpt('cmd', handler=add_parsers), group='blaa') self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'cmd')) self.conf(['a', '--bar', 'Z']) self.assertTrue(hasattr(self.conf.blaa.cmd, 'name')) self.assertTrue(hasattr(self.conf.blaa.cmd, 'bar')) self.assertEqual('a', self.conf.blaa.cmd.name) self.assertEqual('Z', self.conf.blaa.cmd.bar) def test_sub_command_not_cli(self): self.conf.register_opt(cfg.SubCommandOpt('cmd')) self.conf([]) def test_sub_command_resparse(self): def add_parsers(subparsers): subparsers.add_parser('a') self.conf.register_cli_opt( cfg.SubCommandOpt('cmd', handler=add_parsers)) foo_opt = cfg.StrOpt('foo') self.conf.register_cli_opt(foo_opt) self.conf(['--foo=bar', 'a']) self.assertTrue(hasattr(self.conf.cmd, 'name')) self.assertEqual('a', self.conf.cmd.name) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) self.conf.clear() self.conf.unregister_opt(foo_opt) self.conf(['a']) self.assertTrue(hasattr(self.conf.cmd, 'name')) self.assertEqual('a', self.conf.cmd.name) self.assertFalse(hasattr(self.conf, 'foo')) def test_sub_command_no_handler(self): self.conf.register_cli_opt(cfg.SubCommandOpt('cmd')) self.useFixture(fixtures.MonkeyPatch('sys.stderr', io.StringIO())) self.assertRaises(SystemExit, self.conf, []) self.assertIn('error', sys.stderr.getvalue()) def test_sub_command_with_help(self): def add_parsers(subparsers): subparsers.add_parser('a') self.conf.register_cli_opt(cfg.SubCommandOpt('cmd', title='foo foo', description='bar bar', help='blaa blaa', handler=add_parsers)) self.useFixture(fixtures.MonkeyPatch('sys.stdout', io.StringIO())) self.assertRaises(SystemExit, self.conf, ['--help']) self.assertIn('foo foo', sys.stdout.getvalue()) self.assertIn('bar bar', sys.stdout.getvalue()) self.assertIn('blaa blaa', sys.stdout.getvalue()) def test_sub_command_errors(self): def add_parsers(subparsers): sub = subparsers.add_parser('a') sub.add_argument('--bar') self.conf.register_cli_opt(cfg.BoolOpt('bar')) self.conf.register_cli_opt( cfg.SubCommandOpt('cmd', handler=add_parsers)) self.conf(['a']) self.assertRaises(cfg.DuplicateOptError, getattr, self.conf.cmd, 'bar') self.assertRaises(cfg.NoSuchOptError, getattr, self.conf.cmd, 'foo') def test_sub_command_multiple(self): self.conf.register_cli_opt(cfg.SubCommandOpt('cmd1')) self.conf.register_cli_opt(cfg.SubCommandOpt('cmd2')) self.useFixture(fixtures.MonkeyPatch('sys.stderr', io.StringIO())) self.assertRaises(SystemExit, self.conf, []) self.assertIn('multiple', sys.stderr.getvalue()) class SetDefaultsTestCase(BaseTestCase): def test_default_to_none(self): opts = [cfg.StrOpt('foo', default='foo')] self.conf.register_opts(opts) cfg.set_defaults(opts, foo=None) self.conf([]) self.assertIsNone(self.conf.foo) def test_default_from_none(self): opts = [cfg.StrOpt('foo')] self.conf.register_opts(opts) cfg.set_defaults(opts, foo='bar') self.conf([]) self.assertEqual('bar', self.conf.foo) def test_change_default(self): opts = [cfg.StrOpt('foo', default='foo')] self.conf.register_opts(opts) cfg.set_defaults(opts, foo='bar') self.conf([]) self.assertEqual('bar', self.conf.foo) def test_change_default_many(self): opts = [cfg.StrOpt('foo', default='foo'), cfg.StrOpt('foo2', default='foo2')] self.conf.register_opts(opts) cfg.set_defaults(opts, foo='bar', foo2='bar2') self.conf([]) self.assertEqual('bar', self.conf.foo) self.assertEqual('bar2', self.conf.foo2) def test_group_default_to_none(self): opts = [cfg.StrOpt('foo', default='foo')] self.conf.register_opts(opts, group='blaa') cfg.set_defaults(opts, foo=None) self.conf([]) self.assertIsNone(self.conf.blaa.foo) def test_group_default_from_none(self): opts = [cfg.StrOpt('foo')] self.conf.register_opts(opts, group='blaa') cfg.set_defaults(opts, foo='bar') self.conf([]) self.assertEqual('bar', self.conf.blaa.foo) def test_group_change_default(self): opts = [cfg.StrOpt('foo', default='foo')] self.conf.register_opts(opts, group='blaa') cfg.set_defaults(opts, foo='bar') self.conf([]) self.assertEqual('bar', self.conf.blaa.foo) class DeprecatedOptionsTestCase(BaseTestCase): def test_deprecated_opts_equal(self): d1 = cfg.DeprecatedOpt('oldfoo', group='oldgroup') d2 = cfg.DeprecatedOpt('oldfoo', group='oldgroup') self.assertEqual(d1, d2) def test_deprecated_opts_not_equal(self): d1 = cfg.DeprecatedOpt('oldfoo', group='oldgroup') d2 = cfg.DeprecatedOpt('oldfoo2', group='oldgroup') self.assertNotEqual(d1, d2) class MultipleDeprecatedOptionsTestCase(BaseTestCase): def test_conf_file_override_use_deprecated_name_and_group(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo', deprecated_name='oldfoo', deprecated_group='oldgroup'), group='blaa') paths = self.create_tempfiles([('test', '[oldgroup]\n' 'oldfoo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('bar', self.conf.blaa.foo) def test_conf_file_override_use_deprecated_opts(self): self.conf.register_group(cfg.OptGroup('blaa')) oldopts = [cfg.DeprecatedOpt('oldfoo', group='oldgroup')] self.conf.register_opt(cfg.StrOpt('foo', deprecated_opts=oldopts), group='blaa') paths = self.create_tempfiles([('test', '[oldgroup]\n' 'oldfoo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('bar', self.conf.blaa.foo) def test_conf_file_override_use_deprecated_multi_opts(self): self.conf.register_group(cfg.OptGroup('blaa')) oldopts = [cfg.DeprecatedOpt('oldfoo', group='oldgroup'), cfg.DeprecatedOpt('oldfoo2', group='oldgroup2')] self.conf.register_opt(cfg.StrOpt('foo', deprecated_opts=oldopts), group='blaa') paths = self.create_tempfiles([('test', '[oldgroup2]\n' 'oldfoo2 = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('bar', self.conf.blaa.foo) class MultipleDeprecatedCliOptionsTestCase(BaseTestCase): def test_conf_file_override_use_deprecated_name_and_group(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_cli_opt(cfg.StrOpt('foo', deprecated_name='oldfoo', deprecated_group='oldgroup'), group='blaa') paths = self.create_tempfiles([('test', '[oldgroup]\n' 'oldfoo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('bar', self.conf.blaa.foo) def test_conf_file_override_use_deprecated_opts(self): self.conf.register_group(cfg.OptGroup('blaa')) oldopts = [cfg.DeprecatedOpt('oldfoo', group='oldgroup')] self.conf.register_cli_opt(cfg.StrOpt('foo', deprecated_opts=oldopts), group='blaa') paths = self.create_tempfiles([('test', '[oldgroup]\n' 'oldfoo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('bar', self.conf.blaa.foo) def test_conf_file_override_use_deprecated_multi_opts(self): self.conf.register_group(cfg.OptGroup('blaa')) oldopts = [cfg.DeprecatedOpt('oldfoo', group='oldgroup'), cfg.DeprecatedOpt('oldfoo2', group='oldgroup2')] self.conf.register_cli_opt(cfg.StrOpt('foo', deprecated_opts=oldopts), group='blaa') paths = self.create_tempfiles([('test', '[oldgroup2]\n' 'oldfoo2 = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('bar', self.conf.blaa.foo) def test_conf_file_common_deprecated_group(self): self.conf.register_group(cfg.OptGroup('foo')) self.conf.register_group(cfg.OptGroup('bar')) oldopts = [cfg.DeprecatedOpt('foo', group='DEFAULT')] self.conf.register_opt(cfg.StrOpt('common_opt', deprecated_opts=oldopts), group='bar') self.conf.register_opt(cfg.StrOpt('common_opt', deprecated_opts=oldopts), group='foo') paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bla\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('bla', self.conf.foo.common_opt) self.assertEqual('bla', self.conf.bar.common_opt) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bla\n' '[bar]\n' 'common_opt = blabla\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('bla', self.conf.foo.common_opt) self.assertEqual('blabla', self.conf.bar.common_opt) paths = self.create_tempfiles([('test', '[foo]\n' 'common_opt = bla\n' '[bar]\n' 'common_opt = blabla\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('bla', self.conf.foo.common_opt) self.assertEqual('blabla', self.conf.bar.common_opt) class ChoicesTestCase(BaseTestCase): def test_choice_default(self): self.conf.register_cli_opt(cfg.StrOpt('protocol', default='http', choices=['http', 'https', 'ftp'])) self.conf([]) self.assertEqual('http', self.conf.protocol) def test_choice_good(self): self.conf.register_cli_opt(cfg.StrOpt('foo', choices=['bar1', 'bar2'])) self.conf(['--foo', 'bar1']) self.assertEqual('bar1', self.conf.foo) def test_choice_bad(self): self.conf.register_cli_opt(cfg.StrOpt('foo', choices=['bar1', 'bar2'])) self.assertRaises(SystemExit, self.conf, ['--foo', 'bar3']) def test_conf_file_choice_value(self): self.conf.register_opt(cfg.StrOpt('foo', choices=['bar1', 'bar2'])) paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = bar1\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar1', self.conf.foo) def test_conf_file_choice_empty_value(self): self.conf.register_opt(cfg.StrOpt('foo', choices=['', 'bar1', 'bar2'])) paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = \n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('', self.conf.foo) def test_conf_file_choice_none_value(self): self.conf.register_opt(cfg.StrOpt('foo', default=None, choices=[None, 'bar1', 'bar2'])) paths = self.create_tempfiles([('test', '[DEFAULT]\n''\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertIsNone(self.conf.foo) def test_conf_file_bad_choice_value(self): self.conf.register_opt(cfg.StrOpt('foo', choices=['bar1', 'bar2'])) paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = bar3\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') self.assertRaises(ValueError, getattr, self.conf, 'foo') def test_conf_file_choice_value_override(self): self.conf.register_cli_opt(cfg.StrOpt('foo', choices=['baar', 'baaar'])) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = baar\n'), ('2', '[DEFAULT]\n' 'foo = baaar\n')]) self.conf(['--foo', 'baar', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('baaar', self.conf.foo) def test_conf_file_choice_bad_default(self): self.assertRaises(cfg.DefaultValueError, cfg.StrOpt, 'foo', choices=['baar', 'baaar'], default='foobaz') class PortChoicesTestCase(BaseTestCase): def test_choice_default(self): self.conf.register_cli_opt(cfg.PortOpt('port', default=455, choices=[80, 455])) self.conf([]) self.assertEqual(455, self.conf.port) def test_choice_good_with_list(self): self.conf.register_cli_opt(cfg.PortOpt('port', choices=[80, 8080])) self.conf(['--port', '80']) self.assertEqual(80, self.conf.port) def test_choice_good_with_tuple(self): self.conf.register_cli_opt(cfg.PortOpt('port', choices=(80, 8080))) self.conf(['--port', '80']) self.assertEqual(80, self.conf.port) def test_choice_bad(self): self.conf.register_cli_opt(cfg.PortOpt('port', choices=[80, 8080])) self.assertRaises(SystemExit, self.conf, ['--port', '8181']) def test_choice_out_range(self): self.assertRaisesRegex(ValueError, 'out of bounds', cfg.PortOpt, 'port', choices=[80, 65537, 0]) def test_conf_file_choice_value(self): self.conf.register_opt(cfg.PortOpt('port', choices=[80, 8080])) paths = self.create_tempfiles([('test', '[DEFAULT]\n''port = 80\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'port')) self.assertEqual(80, self.conf.port) def test_conf_file_bad_choice_value(self): self.conf.register_opt(cfg.PortOpt('port', choices=[80, 8080])) paths = self.create_tempfiles([('test', '[DEFAULT]\n''port = 8181\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'port') self.assertRaises(ValueError, getattr, self.conf, 'port') def test_conf_file_choice_value_override(self): self.conf.register_cli_opt(cfg.PortOpt('port', choices=[80, 8080])) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'port = 80\n'), ('2', '[DEFAULT]\n' 'port = 8080\n')]) self.conf(['--port', '80', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'port')) self.assertEqual(8080, self.conf.port) def test_conf_file_choice_bad_default(self): self.assertRaises(cfg.DefaultValueError, cfg.PortOpt, 'port', choices=[80, 8080], default=8181) class RegexTestCase(BaseTestCase): def test_regex_good(self): self.conf.register_cli_opt(cfg.StrOpt('foo', regex='foo|bar')) self.conf(['--foo', 'bar']) self.assertEqual('bar', self.conf.foo) self.conf(['--foo', 'foo']) self.assertEqual('foo', self.conf.foo) self.conf(['--foo', 'foobar']) self.assertEqual('foobar', self.conf.foo) def test_regex_bad(self): self.conf.register_cli_opt(cfg.StrOpt('foo', regex='bar')) self.assertRaises(SystemExit, self.conf, ['--foo', 'foo']) def test_conf_file_regex_value(self): self.conf.register_opt(cfg.StrOpt('foo', regex='bar')) paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) def test_conf_file_regex_bad_value(self): self.conf.register_opt(cfg.StrOpt('foo', regex='bar')) paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = other\n')]) self.conf(['--config-file', paths[0]]) self.assertRaisesRegex(cfg.ConfigFileValueError, "doesn't match regex", self.conf._get, 'foo') self.assertRaisesRegex(ValueError, "doesn't match regex", getattr, self.conf, 'foo') def test_regex_with_choice(self): self.assertRaises(ValueError, cfg.StrOpt, 'foo', choices=['bar1'], regex='bar2') class QuotesTestCase(BaseTestCase): def test_quotes_good(self): self.conf.register_cli_opt(cfg.StrOpt('foo', quotes=True)) self.conf(['--foo', '"foobar1"']) self.assertEqual('foobar1', self.conf.foo) self.conf(['--foo', "'foobar2'"]) self.assertEqual('foobar2', self.conf.foo) self.conf(['--foo', 'foobar3']) self.assertEqual('foobar3', self.conf.foo) self.conf(['--foo', 'foobar4"']) self.assertEqual('foobar4"', self.conf.foo) def test_quotes_bad(self): self.conf.register_cli_opt(cfg.StrOpt('foo', quotes=True)) self.assertRaises(SystemExit, self.conf, ['--foo', '"foobar\'']) self.assertRaises(SystemExit, self.conf, ['--foo', '\'foobar"']) self.assertRaises(SystemExit, self.conf, ['--foo', '"foobar']) self.assertRaises(SystemExit, self.conf, ['--foo', "'foobar"]) def test_conf_file_quotes_good_value(self): self.conf.register_opt(cfg.StrOpt('foo', quotes=True)) paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = "bar"\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) def test_conf_file_quotes_bad_value(self): self.conf.register_opt(cfg.StrOpt('foo', quotes=True)) paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = "bar\n')]) self.conf(['--config-file', paths[0]]) self.assertRaisesRegex(cfg.ConfigFileValueError, 'Non-closed quote:', self.conf._get, 'foo') self.assertRaisesRegex(ValueError, 'Non-closed quote:', getattr, self.conf, 'foo') class IgnoreCaseTestCase(BaseTestCase): def test_ignore_case_with_choices(self): self.conf.register_cli_opt(cfg.StrOpt('foo', ignore_case=True, choices=['bar1', 'bar2', 'BAR3'])) self.conf(['--foo', 'bAr1']) self.assertEqual('bAr1', self.conf.foo) self.conf(['--foo', 'BaR2']) self.assertEqual('BaR2', self.conf.foo) self.conf(['--foo', 'baR3']) self.assertEqual('baR3', self.conf.foo) def test_ignore_case_with_regex(self): self.conf.register_cli_opt(cfg.StrOpt('foo', ignore_case=True, regex='fOO|bar')) self.conf(['--foo', 'foo']) self.assertEqual('foo', self.conf.foo) self.conf(['--foo', 'Bar']) self.assertEqual('Bar', self.conf.foo) self.conf(['--foo', 'FOObar']) self.assertEqual('FOObar', self.conf.foo) def test_conf_file_ignore_case_with_choices(self): self.conf.register_opt(cfg.StrOpt('foo', ignore_case=True, choices=['bar1', 'bar2', 'BAR3'])) paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = bAr2\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bAr2', self.conf.foo) def test_conf_file_ignore_case_with_regex(self): self.conf.register_opt(cfg.StrOpt('foo', ignore_case=True, regex='bAr')) paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = BaR\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('BaR', self.conf.foo) class StrOptMaxLengthTestCase(BaseTestCase): def test_stropt_max_length_good(self): self.conf.register_cli_opt(cfg.StrOpt('foo', max_length=5)) self.conf(['--foo', '12345']) self.assertEqual('12345', self.conf.foo) def test_stropt_max_length_bad(self): self.conf.register_cli_opt(cfg.StrOpt('foo', max_length=5)) self.assertRaises(SystemExit, self.conf, ['--foo', '123456']) class URIOptMaxLengthTestCase(BaseTestCase): def test_uriopt_max_length_good(self): self.conf.register_cli_opt(cfg.URIOpt('foo', max_length=30)) self.conf(['--foo', 'http://www.example.com']) self.assertEqual('http://www.example.com', self.conf.foo) def test_uriopt_max_length_bad(self): self.conf.register_cli_opt(cfg.URIOpt('foo', max_length=30)) self.assertRaises(SystemExit, self.conf, ['--foo', 'http://www.example.com/versions']) class URIOptSchemesTestCase(BaseTestCase): def test_uriopt_schemes_good(self): self.conf.register_cli_opt(cfg.URIOpt('foo', schemes=['http', 'ftp'])) self.conf(['--foo', 'http://www.example.com']) self.assertEqual('http://www.example.com', self.conf.foo) self.conf(['--foo', 'ftp://example.com/archives']) self.assertEqual('ftp://example.com/archives', self.conf.foo) def test_uriopt_schemes_bad(self): self.conf.register_cli_opt(cfg.URIOpt('foo', schemes=['http', 'ftp'])) self.assertRaises(SystemExit, self.conf, ['--foo', 'https://www.example.com']) self.assertRaises(SystemExit, self.conf, ['--foo', 'file://www.example.com']) class PrintHelpTestCase(base.BaseTestCase): def test_print_help_without_init(self): conf = cfg.ConfigOpts() conf.register_opts([]) self.assertRaises(cfg.NotInitializedError, conf.print_help) def test_print_help_with_clear(self): conf = cfg.ConfigOpts() conf.register_opts([]) conf([]) conf.clear() self.assertRaises(cfg.NotInitializedError, conf.print_help) class OptTestCase(base.BaseTestCase): def test_opt_eq(self): d1 = cfg.ListOpt('oldfoo') d2 = cfg.ListOpt('oldfoo') self.assertEqual(d1, d2) def test_opt_not_eq(self): d1 = cfg.ListOpt('oldfoo') d2 = cfg.ListOpt('oldbar') self.assertNotEqual(d1, d2) def test_illegal_name(self): self.assertRaises(ValueError, cfg.BoolOpt, '_foo') class SectionsTestCase(BaseTestCase): def test_list_all_sections(self): paths = self.create_tempfiles([('test.ini', '[DEFAULT]\n' 'foo = bar\n' '[BLAA]\n' 'bar = foo\n'), ('test2.ini', '[DEFAULT]\n' 'foo = bar\n' '[BLAA]\n' 'bar = foo\n')]) self.conf(args=[], default_config_files=paths) self.assertEqual(['BLAA', 'DEFAULT'], self.conf.list_all_sections()) def test_list_all_sections_post_mutate(self): paths = self.create_tempfiles([('test.ini', '[DEFAULT]\n' 'foo = bar\n' '[BLAA]\n' 'bar = foo\n'), ('test2.ini', '[WOMBAT]\n' 'woo = war\n' '[BLAA]\n' 'bar = foo\n')]) self.conf(args=[], default_config_files=paths[:1]) self.assertEqual(['BLAA', 'DEFAULT'], self.conf.list_all_sections()) shutil.copy(paths[1], paths[0]) self.conf.mutate_config_files() self.assertEqual(['BLAA', 'DEFAULT', 'WOMBAT'], self.conf.list_all_sections()) class DeprecationWarningTestBase(BaseTestCase): def setUp(self): super(DeprecationWarningTestBase, self).setUp() self.log_fixture = self.useFixture(fixtures.FakeLogger()) self._parser_class = cfg.ConfigParser @mock.patch('oslo_log.versionutils.report_deprecated_feature', _fake_deprecated_feature) class DeprecationWarningTestScenarios(DeprecationWarningTestBase): scenarios = [('default-deprecated', dict(deprecated=True, group='DEFAULT')), ('default-not-deprecated', dict(deprecated=False, group='DEFAULT')), ('other-deprecated', dict(deprecated=True, group='other')), ('other-not-deprecated', dict(deprecated=False, group='other')), ] def test_deprecated_logging(self): self.conf.register_opt(cfg.StrOpt('foo', deprecated_name='bar')) self.conf.register_group(cfg.OptGroup('other')) self.conf.register_opt(cfg.StrOpt('foo', deprecated_name='bar'), group='other') if self.deprecated: content = 'bar=baz' else: content = 'foo=baz' paths = self.create_tempfiles([('test', '[' + self.group + ']\n' + content + '\n')]) self.conf(['--config-file', paths[0]]) # Reference these twice to verify they only get logged once if self.group == 'DEFAULT': self.assertEqual('baz', self.conf.foo) self.assertEqual('baz', self.conf.foo) else: self.assertEqual('baz', self.conf.other.foo) self.assertEqual('baz', self.conf.other.foo) if self.deprecated: expected = ('Deprecated: ' + cfg._Namespace._deprecated_opt_message % {'dep_option': 'bar', 'dep_group': self.group, 'option': 'foo', 'group': self.group} + '\n') else: expected = '' self.assertEqual(expected, self.log_fixture.output) @mock.patch('oslo_log.versionutils.report_deprecated_feature', _fake_deprecated_feature) class DeprecationWarningTests(DeprecationWarningTestBase): log_prefix = 'Deprecated: ' def test_DeprecatedOpt(self): default_deprecated = [cfg.DeprecatedOpt('bar')] other_deprecated = [cfg.DeprecatedOpt('baz', group='other')] self.conf.register_group(cfg.OptGroup('other')) self.conf.register_opt(cfg.StrOpt('foo', deprecated_opts=default_deprecated)) self.conf.register_opt(cfg.StrOpt('foo', deprecated_opts=other_deprecated), group='other') paths = self.create_tempfiles([('test', '[DEFAULT]\n' + 'bar=baz\n' + '[other]\n' + 'baz=baz\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('baz', self.conf.foo) self.assertEqual('baz', self.conf.other.foo) self.assertIn('Option "bar" from group "DEFAULT"', self.log_fixture.output) self.assertIn('Option "baz" from group "other"', self.log_fixture.output) def test_check_deprecated(self): namespace = cfg._Namespace(None) deprecated_list = [('DEFAULT', 'bar')] namespace._check_deprecated(('DEFAULT', 'bar'), (None, 'foo'), deprecated_list) self.assert_message_logged('bar', 'DEFAULT', 'foo', 'DEFAULT') def assert_message_logged(self, deprecated_name, deprecated_group, current_name, current_group): expected = (cfg._Namespace._deprecated_opt_message % {'dep_option': deprecated_name, 'dep_group': deprecated_group, 'option': current_name, 'group': current_group} ) self.assertIn(expected + '\n', self.log_fixture.output) def test_deprecated_for_removal(self): self.conf.register_opt(cfg.StrOpt('foo', deprecated_for_removal=True)) self.conf.register_opt(cfg.StrOpt('bar', deprecated_for_removal=True)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' + 'foo=bar\n')]) self.conf(['--config-file', paths[0]]) # Multiple references should be logged only once. self.assertEqual('bar', self.conf.foo) self.assertEqual('bar', self.conf.foo) # Options not set in the config should not be logged. self.assertIsNone(self.conf.bar) expected = ('Option "foo" from group "DEFAULT" is ' 'deprecated for removal. Its value may be silently ' 'ignored in the future.\n') self.assertIn(expected, self.log_fixture.output) def test_deprecated_for_removal_with_group(self): self.conf.register_group(cfg.OptGroup('other')) self.conf.register_opt(cfg.StrOpt('foo', deprecated_for_removal=True), group='other') self.conf.register_opt(cfg.StrOpt('bar', deprecated_for_removal=True), group='other') paths = self.create_tempfiles([('test', '[other]\n' + 'foo=bar\n')]) self.conf(['--config-file', paths[0]]) # Multiple references should be logged only once. self.assertEqual('bar', self.conf.other.foo) self.assertEqual('bar', self.conf.other.foo) # Options not set in the config should not be logged. self.assertIsNone(self.conf.other.bar) expected = ('Option "foo" from group "other" is ' 'deprecated for removal. Its value may be silently ' 'ignored in the future.\n') self.assertIn(expected, self.log_fixture.output) def test_deprecated_with_dest(self): self.conf.register_group(cfg.OptGroup('other')) self.conf.register_opt(cfg.StrOpt('foo-bar', deprecated_name='bar', dest='foo'), group='other') content = 'bar=baz' paths = self.create_tempfiles([('test', '[other]\n' + content + '\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('baz', self.conf.other.foo) expected = (cfg._Namespace._deprecated_opt_message % {'dep_option': 'bar', 'dep_group': 'other', 'option': 'foo-bar', 'group': 'other'} + '\n') self.assertIn(expected, self.log_fixture.output) class DeprecationWarningTestsNoOsloLog(DeprecationWarningTests): log_prefix = '' def setUp(self): super(DeprecationWarningTestsNoOsloLog, self).setUp() # NOTE(bnemec): For some reason if I apply this as a class decorator # it ends up applying to the parent class too and breaks those tests. self.useFixture(fixtures.MockPatchObject(cfg, 'oslo_log', None)) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/tests/test_fixture.conf0000664000175000017500000000010600000000000023025 0ustar00zuulzuul00000000000000[DEFAULT] first_test_opt=loaded_value_1 second_test_opt=loaded_value_2././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/tests/test_fixture.py0000664000175000017500000001716700000000000022547 0ustar00zuulzuul00000000000000# # 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_wrong_type(self): f = self._make_fixture() self.assertEqual(5, f.conf.get('test2')) self.assertEqual('a', f.conf.get('test3')) # enforce type will always be true now self.assertRaises(ValueError, f.config, test2=-1) self.assertRaises(ValueError, f.config, test3='c') 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) self.assertNotIn('default_config_dirs', f.conf) config_files = ['./test_fixture.conf'] config_dirs = ['./test_fixture.conf.d'] f.set_config_files(config_files) f.set_config_dirs(config_dirs) self.assertEqual(f.conf.default_config_files, config_files) self.assertEqual(f.conf.default_config_dirs, config_dirs) f.cleanUp() self.assertNotIn('default_config_files', f.conf) self.assertNotIn('default_config_dirs', 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) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/tests/test_generator.py0000664000175000017500000017470600000000000023052 0ustar00zuulzuul00000000000000# 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. import io import sys import textwrap from unittest import mock import fixtures from oslotest import base import tempfile import testscenarios from oslo_config import cfg from oslo_config import fixture as config_fixture from oslo_config import generator from oslo_config import types import yaml load_tests = testscenarios.load_tests_apply_scenarios def custom_type(a): """Something that acts like a type, but isn't known""" return a def build_formatter(output_file, **kwargs): conf = cfg.ConfigOpts() conf.register_opts(generator._generator_opts) for k, v in kwargs.items(): conf.set_override(k, v) return generator._OptFormatter(conf, output_file=output_file) class GeneratorTestCase(base.BaseTestCase): groups = { 'group1': cfg.OptGroup(name='group1', help='Lorem ipsum dolor sit amet, consectetur ' 'adipisicing elit, sed do eiusmod tempor ' 'incididunt ut labore et dolore magna ' 'aliqua. Ut enim ad minim veniam, quis ' 'nostrud exercitation ullamco laboris ' 'nisi ut aliquip ex ea commodo ' 'consequat. Duis aute irure dolor in.'), 'group2': cfg.OptGroup(name='group2', title='Group 2'), 'foo': cfg.OptGroup(name='foo', title='Foo Title', help='foo help'), } opts = { 'foo': cfg.StrOpt('foo', help='foo option'), 'bar': cfg.StrOpt('bar', help='bar option'), 'foo-bar': cfg.StrOpt('foo-bar', help='foobar'), 'no_help': cfg.StrOpt('no_help'), 'long_help': cfg.StrOpt('long_help', help='Lorem ipsum dolor sit amet, consectetur ' 'adipisicing elit, sed do eiusmod tempor ' 'incididunt ut labore et dolore magna ' 'aliqua. Ut enim ad minim veniam, quis ' 'nostrud exercitation ullamco laboris ' 'nisi ut aliquip ex ea commodo ' 'consequat. Duis aute irure dolor in ' 'reprehenderit in voluptate velit esse ' 'cillum dolore eu fugiat nulla ' 'pariatur. Excepteur sint occaecat ' 'cupidatat non proident, sunt in culpa ' 'qui officia deserunt mollit anim id est ' 'laborum.'), 'long_help_pre': cfg.StrOpt('long_help_pre', help='This is a very long help text which ' 'is preformatted with line breaks. ' 'It should break when it is too long ' 'but also keep the specified line ' 'breaks. This makes it possible to ' 'create lists with items:\n' '\n' '* item 1\n' '* item 2\n' '\n' 'and should increase the ' 'readability.'), 'choices_opt': cfg.StrOpt('choices_opt', default='a', choices=(None, '', 'a', 'b', 'c'), help='a string with choices'), 'deprecated_opt_without_deprecated_group': cfg.StrOpt( 'bar', deprecated_name='foobar', help='deprecated'), 'deprecated_for_removal_opt': cfg.StrOpt( 'bar', deprecated_for_removal=True, help='deprecated for removal'), 'deprecated_reason_opt': cfg.BoolOpt( 'turn_off_stove', default=False, deprecated_for_removal=True, deprecated_reason='This was supposed to work but it really, ' 'really did not. Always buy house insurance.', help='DEPRECATED: Turn off stove'), 'deprecated_opt_with_deprecated_since': cfg.BoolOpt( 'tune_in', deprecated_for_removal=True, deprecated_since='13.0'), 'deprecated_opt_with_deprecated_group': cfg.StrOpt( 'bar', deprecated_name='foobar', deprecated_group='group1', help='deprecated'), 'opt_with_DeprecatedOpt': cfg.BoolOpt( 'foo-bar', help='Opt with DeprecatedOpt', deprecated_opts=[cfg.DeprecatedOpt('foo-bar', group='deprecated')]), # Unknown Opt default must be a string 'unknown_type': cfg.Opt('unknown_opt', default='123', help='unknown', type=types.String(type_name='unknown type')), 'str_opt': cfg.StrOpt('str_opt', default='foo bar', help='a string'), 'str_opt_sample_default': cfg.StrOpt('str_opt', default='fooishbar', help='a string'), 'str_opt_with_space': cfg.StrOpt('str_opt', default=' foo bar ', help='a string with spaces'), 'str_opt_multiline': cfg.StrOpt('str_opt', default='foo\nbar\nbaz', help='a string with newlines'), 'bool_opt': cfg.BoolOpt('bool_opt', default=False, help='a boolean'), 'int_opt': cfg.IntOpt('int_opt', default=10, min=1, max=20, help='an integer'), 'int_opt_min_0': cfg.IntOpt('int_opt_min_0', default=10, min=0, max=20, help='an integer'), 'int_opt_max_0': cfg.IntOpt('int_opt_max_0', default=-1, max=0, help='an integer'), 'float_opt': cfg.FloatOpt('float_opt', default=0.1, help='a float'), 'list_opt': cfg.ListOpt('list_opt', default=['1', '2', '3'], help='a list'), 'list_opt_with_bounds': cfg.ListOpt('list_opt_with_bounds', default=['1', '2', '3'], help='a list', bounds=True), 'list_opt_single': cfg.ListOpt('list_opt_single', default='1', help='a list'), 'list_int_opt': cfg.ListOpt('list_int_opt', default=[1, 2, 3], help='a list'), 'dict_opt': cfg.DictOpt('dict_opt', default={'1': 'yes', '2': 'no'}, help='a dict'), 'ip_opt': cfg.IPOpt('ip_opt', default='127.0.0.1', help='an ip address'), 'port_opt': cfg.PortOpt('port_opt', default=80, help='a port'), 'hostname_opt': cfg.HostnameOpt('hostname_opt', default='compute01.nova.site1', help='a hostname'), 'uri_opt': cfg.URIOpt('uri_opt', default='http://example.com', help='a URI'), 'multi_opt': cfg.MultiStrOpt('multi_opt', default=['1', '2', '3'], help='multiple strings'), 'multi_opt_none': cfg.MultiStrOpt('multi_opt_none', help='multiple strings'), 'multi_opt_empty': cfg.MultiStrOpt('multi_opt_empty', default=[], help='multiple strings'), 'multi_opt_sample_default': cfg.MultiStrOpt('multi_opt', default=['1', '2', '3'], sample_default=['5', '6'], help='multiple strings'), 'string_type_with_bad_default': cfg.Opt('string_type_with_bad_default', help='string with bad default', default=4096), 'native_str_type': cfg.Opt('native_str_type', help='native help', type=str), 'native_int_type': cfg.Opt('native_int_type', help='native help', type=int), 'native_float_type': cfg.Opt('native_float_type', help='native help', type=float), 'custom_type': cfg.Opt('custom_type', help='custom help', type=custom_type), 'custom_type_name': cfg.Opt('custom_opt_type', type=types.Integer(type_name='port' ' number'), default=5511, help='this is a port'), } content_scenarios = [ ('empty', dict(opts=[], expected='''[DEFAULT] ''')), ('single_namespace', dict(opts=[('test', [(None, [opts['foo']])])], expected='''[DEFAULT] # # From test # # foo option (string value) #foo = ''')), ('multiple_namespaces', dict(opts=[('test', [(None, [opts['foo']])]), ('other', [(None, [opts['bar']])])], expected='''[DEFAULT] # # From other # # bar option (string value) #bar = # # From test # # foo option (string value) #foo = ''')), ('group', dict(opts=[('test', [(groups['group1'], [opts['foo']])])], expected='''[DEFAULT] [group1] # Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do # eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim # ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut # aliquip ex ea commodo consequat. Duis aute irure dolor in. # # From test # # foo option (string value) #foo = ''')), ('empty_group', dict(opts=[('test', [(groups['group1'], [])])], expected='''[DEFAULT] ''')), ('multiple_groups', dict(opts=[('test', [(groups['group1'], [opts['foo']]), (groups['group2'], [opts['bar']])])], expected='''[DEFAULT] [group1] # Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do # eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim # ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut # aliquip ex ea commodo consequat. Duis aute irure dolor in. # # From test # # foo option (string value) #foo = [group2] # # From test # # bar option (string value) #bar = ''')), ('group_in_multiple_namespaces', dict(opts=[('test', [(groups['group1'], [opts['foo']])]), ('other', [(groups['group1'], [opts['bar']])])], expected='''[DEFAULT] [group1] # Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do # eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim # ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut # aliquip ex ea commodo consequat. Duis aute irure dolor in. # # From other # # bar option (string value) #bar = # # From test # # foo option (string value) #foo = ''')), ('hyphenated_name', dict(opts=[('test', [(None, [opts['foo-bar']])])], expected='''[DEFAULT] # # From test # # foobar (string value) #foo_bar = ''')), ('no_help', dict(opts=[('test', [(None, [opts['no_help']])])], log_warning=('"%s" is missing a help string', 'no_help'), expected='''[DEFAULT] # # From test # # (string value) #no_help = ''')), ('long_help', dict(opts=[('test', [(None, [opts['long_help']])])], expected='''[DEFAULT] # # From test # # Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do # eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim # ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut # aliquip ex ea commodo consequat. Duis aute irure dolor in # reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla # pariatur. Excepteur sint occaecat cupidatat non proident, sunt in # culpa qui officia deserunt mollit anim id est laborum. (string # value) #long_help = ''')), ('long_help_wrap_at_40', dict(opts=[('test', [(None, [opts['long_help']])])], wrap_width=40, expected='''[DEFAULT] # # From test # # Lorem ipsum dolor sit amet, # consectetur adipisicing elit, sed do # eiusmod tempor incididunt ut labore et # dolore magna aliqua. Ut enim ad minim # veniam, quis nostrud exercitation # ullamco laboris nisi ut aliquip ex ea # commodo consequat. Duis aute irure # dolor in reprehenderit in voluptate # velit esse cillum dolore eu fugiat # nulla pariatur. Excepteur sint # occaecat cupidatat non proident, sunt # in culpa qui officia deserunt mollit # anim id est laborum. (string value) #long_help = ''')), ('long_help_no_wrapping', dict(opts=[('test', [(None, [opts['long_help']])])], wrap_width=0, expected='''[DEFAULT] # # From test # ''' # noqa '# Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod ' # noqa 'tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, ' # noqa 'quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo ' # noqa 'consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse ' # noqa 'cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat ' # noqa 'non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ' # noqa '(string value)' # noqa ''' #long_help = ''')), # noqa ('long_help_with_preformatting', dict(opts=[('test', [(None, [opts['long_help_pre']])])], wrap_width=70, expected='''[DEFAULT] # # From test # # This is a very long help text which is preformatted with line # breaks. It should break when it is too long but also keep the # specified line breaks. This makes it possible to create lists with # items: # # * item 1 # * item 2 # # and should increase the readability. (string value) #long_help_pre = ''')), ('choices_opt', dict(opts=[('test', [(None, [opts['choices_opt']])])], expected="""[DEFAULT] # # From test # # a string with choices (string value) # Possible values: # - # '' - # a - # b - # c - #choices_opt = a """)), ('deprecated opt without deprecated group', dict(opts=[('test', [(groups['foo'], [opts['deprecated_opt_without_deprecated_group']])])], expected='''[DEFAULT] [foo] # foo help # # From test # # deprecated (string value) # Deprecated group/name - [foo]/foobar #bar = ''')), ('deprecated_for_removal', dict(opts=[('test', [(groups['foo'], [opts['deprecated_for_removal_opt']])])], expected='''[DEFAULT] [foo] # foo help # # From test # # DEPRECATED: deprecated for removal (string value) # This option is deprecated for removal. # Its value may be silently ignored in the future. #bar = ''')), ('deprecated_reason', dict(opts=[('test', [(groups['foo'], [opts['deprecated_reason_opt']])])], expected='''[DEFAULT] [foo] # foo help # # From test # # DEPRECATED: Turn off stove (boolean value) # This option is deprecated for removal. # Its value may be silently ignored in the future. # Reason: This was supposed to work but it really, really did not. # Always buy house insurance. #turn_off_stove = false ''')), ('deprecated_opt_with_deprecated_group', dict(opts=[('test', [(groups['foo'], [opts['deprecated_opt_with_deprecated_group']])])], expected='''[DEFAULT] [foo] # foo help # # From test # # deprecated (string value) # Deprecated group/name - [group1]/foobar #bar = ''')), ('unknown_type', dict(opts=[('test', [(None, [opts['unknown_type']])])], expected='''[DEFAULT] # # From test # # unknown (unknown type) #unknown_opt = 123 ''')), ('str_opt', dict(opts=[('test', [(None, [opts['str_opt']])])], expected='''[DEFAULT] # # From test # # a string (string value) #str_opt = foo bar ''')), ('str_opt_with_space', dict(opts=[('test', [(None, [opts['str_opt_with_space']])])], expected='''[DEFAULT] # # From test # # a string with spaces (string value) #str_opt = " foo bar " ''')), ('str_opt_multiline', dict(opts=[('test', [(None, [opts['str_opt_multiline']])])], expected='''[DEFAULT] # # From test # # a string with newlines (string value) #str_opt = foo # bar # baz ''')), ('bool_opt', dict(opts=[('test', [(None, [opts['bool_opt']])])], expected='''[DEFAULT] # # From test # # a boolean (boolean value) #bool_opt = false ''')), ('int_opt', dict(opts=[('test', [(None, [opts['int_opt']])])], expected='''[DEFAULT] # # From test # # an integer (integer value) # Minimum value: 1 # Maximum value: 20 #int_opt = 10 ''')), ('int_opt_min_0', dict(opts=[('test', [(None, [opts['int_opt_min_0']])])], expected='''[DEFAULT] # # From test # # an integer (integer value) # Minimum value: 0 # Maximum value: 20 #int_opt_min_0 = 10 ''')), ('int_opt_max_0', dict(opts=[('test', [(None, [opts['int_opt_max_0']])])], expected='''[DEFAULT] # # From test # # an integer (integer value) # Maximum value: 0 #int_opt_max_0 = -1 ''')), ('float_opt', dict(opts=[('test', [(None, [opts['float_opt']])])], expected='''[DEFAULT] # # From test # # a float (floating point value) #float_opt = 0.1 ''')), ('list_opt', dict(opts=[('test', [(None, [opts['list_opt']])])], expected='''[DEFAULT] # # From test # # a list (list value) #list_opt = 1,2,3 ''')), ('list_opt_with_bounds', dict(opts=[('test', [(None, [opts['list_opt_with_bounds']])])], expected='''[DEFAULT] # # From test # # a list (list value) #list_opt_with_bounds = [1,2,3] ''')), ('list_opt_single', dict(opts=[('test', [(None, [opts['list_opt_single']])])], expected='''[DEFAULT] # # From test # # a list (list value) #list_opt_single = 1 ''')), ('list_int_opt', dict(opts=[('test', [(None, [opts['list_int_opt']])])], expected='''[DEFAULT] # # From test # # a list (list value) #list_int_opt = 1,2,3 ''')), ('dict_opt', dict(opts=[('test', [(None, [opts['dict_opt']])])], expected='''[DEFAULT] # # From test # # a dict (dict value) #dict_opt = 1:yes,2:no ''')), ('ip_opt', dict(opts=[('test', [(None, [opts['ip_opt']])])], expected='''[DEFAULT] # # From test # # an ip address (IP address value) #ip_opt = 127.0.0.1 ''')), ('port_opt', dict(opts=[('test', [(None, [opts['port_opt']])])], expected='''[DEFAULT] # # From test # # a port (port value) # Minimum value: 0 # Maximum value: 65535 #port_opt = 80 ''')), ('hostname_opt', dict(opts=[('test', [(None, [opts['hostname_opt']])])], expected='''[DEFAULT] # # From test # # a hostname (hostname value) #hostname_opt = compute01.nova.site1 ''')), ('multi_opt', dict(opts=[('test', [(None, [opts['multi_opt']])])], expected='''[DEFAULT] # # From test # # multiple strings (multi valued) #multi_opt = 1 #multi_opt = 2 #multi_opt = 3 ''')), ('multi_opt_none', dict(opts=[('test', [(None, [opts['multi_opt_none']])])], expected='''[DEFAULT] # # From test # # multiple strings (multi valued) #multi_opt_none = ''')), ('multi_opt_empty', dict(opts=[('test', [(None, [opts['multi_opt_empty']])])], expected='''[DEFAULT] # # From test # # multiple strings (multi valued) #multi_opt_empty = ''')), ('str_opt_sample_default', dict(opts=[('test', [(None, [opts['str_opt_sample_default']])])], expected='''[DEFAULT] # # From test # # a string (string value) #str_opt = fooishbar ''')), ('native_str_type', dict(opts=[('test', [(None, [opts['native_str_type']])])], expected='''[DEFAULT] # # From test # # native help (string value) #native_str_type = ''')), ('native_int_type', dict(opts=[('test', [(None, [opts['native_int_type']])])], expected='''[DEFAULT] # # From test # # native help (integer value) #native_int_type = ''')), ('native_float_type', dict(opts=[('test', [(None, [opts['native_float_type']])])], expected='''[DEFAULT] # # From test # # native help (floating point value) #native_float_type = ''')), ('multi_opt_sample_default', dict(opts=[('test', [(None, [opts['multi_opt_sample_default']])])], expected='''[DEFAULT] # # From test # # multiple strings (multi valued) # # This option has a sample default set, which means that # its actual default value may vary from the one documented # below. #multi_opt = 5 #multi_opt = 6 ''')), ('custom_type_name', dict(opts=[('test', [(None, [opts['custom_type_name']])])], expected='''[DEFAULT] # # From test # # this is a port (port number) #custom_opt_type = 5511 ''')), ('custom_type', dict(opts=[('test', [(None, [opts['custom_type']])])], expected='''[DEFAULT] # # From test # # custom help (unknown value) #custom_type = ''')), ('string_type_with_bad_default', dict(opts=[('test', [(None, [opts['string_type_with_bad_default']])])], expected='''[DEFAULT] # # From test # # string with bad default (string value) #string_type_with_bad_default = 4096 ''')), ('str_opt_str_group', dict(opts=[('test', [('foo', [opts['str_opt']]), (groups['foo'], [opts['int_opt']])]), ('foo', [('foo', [opts['bool_opt']])])], expected='''[DEFAULT] [foo] # foo help # # From foo # # a boolean (boolean value) #bool_opt = false # # From test # # a string (string value) #str_opt = foo bar # # From test # # an integer (integer value) # Minimum value: 1 # Maximum value: 20 #int_opt = 10 ''')), ('opt_str_opt_group', dict(opts=[('test', [(groups['foo'], [opts['int_opt']]), ('foo', [opts['str_opt']])]), ('foo', [(groups['foo'], [opts['bool_opt']])])], expected='''[DEFAULT] [foo] # foo help # # From foo # # a boolean (boolean value) #bool_opt = false # # From test # # an integer (integer value) # Minimum value: 1 # Maximum value: 20 #int_opt = 10 # # From test # # a string (string value) #str_opt = foo bar ''')), ('opt_with_DeprecatedOpt', dict(opts=[('test', [(None, [opts['opt_with_DeprecatedOpt']])])], expected='''[DEFAULT] # # From test # # Opt with DeprecatedOpt (boolean value) # Deprecated group/name - [deprecated]/foo_bar #foo_bar = ''')), ] output_file_scenarios = [ ('stdout', dict(stdout=True, output_file=None)), ('output_file', dict(output_file='sample.conf', stdout=False)), ] @classmethod def generate_scenarios(cls): cls.scenarios = testscenarios.multiply_scenarios( cls.content_scenarios, cls.output_file_scenarios) def setUp(self): super(GeneratorTestCase, self).setUp() self.conf = cfg.ConfigOpts() self.config_fixture = config_fixture.Config(self.conf) self.config = self.config_fixture.config self.useFixture(self.config_fixture) self.tempdir = self.useFixture(fixtures.TempDir()) def _capture_stream(self, stream_name): self.useFixture(fixtures.MonkeyPatch("sys.%s" % stream_name, io.StringIO())) return getattr(sys, stream_name) def _capture_stdout(self): return self._capture_stream('stdout') @mock.patch.object(generator, '_get_raw_opts_loaders') @mock.patch.object(generator, 'LOG') def test_generate(self, mock_log, raw_opts_loader): generator.register_cli_opts(self.conf) namespaces = [i[0] for i in self.opts] self.config(namespace=namespaces) for group in self.groups.values(): self.conf.register_group(group) wrap_width = getattr(self, 'wrap_width', None) if wrap_width is not None: self.config(wrap_width=wrap_width) if self.stdout: stdout = self._capture_stdout() else: output_file = self.tempdir.join(self.output_file) self.config(output_file=output_file) # We have a static data structure matching what should be # returned by _list_opts() but we're mocking out a lower level # function that needs to return a namespace and a callable to # return options from that namespace. We have to pass opts to # the lambda to cache a reference to the name because the list # comprehension changes the thing pointed to by the name each # time through the loop. raw_opts_loader.return_value = [ (ns, lambda opts=opts: opts) for ns, opts in self.opts ] generator.generate(self.conf) if self.stdout: self.assertEqual(self.expected, stdout.getvalue()) else: with open(output_file, 'r') as f: actual = f.read() self.assertEqual(self.expected, actual) log_warning = getattr(self, 'log_warning', None) if log_warning is not None: mock_log.warning.assert_called_once_with(*log_warning) else: self.assertFalse(mock_log.warning.called) class GeneratorFileHandlingTestCase(base.BaseTestCase): def setUp(self): super(GeneratorFileHandlingTestCase, self).setUp() self.conf = cfg.ConfigOpts() self.config_fixture = config_fixture.Config(self.conf) self.config = self.config_fixture.config @mock.patch.object(generator, '_get_groups') @mock.patch.object(generator, '_list_opts') def test_close_generated_file(self, a, b): generator.register_cli_opts(self.conf) self.config(output_file='somefile') m = mock.mock_open() m.close = mock.Mock() with mock.patch.object(generator, 'open', m, create=True): generator.generate(self.conf, output_file=None) m().close.assert_called_once() @mock.patch.object(generator, '_get_groups') @mock.patch.object(generator, '_list_opts') def test_not_close_external_file(self, a, b): generator.register_cli_opts(self.conf) self.config() m = mock.Mock() generator.generate(self.conf, output_file=m) m().close.assert_not_called() class DriverOptionTestCase(base.BaseTestCase): def setUp(self): super(DriverOptionTestCase, self).setUp() self.conf = cfg.ConfigOpts() self.config_fixture = config_fixture.Config(self.conf) self.config = self.config_fixture.config self.useFixture(self.config_fixture) @mock.patch.object(generator, '_get_driver_opts_loaders') @mock.patch.object(generator, '_get_raw_opts_loaders') @mock.patch.object(generator, 'LOG') def test_driver_option(self, mock_log, raw_opts_loader, driver_opts_loader): group = cfg.OptGroup( name='test_group', title='Test Group', driver_option='foo', ) regular_opts = [ cfg.MultiStrOpt('foo', help='foo option'), cfg.StrOpt('bar', help='bar option'), ] driver_opts = { 'd1': [ cfg.StrOpt('d1-foo', help='foo option'), ], 'd2': [ cfg.StrOpt('d2-foo', help='foo option'), ], } # We have a static data structure matching what should be # returned by _list_opts() but we're mocking out a lower level # function that needs to return a namespace and a callable to # return options from that namespace. We have to pass opts to # the lambda to cache a reference to the name because the list # comprehension changes the thing pointed to by the name each # time through the loop. raw_opts_loader.return_value = [ ('testing', lambda: [(group, regular_opts)]), ] driver_opts_loader.return_value = [ ('testing', lambda: driver_opts), ] # Initialize the generator to produce YAML output to a buffer. generator.register_cli_opts(self.conf) self.config(namespace=['test_generator'], format_='yaml') stdout = io.StringIO() # Generate the output and parse it back to a data structure. generator.generate(self.conf, output_file=stdout) body = stdout.getvalue() actual = yaml.safe_load(body) test_section = actual['options']['test_group'] self.assertEqual('foo', test_section['driver_option']) found_option_names = [ o['name'] for o in test_section['opts'] ] self.assertEqual( ['foo', 'bar', 'd1-foo', 'd2-foo'], found_option_names ) self.assertEqual( {'d1': ['d1-foo'], 'd2': ['d2-foo']}, test_section['driver_opts'], ) GENERATOR_OPTS = {'format_': 'yaml', 'minimal': False, 'namespace': ['test'], 'output_file': None, 'summarize': False, 'shell_completion': None, 'wrap_width': 70, 'config_source': []} class MachineReadableGeneratorTestCase(base.BaseTestCase): all_opts = GeneratorTestCase.opts all_groups = GeneratorTestCase.groups content_scenarios = [ ('single_namespace', dict(opts=[('test', [(None, [all_opts['foo']])])], expected={'deprecated_options': {}, 'generator_options': GENERATOR_OPTS, 'options': { 'DEFAULT': { 'driver_option': '', 'driver_opts': {}, 'dynamic_group_owner': '', 'help': '', 'standard_opts': ['foo'], 'opts': [{'advanced': False, 'choices': [], 'default': None, 'deprecated_for_removal': False, 'deprecated_opts': [], 'deprecated_reason': None, 'deprecated_since': None, 'dest': 'foo', 'help': 'foo option', 'max': None, 'metavar': None, 'min': None, 'mutable': False, 'name': 'foo', 'namespace': 'test', 'positional': False, 'required': False, 'sample_default': None, 'secret': False, 'short': None, 'type': 'string value'}]}}})), ('long_help', dict(opts=[('test', [(None, [all_opts['long_help']])])], expected={'deprecated_options': {}, 'generator_options': GENERATOR_OPTS, 'options': { 'DEFAULT': { 'driver_option': '', 'driver_opts': {}, 'dynamic_group_owner': '', 'help': '', 'standard_opts': ['long_help'], 'opts': [{'advanced': False, 'choices': [], 'default': None, 'deprecated_for_removal': False, 'deprecated_opts': [], 'deprecated_reason': None, 'deprecated_since': None, 'dest': 'long_help', 'help': all_opts['long_help'].help, 'max': None, 'metavar': None, 'min': None, 'mutable': False, 'name': 'long_help', 'namespace': 'test', 'positional': False, 'required': False, 'sample_default': None, 'secret': False, 'short': None, 'type': 'string value'}]}}})), ('long_help_pre', dict(opts=[('test', [(None, [all_opts['long_help_pre']])])], expected={'deprecated_options': {}, 'generator_options': GENERATOR_OPTS, 'options': { 'DEFAULT': { 'driver_option': '', 'driver_opts': {}, 'dynamic_group_owner': '', 'help': '', 'standard_opts': ['long_help_pre'], 'opts': [{'advanced': False, 'choices': [], 'default': None, 'deprecated_for_removal': False, 'deprecated_opts': [], 'deprecated_reason': None, 'deprecated_since': None, 'dest': 'long_help_pre', 'help': all_opts['long_help_pre'].help, 'max': None, 'metavar': None, 'min': None, 'mutable': False, 'name': 'long_help_pre', 'namespace': 'test', 'positional': False, 'required': False, 'sample_default': None, 'secret': False, 'short': None, 'type': 'string value'}]}}})), ('opt_with_DeprecatedOpt', dict(opts=[('test', [(None, [all_opts['opt_with_DeprecatedOpt']])])], expected={ 'deprecated_options': { 'deprecated': [{'name': 'foo_bar', 'replacement_group': 'DEFAULT', 'replacement_name': 'foo-bar'}]}, 'generator_options': GENERATOR_OPTS, 'options': { 'DEFAULT': { 'driver_option': '', 'driver_opts': {}, 'dynamic_group_owner': '', 'help': '', 'standard_opts': ['foo-bar'], 'opts': [{ 'advanced': False, 'choices': [], 'default': None, 'deprecated_for_removal': False, 'deprecated_opts': [{'group': 'deprecated', 'name': 'foo_bar'}], 'deprecated_reason': None, 'deprecated_since': None, 'dest': 'foo_bar', 'help': all_opts['opt_with_DeprecatedOpt'].help, 'max': None, 'metavar': None, 'min': None, 'mutable': False, 'name': 'foo-bar', 'namespace': 'test', 'positional': False, 'required': False, 'sample_default': None, 'secret': False, 'short': None, 'type': 'boolean value'}]}}})), ('choices_opt', dict(opts=[('test', [(None, [all_opts['choices_opt']])])], expected={'deprecated_options': {}, 'generator_options': GENERATOR_OPTS, 'options': { 'DEFAULT': { 'driver_option': '', 'driver_opts': {}, 'dynamic_group_owner': '', 'help': '', 'standard_opts': ['choices_opt'], 'opts': [{'advanced': False, 'choices': [ (None, None), ('', None), ('a', None), ('b', None), ('c', None) ], 'default': 'a', 'deprecated_for_removal': False, 'deprecated_opts': [], 'deprecated_reason': None, 'deprecated_since': None, 'dest': 'choices_opt', 'help': all_opts['choices_opt'].help, 'max': None, 'metavar': None, 'min': None, 'mutable': False, 'name': 'choices_opt', 'namespace': 'test', 'positional': False, 'required': False, 'sample_default': None, 'secret': False, 'short': None, 'type': 'string value'}]}}})), ('int_opt', dict(opts=[('test', [(None, [all_opts['int_opt']])])], expected={'deprecated_options': {}, 'generator_options': GENERATOR_OPTS, 'options': { 'DEFAULT': { 'driver_option': '', 'driver_opts': {}, 'dynamic_group_owner': '', 'help': '', 'standard_opts': ['int_opt'], 'opts': [{'advanced': False, 'choices': [], 'default': 10, 'deprecated_for_removal': False, 'deprecated_opts': [], 'deprecated_reason': None, 'deprecated_since': None, 'dest': 'int_opt', 'help': all_opts['int_opt'].help, 'max': 20, 'metavar': None, 'min': 1, 'mutable': False, 'name': 'int_opt', 'namespace': 'test', 'positional': False, 'required': False, 'sample_default': None, 'secret': False, 'short': None, 'type': 'integer value'}]}}})), ('group_help', dict(opts=[('test', [(all_groups['group1'], [all_opts['foo']])])], expected={'deprecated_options': {}, 'generator_options': GENERATOR_OPTS, 'options': { 'DEFAULT': { # 'driver_option': '', # 'driver_opts': [], # 'dynamic_group_owner': '', 'help': '', 'standard_opts': [], 'opts': [] }, 'group1': { 'driver_option': '', 'driver_opts': {}, 'dynamic_group_owner': '', 'help': all_groups['group1'].help, 'standard_opts': ['foo'], 'opts': [{'advanced': False, 'choices': [], 'default': None, 'deprecated_for_removal': False, 'deprecated_opts': [], 'deprecated_reason': None, 'deprecated_since': None, 'dest': 'foo', 'help': all_opts['foo'].help, 'max': None, 'metavar': None, 'min': None, 'mutable': False, 'name': 'foo', 'namespace': 'test', 'positional': False, 'required': False, 'sample_default': None, 'secret': False, 'short': None, 'type': 'string value'}]}}})), ] def setUp(self): super(MachineReadableGeneratorTestCase, self).setUp() self.conf = cfg.ConfigOpts() self.config_fixture = config_fixture.Config(self.conf) self.config = self.config_fixture.config self.useFixture(self.config_fixture) @classmethod def generate_scenarios(cls): cls.scenarios = testscenarios.multiply_scenarios( cls.content_scenarios) @mock.patch.object(generator, '_get_raw_opts_loaders') def test_generate(self, raw_opts_loader): generator.register_cli_opts(self.conf) namespaces = [i[0] for i in self.opts] self.config(namespace=namespaces, format_='yaml') # We have a static data structure matching what should be # returned by _list_opts() but we're mocking out a lower level # function that needs to return a namespace and a callable to # return options from that namespace. We have to pass opts to # the lambda to cache a reference to the name because the list # comprehension changes the thing pointed to by the name each # time through the loop. raw_opts_loader.return_value = [ (ns, lambda opts=opts: opts) for ns, opts in self.opts ] test_groups = generator._get_groups( generator._list_opts(self.conf.namespace)) self.assertEqual(self.expected, generator._generate_machine_readable_data(test_groups, self.conf)) class IgnoreDoublesTestCase(base.BaseTestCase): opts = [cfg.StrOpt('foo', help='foo option'), cfg.StrOpt('bar', help='bar option'), cfg.StrOpt('foo_bar', help='foobar'), cfg.StrOpt('str_opt', help='a string'), cfg.BoolOpt('bool_opt', help='a boolean'), cfg.IntOpt('int_opt', help='an integer')] def test_cleanup_opts_default(self): o = [("namespace1", [ ("group1", self.opts)])] self.assertEqual(o, generator._cleanup_opts(o)) def test_cleanup_opts_dup_opt(self): o = [("namespace1", [ ("group1", self.opts + [self.opts[0]])])] e = [("namespace1", [ ("group1", self.opts)])] self.assertEqual(e, generator._cleanup_opts(o)) def test_cleanup_opts_dup_groups_opt(self): o = [("namespace1", [ ("group1", self.opts + [self.opts[1]]), ("group2", self.opts), ("group3", self.opts + [self.opts[2]])])] e = [("namespace1", [ ("group1", self.opts), ("group2", self.opts), ("group3", self.opts)])] self.assertEqual(e, generator._cleanup_opts(o)) def test_cleanup_opts_dup_mixed_case_groups_opt(self): o = [("namespace1", [ ("default", self.opts), ("Default", self.opts + [self.opts[1]]), ("DEFAULT", self.opts + [self.opts[2]]), ("group1", self.opts + [self.opts[1]]), ("Group1", self.opts), ("GROUP1", self.opts + [self.opts[2]])])] e = [("namespace1", [ ("DEFAULT", self.opts), ("group1", self.opts)])] self.assertEqual(e, generator._cleanup_opts(o)) def test_cleanup_opts_dup_namespace_groups_opts(self): o = [("namespace1", [ ("group1", self.opts + [self.opts[1]]), ("group2", self.opts)]), ("namespace2", [ ("group1", self.opts + [self.opts[2]]), ("group2", self.opts)])] e = [("namespace1", [ ("group1", self.opts), ("group2", self.opts)]), ("namespace2", [ ("group1", self.opts), ("group2", self.opts)])] self.assertEqual(e, generator._cleanup_opts(o)) @mock.patch.object(generator, '_get_raw_opts_loaders') def test_list_ignores_doubles(self, raw_opts_loaders): config_opts = [ (None, [cfg.StrOpt('foo'), cfg.StrOpt('bar')]), ] # These are the very same config options, but read twice. # This is possible if one misconfigures the entry point for the # sample config generator. raw_opts_loaders.return_value = [ ('namespace', lambda: config_opts), ('namespace', lambda: config_opts), ] slurped_opts = 0 for _, listing in generator._list_opts(['namespace']): for _, opts in listing: slurped_opts += len(opts) self.assertEqual(2, slurped_opts) class GeneratorAdditionalTestCase(base.BaseTestCase): opts = [cfg.StrOpt('foo', help='foo option', default='fred'), cfg.StrOpt('bar', help='bar option'), cfg.StrOpt('foo_bar', help='foobar'), cfg.StrOpt('str_opt', help='a string'), cfg.BoolOpt('bool_opt', help='a boolean'), cfg.IntOpt('int_opt', help='an integer')] def test_get_groups_empty_ns(self): groups = generator._get_groups([]) self.assertEqual({'DEFAULT': {'object': None, 'namespaces': []}}, groups) def test_get_groups_single_ns(self): config = [("namespace1", [ ("beta", self.opts), ("alpha", self.opts)])] groups = generator._get_groups(config) self.assertEqual(['DEFAULT', 'alpha', 'beta'], sorted(groups)) def test_get_groups_multiple_ns(self): config = [("namespace1", [ ("beta", self.opts), ("alpha", self.opts)]), ("namespace2", [ ("gamma", self.opts), ("alpha", self.opts)])] groups = generator._get_groups(config) self.assertEqual(['DEFAULT', 'alpha', 'beta', 'gamma'], sorted(groups)) def test_output_opts_empty_default(self): config = [("namespace1", [ ("alpha", [])])] groups = generator._get_groups(config) fd, tmp_file = tempfile.mkstemp() with open(tmp_file, 'w+') as f: formatter = build_formatter(f) generator._output_opts(formatter, 'DEFAULT', groups.pop('DEFAULT')) expected = '''[DEFAULT] ''' with open(tmp_file, 'r') as f: actual = f.read() self.assertEqual(expected, actual) def test_output_opts_group(self): config = [("namespace1", [ ("alpha", [self.opts[0]])])] groups = generator._get_groups(config) fd, tmp_file = tempfile.mkstemp() with open(tmp_file, 'w+') as f: formatter = build_formatter(f) generator._output_opts(formatter, 'alpha', groups.pop('alpha')) expected = '''[alpha] # # From namespace1 # # foo option (string value) #foo = fred ''' with open(tmp_file, 'r') as f: actual = f.read() self.assertEqual(expected, actual) def _test_output_default_list_opt_with_string_value(self, default): opt = cfg.ListOpt('list_opt', help='a list', default=default) config = [("namespace1", [ ("alpha", [opt])])] groups = generator._get_groups(config) fd, tmp_file = tempfile.mkstemp() f = open(tmp_file, 'w+') formatter = build_formatter(f) expected = '''[alpha] # # From namespace1 # # a list (list value) #list_opt = %(default)s ''' % {'default': default} generator._output_opts(formatter, 'alpha', groups.pop('alpha')) f.close() content = open(tmp_file).read() self.assertEqual(expected, content) def test_output_default_list_opt_with_string_value_multiple_entries(self): self._test_output_default_list_opt_with_string_value('foo,bar') def test_output_default_list_opt_with_string_value_single_entry(self): self._test_output_default_list_opt_with_string_value('foo') class GeneratorMutableOptionTestCase(base.BaseTestCase): def test_include_message(self): out = io.StringIO() opt = cfg.StrOpt('foo', help='foo option', mutable=True) gen = build_formatter(out) gen.format(opt, 'group1') result = out.getvalue() self.assertIn( 'This option can be changed without restarting.', result, ) def test_do_not_include_message(self): out = io.StringIO() opt = cfg.StrOpt('foo', help='foo option', mutable=False) gen = build_formatter(out) gen.format(opt, 'group1') result = out.getvalue() self.assertNotIn( 'This option can be changed without restarting.', result, ) class GeneratorRaiseErrorTestCase(base.BaseTestCase): def test_generator_raises_error(self): """Verifies that errors from extension manager are not suppressed.""" class FakeException(Exception): pass class FakeEP: def __init__(self): self.name = 'callback_is_expected' self.require = self.resolve self.load = self.resolve def resolve(self, *args, **kwargs): raise FakeException() fake_ep = FakeEP() self.conf = cfg.ConfigOpts() self.conf.register_opts(generator._generator_opts) self.conf.set_default('namespace', [fake_ep.name]) with mock.patch('stevedore.named.NamedExtensionManager', side_effect=FakeException()): self.assertRaises(FakeException, generator.generate, self.conf) def test_generator_call_with_no_arguments_raises_system_exit(self): testargs = ['oslo-config-generator'] with mock.patch('sys.argv', testargs): self.assertRaises(SystemExit, generator.main, []) class ChangeDefaultsTestCase(base.BaseTestCase): @mock.patch.object(generator, '_get_opt_default_updaters') @mock.patch.object(generator, '_get_raw_opts_loaders') def test_no_modifiers_registered(self, raw_opts_loaders, get_updaters): orig_opt = cfg.StrOpt('foo', default='bar') raw_opts_loaders.return_value = [ ('namespace', lambda: [(None, [orig_opt])]), ] get_updaters.return_value = [] opts = generator._list_opts(['namespace']) # NOTE(dhellmann): Who designed this data structure? the_opt = opts[0][1][0][1][0] self.assertEqual('bar', the_opt.default) self.assertIs(orig_opt, the_opt) @mock.patch.object(generator, '_get_opt_default_updaters') @mock.patch.object(generator, '_get_raw_opts_loaders') def test_change_default(self, raw_opts_loaders, get_updaters): orig_opt = cfg.StrOpt('foo', default='bar') raw_opts_loaders.return_value = [ ('namespace', lambda: [(None, [orig_opt])]), ] def updater(): cfg.set_defaults([orig_opt], foo='blah') get_updaters.return_value = [updater] opts = generator._list_opts(['namespace']) # NOTE(dhellmann): Who designed this data structure? the_opt = opts[0][1][0][1][0] self.assertEqual('blah', the_opt.default) self.assertIs(orig_opt, the_opt) class RequiredOptionTestCase(base.BaseTestCase): opts = [cfg.StrOpt('foo', help='foo option', default='fred'), cfg.StrOpt('bar', help='bar option', required=True), cfg.StrOpt('foo_bar', help='foobar'), cfg.StrOpt('bars', help='bars foo', required=True)] def test_required_option_order_single_ns(self): config = [("namespace1", [ ("alpha", self.opts)])] groups = generator._get_groups(config) fd, tmp_file = tempfile.mkstemp() with open(tmp_file, 'w+') as f: formatter = build_formatter(f, minimal=True) generator._output_opts(formatter, 'alpha', groups.pop('alpha')) expected = '''[alpha] # # From namespace1 # # bar option (string value) bar = # bars foo (string value) bars = ''' with open(tmp_file, 'r') as f: actual = f.read() self.assertEqual(expected, actual) class SummarizedOptionsTestCase(base.BaseTestCase): """Validate 'summarize' config option. The 'summarize' switch ensures only summaries of each configuration option are output. """ opts = [ cfg.StrOpt( 'foo', default='fred', help="""This is the summary line for a config option. I can provide a lot more detail here, but I may not want to bloat my config file. In this scenario, I can use the 'summarize' config option to ensure only a summary of the option is output to the config file. However, the Sphinx-generated documentation, hosted online, remains unchanged. Hopefully this works. """), cfg.StrOpt( 'bar', required=True, help="""This is a less carefully formatted configuration option, where the author has not broken their description into a brief summary line and larger description. Watch this person's commit messages!""")] def test_summarized_option_order_single_ns(self): config = [('namespace1', [('alpha', self.opts)])] groups = generator._get_groups(config) fd, tmp_file = tempfile.mkstemp() with open(tmp_file, 'w+') as f: formatter = build_formatter(f, summarize=True) generator._output_opts(formatter, 'alpha', groups.pop('alpha')) expected = '''[alpha] # # From namespace1 # # This is the summary line for a config option. For more information, # refer to the documentation. (string value) #foo = fred # This is a less carefully formatted configuration # option, where the author has not broken their description into a # brief # summary line and larger description. Watch this person's commit # messages! (string value) #bar = ''' with open(tmp_file, 'r') as f: actual = f.read() self.assertEqual(expected, actual) class AdvancedOptionsTestCase(base.BaseTestCase): opts = [cfg.StrOpt('foo', help='foo option', default='fred'), cfg.StrOpt('bar', help='bar option', advanced=True), cfg.StrOpt('foo_bar', help='foobar'), cfg.BoolOpt('bars', help='bars foo', default=True, advanced=True)] def test_advanced_option_order_single_ns(self): config = [("namespace1", [ ("alpha", self.opts)])] groups = generator._get_groups(config) fd, tmp_file = tempfile.mkstemp() with open(tmp_file, 'w+') as f: formatter = build_formatter(f) generator._output_opts(formatter, 'alpha', groups.pop('alpha')) expected = '''[alpha] # # From namespace1 # # foo option (string value) #foo = fred # foobar (string value) #foo_bar = # bar option (string value) # 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. #bar = # bars foo (boolean value) # 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. #bars = true ''' with open(tmp_file, 'r') as f: actual = f.read() self.assertEqual(expected, actual) class HostAddressTestCase(base.BaseTestCase): opts = [cfg.HostAddressOpt('foo', help='foo option', default='0.0.0.0')] def test_host_address(self): config = [("namespace", [("alpha", self.opts)])] groups = generator._get_groups(config) out = io.StringIO() formatter = build_formatter(out) generator._output_opts(formatter, 'alpha', groups.pop('alpha')) result = out.getvalue() expected = textwrap.dedent(''' [alpha] # # From namespace # # foo option (host address value) #foo = 0.0.0.0 ''').lstrip() self.assertEqual(expected, result) class HostDomainTestCase(base.BaseTestCase): opts = [cfg.HostDomainOpt('foo', help='foo option', default='0.0.0.0')] def test_host_domain(self): config = [("namespace", [("alpha", self.opts)])] groups = generator._get_groups(config) out = io.StringIO() formatter = build_formatter(out) generator._output_opts(formatter, 'alpha', groups.pop('alpha')) result = out.getvalue() expected = textwrap.dedent(''' [alpha] # # From namespace # # foo option (host domain value) #foo = 0.0.0.0 ''').lstrip() self.assertEqual(expected, result) GeneratorTestCase.generate_scenarios() MachineReadableGeneratorTestCase.generate_scenarios() ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/tests/test_get_location.py0000664000175000017500000001521600000000000023521 0ustar00zuulzuul00000000000000# 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 io import tempfile import textwrap from oslotest import base from oslo_config import cfg class TestConfigOpts(cfg.ConfigOpts): def __call__(self, args=None, default_config_files=[], default_config_dirs=[]): return cfg.ConfigOpts.__call__( self, args=args, prog='test', version='1.0', usage='%(prog)s FOO BAR', description='somedesc', epilog='tepilog', default_config_files=default_config_files, default_config_dirs=default_config_dirs, validate_default_values=True) class LocationTestCase(base.BaseTestCase): def test_user_controlled(self): self.assertTrue(cfg.Locations.user.is_user_controlled) self.assertTrue(cfg.Locations.command_line.is_user_controlled) def test_not_user_controlled(self): self.assertFalse(cfg.Locations.opt_default.is_user_controlled) self.assertFalse(cfg.Locations.set_default.is_user_controlled) self.assertFalse(cfg.Locations.set_override.is_user_controlled) class GetLocationTestCase(base.BaseTestCase): def setUp(self): super(GetLocationTestCase, self).setUp() def _clear(): cfg._show_caller_details = False self.addCleanup(_clear) cfg._show_caller_details = True self.conf = TestConfigOpts() self.normal_opt = cfg.StrOpt( 'normal_opt', default='normal_opt_default', ) self.conf.register_opt(self.normal_opt) self.cli_opt = cfg.StrOpt( 'cli_opt', default='cli_opt_default', ) self.conf.register_cli_opt(self.cli_opt) self.group_opt = cfg.StrOpt( 'group_opt', default='group_opt_default', ) self.conf.register_opt(self.group_opt, group='group') def test_opt_default(self): self.conf([]) loc = self.conf.get_location('normal_opt') self.assertEqual( cfg.Locations.opt_default, loc.location, ) self.assertIn('test_get_location.py', loc.detail) def test_set_default_on_config_opt(self): self.conf.set_default('normal_opt', self.id()) self.conf([]) loc = self.conf.get_location('normal_opt') self.assertEqual( cfg.Locations.set_default, loc.location, ) self.assertIn('test_get_location.py', loc.detail) def test_set_defaults_func(self): cfg.set_defaults([self.normal_opt], normal_opt=self.id()) self.conf([]) loc = self.conf.get_location('normal_opt') self.assertEqual( cfg.Locations.set_default, loc.location, ) self.assertIn('test_get_location.py', loc.detail) def test_set_override(self): self.conf.set_override('normal_opt', self.id()) self.conf([]) loc = self.conf.get_location('normal_opt') self.assertEqual( cfg.Locations.set_override, loc.location, ) self.assertIn('test_get_location.py', loc.detail) def test_user_cli(self): filename = self._write_opt_to_tmp_file( 'DEFAULT', 'unknown_opt', self.id()) self.conf(['--config-file', filename, '--cli_opt', 'blah']) loc = self.conf.get_location('cli_opt') self.assertEqual( cfg.Locations.command_line, loc.location, ) def test_default_cli(self): filename = self._write_opt_to_tmp_file( 'DEFAULT', 'unknown_opt', self.id()) self.conf(['--config-file', filename]) loc = self.conf.get_location('cli_opt') self.assertEqual( cfg.Locations.opt_default, loc.location, ) def _write_opt_to_tmp_file(self, group, option, value): filename = tempfile.mktemp() with io.open(filename, 'w', encoding='utf-8') as f: f.write(textwrap.dedent(''' [{group}] {option} = {value} ''').format( group=group, option=option, value=value, )) return filename def test_user_cli_opt_in_file(self): filename = self._write_opt_to_tmp_file( 'DEFAULT', 'cli_opt', self.id()) self.conf(['--config-file', filename]) loc = self.conf.get_location('cli_opt') self.assertEqual( cfg.Locations.user, loc.location, ) self.assertEqual( filename, loc.detail, ) def test_user_file(self): filename = self._write_opt_to_tmp_file( 'DEFAULT', 'normal_opt', self.id()) self.conf(['--config-file', filename]) loc = self.conf.get_location('normal_opt') self.assertEqual( cfg.Locations.user, loc.location, ) self.assertEqual( filename, loc.detail, ) def test_duplicate_registration(self): # NOTE(dhellmann): The ZFS driver in Cinder uses multiple Opt # instances with the same settings but in different # modules. We don't want to break that case with the option # location stuff, so we need to test that it works. To # reproduce that test we have to simulate the option being # created in a different file, so we create a new Opt instance # and replace its _set_location data. dupe_opt = cfg.StrOpt( 'normal_opt', default='normal_opt_default', ) dupe_opt._set_location = cfg.LocationInfo( cfg.Locations.opt_default, 'an alternative file', ) # We expect register_opt() to return False to indicate that # the option was already registered. self.assertFalse(self.conf.register_opt(dupe_opt)) def test_group_opt(self): self.conf([]) loc = self.conf.get_location('group_opt', 'group') self.assertEqual( cfg.Locations.opt_default, loc.location, ) self.assertIn('test_get_location.py', loc.detail) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/tests/test_iniparser.py0000664000175000017500000000743100000000000023046 0ustar00zuulzuul00000000000000# 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'") ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/tests/test_sources.py0000664000175000017500000003013200000000000022527 0ustar00zuulzuul00000000000000# 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 oslotest import base from requests import HTTPError import requests_mock import testtools from oslo_config import _list_opts from oslo_config import cfg from oslo_config import fixture from oslo_config import sources from oslo_config.sources import _uri class TestProcessingSources(base.BaseTestCase): # NOTE(dhellmann): These tests use the config() method of the # fixture because that invokes set_override() on the option. The # load_raw_values() method injects data underneath the option, but # only after invoking __call__ on the ConfigOpts instance, which # is when the 'config_source' option is processed. def setUp(self): super(TestProcessingSources, self).setUp() self.conf = cfg.ConfigOpts() self.conf_fixture = self.useFixture(fixture.Config(self.conf)) def test_no_sources_default(self): with base.mock.patch.object( self.conf, '_open_source_from_opt_group') as open_source: open_source.side_effect = AssertionError('should not be called') self.conf([]) def test_no_sources(self): self.conf_fixture.config( config_source=[], ) with base.mock.patch.object( self.conf, '_open_source_from_opt_group') as open_source: open_source.side_effect = AssertionError('should not be called') self.conf([]) def test_source_named(self): self.conf_fixture.config( config_source=['missing_source'], ) with base.mock.patch.object( self.conf, '_open_source_from_opt_group') as open_source: self.conf([]) open_source.assert_called_once_with('missing_source') def test_multiple_sources_named(self): self.conf_fixture.config( config_source=['source1', 'source2'], ) with base.mock.patch.object( self.conf, '_open_source_from_opt_group') as open_source: self.conf([]) open_source.assert_has_calls([ base.mock.call('source1'), base.mock.call('source2'), ]) class TestLoading(base.BaseTestCase): # NOTE(dhellmann): These tests can use load_raw_values() because # they explicitly call _open_source_from_opt_group() after the # ConfigOpts setup is done in __call__(). def setUp(self): super(TestLoading, self).setUp() self.conf = cfg.ConfigOpts() self.conf_fixture = self.useFixture(fixture.Config(self.conf)) def test_source_missing(self): # The group being loaded does not exist at all. source = self.conf._open_source_from_opt_group('missing_source') self.assertIsNone(source) def test_driver_missing(self): # The group exists and has other options, but does not specify # a driver. self.conf_fixture.load_raw_values( group='missing_driver', not_driver='foo', ) source = self.conf._open_source_from_opt_group('missing_driver') self.assertIsNone(source) def test_unknown_driver(self): # The group exists, but does not specify a valid driver. self.conf_fixture.load_raw_values( group='unknown_driver', driver='very_unlikely_to_exist_driver_name', ) source = self.conf._open_source_from_opt_group('unknown_driver') self.assertIsNone(source) class TestEnvironmentConfigurationSource(base.BaseTestCase): def setUp(self): super(TestEnvironmentConfigurationSource, self).setUp() self.conf = cfg.ConfigOpts() self.conf_fixture = self.useFixture(fixture.Config(self.conf)) self.conf.register_opt(cfg.StrOpt('bar'), 'foo') self.conf.register_opt(cfg.StrOpt('baz', regex='^[a-z].*$'), 'foo') def cleanup(): for env in ('OS_FOO__BAR', 'OS_FOO__BAZ'): if env in os.environ: del os.environ[env] self.addCleanup(cleanup) def test_simple_environment_get(self): self.conf(args=[]) env_value = 'goodbye' os.environ['OS_FOO__BAR'] = env_value self.assertEqual(env_value, self.conf['foo']['bar']) def test_env_beats_files(self): file_value = 'hello' env_value = 'goodbye' self.conf(args=[]) self.conf_fixture.load_raw_values( group='foo', bar=file_value, ) self.assertEqual(file_value, self.conf['foo']['bar']) self.conf.reload_config_files() os.environ['OS_FOO__BAR'] = env_value self.assertEqual(env_value, self.conf['foo']['bar']) def test_cli_beats_env(self): env_value = 'goodbye' cli_value = 'cli' os.environ['OS_FOO__BAR'] = env_value self.conf.register_cli_opt(cfg.StrOpt('bar'), 'foo') self.conf(args=['--foo=%s' % cli_value]) self.assertEqual(cli_value, self.conf['foo']['bar']) def test_use_env_false_allows_files(self): file_value = 'hello' env_value = 'goodbye' os.environ['OS_FOO__BAR'] = env_value self.conf(args=[], use_env=False) self.conf_fixture.load_raw_values( group='foo', bar=file_value, ) self.assertEqual(file_value, self.conf['foo']['bar']) self.conf.reset() self.conf(args=[], use_env=True) self.assertEqual(env_value, self.conf['foo']['bar']) def test_invalid_env(self): self.conf(args=[]) env_value = 'ABC' os.environ['OS_FOO__BAZ'] = env_value with testtools.ExpectedException(cfg.ConfigSourceValueError): self.conf['foo']['baz'] def make_uri(name): return "https://oslo.config/{}.conf".format(name) _extra_configs = { make_uri("types"): { "name": "types", "data": { "DEFAULT": { "foo": (cfg.StrOpt, "bar") }, "test": { "opt_str": (cfg.StrOpt, "a nice string"), "opt_bool": (cfg.BoolOpt, True), "opt_int": (cfg.IntOpt, 42), "opt_float": (cfg.FloatOpt, 3.14), "opt_ip": (cfg.IPOpt, "127.0.0.1"), "opt_port": (cfg.PortOpt, 443), "opt_host": (cfg.HostnameOpt, "www.openstack.org"), "opt_uri": (cfg.URIOpt, "https://www.openstack.org"), "opt_multi": (cfg.MultiStrOpt, ["abc", "def", "ghi"]) } } }, make_uri("ini_1"): { "name": "ini_1", "data": { "DEFAULT": { "abc": (cfg.StrOpt, "abc") } } }, make_uri("ini_2"): { "name": "ini_2", "data": { "DEFAULT": { "abc": (cfg.StrOpt, "foo"), "def": (cfg.StrOpt, "def") } } }, make_uri("ini_3"): { "name": "ini_3", "data": { "DEFAULT": { "abc": (cfg.StrOpt, "bar"), "def": (cfg.StrOpt, "bar"), "ghi": (cfg.StrOpt, "ghi") } } } } def opts_to_ini(uri, *args, **kwargs): opts = _extra_configs[uri]["data"] result = "" # 'g': group, 'o': option, 't': type, and 'v': value for g in opts.keys(): result += "[{}]\n".format(g) for o, (t, v) in opts[g].items(): if t == cfg.MultiStrOpt: for i in v: result += "{} = {}\n".format(o, i) else: result += "{} = {}\n".format(o, v) return result class URISourceTestCase(base.BaseTestCase): def setUp(self): super(URISourceTestCase, self).setUp() self.conf = cfg.ConfigOpts() self.conf_fixture = self.useFixture(fixture.Config(self.conf)) def _register_opts(self, opts): # 'g': group, 'o': option, and 't': type for g in opts.keys(): for o, (t, _) in opts[g].items(): self.conf.register_opt(t(o), g if g != "DEFAULT" else None) def test_incomplete_driver(self): # The group exists, but does not specify the # required options for this driver. self.conf_fixture.load_raw_values( group='incomplete_ini_driver', driver='remote_file', ) source = self.conf._open_source_from_opt_group('incomplete_ini_driver') self.assertIsNone(source) @requests_mock.mock() def test_fetch_uri(self, m): m.get("https://bad.uri", status_code=404) self.assertRaises( HTTPError, _uri.URIConfigurationSource, "https://bad.uri") m.get("https://good.uri", text="[DEFAULT]\nfoo=bar\n") source = _uri.URIConfigurationSource("https://good.uri") self.assertEqual( "bar", source.get("DEFAULT", "foo", cfg.StrOpt("foo"))[0]) @base.mock.patch( "oslo_config.sources._uri.URIConfigurationSource._fetch_uri", side_effect=opts_to_ini) def test_configuration_source(self, mock_fetch_uri): group = "types" uri = make_uri(group) self.conf_fixture.load_raw_values( group=group, driver='remote_file', uri=uri ) self.conf_fixture.config(config_source=[group]) # testing driver loading self.assertEqual(self.conf._sources, []) self.conf._load_alternative_sources() self.assertIsInstance(self.conf._sources[0], _uri.URIConfigurationSource) source = self.conf._open_source_from_opt_group(group) self._register_opts(_extra_configs[uri]["data"]) # non-existing option self.assertIs(sources._NoValue, source.get("DEFAULT", "bar", cfg.StrOpt("bar"))[0]) # 'g': group, 'o': option, 't': type, and 'v': value for g in _extra_configs[uri]["data"]: for o, (t, v) in _extra_configs[uri]["data"][g].items(): self.assertEqual(str(v), str(source.get(g, o, t(o))[0])) self.assertEqual(v, self.conf[g][o] if g != "DEFAULT" else self.conf[o]) @base.mock.patch( "oslo_config.sources._uri.URIConfigurationSource._fetch_uri", side_effect=opts_to_ini) def test_multiple_configuration_sources(self, mock_fetch_uri): groups = ["ini_1", "ini_2", "ini_3"] uri = make_uri("ini_3") for group in groups: self.conf_fixture.load_raw_values( group=group, driver='remote_file', uri=make_uri(group) ) self.conf_fixture.config(config_source=groups) self.conf._load_alternative_sources() # ini_3 contains all options to be tested self._register_opts(_extra_configs[uri]["data"]) # where options are 'abc', 'def', and 'ghi' # if the extra configs are loaded in the right order # the option name and option value will match correctly for option in _extra_configs[uri]["data"]["DEFAULT"]: self.assertEqual(option, self.conf[option]) def test_list_opts(self): discovered_group = None for group in _list_opts.list_opts(): if group[0] is not None: if group[0].name == "sample_remote_file_source": discovered_group = group break self.assertIsNotNone(discovered_group) self.assertEqual( _uri.URIConfigurationSourceDriver().list_options_for_discovery(), # NOTE: Ignore 'driver' option inserted automatically. discovered_group[1][1:], ) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/tests/test_sphinxconfiggen.py0000664000175000017500000001325700000000000024246 0ustar00zuulzuul00000000000000# 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 unittest 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']) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/tests/test_sphinxext.py0000664000175000017500000003522200000000000023103 0ustar00zuulzuul00000000000000# 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 from unittest 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_opts( 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: ```` this appears in the default group ''').lstrip(), results) def test_with_default_value(self): results = '\n'.join(list(sphinxext._format_group_opts( 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_opts( 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: ```` :Minimum Value: 1 ''').lstrip(), results) def test_with_min_0(self): results = '\n'.join(list(sphinxext._format_group_opts( 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: ```` :Minimum Value: 0 ''').lstrip(), results) def test_with_max(self): results = '\n'.join(list(sphinxext._format_group_opts( 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: ```` :Maximum Value: 1 ''').lstrip(), results) def test_with_max_0(self): results = '\n'.join(list(sphinxext._format_group_opts( 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: ```` :Maximum Value: 0 ''').lstrip(), results) def test_with_choices(self): results = '\n'.join(list(sphinxext._format_group_opts( 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: ```` :Valid Values: a, b, c, , '' ''').lstrip(), results) def test_with_choices_with_descriptions(self): results = '\n'.join(list(sphinxext._format_group_opts( namespace=None, group_name=None, group_obj=None, opt_list=[ cfg.StrOpt( 'opt_name', choices=[ ('a', 'a is the best'), ('b', 'Actually, may-b I am better'), ('c', 'c, I am clearly the greatest'), (None, 'I am having none of this'), ('', '')]), ], ))) self.assertEqual(textwrap.dedent(''' .. oslo.config:group:: DEFAULT .. oslo.config:option:: opt_name :Type: string :Default: ```` :Valid Values: a, b, c, , '' .. rubric:: Possible values a a is the best b Actually, may-b I am better c c, I am clearly the greatest I am having none of this '' ''').lstrip(), results) def test_group_obj_without_help(self): # option with None group placed in DEFAULT results = '\n'.join(list(sphinxext._format_group_opts( 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: ```` ''').lstrip(), results) def test_group_obj_with_help(self): # option with None group placed in DEFAULT results = '\n'.join(list(sphinxext._format_group_opts( 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: ```` ''').lstrip(), results) def test_deprecated_opts_without_deprecated_group(self): results = '\n'.join(list(sphinxext._format_group_opts( 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: ```` .. 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_opts( 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: ```` .. 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_opts( 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_opts( 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: ```` :Mutable: This option can be changed without restarting. ''').lstrip(), results) def test_not_mutable(self): results = '\n'.join(list(sphinxext._format_group_opts( 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: ```` ''').lstrip(), results) def test_advanced(self): results = '\n'.join(list(sphinxext._format_group_opts( 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: ```` :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_opts( 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: ```` ''').lstrip(), results) class FormatOptionHelpTest(base.BaseTestCase): @mock.patch('oslo_config.generator._list_opts') @mock.patch('oslo_config.sphinxext._format_group_opts') def test_split_namespaces(self, _format_group_opts, _list_opts): _list_opts.return_value = [ ('namespace1', [(None, ['opt1'])]), ('namespace2', [(None, ['opt2'])]), ] list(sphinxext._format_option_help( namespaces=['namespace1', 'namespace2'], split_namespaces=True)) _format_group_opts.assert_any_call( namespace='namespace1', group_name='DEFAULT', group_obj=None, opt_list=['opt1'], ) _format_group_opts.assert_any_call( namespace='namespace2', group_name='DEFAULT', group_obj=None, opt_list=['opt2'], ) @mock.patch('oslo_config.generator._list_opts') @mock.patch('oslo_config.sphinxext._format_group_opts') def test_dont_split_namespaces(self, _format_group_opts, _list_opts): _list_opts.return_value = [ ('namespace1', [(None, ['opt1'])]), ('namespace2', [(None, ['opt2'])]), ] list(sphinxext._format_option_help( namespaces=['namespace1', 'namespace2'], split_namespaces=False)) _format_group_opts.assert_called_once_with( namespace=None, group_name='DEFAULT', group_obj=None, opt_list=['opt1', 'opt2'], ) @mock.patch('oslo_config.generator._list_opts') @mock.patch('oslo_config.sphinxext._format_group_opts') def test_dont_split_namespaces_with_group(self, _format_group_opts, _list_opts): grp_obj = cfg.OptGroup('grp1') _list_opts.return_value = [ ('namespace1', [(grp_obj, ['opt1'])]), ('namespace2', [('grp1', ['opt2'])]), ] list(sphinxext._format_option_help( namespaces=['namespace1', 'namespace2'], split_namespaces=False)) _format_group_opts.assert_any_call( 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_opts') def test_split_namespaces_with_group(self, _format_group_opts, _list_opts): grp_obj = cfg.OptGroup('grp1') _list_opts.return_value = [ ('namespace1', [(grp_obj, ['opt1'])]), ('namespace2', [('grp1', ['opt2'])]), ] list(sphinxext._format_option_help( namespaces=['namespace1', 'namespace2'], split_namespaces=True)) print(_format_group_opts.call_args_list) _format_group_opts.assert_any_call( namespace='namespace1', group_name='grp1', group_obj=grp_obj, opt_list=['opt1'], ) _format_group_opts.assert_any_call( namespace='namespace2', group_name='grp1', group_obj=None, opt_list=['opt2'], ) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/tests/test_types.py0000664000175000017500000011270100000000000022213 0ustar00zuulzuul00000000000000# 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: 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_listed_value_tuple(self): self.type_instance = types.String(choices=('foo', 'bar')) self.assertConvertedValue('foo', 'foo') def test_listed_value_dict(self): self.type_instance = types.String(choices=[ ('foo', 'ab'), ('bar', 'xy')]) 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_single_quote_is_invalid(self): self.type_instance = types.String(quotes=True) self.assertInvalid('"') self.assertInvalid("'") 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_repr_with_choices_tuple(self): t = types.String(choices=('foo', 'bar')) self.assertEqual('String(choices=[\'foo\', \'bar\'])', repr(t)) def test_repr_with_choices_dict(self): t = types.String(choices=[('foo', 'ab'), ('bar', 'xy')]) 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']) t5 = types.String(choices=[('foo', 'ab'), ('bar', 'xy')]) self.assertTrue(t1 == t2 == t3 == t4 == t5) def test_not_equal_with_different_choices(self): t1 = types.String(choices=['foo', 'bar']) t2 = types.String(choices=['foo', 'baz']) t3 = types.String(choices=('foo', 'baz')) self.assertFalse(t1 == t2) self.assertFalse(t1 == t3) 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_repr_with_choices_tuple(self): t = types.Integer(choices=(80, 457)) self.assertEqual('Integer(choices=[80, 457])', repr(t)) def test_repr_with_choices_dict(self): t = types.Integer(choices=[(80, 'ab'), (457, 'xy')]) 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]) t3 = types.Integer(choices=(457, 80)) t4 = types.Integer(choices=[(80, 'ab'), (457, 'xy')]) self.assertTrue(t1 == t2 == t3 == t4) 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(self, t): self.assertRaises(ValueError, t, 1) self.assertRaises(ValueError, t, 200) self.assertRaises(ValueError, t, -457) t(80) t(457) def test_with_choices_list(self): t = types.Integer(choices=[80, 457]) self._test_with_choices(t) def test_with_choices_tuple(self): t = types.Integer(choices=(80, 457)) self._test_with_choices(t) def test_with_choices_dict(self): t = types.Integer(choices=[(80, 'ab'), (457, 'xy')]) self._test_with_choices(t) 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_tuple_of_values(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_values_containing_trailing_comma(self): self.assertConvertedValue('foo, bar, baz, ', ['foo', 'bar', 'baz']) 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_list_of_custom_type_containing_trailing_comma(self): self.type_instance = types.List(types.Integer()) self.assertConvertedValue('1,2,3,5,', [1, 2, 3, 5]) def test_tuple_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 RangeTypeTests(TypeTestHelper, unittest.TestCase): type = types.Range() def assertRange(self, s, r1, r2, step=1): self.assertEqual(list(range(r1, r2, step)), list(self.type_instance(s))) def test_range(self): self.assertRange('0-2', 0, 3) self.assertRange('-2-0', -2, 1) self.assertRange('2-0', 2, -1, -1) self.assertRange('-3--1', -3, 0) self.assertRange('-1--3', -1, -4, -1) self.assertRange('-1', -1, 0) self.assertInvalid('--1') self.assertInvalid('4-') self.assertInvalid('--') self.assertInvalid('1.1-1.2') self.assertInvalid('a-b') def test_range_bounds(self): self.type_instance = types.Range(1, 3) self.assertRange('1-3', 1, 4) self.assertRange('2-2', 2, 3) self.assertRange('2', 2, 3) self.assertInvalid('1-4') self.assertInvalid('0-3') self.assertInvalid('0-4') def test_range_exclusive(self): self.type_instance = types.Range(inclusive=False) self.assertRange('0-2', 0, 2) self.assertRange('-2-0', -2, 0) self.assertRange('2-0', 2, 0, -1) self.assertRange('-3--1', -3, -1) self.assertRange('-1--3', -1, -3, -1) self.assertRange('-1', -1, -1) 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_custom_separator(self): self.type_instance = types.Dict(key_value_separator='=') self.assertConvertedValue(' foo=bar, baz= 123 ', {'foo': 'bar', 'baz': '123'}) 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 HostAddressTypeTests(TypeTestHelper, unittest.TestCase): type = types.HostAddress() def test_invalid_host_addresses(self): self.assertInvalid('-1') self.assertInvalid('_foo') self.assertInvalid('3.14') self.assertInvalid('10.0') self.assertInvalid('host..name') self.assertInvalid('org.10') self.assertInvalid('0.0.00') def test_valid_host_addresses(self): self.assertConvertedValue('foo.bar', 'foo.bar') self.assertConvertedValue('192.168.0.1', '192.168.0.1') self.assertConvertedValue('abcd:ef::1', 'abcd:ef::1') self.assertConvertedValue('home-site-here.org.com', 'home-site-here.org.com') self.assertConvertedValue('3com.com', '3com.com') self.assertConvertedValue('10.org', '10.org') self.assertConvertedValue('cell1.nova.site1', 'cell1.nova.site1') self.assertConvertedValue('ab-c.com', 'ab-c.com') self.assertConvertedValue('abc.com-org', 'abc.com-org') self.assertConvertedValue('abc.0-0', 'abc.0-0') class HostDomainTypeTests(TypeTestHelper, unittest.TestCase): type = types.HostDomain() def test_invalid_host_addresses(self): self.assertInvalid('-1') self.assertInvalid('3.14') self.assertInvalid('10.0') self.assertInvalid('host..name') self.assertInvalid('org.10') self.assertInvalid('0.0.00') def test_valid_host_addresses(self): self.assertConvertedValue('_foo', '_foo') self.assertConvertedValue('host_name', 'host_name') self.assertConvertedValue( 'overcloud-novacompute_edge1-0.internalapi.localdomain', 'overcloud-novacompute_edge1-0.internalapi.localdomain') self.assertConvertedValue('host_01.co.uk', 'host_01.co.uk') self.assertConvertedValue('_site01001', '_site01001') 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("host_01.co.uk") self.assertInvalid("h%ost") 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_invalid_hostnames_with_numeric_characters(self): self.assertInvalid("10.0.0.0") self.assertInvalid("3.14") self.assertInvalid('___site0.1001') self.assertInvalid("org.10") self.assertInvalid('0.0.00') 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('localhost') self.assertConvertedEqual('3com.com') self.assertConvertedEqual('10.org') self.assertConvertedEqual('10ab.10ab') self.assertConvertedEqual('ab-c.com') self.assertConvertedEqual('abc.com-org') self.assertConvertedEqual('abc.0-0') 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') def test_equality(self): a = types.URI() b = types.URI() self.assertEqual(a, b) def test_equality_length(self): a = types.URI(max_length=5) b = types.URI(max_length=5) self.assertEqual(a, b) def test_equality_length_not(self): a = types.URI() b = types.URI(max_length=5) c = types.URI(max_length=10) self.assertNotEqual(a, b) self.assertNotEqual(c, b) def test_equality_schemes(self): a = types.URI(schemes=['ftp']) b = types.URI(schemes=['ftp']) self.assertEqual(a, b) def test_equality_schemes_not(self): a = types.URI() b = types.URI(schemes=['ftp']) c = types.URI(schemes=['http']) self.assertNotEqual(a, b) self.assertNotEqual(c, b) 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_repr_with_choices_tuple(self): t = types.Port(choices=(80, 457)) self.assertEqual('Port(choices=[80, 457])', repr(t)) def _test_with_choices(self, t): self.assertRaises(ValueError, t, 1) self.assertRaises(ValueError, t, 200) self.assertRaises(ValueError, t, -457) t(80) t(457) def test_with_choices_list(self): t = types.Port(choices=[80, 457]) self._test_with_choices(t) def test_with_choices_tuple(self): t = types.Port(choices=(80, 457)) self._test_with_choices(t) def test_with_choices_dict(self): t = types.Port(choices=[(80, 'ab'), (457, 'xy')]) self._test_with_choices(t) def test_invalid_choices(self): """Check for choices that are specifically invalid for ports.""" 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]) t3 = types.Port(choices=(457, 80)) t4 = types.Port(choices=[(457, 'ab'), (80, 'xy')]) self.assertTrue(t1 == t2 == t3 == t4) 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) class FormatSampleDefaultTests(unittest.TestCase): def test_string(self): t = types.String() self.assertEqual([' bar '], t.format_defaults('foo', sample_default=' bar ')) def test_string_non_str(self): t = types.String() e = Exception('bar') self.assertEqual(['bar'], t.format_defaults('', sample_default=e)) def test_string_non_str_spaces(self): t = types.String() e = Exception(' bar ') self.assertEqual(['" bar "'], t.format_defaults('', sample_default=e)) def test_list_string(self): t = types.List(item_type=types.String()) test_list = ['foo', Exception(' bar ')] self.assertEqual(['foo," bar "'], t.format_defaults('', sample_default=test_list)) def test_list_no_type(self): t = types.List() test_list = ['foo', Exception(' bar ')] self.assertEqual(['foo," bar "'], t.format_defaults('', sample_default=test_list)) def test_list_not_list(self): t = types.List() self.assertEqual(['foo'], t.format_defaults('', sample_default=Exception('foo'))) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/tests/test_validator.py0000664000175000017500000001160600000000000023036 0ustar00zuulzuul00000000000000# Copyright 2019 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 unittest import mock from oslotest import base from oslo_config import cfg from oslo_config import fixture from oslo_config import validator OPT_DATA = { "options": { "foo": { "opts": [ { "name": "opt", "default": 1 } ] }, "bar": { "opts": [ { "name": "opt", "default": 2 }, { "name": "foo-bar", "dest": "foo_bar", "default": 2 }, { "name": "bar-foo", "dest": "bar_foo", "default": 2 } ] } }, "deprecated_options": { "bar": [ { "name": "opt", "default": 3 } ] } } VALID_CONF = """ [foo] opt = 1 [bar] opt = 3 foo-bar = 3 bar_foo = 3 """ DEPRECATED_CONF = """ [bar] opt = value """ INVALID_CONF = """ [foo] opts = value """ MISSING_GROUP_CONF = """ [oo] opt = value """ class TestValidator(base.BaseTestCase): def setUp(self): super(TestValidator, self).setUp() self.conf = cfg.ConfigOpts() self.conf_fixture = self.useFixture(fixture.Config(self.conf)) validator._register_cli_opts(self.conf) @mock.patch('oslo_config.validator.load_opt_data') def test_passing(self, mock_lod): mock_lod.return_value = OPT_DATA self.conf_fixture.config(opt_data='mocked.yaml', input_file='mocked.conf') m = mock.mock_open(read_data=VALID_CONF) with mock.patch('builtins.open', m): self.assertEqual(0, validator._validate(self.conf)) @mock.patch('oslo_config.validator.load_opt_data') def test_deprecated(self, mock_lod): mock_lod.return_value = OPT_DATA self.conf_fixture.config(opt_data='mocked.yaml', input_file='mocked.conf') m = mock.mock_open(read_data=DEPRECATED_CONF) with mock.patch('builtins.open', m): self.assertEqual(0, validator._validate(self.conf)) @mock.patch('oslo_config.validator.load_opt_data') def test_deprecated_fatal_warnings(self, mock_lod): mock_lod.return_value = OPT_DATA self.conf_fixture.config(opt_data='mocked.yaml', input_file='mocked.conf', fatal_warnings=True) m = mock.mock_open(read_data=DEPRECATED_CONF) with mock.patch('builtins.open', m): self.assertEqual(1, validator._validate(self.conf)) @mock.patch('oslo_config.validator.load_opt_data') def test_missing(self, mock_lod): mock_lod.return_value = OPT_DATA self.conf_fixture.config(opt_data='mocked.yaml', input_file='mocked.conf') m = mock.mock_open(read_data=INVALID_CONF) with mock.patch('builtins.open', m): self.assertEqual(1, validator._validate(self.conf)) @mock.patch('oslo_config.validator.load_opt_data') def test_missing_group(self, mock_lod): mock_lod.return_value = OPT_DATA self.conf_fixture.config(opt_data='mocked.yaml', input_file='mocked.conf') m = mock.mock_open(read_data=MISSING_GROUP_CONF) with mock.patch('builtins.open', m): self.assertEqual(1, validator._validate(self.conf)) @mock.patch('oslo_config.validator.load_opt_data') def test_exclude_groups(self, mock_lod): mock_lod.return_value = OPT_DATA self.conf_fixture.config(opt_data='mocked.yaml', input_file='mocked.conf', exclude_group=['oo']) m = mock.mock_open(read_data=MISSING_GROUP_CONF) with mock.patch('builtins.open', m): self.assertEqual(0, validator._validate(self.conf)) @mock.patch('oslo_config.validator.load_opt_data') def test_check_defaults(self, mock_lod): mock_lod.return_value = OPT_DATA self.conf_fixture.config(opt_data='mocked.yaml', input_file='mocked.conf', check_defaults=True) m = mock.mock_open(read_data=VALID_CONF) with mock.patch('builtins.open', m): self.assertEqual(0, validator._validate(self.conf)) def test_invalid_options(self): self.assertRaises(RuntimeError, validator._validate, self.conf) ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1724318217.1949978 oslo.config-9.6.0/oslo_config/tests/testmods/0000775000175000017500000000000000000000000021276 5ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/tests/testmods/__init__.py0000664000175000017500000000000000000000000023375 0ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/tests/testmods/bar_foo_opt.py0000664000175000017500000000127600000000000024147 0ustar00zuulzuul00000000000000# 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') ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/tests/testmods/baz_qux_opt.py0000664000175000017500000000130700000000000024204 0ustar00zuulzuul00000000000000# 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') ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/tests/testmods/blaa_opt.py0000664000175000017500000000126200000000000023432 0ustar00zuulzuul00000000000000# 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')) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/tests/testmods/fbaar_baa_opt.py0000664000175000017500000000131300000000000024406 0ustar00zuulzuul00000000000000# 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') ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/tests/testmods/fbar_foo_opt.py0000664000175000017500000000131200000000000024304 0ustar00zuulzuul00000000000000# 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') ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/tests/testmods/fblaa_opt.py0000664000175000017500000000127600000000000023605 0ustar00zuulzuul00000000000000# 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) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/types.py0000664000175000017500000007750200000000000020023 0ustar00zuulzuul00000000000000# 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 collections import operator import re import warnings import abc from debtcollector import removals import netaddr import rfc3986 class ConfigType(metaclass=abc.ABCMeta): def __init__(self, type_name='unknown type'): self.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: if isinstance(sample_default, str): default_str = sample_default else: default_str = self._formatter(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, str): warnings.warn('converting \'%s\' to a string' % str_val) str_val = str(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 either valid values or tuples of valid values with descriptions. 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. .. versionchanged:: 5.2 The *choices* parameter will now accept a sequence of tuples, where each tuple is of form (*choice*, *description*) """ 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 if choices is not None: if not all(isinstance(choice, tuple) for choice in choices): choices = [(choice, None) for choice in choices] self.choices = collections.OrderedDict(choices) else: self.choices = None self.lower_case_choices = None if self.choices is not None and self.ignore_case: self.lower_case_choices = [c.lower() for c in self.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, str): 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 len(value) == 1 or 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.keys())) 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 is not None: details.append("choices={!r}".format(list(self.choices.keys()))) 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 (self.quotes == other.quotes) and (self.regex == other.regex) and (set([x for x in self.choices or []]) == set([x for x in other.choices or []]) if self.choices and other.choices else self.choices == other.choices) ) 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 num_type: the type of number used for casting (i.e int, float) :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. :param choices: Optional sequence of either valid values or tuples of valid values with descriptions. .. versionadded:: 3.14 .. versionchanged:: 5.2 The *choices* parameter will now accept a sequence of tuples, where each tuple is of form (*choice*, *description*) """ 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') if choices is not None: if not all(isinstance(choice, tuple) for choice in choices): choices = [(choice, None) for choice in choices] self.choices = collections.OrderedDict(choices) else: self.choices = None invalid_choices = [c for c in self.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.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}".format(list(self.choices.keys()))) 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([x for x in self.choices or []]) == set([x for x in other.choices or []]) 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 either valid values or tuples of valid values with descriptions. .. 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. .. versionchanged:: 5.2 The *choices* parameter will now accept a sequence of tuples, where each tuple is of form (*choice*, *description*) """ 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 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. .. 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 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 either valid values or tuples of valid values with descriptions. .. versionadded:: 3.16 .. versionchanged:: 5.2 The *choices* parameter will now accept a sequence of tuples, where each tuple is of form (*choice*, *description*) """ 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. Should be an instance of ``ConfigType``. :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, tuple)): return list(map(self.item_type, value)) s = value.strip().rstrip(',') 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: values = s.split(',') else: values = [] if not values: return [] result = [] 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): fmtstr = '[{}]' if self.bounds else '{}' if isinstance(value, str): return fmtstr.format(value) if isinstance(value, list): value = [ self.item_type._formatter(v) for v in value ] return fmtstr.format(','.join(value)) return fmtstr.format(self.item_type._formatter(value)) class Range(ConfigType): """Range type. Represents a range of integers. A range is identified by an integer both sides of a '-' character. Negatives are allowed. A single number is also a valid range. :param min: Optional check that lower bound is greater than or equal to min. :param max: Optional check that upper bound is less than or equal to max. :param inclusive: True if the right bound is to be included in the range. :param type_name: Type name to be used in the sample config file. .. versionadded:: 3.18 """ def __init__(self, min=None, max=None, inclusive=True, type_name='range value'): super(Range, self).__init__(type_name) self.min = min self.max = max self.inclusive = inclusive def __call__(self, value): value = str(value) num = "0|-?[1-9][0-9]*" m = re.match("^(%s)(?:-(%s))?$" % (num, num), value) if not m: raise ValueError('Invalid Range: %s' % value) left = int(m.group(1)) right = int(left if m.group(2) is None else m.group(2)) if left < right: left = Integer(min=self.min)(left) right = Integer(max=self.max)(right) step = 1 else: left = Integer(max=self.max)(left) right = Integer(min=self.min)(right) step = -1 if self.inclusive: right += step return range(left, right, step) def __eq__(self, other): return ( (self.__class__ == other.__class__) and (self.min == other.min) and (self.max == other.max) ) def _formatter(self, value): return 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. .. versionchanged:: 9.5 Added *key_value_separator* parameter. """ def __init__(self, value_type=None, bounds=False, type_name='dict value', key_value_separator=':'): 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') if key_value_separator == ',': raise TypeError('key_value_separator should not be \',\'') self.value_type = value_type self.bounds = bounds self.key_value_separator = key_value_separator 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(self.key_value_separator, 1) if len(key_value) < 2: raise ValueError( 'Value should be NAME%sVALUE pairs ' 'separated by ","' % self.key_value_separator) 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): """Host domain name 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. """ HOSTNAME_REGEX = '(?!-)[A-Z0-9-]{1,63}(? 253: raise ValueError("hostname is greater than 253 characters: %s" % value) if value.endswith("."): value = value[:-1] allowed = re.compile(regex, re.IGNORECASE) if not re.search('[a-zA-Z-]', value.split(".")[-1]): raise ValueError('%s contains no non-numeric characters in the ' 'top-level domain part of the host name and is ' 'invalid' % value) 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 HostAddress(ConfigType): """Host Address type. Represents both valid IP addresses and valid host domain names including fully qualified domain names. Performs strict checks for both IP addresses and valid hostnames, matching the opt values to the respective types as per RFC1912. :param version: defines which version should be explicitly checked (4 or 6) in case of an IP address :param type_name: Type name to be used in the sample config file. """ def __init__(self, version=None, type_name='host address value'): """Check for valid version in case an IP address is provided """ super(HostAddress, self).__init__(type_name=type_name) self.ip_address = IPAddress(version, type_name) self.hostname = Hostname('localhost') def __call__(self, value): """Checks if is a valid IP/hostname. If not a valid IP, makes sure it is not a mistyped IP before performing checks for it as a hostname. """ try: value = self.ip_address(value) except ValueError: try: value = self.hostname(value) except ValueError: raise ValueError( "%s is not a valid host address" % (value,)) return value def __repr__(self): return 'HostAddress' def __eq__(self, other): return self.__class__ == other.__class__ def _formatter(self, value): return value class HostDomain(HostAddress): """Host Domain type. Like HostAddress with the support of _ character. :param version: defines which version should be explicitly checked (4 or 6) in case of an IP address :param type_name: Type name to be used in the sample config file. """ # DOMAIN_REGEX is HOSTNAME_REGEX with the _ character added DOMAIN_REGEX = '(?!-)[A-Z0-9-_]{1,63}(? self.max_length: raise ValueError("Value '%s' exceeds maximum length %d" % (value, self.max_length)) # NOTE(dhellmann): self.value is deprecated, and we don't want # to trigger a deprecation warning ourselves so we modify # self._value directly. self._value = value return value @removals.removed_property def value(self): return self._value @value.setter def value(self, newval): self._value = newval @value.deleter def value(self): del self._value def __repr__(self): return 'URI' def __eq__(self, other): to_compare = ['__class__', 'max_length', 'schemes'] unset = object() my_values = tuple( getattr(self, name, unset) for name in to_compare ) other_values = tuple( getattr(other, name, unset) for name in to_compare ) return my_values == other_values def _formatter(self, value): return value ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/validator.py0000664000175000017500000002055700000000000020642 0ustar00zuulzuul00000000000000#!/usr/bin/env python3 # Copyright 2018 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. """Configuration Validator Uses the sample config generator configuration file to retrieve a list of all the available options in a project, then compares it to what is configured in the provided file. If there are any options set that are not defined in the project then it returns those errors. """ import importlib.metadata import logging import re import sys import yaml from oslo_config import cfg from oslo_config import generator VALIDATE_DEFAULTS_EXCLUSIONS = [ '.*_ur(i|l)', '.*connection', 'password', 'username', 'my_ip', 'host(name)?', 'glance_api_servers', 'osapi_volume_listen', 'osapi_compute_listen', ] _validator_opts = [ cfg.MultiStrOpt( 'namespace', help='Option namespace under "oslo.config.opts" in which to query ' 'for options.'), cfg.StrOpt( 'input-file', required=True, help='Config file to validate.'), cfg.StrOpt( 'opt-data', help='Path to a YAML file containing definitions of options, as ' 'output by the config generator.'), cfg.BoolOpt( 'check-defaults', default=False, help='Report differences between the sample values and current ' 'values.'), cfg.ListOpt( 'exclude-options', default=VALIDATE_DEFAULTS_EXCLUSIONS, help='Exclude options matching these patterns when comparing ' 'the current and sample configurations.'), cfg.BoolOpt( 'fatal-warnings', default=False, help='Report failure if any warnings are found.'), cfg.MultiStrOpt( 'exclude-group', default=[], help='Groups that should not be validated if they are present in the ' 'specified input-file. This may be necessary for dynamically ' 'named groups which do not appear in the sample config data.'), ] KNOWN_BAD_GROUPS = ['keystone_authtoken'] 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(_validator_opts) def _validate_deprecated_opt(group, option, opt_data): if group not in opt_data['deprecated_options']: return False name_data = [o['name'] for o in opt_data['deprecated_options'][group]] name_data += [o.get('dest') for o in opt_data['deprecated_options'][group]] return option in name_data def _validate_defaults(sections, opt_data, conf): """Compares the current and sample configuration and reports differences :param section: ConfigParser instance :param opt_data: machine readable data from the generator instance :param conf: ConfigOpts instance :returns: boolean wether or not warnings were reported """ warnings = False # Generating regex objects from ListOpt exclusion_regexes = [] for pattern in conf.exclude_options: exclusion_regexes.append(re.compile(pattern)) for group, opts in opt_data['options'].items(): if group in conf.exclude_group: continue if group not in sections: logging.warning( 'Group %s from the sample config is not defined in ' 'input-file', group) continue for opt in opts['opts']: # We need to convert the defaults into a list to find # intersections. defaults are only a list if they can # be defined multiple times, but configparser only # returns list if not isinstance(opt['default'], list): defaults = [str(opt['default'])] else: defaults = opt['default'] # Apparently, there's multiple naming conventions for # options, 'name' is mostly with hyphens, and 'dest' # is represented with underscores. opt_names = set([opt['name'], opt.get('dest')]) if not opt_names.intersection(sections[group]): continue try: value = sections[group][opt['name']] keyname = opt['name'] except KeyError: value = sections[group][opt.get('dest')] keyname = opt.get('dest') if any(rex.fullmatch(keyname) for rex in exclusion_regexes): logging.info( '%s/%s Ignoring option because it is part of the excluded ' 'patterns. This can be changed with the --exclude-options ' 'argument', group, keyname) continue if len(value) > 1: logging.info( '%s/%s defined %s times', group, keyname, len(value)) if not opt['default']: logging.warning( '%s/%s sample value is empty but input-file has %s', group, keyname, ", ".join(value)) warnings = True elif not frozenset(defaults).intersection(value): logging.warning( '%s/%s sample value %s is not in %s', group, keyname, defaults, value) warnings = True return warnings def _validate_opt(group, option, opt_data): if group not in opt_data['options']: return False name_data = [o['name'] for o in opt_data['options'][group]['opts']] name_data += [o.get('dest') for o in opt_data['options'][group]['opts']] return option in name_data def load_opt_data(conf): with open(conf.opt_data) as f: return yaml.safe_load(f) def _validate(conf): conf.register_opts(_validator_opts) if conf.namespace: groups = generator._get_groups(generator._list_opts(conf.namespace)) opt_data = generator._generate_machine_readable_data(groups, conf) elif conf.opt_data: opt_data = load_opt_data(conf) else: # TODO(bnemec): Implement this logic with group? raise RuntimeError('Neither namespace nor opt-data provided.') sections = {} parser = cfg.ConfigParser(conf.input_file, sections) parser.parse() warnings = False errors = False if conf.check_defaults: warnings = _validate_defaults(sections, opt_data, conf) for section, options in sections.items(): if section in conf.exclude_group: continue for option in options: if _validate_deprecated_opt(section, option, opt_data): logging.warning('Deprecated opt %s/%s found', section, option) warnings = True elif not _validate_opt(section, option, opt_data): if section in KNOWN_BAD_GROUPS: logging.info('Ignoring missing option "%s" from group ' '"%s" because the group is known to have ' 'incomplete sample config data and thus ' 'cannot be validated properly.', option, section) continue logging.error('%s/%s is not part of the sample config', section, option) errors = True if errors or (warnings and conf.fatal_warnings): return 1 return 0 def main(): """The main function of oslo-config-validator.""" version = importlib.metadata.version('oslo.config') logging.basicConfig(level=logging.INFO) conf = cfg.ConfigOpts() _register_cli_opts(conf) try: conf(sys.argv[1:], version=version) except cfg.RequiredOptError: conf.print_help() if not sys.argv[1:]: raise SystemExit(1) raise return _validate(conf) if __name__ == '__main__': sys.exit(main()) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/oslo_config/version.py0000664000175000017500000000122300000000000020327 0ustar00zuulzuul00000000000000# 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') ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1724318217.1789963 oslo.config-9.6.0/releasenotes/0000775000175000017500000000000000000000000016462 5ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1724318217.1989982 oslo.config-9.6.0/releasenotes/notes/0000775000175000017500000000000000000000000017612 5ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/notes/.placeholder0000664000175000017500000000000000000000000022063 0ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/notes/add-HostAddressOpt-6e7e2afe7c7863cb.yaml0000664000175000017500000000115200000000000026522 0ustar00zuulzuul00000000000000--- prelude: > Configuration option type of ``HostAddressOpt`` added to accept and validate both IP addresses and hostnames. Please refer to the ``features`` section for more information. features: - Configuration option type of ``HostAddressOpt`` added to accept both valid IP address (IPv4 and IPv6) values as well as hostnames. The ``HostAddressOpt`` will accept both IPv4 and IPv6 addresses and ensure that strict checks are performed on the IP versions. This option type will also accept and accurately validate hostnames ensuring that no invalid IP passes as a valid hostname. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/notes/add-HostDomain-implement-rfc1033-c985a3054f824e9d.yaml0000664000175000017500000000044400000000000030513 0ustar00zuulzuul00000000000000--- features: - | Add ``HostDomain`` to handle address with underscore. Underscore are allowed in domain by RFC1033 [1][2][3]. - [1] https://www.ietf.org/rfc/rfc1912.txt - [2] https://www.ietf.org/rfc/rfc1033.txt - [3] http://domainkeys.sourceforge.net/underscore.html ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/notes/add-default-config-dirs-03340ff6689afe94.yaml0000664000175000017500000000267700000000000027237 0ustar00zuulzuul00000000000000--- features: - | Add default config-dir paths if no --config-dir switches are given on the command line. This is similar to the default config-file handling oslo.config already supports. If no --config-dir switches are given, oslo.config searches now in a couple of directories (depending on the given project name) for config file snippets. Non-existing directories are simply skipped. The directories, if no project name is given, are: * ~/${prog}.conf.d/ * /etc/${prog}.conf.d/ Only the first directory is used if that is available. If a project is given, the directories searched is a bit more complicated. 2 directories are searched, first search is for the project related dir: * ~/.${project}/${project}.conf.d/ * ~/${project}.conf.d/ * /etc/${project}/${project}.conf.d/ * /etc/${project}.conf.d/ Then for the program name related configs, the following directories are searched: * ~/.${project}/${prog}.conf.d/ * ~/${prog}.conf.d/ * /etc/${project}/${prog}.conf.d/ * /etc/${prog}.conf.d/ other: - Adding some default config-dirs makes it possible to use config dir snippets also in wsgi environments (like Apache) where it is not easily possible to pass command line parameters to a wsgi app. upgrade: - Similar to 'default_config_files', 'default_config_dirs' is no longer an allowed config key. If that key is used, a ValueError() will be raised. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/notes/add-float-min-max-b1a2e16301c8435c.yaml0000664000175000017500000000010500000000000026006 0ustar00zuulzuul00000000000000--- features: - Added minimum and maximum value limits to FloatOpt.././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/notes/add-missing-config-option-9ee1992eea750200.yaml0000664000175000017500000000025600000000000027607 0ustar00zuulzuul00000000000000--- fixes: - | Adding the missing ``HostDomain`` config option. Previously this available type couldn't been imported because no related config option was defined. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/notes/add-port_type-8704295c6a56265d.yaml0000664000175000017500000000031100000000000025252 0ustar00zuulzuul00000000000000--- 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]. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/notes/add-reno-71dc832ce29b962f.yaml0000664000175000017500000000007300000000000024414 0ustar00zuulzuul00000000000000--- other: - Start using reno for managing release notes.././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/notes/add_choices_parameter_for_IntOpt_class-1b5e42c475c08f08.yaml0000664000175000017500000000014100000000000032443 0ustar00zuulzuul00000000000000--- features: - | Add choice parameter for IntOpt class for conformity with types.Integer. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/notes/add_option_for_shell_completion-47b1b47d41a490e8.yaml0000664000175000017500000000022000000000000031232 0ustar00zuulzuul00000000000000--- features: - | Add ``--shell_completion`` argument to generate shell completion file content. Currently bash and zsh are supported ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/notes/config-from-environment-3feba7b4cc747d2b.yaml0000664000175000017500000000132400000000000027701 0ustar00zuulzuul00000000000000--- features: - | Support for accessing configuration data in environment variables via the environment backend driver, enabled by default. The environment is checked after command line options, but before configuration files. Environment variables are checked for any configuration data. The variable names take the form: * A prefix of ``OS_`` * The group name, uppercased * Separated from the option name by a `__` (double underscore) * Followed by the name For an option that looks like this in the usual INI format:: [placement_database] connection = sqlite:/// the corresponding environment variable would be ``OS_PLACEMENT_DATABASE__CONNECTION``. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/notes/config-validator-256817f2183994fd.yaml0000664000175000017500000000070400000000000025745 0ustar00zuulzuul00000000000000--- features: - | A validator for config files is now available. When run against a config file, it will report an error for any options present that aren't defined in the service and will report a warning for any deprecated options in the file. In order to discover the available options for a service, it can either use the sample config generator configuration file or a machine-readable sample config generated elsewhere. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/notes/dict-key-value-separator-127ce36ba4d9245f.yaml0000664000175000017500000000025000000000000027531 0ustar00zuulzuul00000000000000--- features: - | ``Dict`` type now supports the ``key_value_separator`` parameter. This parameter allows using a customized separator between key and value. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/notes/drop-python27-support-87f1b4089d4cc78b.yaml0000664000175000017500000000017700000000000027102 0ustar00zuulzuul00000000000000--- upgrade: - | Support for Python 2.7 has been dropped. The minimum version of Python now supported is Python 3.6. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/notes/machine-readable-sample-config-e8f8ba43ababcf99.yaml0000664000175000017500000000044400000000000031106 0ustar00zuulzuul00000000000000--- features: - | The sample config generator can now generate machine-readable formats of the sample config data. This can be consumed by deployment tools to automatically generate configuration files that contain all of the information in the traditional sample configs. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/notes/positional-arguments-are-required-22ddca72e6f523bf.yaml0000664000175000017500000000073100000000000031617 0ustar00zuulzuul00000000000000--- upgrade: - | Positional options are now required by default, to match argparse's default behavior. To revert this behavior (and maintain optional positional arguments), you need to explicitly specify ``positional=True, required=False`` as part of the options definition. fixes: - | On the command line, oslo.config now returns command usage information from argparse (instead of dumping a backtrace) when required arguments are missing. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/notes/show-deprecated-reason-361a8eb31e05c97e.yaml0000664000175000017500000000026300000000000027257 0ustar00zuulzuul00000000000000--- features: - | When a deprecated option is used, the message warning that the option is deprecated now includes the reason for the deprecation, if there is one given.././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/notes/support-choice-descriptions-8b2d0c14fbd16b2a.yaml0000664000175000017500000000113600000000000030504 0ustar00zuulzuul00000000000000--- features: - | String, Number, Integer, Float and Port now support value-description tuples in the interable provided for the *choice* parameter. Support for value-only definitions is retained. - | StringOpt and PortOpt now support a value-description tuples in the iterable provided for the *choice* parameter. Support for value-only definitions is retained. - | *oslo-config-generator* and the Sphinx extension will now output descriptions for option choices where provided. This will impact tooling that relies on the *yaml* and *json* output of the former. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/notes/support-fatal-deprecations-ea0513aa58a395ca.yaml0000664000175000017500000000112200000000000030232 0ustar00zuulzuul00000000000000--- features: - | oslo.config now supports the fatal-deprecations option from oslo.log. This behavior is only enabled if oslo.log is installed, but oslo.log is still not a hard requirement to avoid a circular dependency. upgrade: - | Because support for fatal-deprecations was added in this release, users who have fatal-deprecations enabled and have deprecated config opts in use (which previously was not a problem because oslo.config didn't respect the fatal-deprecations option) will need to resolve that before upgrading or services may fail to start. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/notes/validator-check-defaults-e7b596a2fde781a8.yaml0000664000175000017500000000047200000000000027660 0ustar00zuulzuul00000000000000--- features: - | Add a ``--check-defaults`` flag to ``oslo-config-validator``. When set, ``oslo-config-validator`` will compare the ``input-file`` with the sample generated configuration and flag any discrepancies. It also obeys the standard ``--exclude-group`` and ``--fatal-warning`` options. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/notes/validator-exclude-groups-ad2f046522a3407e.yaml0000664000175000017500000000054100000000000027550 0ustar00zuulzuul00000000000000--- features: - | The ``oslo-config-validator`` tool now has a new option, ``--exclude-group``, that allows deployers to ignore certain groups that might not appear in the sample config data, perhaps because the name of the group depends on other configuration values. This way the validator can still be used on the known groups. ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1724318217.2029986 oslo.config-9.6.0/releasenotes/source/0000775000175000017500000000000000000000000017762 5ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/2023.1.rst0000664000175000017500000000020200000000000021233 0ustar00zuulzuul00000000000000=========================== 2023.1 Series Release Notes =========================== .. release-notes:: :branch: stable/2023.1 ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/2023.2.rst0000664000175000017500000000020200000000000021234 0ustar00zuulzuul00000000000000=========================== 2023.2 Series Release Notes =========================== .. release-notes:: :branch: stable/2023.2 ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/2024.1.rst0000664000175000017500000000020200000000000021234 0ustar00zuulzuul00000000000000=========================== 2024.1 Series Release Notes =========================== .. release-notes:: :branch: stable/2024.1 ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1724318217.2029986 oslo.config-9.6.0/releasenotes/source/_static/0000775000175000017500000000000000000000000021410 5ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/_static/.placeholder0000664000175000017500000000000000000000000023661 0ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1724318217.2029986 oslo.config-9.6.0/releasenotes/source/_templates/0000775000175000017500000000000000000000000022117 5ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/_templates/.placeholder0000664000175000017500000000000000000000000024370 0ustar00zuulzuul00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/conf.py0000664000175000017500000001733400000000000021271 0ustar00zuulzuul00000000000000# -*- 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 = [ 'openstackdocstheme', 'reno.sphinxext', ] # openstackdocstheme options openstackdocs_repo_name = 'openstack/oslo.config' openstackdocs_bug_project = 'oslo.config' openstackdocs_bug_tag = '' # 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. copyright = '2016, oslo.config Developers' # Release notes do not need a version in the title, they span # multiple versions. # The full version, including alpha/beta/rc tags. release = '' # The short X.Y version. version = '' # 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 = 'native' # 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 = 'openstackdocs' # 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 # " v 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 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 # -- 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', 'oslo.config Release Notes Documentation', ['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', 'oslo.config Release Notes Documentation', '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 # -- Options for Internationalization output ------------------------------ locale_dirs = ['locale/'] ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/index.rst0000664000175000017500000000045300000000000021625 0ustar00zuulzuul00000000000000=========================== oslo.config Release Notes =========================== .. toctree:: :maxdepth: 1 unreleased 2024.1 2023.2 2023.1 zed yoga xena wallaby victoria ussuri train stein rocky queens pike ocata newton mitaka liberty ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/liberty.rst0000664000175000017500000000021500000000000022164 0ustar00zuulzuul00000000000000============================ Liberty Series Release Notes ============================ .. release-notes:: :branch: origin/stable/liberty ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/mitaka.rst0000664000175000017500000000023200000000000021757 0ustar00zuulzuul00000000000000=================================== Mitaka Series Release Notes =================================== .. release-notes:: :branch: origin/stable/mitaka ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/newton.rst0000664000175000017500000000021600000000000022025 0ustar00zuulzuul00000000000000============================= Newton Series Release Notes ============================= .. release-notes:: :branch: origin/stable/newton ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/ocata.rst0000664000175000017500000000023000000000000021576 0ustar00zuulzuul00000000000000=================================== Ocata Series Release Notes =================================== .. release-notes:: :branch: origin/stable/ocata ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/pike.rst0000664000175000017500000000021700000000000021444 0ustar00zuulzuul00000000000000=================================== Pike Series Release Notes =================================== .. release-notes:: :branch: stable/pike ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/queens.rst0000664000175000017500000000022300000000000022011 0ustar00zuulzuul00000000000000=================================== Queens Series Release Notes =================================== .. release-notes:: :branch: stable/queens ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/rocky.rst0000664000175000017500000000022100000000000021636 0ustar00zuulzuul00000000000000=================================== Rocky Series Release Notes =================================== .. release-notes:: :branch: stable/rocky ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/stein.rst0000664000175000017500000000022100000000000021631 0ustar00zuulzuul00000000000000=================================== Stein Series Release Notes =================================== .. release-notes:: :branch: stable/stein ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/train.rst0000664000175000017500000000017600000000000021635 0ustar00zuulzuul00000000000000========================== Train Series Release Notes ========================== .. release-notes:: :branch: stable/train ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/unreleased.rst0000664000175000017500000000014400000000000022642 0ustar00zuulzuul00000000000000========================== Unreleased Release Notes ========================== .. release-notes:: ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/ussuri.rst0000664000175000017500000000020200000000000022040 0ustar00zuulzuul00000000000000=========================== Ussuri Series Release Notes =========================== .. release-notes:: :branch: stable/ussuri ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/victoria.rst0000664000175000017500000000022000000000000022326 0ustar00zuulzuul00000000000000============================= Victoria Series Release Notes ============================= .. release-notes:: :branch: unmaintained/victoria ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/wallaby.rst0000664000175000017500000000021400000000000022144 0ustar00zuulzuul00000000000000============================ Wallaby Series Release Notes ============================ .. release-notes:: :branch: unmaintained/wallaby ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/xena.rst0000664000175000017500000000020000000000000021437 0ustar00zuulzuul00000000000000========================= Xena Series Release Notes ========================= .. release-notes:: :branch: unmaintained/xena ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/yoga.rst0000664000175000017500000000020000000000000021443 0ustar00zuulzuul00000000000000========================= Yoga Series Release Notes ========================= .. release-notes:: :branch: unmaintained/yoga ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/releasenotes/source/zed.rst0000664000175000017500000000017400000000000021300 0ustar00zuulzuul00000000000000======================== Zed Series Release Notes ======================== .. release-notes:: :branch: unmaintained/zed ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/requirements.txt0000664000175000017500000000030200000000000017250 0ustar00zuulzuul00000000000000debtcollector>=1.2.0 # Apache-2.0 netaddr>=0.7.18 # BSD stevedore>=1.20.0 # Apache-2.0 oslo.i18n>=3.15.3 # Apache-2.0 rfc3986>=1.2.0 # Apache-2.0 PyYAML>=5.1 # MIT requests>=2.18.0 # Apache-2.0 ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1724318217.2029986 oslo.config-9.6.0/setup.cfg0000664000175000017500000000274500000000000015622 0ustar00zuulzuul00000000000000[metadata] name = oslo.config summary = Oslo Configuration API description_file = README.rst author = OpenStack author_email = openstack-discuss@lists.openstack.org home_page = https://docs.openstack.org/oslo.config/latest/ python_requires = >=3.8 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 :: 3 Programming Language :: Python :: 3.8 Programming Language :: Python :: 3.9 Programming Language :: Python :: 3.10 Programming Language :: Python :: 3.11 Programming Language :: Python :: 3 :: Only Programming Language :: Python :: Implementation :: CPython [extras] rst-generator = rst2txt>=1.1.0 # BSD sphinx>=1.8.0 # BSD [files] packages = oslo_config [entry_points] console_scripts = oslo-config-generator = oslo_config.generator:main oslo-config-validator = oslo_config.validator:main oslo.config.opts = oslo.config = oslo_config._list_opts:list_opts oslo.config.driver = remote_file = oslo_config.sources._uri:URIConfigurationSourceDriver [mypy] show_column_numbers = true show_error_context = true ignore_missing_imports = true follow_imports = skip incremental = true check_untyped_defs = true warn_unused_ignores = true [mypy-oslo_config.tests.*] ignore_errors = true [egg_info] tag_build = tag_date = 0 ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/setup.py0000664000175000017500000000127100000000000015504 0ustar00zuulzuul00000000000000# 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. import setuptools setuptools.setup( setup_requires=['pbr>=2.0.0'], pbr=True) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/test-requirements.txt0000664000175000017500000000106600000000000020235 0ustar00zuulzuul00000000000000hacking>=6.1.0,<6.2.0 # Apache-2.0 fixtures>=3.0.0 # Apache-2.0/BSD testscenarios>=0.4 # Apache-2.0/BSD stestr>=2.1.0 # Apache-2.0 testtools>=2.2.0 # MIT oslotest>=3.2.0 # Apache-2.0 mypy>=0.720 # MIT # oslo.log can't be a runtime dep because it would cause a circular dependency, # but we can optionally make use of it so we want to have it installed in our # test environment. oslo.log>=3.36.0 # Apache-2.0 coverage>=4.0 # Apache-2.0 requests_mock>=1.5.0 # Apache-2.0 # Bandit security code scanner bandit>=1.7.0,<1.8.0 # Apache-2.0 pre-commit>=2.6.0 # MIT ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1724318187.0 oslo.config-9.6.0/tox.ini0000664000175000017500000000327100000000000015307 0ustar00zuulzuul00000000000000[tox] minversion = 3.2.0 envlist = py3,pep8 [testenv] allowlist_externals = find setenv = OS_STDOUT_CAPTURE={env:OS_STDOUT_CAPTURE:1} OS_STDERR_CAPTURE={env:OS_STDERR_CAPTURE:1} OS_TEST_TIMEOUT={env:OS_TEST_TIMEOUT:60} deps = -c{env:TOX_CONSTRAINTS_FILE:https://releases.openstack.org/constraints/upper/master} -r{toxinidir}/test-requirements.txt -r{toxinidir}/requirements.txt -r{toxinidir}/doc/requirements.txt commands = find . -type f -name "*.pyc" -delete stestr run --suppress-attachments {posargs} stestr slowest [testenv:pep8] commands = pre-commit run -a # TODO(stephenfin): Add mypy here once it's passing {[testenv:bandit]commands} [testenv:cover] setenv = PYTHON=coverage run --source oslo_config --parallel-mode commands = coverage erase {[testenv]commands} coverage combine coverage html -d cover coverage xml -o cover/coverage.xml coverage report --show-missing [testenv:venv] commands = {posargs} [testenv:docs] allowlist_externals = rm deps = -c{env:TOX_CONSTRAINTS_FILE:https://releases.openstack.org/constraints/upper/master} -r{toxinidir}/doc/requirements.txt commands = rm -fr doc/build sphinx-build -W --keep-going -b html -j auto doc/source doc/build/html {posargs} [testenv:mypy] commands = mypy oslo_config [testenv:bandit] commands = bandit -r oslo_config -x tests -n5 [testenv:releasenotes] allowlist_externals = rm deps = {[testenv:docs]deps} commands = rm -rf releasenotes/build sphinx-build -W --keep-going -b html -j auto releasenotes/source releasenotes/build/html {posargs} [hacking] import_exceptions = oslo_config._i18n [flake8] show-source = True enable-extensions = H203,H106 exclude = .tox,dist,doc,*.egg,build