~ubuntu-branches/ubuntu/vivid/python-heatclient/vivid

« back to all changes in this revision

Viewing changes to heatclient/openstack/common/importutils.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2014-03-06 17:41:15 UTC
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: package-import@ubuntu.com-20140306174115-ecpzxbyb30tl5i7a
Tags: upstream-0.2.8
ImportĀ upstreamĀ versionĀ 0.2.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
 
 
3
 
# Copyright 2011 OpenStack LLC.
 
1
# Copyright 2011 OpenStack Foundation.
4
2
# All Rights Reserved.
5
3
#
6
4
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
24
22
 
25
23
 
26
24
def import_class(import_str):
27
 
    """Returns a class from a string including module and class"""
 
25
    """Returns a class from a string including module and class."""
28
26
    mod_str, _sep, class_str = import_str.rpartition('.')
29
27
    try:
30
28
        __import__(mod_str)
41
39
 
42
40
 
43
41
def import_object_ns(name_space, import_str, *args, **kwargs):
44
 
    """
45
 
    Import a class and return an instance of it, first by trying
 
42
    """Tries to import object from default namespace.
 
43
 
 
44
    Imports a class and return an instance of it, first by trying
46
45
    to find the class in a default namespace, then failing back to
47
46
    a full path if not found in the default namespace.
48
47
    """
57
56
    """Import a module."""
58
57
    __import__(import_str)
59
58
    return sys.modules[import_str]
 
59
 
 
60
 
 
61
def try_import(import_str, default=None):
 
62
    """Try to import a module and if it fails return default."""
 
63
    try:
 
64
        return import_module(import_str)
 
65
    except ImportError:
 
66
        return default