Merges with current master of oslo-incubator, which brings
in several Python 3.x and other code fixes:
ded9bd6 Make dependency on netaddr optional
c331e74 Enable multiple translation domains for gettextutils
17df920 Allow mapping _ to lazy gettext path
ed653bf Fix Message format-string parsing
3198881 Add common methods required to allow translation of REST API responses
df3f2ba BaseException.message is deprecated since Python 2.6
79ee367 Add more robust gettext interpolation handling
45e0cbe python3: Add basic python3 compatibility.
1a2df89 Enable H302 hacking check
d28fa69 python3: Add python3 compatibility.
7b7566b Add netaddr.IPAddress support to to_primitive()
67bd769 python3: Fix traceback while running python3.
b0c51ec Refactors to_bytes
472d8ed Add slugify to strutils
7119e29 Enable hacking H404 test.
4246ce0 Added common code into fileutils and strutils.
e3545f8 Enable hacking H402 test
484a1df Enable hacking H403 test
50cae76 Add basic lazy gettext implementation
536c39e Add 't', 'y', and `strict` to `bool_from_string`
d9b0719 Handle ints passed to `boolean_from_string`
fde1e15 Convert unicode for python3 portability
8c964a2 Do not import openstack.common.log in strutils
9dcf688 Optimise to_primitive common cases
e700d92 Replaces standard logging with common logging
c8f676b Support overriding oslo localedir too
d8b66c7 Add a gettextutils.install() helper function
f75126c gettextutils: fix translation domain
547ab34 Fix Copyright Headers - Rename LLC to Foundation
2cb8e45 support ISO 8601 micro-second precision
4eae556 Don't LOG.error on max_depth (by default).
bd5dad9 Decode / Encode string utils for openstack
a94b9b4 to_primitive imposes what seems to be an arbitary data structure
a878193 Allow to_primitive to ignore datetimes
1461135 timeutils: considers that now is soon
a956f7a Import timeutils.is_soon from keystoneclient
d3a4c8e UTC ISO8601 from timestamp
33b12d3 Implement importutils.try_import.
a08daf1 Use basestring instead of str for type check.
Change-Id: I7dc12e23ba89b74a3beeb49c24986110bd1682c6
69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
# Copyright 2011 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 related utilities and helper functions.
|
|
"""
|
|
|
|
import sys
|
|
import traceback
|
|
|
|
|
|
def import_class(import_str):
|
|
"""Returns a class from a string including module and class."""
|
|
mod_str, _sep, class_str = import_str.rpartition('.')
|
|
try:
|
|
__import__(mod_str)
|
|
return getattr(sys.modules[mod_str], class_str)
|
|
except (ValueError, AttributeError):
|
|
raise ImportError('Class %s cannot be found (%s)' %
|
|
(class_str,
|
|
traceback.format_exception(*sys.exc_info())))
|
|
|
|
|
|
def import_object(import_str, *args, **kwargs):
|
|
"""Import a class and return an instance of it."""
|
|
return import_class(import_str)(*args, **kwargs)
|
|
|
|
|
|
def 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.
|
|
"""
|
|
import_value = "%s.%s" % (name_space, import_str)
|
|
try:
|
|
return import_class(import_value)(*args, **kwargs)
|
|
except ImportError:
|
|
return import_class(import_str)(*args, **kwargs)
|
|
|
|
|
|
def import_module(import_str):
|
|
"""Import a module."""
|
|
__import__(import_str)
|
|
return sys.modules[import_str]
|
|
|
|
|
|
def try_import(import_str, default=None):
|
|
"""Try to import a module and if it fails return default."""
|
|
try:
|
|
return import_module(import_str)
|
|
except ImportError:
|
|
return default
|