示例#1
0
    def _find_actions(self, subparsers, actions_module, version, do_help,
                      input_args):
        for attr in (a for a in dir(actions_module) if a.startswith('do_')):
            # I prefer to be hyphen-separated instead of underscores.
            command = attr[3:].replace('_', '-')
            callback = getattr(actions_module, attr)
            desc = callback.__doc__ or ''
            action_help = desc.strip().split('\n')[0]
            if hasattr(callback, "versioned"):
                additional_msg = ""
                subs = api_versions.get_substitutions(
                    utils.get_function_name(callback))
                if do_help:
                    additional_msg = self._build_versioned_help_message(
                        subs[0].start_version, subs[-1].end_version)
                    if version.is_latest():
                        additional_msg += HINT_HELP_MSG
                subs = [
                    versioned_method for versioned_method in subs
                    if version.matches(versioned_method.start_version,
                                       versioned_method.end_version)
                ]
                if not subs:
                    # There is no proper versioned method.
                    continue
                # Use the "latest" substitution.
                callback = subs[-1].func
                desc = callback.__doc__ or desc
                action_help = desc.strip().split('\n')[0]
                action_help += additional_msg

            exclusive_args = getattr(callback, 'exclusive_args', {})
            arguments = getattr(callback, 'arguments', [])

            subparser = subparsers.add_parser(
                command,
                help=action_help,
                description=desc,
                add_help=False,
                formatter_class=OpenStackHelpFormatter)

            subparser.add_argument(
                '-h',
                '--help',
                action='help',
                help=argparse.SUPPRESS,
            )

            self.subcommands[command] = subparser
            self._add_subparser_args(subparser, arguments, version, do_help,
                                     input_args, command)
            self._add_subparser_exclusive_args(subparser, exclusive_args,
                                               version, do_help, input_args,
                                               command)
            subparser.set_defaults(func=callback)
示例#2
0
    def _find_actions(self, subparsers, actions_module, version,
                      do_help, input_args):
        for attr in (a for a in dir(actions_module) if a.startswith('do_')):
            # I prefer to be hyphen-separated instead of underscores.
            command = attr[3:].replace('_', '-')
            callback = getattr(actions_module, attr)
            desc = callback.__doc__ or ''
            action_help = desc.strip().split('\n')[0]
            if hasattr(callback, "versioned"):
                additional_msg = ""
                subs = api_versions.get_substitutions(
                    utils.get_function_name(callback))
                if do_help:
                    additional_msg = self._build_versioned_help_message(
                        subs[0].start_version, subs[-1].end_version)
                    if version.is_latest():
                        additional_msg += HINT_HELP_MSG
                subs = [versioned_method for versioned_method in subs
                        if version.matches(versioned_method.start_version,
                                           versioned_method.end_version)]
                if not subs:
                    # There is no proper versioned method.
                    continue
                # Use the "latest" substitution.
                callback = subs[-1].func
                desc = callback.__doc__ or desc
                action_help = desc.strip().split('\n')[0]
                action_help += additional_msg

            exclusive_args = getattr(callback, 'exclusive_args', {})
            arguments = getattr(callback, 'arguments', [])

            subparser = subparsers.add_parser(
                command,
                help=action_help,
                description=desc,
                add_help=False,
                formatter_class=OpenStackHelpFormatter)

            subparser.add_argument('-h', '--help',
                                   action='help',
                                   help=argparse.SUPPRESS,)

            self.subcommands[command] = subparser
            self._add_subparser_args(subparser, arguments, version, do_help,
                                     input_args, command)
            self._add_subparser_exclusive_args(subparser, exclusive_args,
                                               version, do_help, input_args,
                                               command)
            subparser.set_defaults(func=callback)
    def decor(func):
        func.versioned = True
        name = utils.get_function_name(func)
        versioned_method = VersionedMethod(name, start_version,
                                           end_version, func)
        add_substitution(versioned_method)

        @functools.wraps(func)
        def substitution(obj, *args, **kwargs):
            methods = get_substitutions(name, obj.api_version)

            if not methods:
                raise exceptions.VersionNotFoundForAPIMethod(
                    obj.api_version.get_string(), name)

            method = max(methods, key=lambda f: f.start_version)

            return method.func(obj, *args, **kwargs)

        if hasattr(func, 'arguments'):
            for cli_args, cli_kwargs in func.arguments:
                utils.add_arg(substitution, *cli_args, **cli_kwargs)
        return substitution
示例#4
0
    def decor(func):
        func.versioned = True
        name = utils.get_function_name(func)
        versioned_method = VersionedMethod(name, start_version, end_version,
                                           func)
        add_substitution(versioned_method)

        @functools.wraps(func)
        def substitution(obj, *args, **kwargs):
            methods = get_substitutions(name, obj.api_version)

            if not methods:
                raise exceptions.VersionNotFoundForAPIMethod(
                    obj.api_version.get_string(), name)

            method = max(methods, key=lambda f: f.start_version)

            return method.func(obj, *args, **kwargs)

        if hasattr(func, 'arguments'):
            for cli_args, cli_kwargs in func.arguments:
                utils.add_arg(substitution, *cli_args, **cli_kwargs)
        return substitution
示例#5
0
    def _find_actions(self, subparsers, actions_module, version,
                      do_help, input_args):
        for attr in (a for a in dir(actions_module) if a.startswith('do_')):
            # I prefer to be hyphen-separated instead of underscores.
            command = attr[3:].replace('_', '-')
            callback = getattr(actions_module, attr)
            desc = callback.__doc__ or ''
            action_help = desc.strip().split('\n')[0]
            if hasattr(callback, "versioned"):
                additional_msg = ""
                subs = api_versions.get_substitutions(
                    utils.get_function_name(callback))
                if do_help:
                    additional_msg = self._build_versioned_help_message(
                        subs[0].start_version, subs[-1].end_version)
                    if version.is_latest():
                        additional_msg += HINT_HELP_MSG
                subs = [versioned_method for versioned_method in subs
                        if version.matches(versioned_method.start_version,
                                           versioned_method.end_version)]
                if not subs:
                    # There is no proper versioned method.
                    continue
                # Use the "latest" substitution.
                callback = subs[-1].func
                desc = callback.__doc__ or desc
                action_help = desc.strip().split('\n')[0]
                action_help += additional_msg

            arguments = getattr(callback, 'arguments', [])

            subparser = subparsers.add_parser(
                command,
                help=action_help,
                description=desc,
                add_help=False,
                formatter_class=OpenStackHelpFormatter)

            subparser.add_argument('-h', '--help',
                                   action='help',
                                   help=argparse.SUPPRESS,)

            self.subcommands[command] = subparser

            # NOTE(ntpttr): We get a counter for each argument in this
            # command here because during the microversion check we only
            # want to raise an exception if no version of the argument
            # matches the current microversion. The exception will only
            # be raised after the last instance of a particular argument
            # fails the check.
            arg_counter = dict()
            for (args, kwargs) in arguments:
                arg_counter[args[0]] = arg_counter.get(args[0], 0) + 1

            for (args, kwargs) in arguments:
                start_version = kwargs.get("start_version", None)
                start_version = api_versions.APIVersion(start_version)
                end_version = kwargs.get('end_version', None)
                end_version = api_versions.APIVersion(end_version)
                if do_help and (start_version or end_version):
                    kwargs["help"] = kwargs.get("help", "") + (
                        self._build_versioned_help_message(start_version,
                                                           end_version))
                if not version.matches(start_version, end_version):
                    if args[0] in input_args and command == input_args[0]:
                        if arg_counter[args[0]] == 1:
                            # This is the last version of this argument,
                            # raise the exception.
                            raise exc.UnsupportedAttribute(args[0],
                                start_version, end_version)
                        arg_counter[args[0]] -= 1
                    continue
                kw = kwargs.copy()
                kw.pop("start_version", None)
                kw.pop("end_version", None)
                subparser.add_argument(*args, **kw)
            subparser.set_defaults(func=callback)
示例#6
0
    def _find_actions(self, subparsers, actions_module, version, do_help,
                      input_args):
        for attr in (a for a in dir(actions_module) if a.startswith('do_')):
            # I prefer to be hyphen-separated instead of underscores.
            command = attr[3:].replace('_', '-')
            callback = getattr(actions_module, attr)
            desc = callback.__doc__ or ''
            action_help = desc.strip().split('\n')[0]
            if hasattr(callback, "versioned"):
                additional_msg = ""
                subs = api_versions.get_substitutions(
                    utils.get_function_name(callback))
                if do_help:
                    additional_msg = self._build_versioned_help_message(
                        subs[0].start_version, subs[-1].end_version)
                    if version.is_latest():
                        additional_msg += HINT_HELP_MSG
                subs = [
                    versioned_method for versioned_method in subs
                    if version.matches(versioned_method.start_version,
                                       versioned_method.end_version)
                ]
                if not subs:
                    # There is no proper versioned method.
                    continue
                # Use the "latest" substitution.
                callback = subs[-1].func
                desc = callback.__doc__ or desc
                action_help = desc.strip().split('\n')[0]
                action_help += additional_msg

            arguments = getattr(callback, 'arguments', [])

            subparser = subparsers.add_parser(
                command,
                help=action_help,
                description=desc,
                add_help=False,
                formatter_class=OpenStackHelpFormatter)

            subparser.add_argument(
                '-h',
                '--help',
                action='help',
                help=argparse.SUPPRESS,
            )

            self.subcommands[command] = subparser

            # NOTE(ntpttr): We get a counter for each argument in this
            # command here because during the microversion check we only
            # want to raise an exception if no version of the argument
            # matches the current microversion. The exception will only
            # be raised after the last instance of a particular argument
            # fails the check.
            arg_counter = dict()
            for (args, kwargs) in arguments:
                arg_counter[args[0]] = arg_counter.get(args[0], 0) + 1

            for (args, kwargs) in arguments:
                start_version = kwargs.get("start_version", None)
                start_version = api_versions.APIVersion(start_version)
                end_version = kwargs.get('end_version', None)
                end_version = api_versions.APIVersion(end_version)
                if do_help and (start_version or end_version):
                    kwargs["help"] = kwargs.get(
                        "help", "") + (self._build_versioned_help_message(
                            start_version, end_version))
                if not version.matches(start_version, end_version):
                    if args[0] in input_args and command == input_args[0]:
                        if arg_counter[args[0]] == 1:
                            # This is the last version of this argument,
                            # raise the exception.
                            raise exc.UnsupportedAttribute(
                                args[0], start_version, end_version)
                        arg_counter[args[0]] -= 1
                    continue
                kw = kwargs.copy()
                kw.pop("start_version", None)
                kw.pop("end_version", None)
                subparser.add_argument(*args, **kw)
            subparser.set_defaults(func=callback)