osloutils(1)
| OSLOUTILS(1) | oslo.utils | OSLOUTILS(1) |
NAME
osloutils - oslo.utils 8.2.0
The Oslo utils library provides support for common utility type functions, such as encoding, exception handling, string manipulation, and time handling.
CONTENTS
Installation
At the command line:
$ pip install oslo.utils
Or, if you have virtualenvwrapper installed:
$ mkvirtualenv oslo.utils $ pip install oslo.utils
Using oslo.utils
Usage
To use oslo.utils in a project, import the individual module you need. For example:
from oslo_utils import strutils
slug = strutils.to_slug('input value')
timeutils
Using a stopwatch (as a context manager)
>>> from oslo_utils import timeutils >>> import time >>> >>> def slow_routine(delay): ... def i_am_slow(): ... time.sleep(delay) ... return i_am_slow ... >>> >>> half_sec_func = slow_routine(0.5) >>> with timeutils.StopWatch() as w: ... half_sec_func() ... >>> print(w.elapsed()) 0.500243999995
Manually using a stopwatch
>>> from oslo_utils import timeutils >>> import time >>> w = timeutils.StopWatch() >>> w.start() <oslo_utils.timeutils.StopWatch object at 0x2b85a0ab7590> >>> time.sleep(0.1) >>> time.sleep(0.1) >>> time.sleep(0.1) >>> time.sleep(0.1) >>> w.stop() <oslo_utils.timeutils.StopWatch object at 0x2b85a0ab7590> >>> w.elapsed() 13.96467600017786
Tracking durations with a stopwatch
>>> from oslo_utils import timeutils >>> w = timeutils.StopWatch(duration=10) >>> w.start() <oslo_utils.timeutils.StopWatch object at 0x2b85a7940a10> >>> w.elapsed() 2.023942000232637 >>> w.leftover() 4.648160999640822 >>> w.leftover() 3.5522090001031756 >>> w.leftover() 3.0481000002473593 >>> w.leftover() 2.1918740002438426 >>> w.leftover() 1.6966530000790954 >>> w.leftover() 1.1202940000221133 >>> w.leftover() 0.0 >>> w.expired() True
Tracking and splitting with a stopwatch
>>> from oslo_utils import timeutils >>> w = timeutils.StopWatch() >>> w.start() <oslo_utils.timeutils.StopWatch object at 0x2ba75c12b050> >>> w.split() Split(elapsed=3.02423300035, length=3.02423300035) >>> w.split() Split(elapsed=6.44820600003, length=3.42397299968) >>> w.split() Split(elapsed=7.9678720003, length=1.51966600027) >>> w.splits (Split(elapsed=3.02423300035, length=3.02423300035), Split(elapsed=6.44820600003, length=3.42397299968), Split(elapsed=7.9678720003, length=1.51966600027)) >>> w.stop() <oslo_utils.timeutils.StopWatch object at 0x2ba75c12b050> >>> w.elapsed() 16.799759999848902 >>> w.splits (Split(elapsed=3.02423300035, length=3.02423300035), Split(elapsed=6.44820600003, length=3.42397299968), Split(elapsed=7.9678720003, length=1.51966600027))
CHANGES
8.2.0
- Skip installation to speed up pep8
- Run pyupgrade to clean up Python 2 syntaxes
8.1.0
- Adjust warning message for eventlet support deprecation
- Fix wrong warning category
- deprecate the eventletutils module
- Remove unused private constants
- Deprecate redundant exception_to_unicode function
- Drop compatibility code for Python 3.8
- Add image checker to imageutils
8.0.0
- Adjust the version in descriptions
- Add a release note about the new crypt utilities
- Fix get_my_ipv4 in absence of IPv4
- Fix get_my_ipv6 in absence of IPv6
- Add utility to replace crypt.mksalt
- reno: Update master for unmaintained/2023.1
- Add utility to replace crypt.crypt
- is_valid_ipv4: Enable strict check by default
- Add note about requirements lower bounds
7.4.0
- Vendor VersionPredicate
- Declare Python 3.12 support
- Allow multiple format hits if explicitly handled
- Abort the stream early if no match
- Fix compatibility with netaddr 1.1.0
- Drop dependency on netifaces
- Use context manager for socket
- Clarify GPT structure offset
- Deprecate redundant constant_time_compare function
- Deprecate redundant md5 method
- Remove Python 3.8 support
- Squelch irrelevant format complaints
- Add qemu-img to bindep
- Avoid detecting FAT VBR as an MBR
- Update master for stable/2024.2
- Add LUKSv1 inspector
7.3.0
- Add a function to trim scope out of ipv6 addr
- Add release note for format_inspector
- Add stream-based detection
- Add GPT/MBR inspector
- Fix qcow2 feature flag checks (for the future)
- Refactor some things for oslo
- Support VMDK sparse with footer
- Make VMDKInspector support non-sparse formats
- Add region-complete signaling for inspectors
- Reinstate VMDK safety check coverage
- Modularize image safety checks
- Imported Translations from Zanata
- Make FileInspector an ABC
- Test cover target in CI
- Import format_inspector from nova
- Make imageutils a directory
- Fix coverage target
7.2.0
- reno: Update master for unmaintained/zed
- Remove old excludes
- Update master for stable/2024.1
- reno: Update master for unmaintained/xena
- reno: Update master for unmaintained/wallaby
- reno: Update master for unmaintained/victoria
7.1.0
- add <range-in> spec DSL operator
- netutils: Explicitly require INET_ATON
- reno: Update master for unmaintained/yoga
- pre-commit: Integrate bandit
- pre-commit: Bump versions
- Bump hacking
7.0.0
- Update python classifier in setup.cfg
- Python-3.12: do not use datetime.datetime.utcnow()
- Require pytz only in Python < 3.9
6.3.0
- versionutil: Remove trailing alpha/beta/rc suffix
- Fix missing PyYAML dependency
- Mask chapsecret
- Imported Translations from Zanata
- Update master for stable/2023.2
6.2.1
- •
- Imported Translations from Zanata
6.2.0
- Replace deprecated assertAlmostEquals method
- strutils: update string_to_bytes
- Add netutils.get_my_ipv6()
- Bump bandit
- Fix compatibility with Python 3.8
- Remove reference to monotonic on PyPI
- Revert "Moves supported python runtimes from version 3.8 to 3.10"
- Moves supported python runtimes from version 3.8 to 3.10
- Update units for current SI prefixes
- Implement zoneinfo support to drop dependency to pytz
- Use the new openstack-python3-jobs template
- Remove strict from is_same_callback()
- Update master for stable/2023.1
6.1.0
- [imageutils] Fix __str__ for QemuImgInfo
- Imported Translations from Zanata
- Add Python3 antelope unit tests
- Update master for stable/zed
6.0.1
- Imported Translations from Zanata
- Imported Translations from Zanata
6.0.0
5.0.0
- bindep: Use Python 3 devel packages
- Remove deprecated helpers from oslo_utils.timeutils
- Remove oslo_utils.fnmatch
- requirements: Remove explicit pbr dependency
- strutils: Defer import of pyparsing
- Drop python3.6/3.7 support in testing runtime
- Remove unnecessary unicode prefixes
4.13.0
- fix strutils password regex
- Add Python3 zed unit tests
- Update master for stable/yoga
4.12.2
- •
- Fix formatting of release list
4.12.1
- Fix regex used to mask password
- Update python testing classifier
- Use LOG.warning instead of deprecated LOG.warn
4.12.0
- •
- Add backing file format to the output
4.11.0
- Add Python3 yoga unit tests
- Update master for stable/xena
- QemuImgInfo: Fix inconsistent value format of encrypted
- setup.cfg: Replace dashes with underscores
- Changed minversion in tox to 3.18.0
4.10.0
- •
- Modify UUID sentinel to support keystone-like UUIDs
4.9.2
- QemuImgInfo: Skip deprecation warning when output is not passed
- Drop warnings enable filter
4.9.1
- Remove references to 'sys.version_info'
- Deprecate the fnmatch module
- Deprecate the human format on QemuImgInfo
- Ussuri+ is python3 only and update python to python3
- Dropping lower constraints testing
4.9.0
- Drop lower-constraints
- Move flake8 as a pre-commit local target
- Add Python3 xena unit tests
- Update master for stable/wallaby
4.8.0
- Add a ``strict`` flag allowing users to restrict validation of IPv4 format
- Address nits from six removal patch
- Switch to collections.abc.*
- Use TOX_CONSTRAINTS_FILE
- Dropping lower constraints testing
- strutils: Stop masking encryption_key_id
- Use TOX_CONSTRAINTS_FILE
- Use py3 as the default runtime for tox
- Remove all usage of six library
4.7.0
- Add function to encapsule md5 for FIPS systems
- Add Python3 wallaby unit tests
- Update master for stable/victoria
- Adding pre-commit
4.6.0
- [goal] Migrate testing to ubuntu focal
- Add Python3 victoria unit tests
- Fix is_same_callback() testing for python3.8
4.5.0
- Add util methods for checking json and yaml formatted file
- Bump bandit version
4.4.0
4.3.0
- versionutils: switch from pkg_resources to packaging
- New method in netutils: get_mac_addr_by_ipv6
4.2.2
- •
- Fix uuidsentinel to follow getattr protocol
4.2.1
- Release greenthread when computing checksum
- Fix pygments style
4.2.0
- Stop to use the __future__ module
- Fix hacking min version to 3.0.1
- Switch to newer openstackdocstheme and reno versions
- Remove the unused coding style modules
- Remove translation sections from setup.cfg
- Remove monotonic usage
- Align contributing doc with oslo's policy
- Imported Translations from Zanata
- Add release notes links to doc index
- Update master for stable/ussuri
4.1.1
- Update hacking for Python3
- Use unittest.mock instead of third party mock
- tox: Use upper-constraints for docs jobs
4.1.0
- Add test to check scientific notation on disk virtual size
- tests: Convert remaining tests to mock
- Flatten test case
- Fix regex to correctly recognize scientific notation with QemuImgInfo
- imageutils: Report format specific details when using JSON output format
4.0.1
- remove outdated header
- Remove universal wheel configuration
- reword releasenote for py27 support dropping
4.0.0
- [ussuri][goal] Drop python 2.7 support and testing
- tox: Trivial cleanup
- Ignore releasenote cache within git untracked files
- trivial: Move setup code into setUp helper
3.42.1
- Verify the sanitize keys are lowered
- Fix invalid escapes in regular expression strings
- Ignore the .eggs directory
- Make mask_dict_password case insensitive and add new patterns
- Bump the openstackdocstheme extension to 1.20
3.42.0
- Make mask_password case insensitive, and add new patterns
- tox: Keeping going with docs
- Switch to Ussuri jobs
- Update the constraints url
- Support "qemu-img info" virtual size in QEMU 4.1 and later
- Update master for stable/train
3.41.1
- Add digestmod when using hmac
- Add Python 3 Train unit tests
- Cap Bandit below 1.6.0 and update Sphinx requirement
- Replace git.openstack.org URLs with opendev.org URLs
3.41.0
- OpenDev Migration Patch
- Dropping the py35 testing
- Update master for stable/stein
3.40.3
- add python 3.7 unit test job
- Update hacking version
- Mask encryption_key_id
- eventletutils: Optimise EventletEvent.clear()
3.40.2
- •
- Avoid double-setting event
3.40.1
3.40.0
3.39.1
- Avoid calling eventlet.event.Event.reset()
- Use template for lower-constraints
3.39.0
- Fix race condition in eventletutils Event
- Don't use monotonic on Python >=3.3
- Update mailinglist from dev to discuss
- Support non-dict mappings in mask_dict_password
3.38.0
- Expose eventlet Event wrapper class
- Clean up .gitignore references to personal tools
3.37.1
- •
- Fix exception raise at rpdb session
3.37.0
- Fix docstring formatting nit in uuidsentinel
- UUID sentinel
- Remove moxstubout usage
- add lib-forward-testing-python3 test job
- add python 3.6 unit test job
- import zuul job settings from project-config
- Update reno for stable/rocky
- Remove extra copy.deepcopy
3.36.4
- Handle non-string keys appropriately
- Switch to stestr
- Add release notes link to README
3.36.3
- fix tox python3 overrides
- Fix exception with secretutils
3.36.2
- Add private_key to the list of sanitized keys
- Remove stale pip-missing-reqs tox test
- Capitalize Oslo
3.36.1
- Trivial: Update pypi url to new url
- set default python to python3
- eventletutils: Fix behavior discrepency when reusing Events
- Fix project name in user docs
- add lower-constraints job
- Clean old output before new doc builds
- Remove sphinx settings from setup.cfg
- Add bindep.txt file to prevent fallback to generic list
- Updated from global requirements
3.36.0
- Add -W for document build
- Imported Translations from Zanata
- Update links in README
- Imported Translations from Zanata
- Fix breaking unit tests due to iso8601 changes
- Document specs_matcher.py functions
- Clean imports in code
- Update reno for stable/queens
- Updated from global requirements
- Replace 'assertFalse(a in b)' with 'assertNotIn(a, b)'
- Updated from global requirements
- Updated from global requirements
3.35.0
3.34.0
- Updated from global requirements
- Cleanup test-requirements
- improve docstring for last_bytes()
- Add method last_bytes in fileutils
- Follow the new PTI for document build
- Add missing information in docstring of validate_integer
3.33.0
3.32.0
- Avoid tox_install.sh for constraints support
- Updated from global requirements
- Remove setting of version/release from releasenotes
- Updated from global requirements
3.31.0
- Add method to compute a file's checksum to fileutils
- Imported Translations from Zanata
- Add method validate_integer
3.30.0
- Updated from global requirements
- Use six.binary_type to point to the right type
- Add a mixed mode parser to string_to_bytes
- Updated from global requirements
- Fix some reST field lists in docstrings
3.29.0
- Imported Translations from Zanata
- Add method to escape ipv6 ip addresses
- Updated from global requirements
- Update reno for stable/pike
- Updated from global requirements
- Prevent deprecation error messages from pkg_resources
3.28.0
- •
- Update URLs in documents according to document migration
3.27.0
- rearrange existing documentation to fit the new standard layout
- switch from oslosphinx to openstackdocstheme
- Updated from global requirements
- Updated from global requirements
3.26.0
- Updated from global requirements
- Updated from global requirements
- Updated from global requirements
- Updated from global requirements
3.25.1
- Updated from global requirements
- Remove split conversion to tuple
- Updated from global requirements
3.25.0
- •
- Remove log translations
3.24.0
- Use Sphinx 1.5 warning-is-error
- Updated from global requirements
- Adding a check of string type for hmacs
3.23.0
- Updated from global requirements
- [Fix gate]Update test requirement
- Add missing documentation for secretutils
- Updated from global requirements
- Updated from global requirements
- Update reno for stable/ocata
3.22.0
- Remove references to Python 3.4
- Added the token 'encrypted_key' to mask_password list
- Add Constraints support
3.21.0
- •
- Allow 'get_all_class_names' to pass kwargs
3.20.0
- Add toggle 'dashed' to 'generate_uuid' function
- Show team and repo badges on README
3.19.0
- Updated from global requirements
- Improve eventlet check when selecting Event backend
- Updated from global requirements
3.18.0
- Add option to not truncate built-ins
- Create dictutils and add 'flatten_dict_to_keypairs'
- Updated from global requirements
- Add reno for release notes management
- Allow scoped ipv6 addresses
- Trivial fixes to the usage doc
- Add threading<->eventlet compatible Event
- Updated from global requirements
- [TrivialFix] Replace 'assertEqual(None, ...)' with 'assertIsNone(...)'
3.17.0
- Add method is_valid_mac
- Add __ne__ built-in function
- Make method import_versioned_module work
- Change assertTrue(isinstance()) by optimal assert
- doc: Fix docstring of method bool_from_string
- Change assertTrue(isinstance()) by optimal assert
- Add method is_valid_boolstr
- Add method is_valid_ipv6_cidr
- Updated from global requirements
- Updated from global requirements
- Add missing specs_matcher documentation
- Update homepage with developer documentation page
- Updated from global requirements
- Updated from global requirements
- Add utils for validating and splitting quotes
- Updated from global requirements
- Extend specs matcher to support ">" and "<"
- Remove discover from test-requirements
3.16.0
- Fix mask_dict_password for non string/dict type key in dict
- Restore <all-in> operator
- More unit tests for specs matcher
- Imported Translations from Zanata
- Add Python 3.5 classifier and venv
- Use an actual well defined parser for spec matching
- Remove unused LOG to keep code clean
- Updated from global requirements
3.15.0
- Add basic docstrings to stopwatch has_started/stopped methods
- Make mask_dict_password consistent with mask_password
- Updated from global requirements
- improve tests for mask_password and mask_dict_password
- Simplify boolean expression in executils.py
3.14.0
- Support json format output from qemu command
- Fix flake8 issues
- Use is_valid_ipv4 in get_ipv6_addr_by_EUI64
- Imported Translations from Zanata
3.13.0
- •
- Allow assigning "0" to port
3.12.0
- Updated from global requirements
- Fix method split_path's docstring 'versionadded'
- Updated from global requirements
- Updated from global requirements
- Avoid catching generic exception
- Remove method total_seconds in timeuitls
- Fix is_valid_cidr raises TypeError
3.11.0
- Trivial: ignore openstack/common in flake8 exclude list
- Move method split_path into oslo.utils
3.10.0
- Imported Translations from Zanata
- Updated from global requirements
- Move nova extra_specs_ops to oslo.utils
3.9.0
- Imported Translations from Zanata
- Provide single step check if eventlet is monkey_patched
- Add method is_valid_cidr to netutils
- Updated from global requirements
- Updated from global requirements
- Add importutils.import_any method
- Add excutils.exception_filter
- Explicitly exclude tests from bandit scan
- Add CHAPPASSWORD to list of sanitize keys
- Enable bandit in gate
- Updated from global requirements
3.7.0
- Add method check_string_length
- Add missing versionchanged for configdrive
3.6.0
3.5.0
- Updated from global requirements
- Imported Translations from Zanata
- Remove bandit.yaml in favor of defaults
- Updated from global requirements
- Narrow mock for getfilesystemencoding
- Update translation setup
- Revert "Use assertTrue/False instead of assertEqual(T/F)"
- Updated from global requirements
- Updated from global requirements
- Add excutils.save_and_reraise_exception force_reraise + capture
- Add encodeutils.to_utf8() function
- Create secretutils and include 'constant_time_compare' function
- Fix coverage
- Imported Translations from Zanata
- Updated from global requirements
3.4.0
- Updated from global requirements
- Use assertTrue/False instead of assertEqual(T/F)
- Add a mechanism to mask passwords in dictionaries
- Add "configdrive" to the list of keys used by mask_password()
- assertIsNone(val) instead of assertEqual(None,val)
3.3.0
- Fix DeprecationWarning when call method delta_seconds
- fix fnmatch.filter in non-posix system
- fix fileutils ut code random failure
- Add missing doc index for imageutils and fnmatch
- re-implement thread safe fnmatch
- Fix the bug of can't get the desired image info
3.2.0
- Revert "Move netifaces to extras"
- Remove Babel from requirements
3.1.0
- Remove duplicated profiles section from bandit.yaml
- Allow get_class_name to accept bound method and class method
- deprecate timeutils.total_seconds()
- Move imageutils from oslo-incubator to oslo.utils
- add comment explaining why we don't want extra values passed to mask_password
- networkutils: drop python 2.6 support
- Remove 'MANIFEST.in'
- Move netifaces to extras
3.0.0
- Add a bandit target to tox.ini
- Updated from global requirements
- Remove python 2.6 classifier
- Fix wrong bug tracking link
- Remove python 2.6 and cleanup tox.ini
- Refactor Port number validation
- Add useful 'time_it' decorator
2.8.0
- Fix get_class_name() on Python 3
- Added ICMP 'type' and 'code' checking capability to 'netutils' module
- Updated from global requirements
- Imported Translations from Zanata
- comment in write_to_tempfile
- Use versionadded and versionchanged in doc
2.7.0
- Expose function signature fetching function
- Allow 'forever_retry_uncaught_exceptions' to take in different defaults
- Write document for each unit of oslo_utils.utils
- Fix usage of "deprecated" markup in docstrings
- Just use 'exception_to_unicode' to handle exception to string
- Add 'secret' to sensitive keys
2.6.0
- Fix coverage configuration and execution
- Use a stopwatch in 'forever_retry_uncaught_exceptions'
- No need for Oslo Incubator Sync
- Make forever_retry_uncaught_exceptions handle its own failures
- Ensure stopwatch __enter__, __exit__ are in docs
- Add some timeutils stop watch examples
- Imported Translations from Zanata
- Move 'history' -> release notes section
- Fix bad acting classes and 'is_bound_method' check
- Change ignore-errors to ignore_errors
- Updated from global requirements
- If 'bool_from_string' provided a boolean just return it
- Imported Translations from Zanata
- only capture the ImportError when importing
- Add 'token' to list of fields to be santized by mask_password
2.5.0
- Updated from global requirements
- Imported Translations from Transifex
- Updated from global requirements
- Updated from global requirements
2.4.0
2.3.0
- Updated from global requirements
- Update docstring on stop watch to reflect monotonic lib. usage
- Updated from global requirements
- flake8 - remove unused rules
- Bump monotonic to 0.3 to remove exception catching on import
- Provide a common exception caused by base class
- Imported Translations from Transifex
- Allow access to reflection 'get_members'
- Updated from global requirements
2.2.0
- Imported Translations from Transifex
- Updated from global requirements
2.1.0
- Imported Translations from Transifex
- Updated from global requirements
- Adding checking around the monotonic import
2.0.0
- Updated from global requirements
- Updated from global requirements
- Add oslo.config to test requirements
- Remove oslo namespace package
- Updated from global requirements
1.9.0
- Updated from global requirements
- versionutils: add version convert helper methods
- Imported Translations from Transifex
- Add write_to_tempfile back to fileutils
- Use monotonic library to avoid finding monotonic time function
- Fix exception_to_unicode() for oslo_i18n Message
1.8.0
- Add fileutils to oslo_utils
- Updated from global requirements
- Add tox target to find missing requirements
1.7.0
- Updated from global requirements
- Updated from global requirements
- Switch badges from 'pypip.in' to 'shields.io'
- timeutils: fix newer/older comparison with TZ aware datetime
- Replace parse_strtime with parse_isotime in older/newer
1.6.0
- Add pytz to requirements
- Deprecate strtime
- Imported Translations from Transifex
- timeutils: utcnow() can return a value with a timezone
- Add 'raise_with_cause' chaining helper to excutils
- timeutils: deprecate isotime()
- timeutils: make marshall timezone aware
- Advertise support for Python3.4 / Remove support for Python 3.3
- Updated from global requirements
- Add exception_to_unicode() function
- Remove run_cross_tests.sh
- Imported Translations from Transifex
- Move versionutils into place and remove deprecation tools
- Denote monotonic import ordering + usage
1.5.0
- Add liberty release name to versionutils
- Expose opts entry point for version_utils
- Switch from oslo.config to oslo_config
- Remove oslo.log code and clean up versionutils API
- Remove code that moved to oslo.i18n
- Enhance versionutils.deprecated to work with classes
- Add Kilo release name to versionutils
- Allow deprecated decorator to specify no plan for removal
- Uncap library requirements for liberty
- Add JUNO as a target to versionutils module
- Add missing reflection + uuidutils docs
- timeutils: avoid passing leap second to datetime
- Add pypi download + version badges
- Cleanup README.rst and setup.cfg
- pep8: fixed multiple violations
- Use oslotest instead of common test module
- Use hacking import_exceptions for gettextutils._
- fixed typos
- Fix violations of H302:import only modules
- Adds decorator to deprecate functions and methods
- Remove vim header
- Add `versionutils` for version compatibility checks
- Update hacking setting
- Updated from global requirements
- Imported Translations from Transifex
- Clean up TestIsIPv6Enabled
- Fix test_netutils: stop patches
- Add a new string to the list of masked patterns
- Provide common `fetch_current_thread_functor` function
- Imported Translations from Transifex
1.4.0
- Add a stopwatch + split for duration(s)
- Allow providing a logger to save_and_reraise_exception
- Updated from global requirements
- Utility API to generate EUI-64 IPv6 address
1.3.0
- Add a eventlet utils helper module
- Add microsecond support to iso8601_from_timestamp
- add dependency warning to requirements.txt
- Updated from global requirements
- Update Oslo imports to remove namespace package
- Add TimeFixture
- Add microsecond support to timeutils.utcnow_ts()
- Make setup.cfg packages include oslo.utils
1.2.1
- Return LOCALHOST if no default interface
- fix link to bug tracker in README
1.2.0
- Improve performance of strutils.mask_password
- Move files out of the namespace package
- Add method is_valid_port in netutils
- Support non-lowercase uuids in is_uuid_like
- Add 'secret_uuid' in _SANITIZE_KEYS for strutils
- Imported Translations from Transifex
- Workflow documentation is now in infra-manual
1.1.0
- Improve error reporting in _get_my_ipv4_address()
- Add get_my_ip()
- Updated from global requirements
- Add 'auth_password' in _SANITIZE_KEYS for strutils
- Updated from global requirements
- Activate pep8 check that _ is imported
- Add uuidutils to oslo.utils
- Add pbr to installation requirements
- Updated from global requirements
- Add is_int_like() function
- Hide auth_token and new_pass
- Imported Translations from Transifex
- Add history/changelog to docs
- Imported Translations from Transifex
- Support building wheels (PEP-427)
- Imported Translations from Transifex
- Improve docstrings for IP verification functions
- Imported Translations from Transifex
- Add ip address validation
- Fix how it appears we need to use mock_anything to avoid 'self' errors
- Updated from global requirements
- Move over a reflection module that taskflow uses
- Make safe_encode func case-insensitive
- Enable mask_password to handle byte code strings
- Updated from global requirements
1.0.0
- Imported Translations from Transifex
- Add the ability to extract the query params from a urlsplit
- Work toward Python 3.4 support and testing
- warn against sorting requirements
- Remove unused dependency on oslo.config
- Updated from global requirements
- Just use int(BOOL) to convert to 1 or 0
- Re-enable _import* hidden methods in import_utils
0.2.0
- Make strutils.mask_password more secure
- New public API for mask_password ported from incubator
- Imported Translations from Transifex
0.1.1
- Make return type from urlsplit private
- Add API docs and clean up other docs
- Make the i18n integration module private
- Cleaning up index.rst file
- export only try_import in __all__
- Switch to oslo.i18n and remove any dependency on oslo-incubator
- Move units into oslo.utils
- Switch to standard python logging
- Setup for translation
- Split strutils into 2 different modules
- Rename network_utils into netutils
- get pep8 working
- Get the tox tests working
- exported from oslo-incubator by graduate.sh
- Fixed a new pep8 error and a small typo
- Set pbr 'warnerrors' option for doc build
- fixed typos found by RETF rules
- Use moxstubout and mockpatch from oslotest
- Remove ValueError when accessing sys.modules
- Enable configuring tcp keepalive
- Avoid raising index error when no host
- Remove str() from LOG.* and exceptions
- Remove import workaround of SplitResult
- Use oslotest instead of common test module
- Partial fix of test_strutils.py on Python 3
- Fix safe_encode(): return bytes on Python 3
- urlsplit issues with IPv6 addresses in python26
- save_and_reraise_exception: make logging respect the reraise parameter
- strutils: Allow safe_{encode,decode} to take bytes as input
- Fix import order in test_excutils
- Update oslo log messages with translation domains
- Implements SI/IEC unit system conversion to bytes
- Add basic Python 3 tests
- py3kcompat: remove
- Deleted duplicated method in cliutils
- strutils bool_from_string, allow specified default
- Utilizes assertIsNone and assertIsNotNone
- Fix spelling errors in comments
- Use hacking import_exceptions for gettextutils._
- Correct invalid docstrings
- Fix a bug in safe_encode where it returns a bytes object in py3
- Fix typo in parameter documentation (timeutils)
- Avoid TypeError in is_older_than, is_newer_than
- Remove vim header
- Use py3kcompat urlutils functions instead of urlparse
- Add helper method total_seconds in timeutils.py
- Do not name variables as builtins
- Use six.text_type instead of unicode function in tests
- Fix typos in oslo
- Adjust import order according to PEP8 imports rule
- python3: use six.text_types for unicode()
- Don't shadow str
- Fix timeutils.set_override_time not defaulting to current wall time
- Fix misused assertTrue in unit tests
- Optimize timeutils.utcnow_ts()
- excutils: replace unicode by six.u
- excutils: use six.reraise to re-raise
- Replace using tests.utils part2
- Bump hacking to 0.7.0
- Replace using tests.utils with openstack.common.test
- BaseException.message is deprecated since Python 2.6
- Enable H302 hacking check
- Add conditional exception reraise
- python3: Add python3 compatibility
- Make AMQP based RPC consumer threads more robust
- Add network_utils.urlsplit
- Remove useless logging in networks_utils
- python3: Fix traceback while running python3
- Refactors to_bytes
- Add slugify to strutils
- Enable hacking H404 test
- Added common code into fileutils and strutils
- Enable hacking H402 test
- Enable hacking H403 test
- Enable hacking H201 test
- Add 't', 'y', and `strict` to `bool_from_string`
- Handle ints passed to `boolean_from_string`
- Removes leading zeros on integers in test_timeutils
- Convert unicode strings for python3 portability
- Do not import openstack.common.log in strutils
- Improve Python 3.x compatibility
- Replaces standard logging with common logging
- Removes unused imports in the tests module
- Fix Copyright Headers - Rename LLC to Foundation
- support ISO 8601 micro-second precision
- Decode / Encode string utils for openstack
- timeutils: considers that now is soon
- Replace direct use of testtools BaseTestCase
- Use testtools as test base class
- Import timeutils.is_soon from keystoneclient
- UTC ISO8601 from timestamp
- Implement importutils.try_import
- Use basestring instead of str for type check
- Make time comparison functions accept strings
- Fix timezone handling in timeutils tests
- Rename utils.py to strutils.py
- Provide i18n to those messages without _()
- Make project pyflakes clean
- Account for tasks duration in LoopingCall delay
- Convenience wrapper for datetime.timedelta.total_seconds()
- Added is_newer_than function
- Extracted parse_host_port into network_utils
- Normalize_time() always return naive object
- Use pep8 v1.3.3
- Don't trap then re-raise ImportError
- Fix spelling typos
- Support for marshalling datetime while preserving microseconds
- Remove unused imports
- fix bug lp:1019348,update openstack-common to support pep8 1.3
- Use strtime() in to_primitive() for datetime objs
- Improve exception from importutils.import_class()
- Update common code to support pep 1.3. bug 1014216
- add import_object_ns function
- add more realistic unit tests for importutils
- Fix utcnow_ts to return UTC timestamp
- Add nova time util functions to timeutils
- Replace datetime.utcnow with timeutils.utcnow
- Remove common.exception from common.importutils
- Add save_and_reraise_exception()
- Update exception from importutils.import_class()
- Change behavior in utils.import_object()
- Create openstack.common.timeutils
- Initial skeleton project
API Reference
dictutils
- oslo_utils.dictutils.flatten_dict_to_keypairs(d, separator=':')
- Generator that produces sequence of keypairs for nested dictionaries.
- Parameters
- d -- dictionaries which may be nested
- separator -- symbol between names
encodeutils
- oslo_utils.encodeutils.exception_to_unicode(exc)
- Get the message of an exception as a Unicode string.
On Python 3, the exception message is always a Unicode string.
Added in version 1.6.
- oslo_utils.encodeutils.safe_decode(text, incoming=None, errors='strict')
- Decodes incoming text/bytes string using incoming if they're not
- already unicode.
- Parameters
- incoming -- Text's current encoding
- errors -- Errors handling policy. See here for valid values http://docs.python.org/2/library/codecs.html
- Returns
- text or a unicode incoming encoded representation of it.
- Raises
- TypeError -- If text is not an instance of str
- oslo_utils.encodeutils.safe_encode(text, incoming=None, encoding='utf-8', errors='strict')
- Encodes incoming text/bytes string using encoding.
If incoming is not specified, text is expected to be encoded with current python's default encoding. (sys.getdefaultencoding)
- Parameters
- incoming -- Text's current encoding
- encoding -- Expected encoding for text (Default UTF-8)
- errors -- Errors handling policy. See here for valid values http://docs.python.org/2/library/codecs.html
- Returns
- text or a bytestring encoding encoded representation of it.
- Raises
- TypeError -- If text is not an instance of str
See also to_utf8() function which is simpler and don't depend on the locale encoding.
- oslo_utils.encodeutils.to_utf8(text)
- Encode Unicode to UTF-8, return bytes unchanged.
Raise TypeError if text is not a bytes string or a Unicode string.
Added in version 3.5.
eventletutils
Eventlet utils helper module.
Added in version 1.3.
- class oslo_utils.eventletutils.EventletEvent(*args, **kwargs)
- A class that provides consistent eventlet/threading Event API.
This wraps the eventlet.event.Event class to have the same API as the standard threading.Event object.
- oslo_utils.eventletutils.fetch_current_thread_functor()
- Get the current thread.
If eventlet is used to monkey-patch the threading module, return the current eventlet greenthread. Otherwise, return the current Python thread.
Added in version 1.5.
- oslo_utils.eventletutils.is_monkey_patched(module)
- Determines safely is eventlet patching for module enabled or not :param module: String, module name :return Bool: True if module is patched, False otherwise
- oslo_utils.eventletutils.warn_eventlet_not_patched(expected_patched_modules=None, what='this library')
- Warns if eventlet is being used without patching provided modules.
- Parameters
- expected_patched_modules (list/tuple/iterable) -- list of modules to check to ensure that they are patched (and to warn if they are not); these names should correspond to the names passed into the eventlet monkey_patch() routine. If not provided then all the modules that could be patched are checked. The currently valid selection is one or multiple of ['MySQLdb', '__builtin__', 'all', 'os', 'psycopg', 'select', 'socket', 'thread', 'time'] (where 'all' has an inherent special meaning).
- what (string) -- string to merge into the warnings message to identify what is being checked (used in forming the emitted warnings message).
excutils
Exception related utilities.
- exception oslo_utils.excutils.CausedByException(message, cause=None)
- Base class for exceptions which have associated causes.
NOTE(harlowja): in later versions of python we can likely remove the need to have a cause here as PY3+ have implemented PEP 3134 which handles chaining in a much more elegant manner.
- Parameters
- message -- the exception message, typically some string that is useful for consumers to view when debugging or analyzing failures.
- cause -- the cause of the exception being raised, when provided this should itself be an exception instance, this is useful for creating a chain of exceptions for versions of python where this is not yet implemented/supported natively.
Added in version 2.4.
- pformat(indent=2, indent_text=' ', show_root_class=False)
- Pretty formats a caused exception + any connected causes.
- class oslo_utils.excutils.exception_filter(should_ignore_ex)
- A context manager that prevents some exceptions from being raised.
Use this class as a decorator for a function that returns whether a given exception should be ignored, in cases where complex logic beyond subclass matching is required. e.g.
>>> @exception_filter >>> def ignore_test_assertions(ex): ... return isinstance(ex, AssertionError) and 'test' in str(ex)The filter matching function can then be used as a context manager:
>>> with ignore_test_assertions: ... assert False, 'This is a test'or called directly:
>>> try: ... assert False, 'This is a test' ... except Exception as ex: ... ignore_test_assertions(ex)Any non-matching exception will be re-raised. When the filter is used as a context manager, the traceback for re-raised exceptions is always preserved. When the filter is called as a function, the traceback is preserved provided that no other exceptions have been raised in the intervening time. The context manager method is preferred for this reason except in cases where the ignored exception affects control flow.
- oslo_utils.excutils.forever_retry_uncaught_exceptions(*args, **kwargs)
- Decorates provided function with infinite retry behavior.
The function retry delay is always one second unless keyword argument retry_delay is passed that defines a value different than 1.0 (less than zero values are automatically changed to be 0.0).
If repeated exceptions with the same message occur, logging will only output/get triggered for those equivalent messages every 60.0 seconds, this can be altered by keyword argument same_log_delay to be a value different than 60.0 seconds (exceptions that change the message are always logged no matter what this delay is set to). As in the retry_delay case if this is less than zero, it will be automatically changed to be 0.0.
- oslo_utils.excutils.raise_with_cause(exc_cls, message, *args, **kwargs)
- Helper to raise + chain exceptions (when able) and associate a
cause.
NOTE(harlowja): Since in py3.x exceptions can be chained (due to PEP 3134) we should try to raise the desired exception with the given cause (or extract a cause from the current stack if able) so that the exception formats nicely in old and new versions of python. Since py2.x does not support exception chaining (or formatting) the exception class provided should take a cause keyword argument (which it may discard if it wants) to its constructor which can then be inspected/retained on py2.x to get similar information as would be automatically included/obtainable in py3.x.
- Parameters
- exc_cls -- the exception class to raise (typically one derived from CausedByException or equivalent).
- message -- the text/str message that will be passed to the exceptions constructor as its first positional argument.
- args -- any additional positional arguments to pass to the exceptions constructor.
- kwargs -- any additional keyword arguments to pass to the exceptions constructor.
Added in version 1.6.
- class oslo_utils.excutils.save_and_reraise_exception(reraise=True, logger=None)
- Save current exception, run some code and then re-raise.
In some cases the exception context can be cleared, resulting in None being attempted to be re-raised after an exception handler is run. This can happen when eventlet switches greenthreads or when running an exception handler, code raises and catches an exception. In both cases the exception context will be cleared.
To work around this, we save the exception state, run handler code, and then re-raise the original exception. If another exception occurs, the saved exception is logged and the new exception is re-raised.
In some cases the caller may not want to re-raise the exception, and for those circumstances this context provides a reraise flag that can be used to suppress the exception. For example:
except Exception:
with save_and_reraise_exception() as ctxt:
decide_if_need_reraise()
if not should_be_reraised:
ctxt.reraise = False
If another exception occurs and reraise flag is False, the saved exception will not be logged.
If the caller wants to raise new exception during exception handling he/she sets reraise to False initially with an ability to set it back to True if needed:
except Exception:
with save_and_reraise_exception(reraise=False) as ctxt:
[if statements to determine whether to raise a new exception]
# Not raising a new exception, so reraise
ctxt.reraise = True
Changed in version 1.4: Added logger optional parameter.
fileutils
File utilities.
Added in version 1.8.
- oslo_utils.fileutils.compute_file_checksum(path, read_chunksize=65536, algorithm='sha256')
- Compute checksum of a file's contents.
- Parameters
- path -- Path to the file
- read_chunksize -- Maximum number of bytes to be read from the file at once. Default is 65536 bytes or 64KB
- algorithm -- The hash algorithm name to use. For example, 'md5', 'sha256', 'sha512' and so on. Default is 'sha256'. Refer to hashlib.algorithms_available for available algorithms
- Returns
- Hex digest string of the checksum
Added in version 3.31.0.
- oslo_utils.fileutils.delete_if_exists(path, remove=<built-in function unlink>)
- Delete a file, but ignore file not found error.
- Parameters
- path -- File to delete
- remove -- Optional function to remove passed path
- oslo_utils.fileutils.ensure_tree(path, mode=511)
- Create a directory (and any ancestor directories required)
- Parameters
- path -- Directory to create
- mode -- Directory creation permissions
- oslo_utils.fileutils.is_json(file_path)
- Check if file is of json type or not.
This function try to load the input file using json.loads() and return False if ValueError otherwise True.
- Parameters
- file_path -- The file path to check
- Returns
- bool
- oslo_utils.fileutils.is_yaml(file_path)
- Check if file is of yaml type or not.
This function try to load the input file using yaml.safe_load() and return True if loadable. Because every json file can be loadable in yaml, so this function return False if file is loadable using json.loads() means it is json file.
- Parameters
- file_path -- The file path to check
- Returns
- bool
- oslo_utils.fileutils.last_bytes(path, num)
- Return num bytes from the end of the file and unread byte count.
Returns a tuple containing some content from the file and the number of bytes that appear in the file before the point at which reading started. The content will be at most num bytes, taken from the end of the file. If the file is smaller than num bytes the entire content of the file is returned.
- Parameters
- path -- The file path to read
- num -- The number of bytes to return
- Returns
- (data, unread_bytes)
- oslo_utils.fileutils.remove_path_on_error(path, remove=<function delete_if_exists>)
- Protect code that wants to operate on PATH atomically. Any exception will cause PATH to be removed.
- Parameters
- path -- File to work with
- remove -- Optional function to remove passed path
- oslo_utils.fileutils.write_to_tempfile(content, path=None, suffix='', prefix='tmp')
- Create a temporary file containing data.
Create a temporary file containing specified content, with a specified filename suffix and prefix. The tempfile will be created in a default location, or in the directory path, if it is not None. path and its parent directories will be created if they don't exist.
- Parameters
- content -- bytestring to write to the file
- path -- same as parameter 'dir' for mkstemp
- suffix -- same as parameter 'suffix' for mkstemp
- prefix -- same as parameter 'prefix' for mkstemp
For example: it can be used in database tests for creating configuration files.
Added in version 1.9.
fixture
Test fixtures.
Added in version 1.3.
- class oslo_utils.fixture.TimeFixture(override_time=None)
- A fixture for overriding the time returned by timeutils.utcnow().
- Parameters
- override_time -- datetime instance or list thereof. If not given, defaults to the current UTC time.
- advance_time_delta(timedelta)
- Advance overridden time using a datetime.timedelta.
- advance_time_seconds(seconds)
- Advance overridden time by seconds.
- setUp()
- Prepare the Fixture for use.
This should not be overridden. Concrete fixtures should implement _setUp. Overriding of setUp is still supported, just not recommended.
After setUp has completed, the fixture will have one or more attributes which can be used (these depend totally on the concrete subclass).
- Raises
- MultipleExceptions if _setUp fails. The last exception captured within the MultipleExceptions will be a SetupError exception.
- Returns
- None.
- Changed in 1.3
- The recommendation to override setUp has been reversed - before 1.3, setUp() should be overridden, now it should not be.
- Changed in 1.3.1
- BaseException is now caught, and only subclasses of Exception are wrapped in MultipleExceptions.
importutils
Import related utilities and helper functions.
- oslo_utils.importutils.import_any(module, *modules)
- Try to import a module from a list of modules.
- Parameters
- modules -- A list of modules to try and import
- Returns
- The first module found that can be imported
- Raises
- ImportError -- If no modules can be imported from list
Added in version 3.8.
- oslo_utils.importutils.import_class(import_str)
- Returns a class from a string including module and class.
Added in version 0.3.
- oslo_utils.importutils.import_module(import_str)
- Import a module.
Added in version 0.3.
- oslo_utils.importutils.import_object(import_str, *args, **kwargs)
- Import a class and return an instance of it.
Added in version 0.3.
- oslo_utils.importutils.import_object_ns(name_space, import_str, *args, **kwargs)
- Tries to import object from default namespace.
Imports a class and return an instance of it, first by trying to find the class in a default namespace, then failing back to a full path if not found in the default namespace.
Added in version 0.3.
Changed in version 2.6: Don't capture ImportError when instanciating the object, only when importing the object class.
- oslo_utils.importutils.import_versioned_module(module, version, submodule=None)
- Import a versioned module in format {module}.v{version][.{submodule}].
- Parameters
- module -- the module name.
- version -- the version number.
- submodule -- the submodule name.
- Raises
- ValueError -- For any invalid input.
Added in version 0.3.
Changed in version 3.17: Added module parameter.
- oslo_utils.importutils.try_import(import_str, default=None)
- Try to import a module and if it fails return default.
netutils
Network-related utilities and helper functions.
- oslo_utils.netutils.escape_ipv6(address)
- Escape an IP address in square brackets if IPv6
- Parameters
- address (string) -- address to optionaly escape
- Returns
- string
Added in version 3.29.0.
- oslo_utils.netutils.get_ipv6_addr_by_EUI64(prefix, mac)
- Calculate IPv6 address using EUI-64 specification.
This method calculates the IPv6 address using the EUI-64 addressing scheme as explained in rfc2373.
- Parameters
- prefix -- IPv6 prefix.
- mac -- IEEE 802 48-bit MAC address.
- Returns
- IPv6 address on success.
- Raises
- ValueError, TypeError -- For any invalid input.
Added in version 1.4.
- oslo_utils.netutils.get_mac_addr_by_ipv6(ipv6, dialect=<class 'netaddr.strategy.eui48.mac_unix_expanded'>)
- Extract MAC address from interface identifier based IPv6 address.
For example from link-local addresses (fe80::/10) generated from MAC.
- Parameters
- ipv6 -- An interface identifier (i.e. mostly MAC) based IPv6 address as a netaddr.IPAddress() object.
- dialect -- The netaddr dialect of the the object returned. Defaults to netaddr.mac_unix_expanded.
- Returns
- A MAC address as a netaddr.EUI() object.
See also: * https://tools.ietf.org/html/rfc4291#appendix-A * https://tools.ietf.org/html/rfc4291#section-2.5.6
Added in version 4.3.0.
- oslo_utils.netutils.get_my_ipv4()
- Returns the actual ipv4 of the local machine.
This code figures out what source address would be used if some traffic were to be sent out to some well known address on the Internet. In this case, IP from RFC5737 is used, but the specific address does not matter much. No traffic is actually sent.
Added in version 1.1.
Changed in version 1.2.1: Return '127.0.0.1' if there is no default interface.
- oslo_utils.netutils.get_my_ipv6()
- Returns the actual IPv6 address of the local machine.
This code figures out what source address would be used if some traffic were to be sent out to some well known address on the Internet. In this case, IPv6 from RFC3849 is used, but the specific address does not matter much. No traffic is actually sent.
Added in version 6.1: Return '::1' if there is no default interface.
- oslo_utils.netutils.get_noscope_ipv6(address)
- Take an IPv6 address and trim scope out if present.
- Parameters
- address (string) -- Value to change
- Returns
- string
- oslo_utils.netutils.is_ipv6_enabled()
- Check if IPv6 support is enabled on the platform.
This api will look into the proc entries of the platform to figure out the status of IPv6 support on the platform.
- Returns
- True if the platform has IPv6 support, False otherwise.
Added in version 1.4.
- oslo_utils.netutils.is_valid_cidr(address)
- Verify that address represents a valid CIDR address.
- Parameters
- address (string) -- Value to verify
- Returns
- bool
Added in version 3.8.
- oslo_utils.netutils.is_valid_icmp_code(code)
- Verify if ICMP code is valid.
- Parameters
- code -- ICMP code field can be valid integer or None
- Returns
- bool
ICMP code field can be either None or valid integer having a value of 0 up to and including 255.
- oslo_utils.netutils.is_valid_icmp_type(type)
- Verify if ICMP type is valid.
- Parameters
- type -- ICMP type field can only be a valid integer
- Returns
- bool
ICMP type field can be valid integer having a value of 0 up to and including 255.
- oslo_utils.netutils.is_valid_ip(address)
- Verify that address represents a valid IP address.
- Parameters
- address (string) -- Value to verify
- Returns
- bool
Added in version 1.1.
- oslo_utils.netutils.is_valid_ipv4(address, strict=True)
- Verify that address represents a valid IPv4 address.
- Parameters
- address (string) -- Value to verify
- strict -- flag allowing users to restrict validation to IP addresses in presentation format (a.b.c.d) as opposed to address format (a.b.c.d, a.b.c, a.b, a).
- Returns
- bool
Added in version 1.1.
Changed in version 4.8.0: Allow to restrict validation to IP addresses in presentation format (a.b.c.d) as opposed to address format (a.b.c.d, a.b.c, a.b, a).
- oslo_utils.netutils.is_valid_ipv6(address)
- Verify that address represents a valid IPv6 address.
- Parameters
- address (string) -- Value to verify
- Returns
- bool
Added in version 1.1.
- oslo_utils.netutils.is_valid_ipv6_cidr(address)
- Verify that address represents a valid IPv6 CIDR address.
- Parameters
- address (string) -- address to verify
- Returns
- true if address is valid, false otherwise
Added in version 3.17.
- oslo_utils.netutils.is_valid_mac(address)
- Verify the format of a MAC address.
Check if a MAC address is valid and contains six octets. Accepts colon-separated format only.
- Parameters
- address -- MAC address to be validated.
- Returns
- True if valid. False if not.
Added in version 3.17.
- oslo_utils.netutils.is_valid_port(port)
- Verify that port represents a valid port number.
Port can be valid integer having a value of 0 up to and including 65535.
Added in version 1.1.1.
- oslo_utils.netutils.parse_host_port(address, default_port=None)
- Interpret a string as a host:port pair.
An IPv6 address MUST be escaped if accompanied by a port, because otherwise ambiguity ensues: 2001:db8:85a3::8a2e:370:7334 means both [2001:db8:85a3::8a2e:370:7334] and [2001:db8:85a3::8a2e:370]:7334.
>>> parse_host_port('server01:80') ('server01', 80) >>> parse_host_port('server01') ('server01', None) >>> parse_host_port('server01', default_port=1234) ('server01', 1234) >>> parse_host_port('[::1]:80') ('::1', 80) >>> parse_host_port('[::1]') ('::1', None) >>> parse_host_port('[::1]', default_port=1234) ('::1', 1234) >>> parse_host_port('2001:db8:85a3::8a2e:370:7334', default_port=1234) ('2001:db8:85a3::8a2e:370:7334', 1234) >>> parse_host_port(None) (None, None)
- oslo_utils.netutils.set_tcp_keepalive(sock, tcp_keepalive=True, tcp_keepidle=None, tcp_keepalive_interval=None, tcp_keepalive_count=None)
- Set values for tcp keepalive parameters
This function configures tcp keepalive parameters if users wish to do so.
- Parameters
- tcp_keepalive -- Boolean, turn on or off tcp_keepalive. If users are not sure, this should be True, and default values will be used.
- tcp_keepidle -- time to wait before starting to send keepalive probes
- tcp_keepalive_interval -- time between successive probes, once the initial wait time is over
- tcp_keepalive_count -- number of probes to send before the connection is killed
- oslo_utils.netutils.urlsplit(url, scheme='', allow_fragments=True)
- Parse a URL using urlparse.urlsplit(), splitting query and fragments. This
function papers over Python issue9374 when needed.
The parameters are the same as urlparse.urlsplit.
reflection
Reflection module.
Added in version 1.1.
- oslo_utils.reflection.accepts_kwargs(function)
- Returns True if function accepts kwargs otherwise False.
- oslo_utils.reflection.get_all_class_names(obj, up_to=<class 'object'>, fully_qualified=True, truncate_builtins=True)
- Get class names of object parent classes.
Iterate over all class names object is instance or subclass of, in order of method resolution (mro). If up_to parameter is provided, only name of classes that are sublcasses to that class are returned.
- oslo_utils.reflection.get_callable_args(function, required_only=False)
- Get names of callable arguments.
Special arguments (like *args and **kwargs) are not included into output.
If required_only is True, optional arguments (with default values) are not included into output.
- oslo_utils.reflection.get_callable_name(function)
- Generate a name from callable.
Tries to do the best to guess fully qualified callable name.
- oslo_utils.reflection.get_class_name(obj, fully_qualified=True, truncate_builtins=True)
- Get class name for object.
If object is a type, returns name of the type. If object is a bound method or a class method, returns its self object's class name. If object is an instance of class, returns instance's class name. Else, name of the type of the object is returned. If fully_qualified is True, returns fully qualified name of the type. For builtin types, just name is returned. TypeError is raised if can't get class name from object.
- oslo_utils.reflection.get_member_names(obj, exclude_hidden=True)
- Get all the member names for a object.
- oslo_utils.reflection.get_members(obj, exclude_hidden=True)
- Yields the members of an object, filtering by hidden/not hidden.
Added in version 2.3.
- oslo_utils.reflection.get_method_self(method)
- Gets the self object attached to this method (or none).
- oslo_utils.reflection.is_bound_method(method)
- Returns if the given method is bound to an object.
- oslo_utils.reflection.is_same_callback(callback1, callback2)
- Returns if the two callbacks are the same.
- oslo_utils.reflection.is_subclass(obj, cls)
- Returns if the object is class and it is subclass of a given class.
secretutils
Secret utilities.
Added in version 3.5.
specs_matcher
- oslo_utils.specs_matcher.make_grammar()
- Creates the grammar to be used by a spec matcher.
The grammar created supports the following operations.
- Numerical values:
- = : equal to or greater than. This is equivalent to >= and is supported for legacy reasons
- != : Float/integer value not equal
- <= : Float/integer value less than or equal
- < : Float/integer value less than
- == : Float/integer value equal
- >= : Float/integer value greater than or equal
- > : Float/integer value greater
- String operations:
- s!= : Not equal
- s< : Less than
- s<= : Less than or equal
- s== : Equal
- s> : Greater than
- s>= : Greater than or equal
- Other operations:
- <all-in> : All items 'in' value
- <in> : Item 'in' value, like a substring in a string.
- <or> : Logical 'or'
- <range-in>: Range tester with customizable boundary conditions, tests
- whether value is in the range, boundary condition could be inclusve '[' or exclusive '('.
If no operator is specified the default is s== (string equality comparison)
- Example operations:
- ">= 60" Is the numerical value greater than or equal to 60
- "<or> spam <or> eggs" Does the value contain spam or eggs
- "s== 2.1.0" Is the string value equal to 2.1.0
- "<in> gcc" Is the string gcc contained in the value string
- "<all-in> aes mmx" Are both aes and mmx in the value
- "<range-in> [ 10 20 ]" float(value) >= 10 and float(value) <= 20
- "<range-in> ( 10 20 ]" float(value) > 10 and float(value) <= 20
- "<range-in> ( 10 20 )" float(value) > 10 and float(value) < 20
- Returns
- A pyparsing.MatchFirst object. See https://pythonhosted.org/pyparsing/ for details on pyparsing.
- oslo_utils.specs_matcher.match(cmp_value, spec)
- Match a given value to a given spec DSL.
This uses the grammar defined by make_grammar()
- Parameters
- cmp_value -- Value to be checked for match.
- spec -- The comparison specification string, for example ">= 70" or "s== string_value". See make_grammar() for examples of a specification string.
- Returns
- True if cmp_value is a match for spec. False otherwise.
strutils
System-level utilities and helper functions.
- oslo_utils.strutils.bool_from_string(subject, strict=False, default=False)
- Interpret a subject as a boolean.
A subject can be a boolean, a string or an integer. Boolean type value will be returned directly, otherwise the subject will be converted to a string. A case-insensitive match is performed such that strings matching 't','true', 'on', 'y', 'yes', or '1' are considered True and, when strict=False, anything else returns the value specified by 'default'.
Useful for JSON-decoded stuff and config file parsing.
If strict=True, unrecognized values, including None, will raise a ValueError which is useful when parsing values passed in from an API call. Strings yielding False are 'f', 'false', 'off', 'n', 'no', or '0'.
- oslo_utils.strutils.check_string_length(value, name=None, min_length=0, max_length=None)
- Check the length of specified string.
- Parameters
- value -- the value of the string
- name -- the name of the string
- min_length -- the min_length of the string
- max_length -- the max_length of the string
- Raises
- TypeError, ValueError -- For any invalid input.
Added in version 3.7.
- oslo_utils.strutils.int_from_bool_as_string(subject)
- Interpret a string as a boolean and return either 1 or 0.
Any string value in:
is interpreted as a boolean True.
Useful for JSON-decoded stuff and config file parsing
- oslo_utils.strutils.is_int_like(val)
- Check if a value looks like an integer with base 10.
- Parameters
- val (string) -- Value to verify
- Returns
- bool
Added in version 1.1.
- oslo_utils.strutils.is_valid_boolstr(value)
- Check if the provided string is a valid bool string or not.
- Parameters
- value (string) -- value to verify
- Returns
- true if value is boolean string, false otherwise
Added in version 3.17.
- oslo_utils.strutils.mask_dict_password(dictionary, secret='***')
- Replace password with secret in a dictionary recursively.
- Parameters
- dictionary -- The dictionary which includes secret information.
- secret -- value with which to replace secret information.
- Returns
- The dictionary with string substitutions.
A dictionary (which may contain nested dictionaries) contains information (such as passwords) which should not be revealed, and this function helps detect and replace those with the 'secret' provided (or *** if none is provided).
Substitution is performed in one of three situations:
If the key is something that is considered to be indicative of a secret, then the corresponding value is replaced with the secret provided (or *** if none is provided).
If a value in the dictionary is a string, then it is masked using the mask_password() function.
Finally, if a value is a dictionary, this function will recursively mask that dictionary as well.
For example:
>>> mask_dict_password({'password': 'd81juxmEW_',
>>> 'user': 'admin',
>>> 'home-dir': '/home/admin'},
>>> '???')
{'password': '???', 'user': 'admin', 'home-dir': '/home/admin'}
For example (the value is masked using mask_password())
>>> mask_dict_password({'password': '--password d81juxmEW_',
>>> 'user': 'admin',
>>> 'home-dir': '/home/admin'},
>>> '???')
{'password': '--password ???', 'user': 'admin',
'home-dir': '/home/admin'}
For example (a nested dictionary is masked):
>>> mask_dict_password({"nested": {'password': 'd81juxmEW_',
>>> 'user': 'admin',
>>> 'home': '/home/admin'}},
>>> '???')
{"nested": {'password': '???', 'user': 'admin', 'home': '/home/admin'}}
Added in version 3.4.
- oslo_utils.strutils.mask_password(message, secret='***')
- Replace password with secret in message.
- Parameters
- message -- The string which includes security information.
- secret -- value with which to replace passwords.
- Returns
- The unicode value of message with the password fields masked.
For example:
>>> mask_password("'adminPass' : 'aaaaa'")
"'adminPass' : '***'"
>>> mask_password("'admin_pass' : 'aaaaa'")
"'admin_pass' : '***'"
>>> mask_password('"password" : "aaaaa"')
'"password" : "***"'
>>> mask_password("'original_password' : 'aaaaa'")
"'original_password' : '***'"
Added in version 0.2.
Changed in version 1.1: Replace also 'auth_token', 'new_pass' and 'auth_password' keys.
Changed in version 1.1.1: Replace also 'secret_uuid' key.
Changed in version 1.5: Replace also 'sys_pswd' key.
Changed in version 2.6: Replace also 'token' key.
Changed in version 2.7: Replace also 'secret' key.
Changed in version 3.4: Replace also 'configdrive' key.
Changed in version 3.8: Replace also 'CHAPPASSWORD' key.
- oslo_utils.strutils.split_by_commas(value)
- Split values by commas and quotes according to api-wg
- Parameters
- value -- value to be split
Added in version 3.17.
- oslo_utils.strutils.split_path(path, minsegs=1, maxsegs=None, rest_with_last=False)
- Validate and split the given HTTP request path.
Examples:
['a'] = _split_path('/a')
['a', None] = _split_path('/a', 1, 2)
['a', 'c'] = _split_path('/a/c', 1, 2)
['a', 'c', 'o/r'] = _split_path('/a/c/o/r', 1, 3, True)
- Parameters
- path -- HTTP Request path to be split
- minsegs -- Minimum number of segments to be extracted
- maxsegs -- Maximum number of segments to be extracted
- rest_with_last -- If True, trailing data will be returned as part of last segment. If False, and there is trailing data, raises ValueError.
- Returns
- list of segments with a length of maxsegs (non-existent segments will return as None)
- Raises
- ValueError if given an invalid path
Added in version 3.11.
- oslo_utils.strutils.string_to_bytes(text, unit_system='IEC', return_int=False)
- Converts a string into an float representation of bytes.
The units supported for IEC / mixed:
Kb(it), Kib(it), Mb(it), Mib(it), Gb(it), Gib(it), Tb(it), Tib(it), Pb(it), Pib(it), Eb(it), Eib(it), Zb(it), Zib(it), Yb(it), Yib(it), Rb(it), Rib(it), Qb(it), Qib(it) KB, KiB, MB, MiB, GB, GiB, TB, TiB, PB, PiB, EB, EiB, ZB, ZiB, YB, YiB, RB, RiB, QB, QiB
The units supported for SI
kb(it), Mb(it), Gb(it), Tb(it), Pb(it), Eb(it), Zb(it), Yb(it), Rb(it), Qb(it) kB, MB, GB, TB, PB, EB, ZB, YB, RB, QB
SI units are interpreted as power-of-ten (e.g. 1kb = 1000b). Note that the SI unit system does not support capital letter 'K'
IEC units are interpreted as power-of-two (e.g. 1MiB = 1MB = 1024b)
Mixed units interpret the "i" to mean IEC, and no "i" to mean SI (e.g. 1kb = 1000b, 1kib == 1024b). Additionaly, mixed units interpret 'K' as power-of-ten. This mode is not particuarly useful for new code, but can help with compatability for parsers such as GNU parted.
- Parameters
- text -- String input for bytes size conversion.
- unit_system -- Unit system for byte size conversion.
- return_int -- If True, returns integer representation of text in bytes. (default: decimal)
- Returns
- Numerical representation of text in bytes.
- Raises
- ValueError -- If text has an invalid value.
- oslo_utils.strutils.to_slug(value, incoming=None, errors='strict')
- Normalize string.
Convert to lowercase, remove non-word characters, and convert spaces to hyphens.
Inspired by Django's slugify filter.
- Parameters
- value -- Text to slugify
- incoming -- Text's current encoding
- errors -- Errors handling policy. See here for valid values http://docs.python.org/2/library/codecs.html
- Returns
- slugified unicode representation of value
- Raises
- TypeError -- If text is not an instance of str
- oslo_utils.strutils.validate_integer(value, name, min_value=None, max_value=None)
- Make sure that value is a valid integer, potentially within range.
- Parameters
- value -- value of the integer
- name -- name of the integer
- min_value -- min_value of the integer
- max_value -- max_value of the integer
- Returns
- integer
- Raises
- ValueError if value is an invalid integer
Added in version 3.33.
timeutils
Time related utilities and helper functions.
- class oslo_utils.timeutils.Split(elapsed, length)
- A immutable stopwatch split.
See: http://en.wikipedia.org/wiki/Stopwatch for what this is/represents.
Added in version 1.4.
- property elapsed
- Duration from stopwatch start.
- property length
- Seconds from last split (or the elapsed time if no prior split).
- class oslo_utils.timeutils.StopWatch(duration=None)
- A simple timer/stopwatch helper class.
Inspired by: apache-commons-lang java stopwatch.
Not thread-safe (when a single watch is mutated by multiple threads at the same time). Thread-safe when used by a single thread (not shared) or when operations are performed in a thread-safe manner on these objects by wrapping those operations with locks.
Added in version 1.4.
- __enter__()
- Starts the watch.
- __exit__(type, value, traceback)
- Stops the watch (ignoring errors if stop fails).
- elapsed(maximum=None)
- Returns how many seconds have elapsed.
- expired()
- Returns if the watch has expired (ie, duration provided elapsed).
- has_started()
- Returns True if the watch is in a started state.
- has_stopped()
- Returns True if the watch is in a stopped state.
- leftover(return_none=False)
- Returns how many seconds are left until the watch expires.
- Parameters
- return_none (boolean) -- when True instead of raising a RuntimeError when no duration has been set this call will return None instead.
- restart()
- Restarts the watch from a started/stopped state.
- resume()
- Resumes the watch from a stopped state.
- split()
- Captures a split/elapsed since start time (and doesn't stop).
- property splits
- Accessor to all/any splits that have been captured.
- start()
- Starts the watch (if not already started).
NOTE(harlowja): resets any splits previously captured (if any).
- stop()
- Stops the watch.
- oslo_utils.timeutils.advance_time_delta(timedelta)
- Advance overridden time using a datetime.timedelta.
See oslo_utils.fixture.TimeFixture.
- oslo_utils.timeutils.advance_time_seconds(seconds)
- Advance overridden time by seconds.
See oslo_utils.fixture.TimeFixture.
- oslo_utils.timeutils.clear_time_override()
- Remove the overridden time.
See oslo_utils.fixture.TimeFixture.
- oslo_utils.timeutils.delta_seconds(before, after)
- Return the difference between two timing objects.
Compute the difference in seconds between two date, time, or datetime objects (as a float, to microsecond resolution).
- oslo_utils.timeutils.is_newer_than(after, seconds)
- Return True if after is newer than seconds.
Changed in version 1.7: Accept datetime string with timezone information. Fix comparison with timezone aware datetime.
- oslo_utils.timeutils.is_older_than(before, seconds)
- Return True if before is older than seconds.
Changed in version 1.7: Accept datetime string with timezone information. Fix comparison with timezone aware datetime.
- oslo_utils.timeutils.is_soon(dt, window)
- Determines if time is going to happen in the next window seconds.
- Parameters
- dt -- the time
- window -- minimum seconds to remain to consider the time not soon
- Returns
- True if expiration is within the given duration
- oslo_utils.timeutils.marshall_now(now=None)
- Make an rpc-safe datetime with microseconds.
Changed in version 1.6: Timezone information is now serialized instead of being stripped.
- oslo_utils.timeutils.normalize_time(timestamp)
- Normalize time in arbitrary timezone to UTC naive object.
- oslo_utils.timeutils.parse_isotime(timestr)
- Parse time from ISO 8601 format.
- oslo_utils.timeutils.parse_strtime(timestr, fmt='%Y-%m-%dT%H:%M:%S.%f')
- Turn a formatted time back into a datetime.
- oslo_utils.timeutils.set_time_override(override_time=None)
- Overrides utils.utcnow.
Make it return a constant time or a list thereof, one at a time.
See oslo_utils.fixture.TimeFixture.
- Parameters
- override_time -- datetime instance or list thereof. If not given, defaults to the current UTC time.
- oslo_utils.timeutils.time_it(logger, log_level=10, message="It took %(seconds).02f seconds to run function '%(func_name)s'", enabled=True, min_duration=0.01)
- Decorator that will log how long its decorated function takes to run.
This does not output a log if the decorated function fails with an exception.
- Parameters
- logger -- logger instance to use when logging elapsed time
- log_level -- logger logging level to use when logging elapsed time
- message -- customized message to use when logging elapsed time, the message may use automatically provide values %(seconds) and %(func_name) if it finds those values useful to record
- enabled -- whether to enable or disable this decorator (useful to decorate a function with this decorator, and then easily be able to switch that decoration off by some config or other value)
- min_duration -- argument that determines if logging is triggered or not, it is by default set to 0.01 seconds to avoid logging when durations and/or elapsed function call times are less than 0.01 seconds, to disable any min_duration checks this value should be set to less than or equal to zero or set to none
- oslo_utils.timeutils.unmarshall_time(tyme)
- Unmarshall a datetime dict.
Changed in version 1.5: Drop leap second.
Changed in version 1.6: Added support for timezone information.
- oslo_utils.timeutils.utcnow(with_timezone=False)
- Overridable version of utils.utcnow that can return a TZ-aware datetime.
See oslo_utils.fixture.TimeFixture.
Changed in version 1.6: Added with_timezone parameter.
- oslo_utils.timeutils.utcnow_ts(microsecond=False)
- Timestamp version of our utcnow function.
See oslo_utils.fixture.TimeFixture.
Changed in version 1.3: Added optional microsecond parameter.
units
Unit constants
- oslo_utils.units.E = 1000000000000000000
- Decimal exa unit
- oslo_utils.units.Ei = 1152921504606846976
- Binary exa unit
- oslo_utils.units.G = 1000000000
- Decimal giga unit
- oslo_utils.units.Gi = 1073741824
- Binary giga unit
- oslo_utils.units.Ki = 1024
- Binary kilo unit
- oslo_utils.units.M = 1000000
- Decimal mega unit
- oslo_utils.units.Mi = 1048576
- Binary mega unit
- oslo_utils.units.P = 1000000000000000
- Decimal peta unit
- oslo_utils.units.Pi = 1125899906842624
- Binary peta unit
- oslo_utils.units.Q = 1000000000000000000000000000000
- Decimal quetta unit
- oslo_utils.units.Qi = 1267650600228229401496703205376
- Binary quetta unit
- oslo_utils.units.R = 1000000000000000000000000000
- Decimal ronna unit
- oslo_utils.units.Ri = 1237940039285380274899124224
- Binary ronna unit
- oslo_utils.units.T = 1000000000000
- Decimal tera unit
- oslo_utils.units.Ti = 1099511627776
- Binary tera unit
- oslo_utils.units.Y = 1000000000000000000000000
- Decimal yotta unit
- oslo_utils.units.Yi = 1208925819614629174706176
- Binary yotta unit
- oslo_utils.units.Z = 1000000000000000000000
- Decimal zetta unit
- oslo_utils.units.Zi = 1180591620717411303424
- Binary zetta unit
- oslo_utils.units.k = 1000
- Decimal kilo unit
uuidutils
UUID related utilities and helper functions.
Added in version 1.1.
- oslo_utils.uuidutils.generate_uuid(dashed=True)
- Creates a random uuid string.
- Parameters
- dashed (bool) -- Generate uuid with dashes or not
- Returns
- string
- oslo_utils.uuidutils.is_uuid_like(val)
- Returns validation of a value as a UUID.
- Parameters
- val (string) -- Value to verify
- Returns
- bool
Changed in version 1.1.1: Support non-lowercase UUIDs.
versionutils
Helpers for comparing version strings.
Added in version 1.6.
- class oslo_utils.versionutils.VersionPredicate(predicate_str)
- Parse version predicate and check version requirements
This is based on the implementation of distutils.VersionPredicate
Added in version 7.4.
- oslo_utils.versionutils.convert_version_to_int(version)
- Convert a version to an integer.
version must be a string with dots or a tuple of integers.
Added in version 2.0.
- oslo_utils.versionutils.convert_version_to_str(version_int)
- Convert a version integer to a string with dots.
Added in version 2.0.
- oslo_utils.versionutils.convert_version_to_tuple(version_str)
- Convert a version string with dots to a tuple.
Added in version 2.0.
- oslo_utils.versionutils.is_compatible(requested_version, current_version, same_major=True)
- Determine whether requested_version is satisfied by current_version; in other words, current_version is >= requested_version.
- Parameters
- requested_version -- version to check for compatibility
- current_version -- version to check against
- same_major -- if True, the major version must be identical between requested_version and current_version. This is used when a major-version difference indicates incompatibility between the two versions. Since this is the common-case in practice, the default is True.
- Returns
- True if compatible, False if not
Contributing
If you would like to contribute to the development of oslo's libraries, first you must take a look to this page:
If you would like to contribute to the development of OpenStack, you must follow the steps in this page:
Once those steps have been completed, changes to OpenStack should be submitted for review via the Gerrit tool, following the workflow documented at:
Pull requests submitted through GitHub will be ignored.
Bugs should be filed on Launchpad, not GitHub:
RELEASE NOTES
Read also the oslo.utils Release Notes.
INDICES AND TABLES
- Index
- Module Index
- Search Page
AUTHOR
Author name not set
COPYRIGHT
2025, OpenStack Foundation
| April 2, 2025 | 8.2.0 |
