common Package

common Package

gettextutils Module

gettext for openstack-common modules.

Usual usage in an openstack.common module:

from keystoneclient.openstack.common.gettextutils import _
class keystoneclient.openstack.common.gettextutils.LocaleHandler(locale, target)

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.

emit(record)
class keystoneclient.openstack.common.gettextutils.Message(msg, domain)

Bases: UserString.UserString, object

Class used to encapsulate translatable messages.

data
locale
keystoneclient.openstack.common.gettextutils.enable_lazy()

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.

keystoneclient.openstack.common.gettextutils.get_available_languages(domain)

Lists the available languages for the given translation domain.

Parameters:domain – the domain to get languages for
keystoneclient.openstack.common.gettextutils.get_localized_message(message, user_locale)

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
keystoneclient.openstack.common.gettextutils.install(domain, lazy=False)

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:
  • domain – the translation domain
  • lazy – indicates whether or not to install the lazy _() function. The lazy _() introduces a way to do deferred translation of messages by installing a _ that builds Message objects, instead of strings, which can then be lazily translated into any available locale.

importutils Module

Import related utilities and helper functions.

keystoneclient.openstack.common.importutils.import_class(import_str)

Returns a class from a string including module and class.

keystoneclient.openstack.common.importutils.import_module(import_str)

Import a module.

keystoneclient.openstack.common.importutils.import_object(import_str, *args, **kwargs)

Import a class and return an instance of it.

keystoneclient.openstack.common.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.

keystoneclient.openstack.common.importutils.try_import(import_str, default=None)

Try to import a module and if it fails return default.

jsonutils Module

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.

keystoneclient.openstack.common.jsonutils.dumps(value, default=<function to_primitive at 0x4e8f488>, **kwargs)
keystoneclient.openstack.common.jsonutils.load(s)
keystoneclient.openstack.common.jsonutils.loads(s)
keystoneclient.openstack.common.jsonutils.to_primitive(value, convert_instances=False, convert_datetime=True, level=0, max_depth=3)

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.

memorycache Module

Super simple fake memcache client.

class keystoneclient.openstack.common.memorycache.Client(*args, **kwargs)

Bases: object

Replicates a tiny subset of memcached client interface.

add(key, value, time=0, min_compress_len=0)

Sets the value for a key if it doesn’t exist.

delete(key, time=0)

Deletes the value associated with a key.

get(key)

Retrieves the value for a key or None.

This expunges expired keys during each get.

incr(key, delta=1)

Increments the value for a key.

set(key, value, time=0, min_compress_len=0)

Sets the value for a key.

keystoneclient.openstack.common.memorycache.get_client(memcached_servers=None)

strutils Module

System-level utilities and helper functions.

keystoneclient.openstack.common.strutils.bool_from_string(subject, strict=False)

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’.

keystoneclient.openstack.common.strutils.int_from_bool_as_string(subject)

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

keystoneclient.openstack.common.strutils.safe_decode(text, incoming=None, errors='strict')

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

keystoneclient.openstack.common.strutils.safe_encode(text, incoming=None, encoding='utf-8', errors='strict')

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

keystoneclient.openstack.common.strutils.to_bytes(text, default=0)

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:
  • text – String input for bytes size conversion.
  • default – Default return value when text is blank.
keystoneclient.openstack.common.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:
Returns:

slugified unicode representation of value

Raises TypeError:
 

If text is not an instance of str

timeutils Module

Time related utilities and helper functions.

keystoneclient.openstack.common.timeutils.advance_time_delta(timedelta)

Advance overridden time using a datetime.timedelta.

keystoneclient.openstack.common.timeutils.advance_time_seconds(seconds)

Advance overridden time by seconds.

keystoneclient.openstack.common.timeutils.clear_time_override()

Remove the overridden time.

keystoneclient.openstack.common.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).

keystoneclient.openstack.common.timeutils.is_newer_than(after, seconds)

Return True if after is newer than seconds.

keystoneclient.openstack.common.timeutils.is_older_than(before, seconds)

Return True if before is older than seconds.

keystoneclient.openstack.common.timeutils.is_soon(dt, window)

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
keystoneclient.openstack.common.timeutils.iso8601_from_timestamp(timestamp)

Returns a iso8601 formated date from timestamp.

keystoneclient.openstack.common.timeutils.isotime(at=None, subsecond=False)

Stringify time in ISO 8601 format.

keystoneclient.openstack.common.timeutils.marshall_now(now=None)

Make an rpc-safe datetime with microseconds.

Note: tzinfo is stripped, but not required for relative times.

keystoneclient.openstack.common.timeutils.normalize_time(timestamp)

Normalize time in arbitrary timezone to UTC naive object.

keystoneclient.openstack.common.timeutils.parse_isotime(timestr)

Parse time from ISO 8601 format.

keystoneclient.openstack.common.timeutils.parse_strtime(timestr, fmt='%Y-%m-%dT%H:%M:%S.%f')

Turn a formatted time back into a datetime.

keystoneclient.openstack.common.timeutils.set_time_override(override_time=None)

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.
keystoneclient.openstack.common.timeutils.strtime(at=None, fmt='%Y-%m-%dT%H:%M:%S.%f')

Returns formatted utcnow.

keystoneclient.openstack.common.timeutils.total_seconds(delta)

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.

keystoneclient.openstack.common.timeutils.unmarshall_time(tyme)

Unmarshall a datetime dict.

keystoneclient.openstack.common.timeutils.utcnow()

Overridable version of utils.utcnow.

keystoneclient.openstack.common.timeutils.utcnow_ts()

Timestamp version of our utcnow function.

Table Of Contents

Previous topic

openstack Package

Next topic

apiclient Package

This Page