Juan Hernandez has uploaded a new change for review. Change subject: cli: Create type indexes during startup ......................................................................
cli: Create type indexes during startup Currently the type indexes used by the type helper are created lazyly, when they are first needed. To simplify the type helper this patch changes it so that the indexes are created during the initialization of the module. Change-Id: I9f1cd754bb9c9e196cbc1a7f077c1993ceeacf61 Signed-off-by: Juan Hernandez <juan.hernan...@redhat.com> --- M src/ovirtcli/utils/typehelper.py 1 file changed, 20 insertions(+), 38 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-cli refs/changes/82/37982/1 diff --git a/src/ovirtcli/utils/typehelper.py b/src/ovirtcli/utils/typehelper.py index 0dfdba9..bd4c0d8 100644 --- a/src/ovirtcli/utils/typehelper.py +++ b/src/ovirtcli/utils/typehelper.py @@ -21,49 +21,31 @@ from ovirtsdk.infrastructure import brokers from ovirtcli.utils.methodhelper import MethodHelper +# Create an index of wrapper types, where the key is the name of the type +# converted to lower case: +_wrapper_types = {} +for name, obj in inspect.getmembers(params, inspect.isclass): + if issubclass(obj, params.GeneratedsSuper): + _wrapper_types[name.lower()] = name + +# Create an index of decorator types, where the key is the name of the type +# converted to lower case: +_decorator_types = {} +for name, obj in inspect.getmembers(brokers, inspect.isclass): + _decorator_types[name.lower()] = name + + class TypeHelper(): - __known_wrapper_types = None - __known_decorators_types = None - - @staticmethod - def _getKnownTypes(): - known_wrapper_types = {} - for name, obj in inspect.getmembers(params): - if inspect.isclass(obj) and (isinstance(obj, type(params.BaseResource)) or - isinstance(obj, type(params.BaseResources)) or - isinstance(obj, type(params.GeneratedsSuper)) or - isinstance(obj, type(params.BaseDevices)) or - isinstance(obj, type(params.BaseDevice))): - known_wrapper_types[name.lower()] = name - return known_wrapper_types - - @staticmethod - def _getKnownDecoratorsTypes(): - __known_decorators_types = {} - for name, obj in inspect.getmembers(brokers): - if inspect.isclass(obj): - __known_decorators_types[name.lower()] = name - return __known_decorators_types @staticmethod def isKnownType(typ): - if TypeHelper.__known_wrapper_types == None: - TypeHelper.__known_wrapper_types = TypeHelper._getKnownTypes() - return TypeHelper.__known_wrapper_types.has_key(typ.lower()) - - @staticmethod - def getKnownDecoratorsTypes(): - if TypeHelper.__known_decorators_types == None: - TypeHelper.__known_decorators_types = TypeHelper._getKnownDecoratorsTypes() - return TypeHelper.__known_decorators_types.values() + return typ.lower() in _wrapper_types @staticmethod def getDecoratorType(name): if name and name != '': - if TypeHelper.__known_decorators_types == None: - TypeHelper.__known_decorators_types = TypeHelper._getKnownDecoratorsTypes() - if TypeHelper.__known_decorators_types.has_key(name.lower()): - return TypeHelper.__known_decorators_types[name.lower()] + if name.lower() in _decorator_types: + return _decorator_types[name.lower()] return None @staticmethod @@ -72,7 +54,7 @@ types = {} exceptions = ['delete', 'update'] - for decorator in TypeHelper.getKnownDecoratorsTypes(): + for decorator in _decorator_types.itervalues(): if not decorator.endswith('s'): dct = getattr(brokers, decorator).__dict__ if dct and len(dct) > 0: @@ -89,7 +71,7 @@ """return a list of types by method including context in which this method available.""" types = {} - for decorator in TypeHelper.getKnownDecoratorsTypes(): + for decorator in _decorator_types.itervalues(): if not decorator.endswith('s'): dct = getattr(brokers, decorator).__dict__ if dct and len(dct) > 0 and dct.has_key(method): @@ -104,7 +86,7 @@ sing_types = {} if method: - for decorator in TypeHelper.getKnownDecoratorsTypes(): + for decorator in _decorator_types.itervalues(): dct = getattr(brokers, decorator).__dict__ if dct.has_key(method): if decorator.endswith('s'): -- To view, visit http://gerrit.ovirt.org/37982 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I9f1cd754bb9c9e196cbc1a7f077c1993ceeacf61 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine-cli Gerrit-Branch: master Gerrit-Owner: Juan Hernandez <juan.hernan...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches