Add common method to find a resource

The find_resource_cls method can be used by several commands to
locate a resource.  It may be called with a file name or import
path.

Change-Id: Ia83c9e6c1a9745088c3558bfbef32d4a4ca234b2
This commit is contained in:
Terry Howe
2014-06-25 11:21:29 -06:00
parent 34084339c8
commit 8843fe03da

View File

@@ -37,6 +37,24 @@ CONSOLE_MESSAGE_FORMAT = '%(levelname)s: %(name)s %(message)s'
_logger = logging.getLogger(__name__)
def find_resource_cls(opts):
argument = opts.argument
if argument.find('/') > 0:
# called with file e.g.: openstack/network/v2_0/network.py
args = argument.split('/')
args[-1] = args[-1].replace('.py', '')
from_str = '.'.join(args)
class_str = args[-1].title()
else:
# called with path e.g.: openstack.network.v2_0.network.Network
args = argument.rpartition('.')
from_str = args[0]
class_str = args[2]
__import__(from_str)
mod = sys.modules[from_str]
return getattr(mod, class_str)
def get_open_fds():
'''Return the open file descriptors for current process