From aa06b2dd5442704548a2372cffc56a24de98882d Mon Sep 17 00:00:00 2001 From: huangtianhua Date: Fri, 18 Dec 2015 09:39:25 +0800 Subject: [PATCH] Make _discover_extensions public Heat wants to use `cinderclient.shell.OpenStackCinderShell._discover_extensions` function (https://bugs.launchpad.net/heat/+bug/1527071), which is private currently. It's better to change this method to public for public use. This change will do it as novaclient did. Change-Id: Iaaa4cab530fd495877e15a372fff1354ddbeaa17 Closes-Bug: #1527169 --- cinderclient/client.py | 45 ++++++++++++++++++++++++++++++++++++++++++ cinderclient/shell.py | 44 +---------------------------------------- 2 files changed, 46 insertions(+), 43 deletions(-) diff --git a/cinderclient/client.py b/cinderclient/client.py index 3dac516..2717152 100644 --- a/cinderclient/client.py +++ b/cinderclient/client.py @@ -20,7 +20,12 @@ OpenStack Client interface. Handles the REST calls and responses. from __future__ import print_function +import glob +import imp +import itertools import logging +import os +import pkgutil import re import six @@ -31,6 +36,7 @@ from keystoneclient import discover import requests from cinderclient import exceptions +import cinderclient.extension from cinderclient.openstack.common import importutils from cinderclient.openstack.common.gettextutils import _ from oslo_utils import strutils @@ -570,6 +576,45 @@ def get_client_class(version): return importutils.import_class(client_path) +def discover_extensions(version): + extensions = [] + for name, module in itertools.chain( + _discover_via_python_path(), + _discover_via_contrib_path(version)): + + extension = cinderclient.extension.Extension(name, module) + extensions.append(extension) + + return extensions + + +def _discover_via_python_path(): + for (module_loader, name, ispkg) in pkgutil.iter_modules(): + if name.endswith('python_cinderclient_ext'): + if not hasattr(module_loader, 'load_module'): + # Python 2.6 compat: actually get an ImpImporter obj + module_loader = module_loader.find_module(name) + + module = module_loader.load_module(name) + yield name, module + + +def _discover_via_contrib_path(version): + module_path = os.path.dirname(os.path.abspath(__file__)) + version_str = "v%s" % version.replace('.', '_') + ext_path = os.path.join(module_path, version_str, 'contrib') + ext_glob = os.path.join(ext_path, "*.py") + + for ext_path in glob.iglob(ext_glob): + name = os.path.basename(ext_path)[:-3] + + if name == "__init__": + continue + + module = imp.load_source(name, ext_path) + yield name, module + + def Client(version, *args, **kwargs): client_class = get_client_class(version) return client_class(*args, **kwargs) diff --git a/cinderclient/shell.py b/cinderclient/shell.py index f6c7466..fc840e9 100644 --- a/cinderclient/shell.py +++ b/cinderclient/shell.py @@ -22,12 +22,7 @@ from __future__ import print_function import argparse import getpass -import glob -import imp -import itertools import logging -import os -import pkgutil import sys import requests @@ -36,7 +31,6 @@ from cinderclient import client from cinderclient import exceptions as exc from cinderclient import utils import cinderclient.auth_plugin -import cinderclient.extension from cinderclient.openstack.common import importutils from cinderclient.openstack.common.gettextutils import _ from cinderclient.v1 import shell as shell_v1 @@ -412,42 +406,6 @@ class OpenStackCinderShell(object): return parser - def _discover_extensions(self, version): - extensions = [] - for name, module in itertools.chain( - self._discover_via_python_path(version), - self._discover_via_contrib_path(version)): - - extension = cinderclient.extension.Extension(name, module) - extensions.append(extension) - - return extensions - - def _discover_via_python_path(self, version): - for (module_loader, name, ispkg) in pkgutil.iter_modules(): - if name.endswith('python_cinderclient_ext'): - if not hasattr(module_loader, 'load_module'): - # Python 2.6 compat: actually get an ImpImporter obj - module_loader = module_loader.find_module(name) - - module = module_loader.load_module(name) - yield name, module - - def _discover_via_contrib_path(self, version): - module_path = os.path.dirname(os.path.abspath(__file__)) - version_str = "v%s" % version.replace('.', '_') - ext_path = os.path.join(module_path, version_str, 'contrib') - ext_glob = os.path.join(ext_path, "*.py") - - for ext_path in glob.iglob(ext_glob): - name = os.path.basename(ext_path)[:-3] - - if name == "__init__": - continue - - module = imp.load_source(name, ext_path) - yield name, module - def _add_bash_completion_subparser(self, subparsers): subparser = subparsers.add_parser( 'bash_completion', @@ -540,7 +498,7 @@ class OpenStackCinderShell(object): api_version_input = False # build available subcommands based on version - self.extensions = self._discover_extensions( + self.extensions = client.discover_extensions( options.os_volume_api_version) self._run_extension_hooks('__pre_parse_args__')