U e([V*@sddlZddlZddlmZddlmZddlmZddlm Z ddl m Z dd Z d d Z d$d dZddZd%ddZd&ddZddZddZddZddZddZd'dd Zd!d"Zdd#lmZmZmZmZdS)(Nupdate_wrapper) iteritems)_check_for_unicode_literals)echoget_current_contextcsfdd}t|S)z]Marks a callback as wanting to receive the current context object as first argument. cstf||SNrargskwargsf/decorators.pynew_funcszpass_context..new_funcrrrrrr pass_context s rcsfdd}t|S)zSimilar to :func:`pass_context`, but only pass the object on the context onwards (:attr:`Context.obj`). This is useful if that object represents the state of a nested system. cstjf||Sr )r objr rrrrszpass_obj..new_funcrrrrrpass_objs rFcsfdd}|S)a.Given an object type this creates a decorator that will work similar to :func:`pass_obj` but instead of passing the object of the current context, it will find the innermost context of type :func:`object_type`. This generates a decorator that works roughly like this:: from functools import update_wrapper def decorator(f): @pass_context def new_func(ctx, *args, **kwargs): obj = ctx.find_object(object_type) return ctx.invoke(f, obj, *args, **kwargs) return update_wrapper(new_func, f) return decorator :param object_type: the type of the object to pass. :param ensure: if set to `True`, a new object will be created and remembered on the context if it's not there yet. csfdd}t|S)NcsRt}r|}n |}|dkr6tdj|j|f|dd|S)NzGManaged to invoke callback without a context object of type %r existingr)r Z ensure_objectZ find_object RuntimeError__name__Zinvoke)r r ctxr)ensurer object_typerrr6s  z8make_pass_decorator..decorator..new_funcrrrrrr decorator5s z&make_pass_decorator..decoratorr)rrrrrrmake_pass_decorators rcCst|trtdz|j}||`Wntk r@g}YnX|d}|dkrtt|}t|t r~| d}n t |}||d<t |f|p|j ||d|S)Nz5Attempted to convert a callback into a command twice.helpzutf-8)namecallbackparams) isinstanceCommand TypeError__click_params__reverseAttributeErrorgetinspectZgetdocbytesdecodecleandocrrlower)rr attrsclsr"rrrr _make_commandEs,       r1c s dkr tfdd}|S)a*Creates a new :class:`Command` and uses the decorated function as callback. This will also automatically attach all decorated :func:`option`\s and :func:`argument`\s as parameters to the command. The name of the command defaults to the name of the function. If you want to change that, you can pass the intended name as the first argument. All keyword arguments are forwarded to the underlying command class. Once decorated the function turns into a :class:`Command` instance that can be invoked as a command line utility or be attached to a command :class:`Group`. :param name: the name of the command. This defaults to the function name. :param cls: the command class to instantiate. This defaults to :class:`Command`. Ncst|}|j|_|Sr )r1__doc__)rcmdr/r0r rrrrszcommand..decorator)r$)r r0r/rrr4rcommand\sr5cKs|dtt|f|S)zCreates a new :class:`Group` with a function as callback. This works otherwise the same as :func:`command` just that the `cls` parameter is set to :class:`Group`. r0) setdefaultGroupr5)r r/rrrgroupys r8cCs8t|tr|j|nt|ds(g|_|j|dS)Nr&)r#r$r"appendhasattrr&)rparamrrr _param_memos   r<csfdd}|S)aAttaches an argument to the command. All positional arguments are passed as parameter declarations to :class:`Argument`; all keyword arguments are forwarded unchanged (except ``cls``). This is equivalent to creating an :class:`Argument` instance manually and attaching it to the :attr:`Command.params` list. :param cls: the argument class to instantiate. This defaults to :class:`Argument`. cs"dt}t||f|S)Nr0)popArgumentr<)rZ ArgumentClassr/ param_declsrrrs zargument..decoratorrr@r/rrr?rarguments rBcsfdd}|S)aAttaches an option to the command. All positional arguments are passed as parameter declarations to :class:`Option`; all keyword arguments are forwarded unchanged (except ``cls``). This is equivalent to creating an :class:`Option` instance manually and attaching it to the :attr:`Command.params` list. :param cls: the option class to instantiate. This defaults to :class:`Option`. cs<dkrtdd<dt}t||f|S)Nrr0)r*r-r=Optionr<)rZ OptionClassr?rrrs  zoption..decoratorrrArr?roptions rDcsfdd}|S)aShortcut for confirmation prompts that can be ignored by passing ``--yes`` as parameter. This is equivalent to decorating a function with :func:`option` with the following parameters:: def callback(ctx, param, value): if not value: ctx.abort() @click.command() @click.option('--yes', is_flag=True, callback=callback, expose_value=False, prompt='Do you want to continue?') def dropdb(): pass csVdd}ddd|dddd d d tpLd |S) NcSs|s |dSr )abortrr;valuerrrr!sz8confirmation_option..decorator..callbackis_flagTr! expose_valueFpromptzDo you want to continue?rz%Confirm the action without prompting.)z--yesr6rDrr!r?rrrs     z&confirmation_option..decoratorrrArr?rconfirmation_options rMcsfdd}|S)aLShortcut for password prompts. This is equivalent to decorating a function with :func:`option` with the following parameters:: @click.command() @click.option('--password', prompt=True, confirmation_prompt=True, hide_input=True) def changeadmin(password): pass cs6ddddddtp,d|S)NrJTZconfirmation_promptZ hide_input)z --passwordrKrr?rrrs   z"password_option..decoratorrrArr?rpassword_options rNcs0dkrtdjdfdd}|S)acAdds a ``--version`` option which immediately ends the program printing out the version number. This is implemented as an eager option that prints the version and exits the program in the callback. :param version: the version number to show. If not provided Click attempts an auto discovery via setuptools. :param prog_name: the name of the program (defaults to autodetection) :param message: custom message to show instead of the default (``'%(prog)s, version %(version)s'``) :param others: everything else is forwarded to :func:`option`. Nrrcstddddfdd}dddd d dd d |d <tpjd|S)N prog_namemessagez%(prog)s, version %(version)sc s|r |jrdS}|dkr$|j}}|dkrz ddl}Wntk rPYnFX|jD]<}|dpli}t|D]\}} | j krv|j }qXqvqX|dkrt dt ||d|j d|dS)NrZconsole_scriptszCould not determine version)progversioncolor)resilient_parsingZ find_rootZ info_name pkg_resources ImportErrorZ working_setZ get_entry_mapr)rZ module_namerRrrrTexit) rr;rGrQZverrVZdistZscriptsZ script_nameZ entry_point)rPmodulerOrRrrr!s4     z3version_option..decorator..callbackrHTrIFis_eagerrzShow the version and exit.r!)z --version)r=r6rDrLr/rYr@rR)rPrOrrs      z!version_option..decorator)sys _getframe f_globalsr))rRr@r/rrr[rversion_options %r_csfdd}|S)alAdds a ``--help`` option which immediately ends the program printing out the help page. This is usually unnecessary to add as this is added by default to all commands unless suppressed. Like :func:`version_option`, this is implemented as eager option that prints in the callback and exits. All arguments are forwarded to :func:`option`. csRdd}ddddddd d|d <tpHd |S) NcSs(|r$|js$t||jd|dS)NrS)rUrZget_helprTrXrFrrrr!"s z0help_option..decorator..callbackrHTrIFrzShow this message and exit.rZr!)z--helprKrLr?rrr!s    zhelp_option..decoratorrrArr?r help_options  r`)r$r7r>rC)F)NN)N)N)r\r* functoolsrZ_compatrZ _unicodefunrZutilsrglobalsr rrrr1r5r8r<rBrDrMrNr_r`Zcorer$r7r>rCrrrrs(       &    6