Exemplo n.º 1
0
    def decorator(function):
        # Following makes interactive really slow. (T20898480)
        # This should be revisited in T20899641
        #  if (description is not None and \
        #       arg is not None and type is not None):
        #      append_doc(function, arg, type, description)
        fn_specs = get_arg_spec(function)
        args = fn_specs.args or []
        if arg not in args and not fn_specs.varkw:
            raise NameError(
                "Argument {} does not exist in function {}".format(
                    arg, function_to_str(function)
                )
            )

        # init structures to store decorator data if not present
        _init_attr(function, "__annotations__", OrderedDict())
        _init_attr(function, "__arguments_decorator_specs", {})

        # Check if there is a conflict in type annotations
        current_type = function.__annotations__.get(arg)
        if current_type and type and current_type != type:
            raise TypeError(
                "Argument {} in {} is both specified as {} "
                "and {}".format(
                    arg, function_to_str(function), current_type, type
                )
            )

        if arg in function.__arguments_decorator_specs:
            raise ValueError(
                "@argument decorator was applied twice "
                "for the same argument {} on function {}".format(arg, function)
            )

        if positional and aliases:
            msg = "Aliases are not yet supported for positional arguments @ {}".format(
                arg
            )
            raise ValueError(msg)

        # reject positional=True if we are applied over a class
        if isclass(function) and positional:
            raise ValueError(
                "Cannot set positional arguments for super " "commands"
            )

        # We use __annotations__ to allow the usage of python 3 typing
        function.__annotations__.setdefault(arg, type)

        function.__arguments_decorator_specs[arg] = _ArgDecoratorSpec(
            arg=arg,
            description=description,
            name=name or transform_name(arg),
            aliases=aliases or [],
            positional=positional,
            choices=choices,
        )

        return function
Exemplo n.º 2
0
def _empty_arg_decorator_spec(arg):
    return _ArgDecoratorSpec(
        arg=arg,
        name=transform_name(arg),
        aliases=[],
        description=None,
        positional=False,
        choices=None,
    )
Exemplo n.º 3
0
    def decorator(function, name=None):
        is_supercommand = isclass(name_or_function)
        exclusive_arguments_ = _normalize_exclusive_arguments(
            exclusive_arguments)
        _validate_exclusive_arguments(function, exclusive_arguments_)

        _init_attr(function, "__command", {})
        if name:
            function.__command["name"] = name
        else:
            function.__command["name"] = (
                transform_name(function.__name__) if not is_supercommand else
                transform_class_name(function.__name__))
        function.__command["help"] = help
        function.__command["aliases"] = aliases or []
        function.__command["exclusive_arguments"] = exclusive_arguments_
        return function