gettext for openstack-common modules.
Usual usage in an openstack.common module:
from keystoneclient.openstack.common.gettextutils import _
Bases: logging.Handler
Handler that can have a locale associated to translate Messages.
A quick example of how to utilize the Message class above. LocaleHandler takes a locale and a target logging.Handler object to forward LogRecord objects to after translating the internal Message.
Bases: UserString.UserString, object
Class used to encapsulate translatable messages.
Convenience function for configuring _() to use lazy gettext
Call this at the start of execution to enable the gettextutils._ function to use lazy gettext functionality. This is useful if your project is importing _ directly instead of using the gettextutils.install() way of importing the _ function.
Lists the available languages for the given translation domain.
| Parameters: | domain – the domain to get languages for |
|---|
Gets a localized version of the given message in the given locale.
If the message is not a Message object the message is returned as-is. If the locale is None the message is translated to the default locale.
| Returns: | the translated message in unicode, or the original message if it could not be translated |
|---|
Install a _() function using the given translation domain.
Given a translation domain, install a _() function using gettext’s install() function.
The main difference from gettext.install() is that we allow overriding the default localedir (e.g. /usr/share/locale) using a translation-domain-specific environment variable (e.g. NOVA_LOCALEDIR).
| Parameters: |
|
|---|
Import related utilities and helper functions.
Returns a class from a string including module and class.
Import a module.
Import a class and return an instance of it.
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.
Try to import a module and if it fails return default.
JSON related utilities.
This module provides a few things:
1) A handy function for getting an object down to something that can be JSON serialized. See to_primitive().
2) Wrappers around loads() and dumps(). The dumps() wrapper will automatically use to_primitive() for you if needed.
3) This sets up anyjson to use the loads() and dumps() wrappers if anyjson is available.
Convert a complex object into primitives.
Handy for JSON serialization. We can optionally handle instances, but since this is a recursive function, we could have cyclical data structures.
To handle cyclical data structures we could track the actual objects visited in a set, but not all objects are hashable. Instead we just track the depth of the object inspections and don’t go too deep.
Therefore, convert_instances=True is lossy ... be aware.
Super simple fake memcache client.
Bases: object
Replicates a tiny subset of memcached client interface.
Sets the value for a key if it doesn’t exist.
Deletes the value associated with a key.
Retrieves the value for a key or None.
This expunges expired keys during each get.
Increments the value for a key.
Sets the value for a key.
System-level utilities and helper functions.
Interpret a string as a boolean.
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 is considered False.
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’.
Interpret a string as a boolean and return either 1 or 0.
Any string value in:
(‘True’, ‘true’, ‘On’, ‘on’, ‘1’)
is interpreted as a boolean True.
Useful for JSON-decoded stuff and config file parsing
Decodes incoming str using incoming if they’re not already unicode.
| Parameters: |
|
|---|---|
| Returns: | text or a unicode incoming encoded representation of it. |
| Raises TypeError: | |
If text is not an instance of str |
|
Encodes incoming str/unicode using encoding.
If incoming is not specified, text is expected to be encoded with current python’s default encoding. (sys.getdefaultencoding)
| Parameters: |
|
|---|---|
| Returns: | text or a bytestring encoding encoded representation of it. |
| Raises TypeError: | |
If text is not an instance of str |
|
Converts a string into an integer of bytes.
Looks at the last characters of the text to determine what conversion is needed to turn the input text into a byte number. Supports “B, K(B), M(B), G(B), and T(B)”. (case insensitive)
| Parameters: |
|
|---|
Normalize string.
Convert to lowercase, remove non-word characters, and convert spaces to hyphens.
Inspired by Django’s slugify filter.
| Parameters: |
|
|---|---|
| Returns: | slugified unicode representation of value |
| Raises TypeError: | |
If text is not an instance of str |
|
Time related utilities and helper functions.
Advance overridden time using a datetime.timedelta.
Advance overridden time by seconds.
Remove the overridden time.
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).
Return True if after is newer than seconds.
Return True if before is older than seconds.
Determines if time is going to happen in the next window seconds.
| Params dt: | the time |
|---|---|
| Params window: | minimum seconds to remain to consider the time not soon |
| Returns: | True if expiration is within the given duration |
Returns a iso8601 formated date from timestamp.
Stringify time in ISO 8601 format.
Make an rpc-safe datetime with microseconds.
Note: tzinfo is stripped, but not required for relative times.
Normalize time in arbitrary timezone to UTC naive object.
Parse time from ISO 8601 format.
Turn a formatted time back into a datetime.
Overrides utils.utcnow.
Make it return a constant time or a list thereof, one at a time.
| Parameters: | override_time – datetime instance or list thereof. If not given, defaults to the current UTC time. |
|---|
Returns formatted utcnow.
Return the total seconds of datetime.timedelta object.
Compute total seconds of datetime.timedelta, datetime.timedelta doesn’t have method total_seconds in Python2.6, calculate it manually.
Unmarshall a datetime dict.
Overridable version of utils.utcnow.
Timestamp version of our utcnow function.