From 8843fe03da0eaf008418eb335f58d2e6c73f3750 Mon Sep 17 00:00:00 2001 From: Terry Howe Date: Wed, 25 Jun 2014 11:21:29 -0600 Subject: [PATCH] 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 --- examples/common.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/examples/common.py b/examples/common.py index 7749893f..ee450720 100755 --- a/examples/common.py +++ b/examples/common.py @@ -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