Source code for ads.common.decorator.argument_to_case
#!/usr/bin/env python# -*- coding: utf-8 -*--# Copyright (c) 2022 Oracle and its affiliates.# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/"""The module that provides the decorator helping to convert function argumentsto specific case (lower/upper).Examples-------->>> @argument_to_case("lower", ["name"])... def test_function(name: str, last_name: str)... print(name)...>>> test_function("myname", "mylastname")... MYNAME"""importinspectfromcopyimportcopyfromenumimportEnumfromfunctoolsimportwrapsfromtypingimportCallable,Listfrom.utilsimport_get_original_func
[docs]defargument_to_case(case:ArgumentCase,arguments:List[str]):"""The decorator helping to convert function arguments to specific case. Parameters ---------- case: ArgumentCase The case to convert specific arguments. arguments: List[str] The list of arguments to convert to specific case. Examples -------- >>> @argument_to_case("lower", ["name"]) ... def test_function(name: str, last_name: str) ... print(name) ... >>> test_function("myname", "mylastname") ... MYNAME """defdecorator(func:Callable):@wraps(func)defwrapper(*args,**kwargs):# Retrieving the original function from the decorated one.# This is necessary when the chain of decorators is used.# The only original function arguments should be processed.original_func=_get_original_func(func)# Getting the original function argumentsfunc_args=inspect.getfullargspec(original_func).args# Saving original args and kwargs.new_args=list(copy(args))new_kwargs=copy(kwargs)# Converting args and kwargs to the specific case.forfunc_arginfunc_args:iffunc_arginarguments:iffunc_arginnew_kwargs:new_kwargs[func_arg]=(new_kwargs[func_arg].lower()ifcase==ArgumentCase.LOWERelsenew_kwargs[func_arg].upper())else:arg_index=func_args.index(func_arg)ifarg_index>=0:new_args[arg_index]=(new_args[arg_index].lower()ifcase==ArgumentCase.LOWERelsenew_args[arg_index].upper())returnfunc(*new_args,**new_kwargs)returnwrapperreturndecorator