U e([V½*ã@sØddlZddlZddlmZddlmZddlmZddlm Z ddl m Z dd „Z d d „Z d$d d„Zdd„Zd%dd„Zd&dd„Zdd„Zdd„Zdd„Zdd„Zdd„Zd'dd „Zd!d"„Zdd#lmZmZmZmZdS)(éN©Úupdate_wrapperé)Ú iteritems)Ú_check_for_unicode_literals)Úecho©Úget_current_contextcs‡fdd„}t|ˆƒS)z]Marks a callback as wanting to receive the current context object as first argument. csˆtƒf|ž|ŽS©Nr©ÚargsÚkwargs©Úf©ú/decorators.pyÚnew_funcszpass_context..new_funcr©rrrrrÚ pass_context s rcs‡fdd„}t|ˆƒS)z¼Similar 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. csˆtƒjf|ž|ŽSr )r Úobjr rrrrszpass_obj..new_funcrrrrrÚpass_objs rFcs‡‡fdd„}|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. cs‡‡‡fdd„}t|ˆƒS)NcsRtƒ}ˆr| ˆ¡}n | ˆ¡}|dkr6tdˆjƒ‚|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_funcrr©rrrrÚ decorator5s z&make_pass_decorator..decoratorr)rrrrrrÚmake_pass_decorators rcCs¬t|tƒrtdƒ‚z|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)ÚnameÚcallbackÚparams)Ú isinstanceÚCommandÚ TypeErrorÚ__click_params__ÚreverseÚAttributeErrorÚgetÚinspectZgetdocÚbytesÚdecodeÚcleandocrrÚlower)rr ÚattrsÚclsr"rrrrÚ _make_commandEs,       ÿÿr1c s ˆdkr t‰‡‡‡fdd„}|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__)rÚcmd©r/r0r rrrrszcommand..decorator)r$)r r0r/rrr4rÚcommand\sr5cKs| dt¡t|f|ŽS)z°Creates 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)Ú setdefaultÚGroupr5)r r/rrrÚgroupys r8cCs8t|tƒr|j |¡nt|dƒs(g|_|j |¡dS)Nr&)r#r$r"ÚappendÚhasattrr&)rÚparamrrrÚ _param_memo‚s   r<cs‡‡fdd„}|S)a²Attaches 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)ÚpopÚArgumentr<)rZ ArgumentClass©r/Ú param_declsrrr•s zargument..decoratorr©r@r/rrr?rÚargument‹s rBcs‡‡fdd„}|S)a¨Attaches 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<dˆkrt ˆd¡ˆd<ˆ dt¡}t||ˆfˆŽƒ|S)Nrr0)r*r-r=ÚOptionr<)rZ OptionClassr?rrr¦s  zoption..decoratorrrArr?rÚoptionœs rDcs‡‡fdd„}|S)aøShortcut 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„}ˆ dd¡ˆ d|¡ˆ dd¡ˆ dd ¡ˆ d d ¡tˆpLd ˆŽ|ƒS) NcSs|s | ¡dSr )Úabort©rr;Úvaluerrrr!Ász8confirmation_option..decorator..callbackÚis_flagTr!Ú expose_valueFÚpromptzDo you want to continue?rz%Confirm the action without prompting.)z--yes©r6rD©rr!r?rrrÀs     z&confirmation_option..decoratorrrArr?rÚconfirmation_option¯s rMcs‡‡fdd„}|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 cs6ˆ dd¡ˆ dd¡ˆ dd¡tˆp,dˆŽ|ƒS)NrJTZconfirmation_promptZ hide_input)z --passwordrKrr?rrrÙs   z"password_option..decoratorrrArr?rÚpassword_optionÍs rNcs0ˆdkrt d¡j d¡‰‡‡‡‡fdd„}|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`. Nrrcstˆ dd¡‰ˆ dd¡‰‡‡‡‡fdd„}ˆ dd¡ˆ dd ¡ˆ d d¡ˆ d d ¡|ˆd <tˆpjdˆŽ|ƒS)NÚ prog_nameÚmessagez%(prog)s, version %(version)sc sÊ|r |jrdSˆ}|dkr$| ¡j}ˆ}|dkr¦z ddl}Wntk rPYnFX|jD]<}| ¡ d¡pli}t|ƒD]\}} | j ˆkrv|j }qXqvqX|dkr¦t dƒ‚t ˆ||dœ|j d| ¡dS)NrZconsole_scriptszCould not determine version)ÚprogÚversion©Úcolor)Úresilient_parsingZ find_rootZ info_nameÚ pkg_resourcesÚ ImportErrorZ working_setZ get_entry_mapr)rZ module_namerRrrrTÚexit) rr;rGrQZverrVZdistZscriptsZ script_nameZ entry_point)rPÚmodulerOrRrrr!ós4     þýz3version_option..decorator..callbackrHTrIFÚis_eagerrzShow the version and exit.r!)z --version)r=r6rDrL©r/rYr@rR)rPrOrrïs      z!version_option..decorator)ÚsysÚ _getframeÚ f_globalsr))rRr@r/rrr[rÚversion_optionás %r_cs‡‡fdd„}|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„}ˆ dd¡ˆ dd¡ˆ dd¡ˆ d d¡|ˆd <tˆpHd ˆŽ|ƒ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 _unicodefunrZutilsrÚglobalsr rrrr1r5r8r<rBrDrMrNr_r`Zcorer$r7r>rCrrrrÚs(       &    6