a =jgF@s>ddlZddlZddlZddlZddlmZddlmZddlmZddl m Z ddl m Z ddl m Z dd lmZdd lmZdd lmZejrddlZdd lmZdd lmZddlmZGdddZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZ GdddeZ!Gdd d e!Z"Gd!d"d"e!Z#Gd#d$d$e"e#Z$Gd%d&d&e!Z%Gd'd(d(e"e%Z&Gd)d*d*eZ'Gd+d,d,eZ(Gd-d.d.eZ)ej*d/d0d1d2Z+Gd3d4d4eZ,Gd5d6d6eZ-d:ej.ej*ej.ej*ed7d8d9Z/eZ0eZ1e#Z2e%Z3e'Z4e(Z5dS);N)datetime)gettext)ngettext)_get_argv_encoding) open_stream BadParameter)format_filename)LazyFile)safecall)Context) ParameterCompletionItemc@s8eZdZUdZdZejeed<dZ eje ed<e ed<dZ ejej e ed<eje ejfd d d Zd#ejej d ej d ejdddZd ej e dddZd ej e dddZejej d ej d ejdddZe eje dddZd$e ej d ej d ddddZd d e ejdd d!d"ZdS)% ParamTypeaRepresents the type of a parameter. Validates and converts values from the command line or Python into the correct type. To implement a custom type, subclass and implement at least the following: - The :attr:`name` class attribute must be set. - Calling an instance of the type with ``None`` must return ``None``. This is already implemented by default. - :meth:`convert` must convert string values to the correct type. - :meth:`convert` must accept values that are already the correct type. - It must be able to convert a value if the ``ctx`` and ``param`` arguments are ``None``. This can occur when converting prompt input. F is_compositeraritynameNenvvar_list_splitterreturncCsBt|jdd}|dd}t|dr4|j}n|}||dS)zGather information that could be useful for a tool generating user-facing documentation. Use :meth:`click.Context.to_info_dict` to traverse the entire CLI structure. .. versionadded:: 8.0 rrZ ParameterTyper) param_typer)type__name__ partitionhasattrr)selfrrr5/usr/local/lib/python3.9/site-packages/click/types.py to_info_dict8s  zParamType.to_info_dictrr valueparamctxrcCs|dur||||SdSN)convertrr"r#r$rrr__call__MszParamType.__call__r#rcCsdS)z>Returns the metavar default for this param if it provides one.Nrrr#rrr get_metavarVszParamType.get_metavarcCsdS)ztOptionally might return extra information about a missing parameter. .. versionadded:: 2.0 Nrr*rrrget_missing_messageYszParamType.get_missing_messagecCs|S)aConvert the value to the correct type. This is not called if the value is ``None`` (the missing value). This must accept string values from the command line, as well as values that are already the correct type. It may also convert other compatible types. The ``param`` and ``ctx`` arguments may be ``None`` in certain situations, such as when converting prompt input. If the value cannot be converted, call :meth:`fail` with a descriptive message. :param value: The value to convert. :param param: The parameter that is using this type to convert its value. May be ``None``. :param ctx: The current context that arrived at this value. May be ``None``. rr'rrrr&`szParamType.convert)rvrcCs|pd|jS)auGiven a value from an environment variable this splits it up into small chunks depending on the defined envvar list splitter. If the splitter is set to `None`, which means that whitespace splits, then leading and trailing whitespace is ignored. Otherwise, leading and trailing splitters usually lead to empty items being included. )splitr)rr-rrrsplit_envvar_valuexszParamType.split_envvar_valuez t.NoReturn)messager#r$rcCst|||ddS)z4Helper method to fail with an invalid value message.r$r#Nr)rr1r#r$rrrfailszParamType.failrr$r# incompletercCsgS)aReturn a list of :class:`~click.shell_completion.CompletionItem` objects for the incomplete value. Most types do not provide completions, but some do, and this allows custom types to provide custom completions as well. :param ctx: Invocation context for this command. :param param: The parameter that is requesting completion. :param incomplete: Value being completed. May be empty. .. versionadded:: 8.0 r)rr$r#r5rrrshell_completeszParamType.shell_complete)NN)NN)r __module__ __qualname____doc__rtClassVarbool__annotations__rintstrrOptionalDictAnyr r(r+r,r&Sequencer0r3Listr6rrrrrs>     rc@s"eZdZdZeedddZdS)CompositeParamTypeTrcCs tdSr%NotImplementedErrorrrrrrszCompositeParamType.arityN)rr7r8rpropertyr>rrrrrrEsrEcsreZdZejejgejfddddZejeejfdfdd Z ejej dej d ejd d d Z Z S) FuncParamTypeN)funcrcCs|j|_||_dSr%)rrrK)rrKrrr__init__szFuncParamType.__init__rcst}|j|d<|S)NrK)superr rKrZ info_dict __class__rrr s  zFuncParamType.to_info_dictrr r!c Cs^z ||WStyXz t|}WntyD|dd}Yn0||||Yn0dSNzutf-8replace)rK ValueErrorr? UnicodeErrordecoder3r'rrrr&s    zFuncParamType.convert) rr7r8r:CallablerBrLrAr?r r@r& __classcell__rrrOrrJs  rJc@sBeZdZdZejejdejdejdddZeddd Z d S) UnprocessedParamTypetextrr r!cCs|Sr%rr'rrrr&szUnprocessedParamType.convertrcCsdS)N UNPROCESSEDrrHrrr__repr__szUnprocessedParamType.__repr__N rr7r8rr:rBr@r&r?r[rrrrrXs  rXc@sBeZdZdZejejdejdejdddZeddd Z d S) StringParamTyperYrr r!c Cst|trt}z||}Wn^ty|t}||krlz||}Wqxtyh|dd}Yqx0n |dd}Yn0|St|SrQ) isinstancebytesrrUrTsysgetfilesystemencodingr?)rr"r#r$encZfs_encrrrr&s   zStringParamType.convertrcCsdS)NSTRINGrrHrrrr[szStringParamType.__repr__Nr\rrrrr]s  r]cseZdZdZdZdejeeddddZ ej eej fdfd d Z d ed d dZ d ed ddZej ejd ejdej dddZedddZdd eejddddZZS)Choicea>The choice type allows a value to be checked against a fixed set of supported values. All of these values have to be strings. You should only pass a list or tuple of choices. Other iterables (like generators) may lead to surprising results. The resulting value will always be one of the originally passed choices regardless of ``case_sensitive`` or any ``ctx.token_normalize_func`` being specified. See :ref:`choice-opts` for an example. :param case_sensitive: Set to false to make choices case insensitive. Defaults to true. choiceTN)choicescase_sensitivercCs||_||_dSr%)rfrg)rrfrgrrrrLszChoice.__init__rcs"t}|j|d<|j|d<|S)Nrfrg)rMr rfrgrNrOrrr s   zChoice.to_info_dictrr)cCs4d|j}|jr(|jdkr(d|dSd|dS)N|argument{}[])joinrfrequiredZparam_type_name)rr# choices_strrrrr+s  zChoice.get_metavarcCstdjd|jdS)NzChoose from: {choices}z, )rf)_formatrnrfr*rrrr, szChoice.get_missing_messager r!cs|}dd|jD}durFjdurF|}fdd|D}|jsf|}dd|D}||vrv||Sdtt|j}|t ddt |jj |||d|dS) NcSsi|] }||qSrr).0rerrr z"Choice.convert..csi|]\}}||qSr)token_normalize_funcrsZ normed_choiceoriginalr$rrrtscSsi|]\}}||qSr)casefoldrwrrrrt s, z{value!r} is not {choice}.z"{value!r} is not one of {choices}.)r"rerf) rfrvitemsrgrzrnmapreprr3rlenrr)rr"r#r$Z normed_valueZnormed_choicesrprryrr& s4  zChoice.convertcCsdt|jdS)NzChoice())listrfrHrrrr[3szChoice.__repr__rr4cs^ddlmtt|j}|jr2fdd|D}nfdd|D}fdd|DS)aComplete choices that start with the incomplete value. :param ctx: Invocation context for this command. :param param: The parameter that is requesting completion. :param incomplete: Value being completed. May be empty. .. versionadded:: 8.0 rrc3s|]}|r|VqdSr%) startswithrscr5rr Fruz(Choice.shell_complete..c3s |]}|r|VqdSr%)lowerrrrrrrIrucsg|] }|qSrrrrrr Kruz)Choice.shell_complete..)click.shell_completionrr}r?rfrgr)rr$r#r5Z str_choicesZmatchedr)rr5rr66s  zChoice.shell_complete)T)rr7r8r9rr:rCr?r<rLrArBr r+r,r@r&r[rDr6rWrrrOrrds  &rdcseZdZdZdZdejejedddZ ej eej fdfdd Z d ed d d Z ej eejedddZej ejd ejdej dddZedddZZS)DateTimeaKThe DateTime type converts date strings into `datetime` objects. The format strings which are checked are configurable, but default to some common (non-timezone aware) ISO 8601 formats. When specifying *DateTime* formats, you should only pass a list or a tuple. Other iterables, like generators, may lead to surprising results. The format strings are processed using ``datetime.strptime``, and this consequently defines the format strings which are allowed. Parsing is tried using each format, in order, and the first format which parses successfully is used. :param formats: A list or tuple of date format strings, in the order in which they should be tried. Defaults to ``'%Y-%m-%d'``, ``'%Y-%m-%dT%H:%M:%S'``, ``'%Y-%m-%d %H:%M:%S'``. rNformatscCs|p gd|_dS)N)z%Y-%m-%dz%Y-%m-%dT%H:%M:%Sz%Y-%m-%d %H:%M:%Sr)rrrrrrLeszDateTime.__init__rcst}|j|d<|S)Nr)rMr rrNrOrrr ls  zDateTime.to_info_dictrr)cCsdd|jdS)Nrlrhrm)rnrr*rrrr+qszDateTime.get_metavar)r"rrrcCs(zt||WSty"YdS0dSr%)rstrptimerS)rr"rrrrr_try_to_convert_datets zDateTime._try_to_convert_dater r!cCstt|tr|S|jD] }|||}|dur|Sqdtt|j}|tddt |jj |||d||dS)Nr{z-{value!r} does not match the format {format}.z/{value!r} does not match the formats {formats}.)r"rrr) r^rrrrnr}r~r3rrrr)rr"r#r$rrZ convertedZ formats_strrrrr&zs$    zDateTime.convertcCsdS)NrrrHrrrr[szDateTime.__repr__)N)rr7r8r9rr:r@rCr?rLrArBr r+rrr&r[rWrrrOrrNs rc@sHeZdZUejejejed<ejejdejdejdddZ dS)_NumberParamTypeBase _number_classrr r!c CsBz ||WSty<|tdj||jd||Yn0dS)Nz'{value!r} is not a valid {number_type}.)r" number_type)rrSr3rqrrrr'rrrr&s  z_NumberParamTypeBase.convertN) rr7r8r:r;TyperBr=r@r&rrrrrs rcseZdZdejeejeeeeddddZeje ej fdfdd Z ej ejd ejd ej d fd d Z edeedddZ e dddZe dddZZS)_NumberRangeBaseNFminmaxmin_openmax_openclamprcCs"||_||_||_||_||_dSr%rrrrrrrrrrrrrrrLs z_NumberRangeBase.__init__rcs,t}|j|j|j|j|j|jd|S)Nr)rMr updaterrrrrrNrOrrr s z_NumberRangeBase.to_info_dictrr r!csddl}t|||}|jduo:|jr.|jn|j||j}|jduo^|jrR|j n|j ||j}|j r|r|| |jd|jS|r| |jd|jS|s|r| tdj||d|||S)Nrrz${value} is not in the range {range}.)r"range)operatorrMr&rrleltrrgegtr_clampr3rqrr_describe_range)rr"r#r$rr-Zlt_minZgt_maxrOrrr&s0  z_NumberRangeBase.convertte.Literal[1, -1]bounddiropenrcCstdS)zFind the valid value to clamp to bound in the given direction. :param bound: The boundary value. :param dir: 1 or -1 indicating the direction to move. :param open: If true, the range does not include the bound. NrFrrrrrrrrsz_NumberRangeBase._clampcCs|jdur(|jrdnd}d||jS|jdurP|jrz>=)rrrr)ropZlopZroprrrrs  z _NumberRangeBase._describe_rangecCs.|jr dnd}dt|jd||dS)Nz clampedr.r r)rrrr)rrrrrr[sz_NumberRangeBase.__repr__)NNFFF)rr7r8r:r@floatr<rLrAr?rBr r&rrr[rWrrrOrrs(   rc@s"eZdZdZeZedddZdS) IntParamTypeintegerrcCsdS)NINTrrHrrrr[szIntParamType.__repr__N)rr7r8rr>rr?r[rrrrrsrc@s(eZdZdZdZedeedddZdS)IntRangeaRestrict an :data:`click.INT` value to a range of accepted values. See :ref:`ranges`. If ``min`` or ``max`` are not passed, any value is accepted in that direction. If ``min_open`` or ``max_open`` are enabled, the corresponding boundary is not included in the range. If ``clamp`` is enabled, a value outside the range is clamped to the boundary instead of failing. .. versionchanged:: 8.0 Added the ``min_open`` and ``max_open`` parameters. z integer rangerrcCs|s|S||Sr%rrrrrrszIntRange._clampN)rr7r8r9rr>r<rrrrrrs rc@s"eZdZdZeZedddZdS)FloatParamTyperrcCsdS)NFLOATrrHrrrr[#szFloatParamType.__repr__N)rr7r8rrrr?r[rrrrrsrcsVeZdZdZdZd ejeejeeeeddfdd Z edeed d d Z Z S) FloatRangea&Restrict a :data:`click.FLOAT` value to a range of accepted values. See :ref:`ranges`. If ``min`` or ``max`` are not passed, any value is accepted in that direction. If ``min_open`` or ``max_open`` are enabled, the corresponding boundary is not included in the range. If ``clamp`` is enabled, a value outside the range is clamped to the boundary instead of failing. This is not supported if either boundary is marked ``open``. .. versionchanged:: 8.0 Added the ``min_open`` and ``max_open`` parameters. z float rangeNFrcs.tj|||||d|s|r*|r*tddS)Nr*Clamping is not supported for open bounds.)rMrL TypeErrorrrOrrrL9s   zFloatRange.__init__rrcCs|s|StddS)Nr) RuntimeErrorrrrrrHszFloatRange._clamp)NNFFF) rr7r8r9rr:r@rr<rLrrWrrrOrr's rc@sBeZdZdZejejdejdejdddZeddd Z d S) BoolParamTypebooleanrr r!cCsR|dvrt|S|}|dvr(dS|dvr4dS|tdj|d||dS)N>FT>truer:1yesonyT>0offnfalsefnoFz!{value!r} is not a valid boolean.r")r<striprr3rqrr)rr"r#r$Znormrrrr&Us zBoolParamType.convertrcCsdS)NBOOLrrHrrrr[gszBoolParamType.__repr__Nr\rrrrrRs  rc@sBeZdZdZejejdejdejdddZeddd Z d S) UUIDParameterTypeuuidrr r!cCs^ddl}t||jr|S|}z ||WStyX|tdj|d||Yn0dS)Nrz{value!r} is not a valid UUID.r)rr^UUIDrrSr3rqrr)rr"r#r$rrrrr&ns   zUUIDParameterType.convertrcCsdS)NrrrHrrrr[szUUIDParameterType.__repr__Nr\rrrrrks  rcseZdZUdZdZejjZe j e e d<de e j e e j e e j eeddd d Ze je e jfd fd d ZdedddZe je de je jfe j de j de je jdddZdde e jddddZZS)Filea+Declares a parameter to be a file for reading or writing. The file is automatically closed once the context tears down (after the command finished working). Files can be opened for reading or writing. The special value ``-`` indicates stdin or stdout depending on the mode. By default, the file is opened for reading text data, but it can also be opened in binary mode or for writing. The encoding parameter can be used to force a specific encoding. The `lazy` flag controls if the file should be opened immediately or upon first IO. The default is to be non-lazy for standard input and output streams as well as files opened for reading, `lazy` otherwise. When opening a file lazily for reading, it is still opened temporarily for validation, but will not be held open until first IO. lazy is mainly useful when opening for writing to avoid creating the file until it is needed. Files can also be opened atomically in which case all writes go into a separate file in the same folder and upon completion the file will be moved over to the original location. This is useful if a file regularly read by other users is modified. See :ref:`file-args` for more information. .. versionchanged:: 2.0 Added the ``atomic`` parameter. filenamerrNstrictF)modeencodingerrorslazyatomicrcCs"||_||_||_||_||_dSr%)rrrrr)rrrrrrrrrrLs z File.__init__rcs t}|j|j|jd|S)N)rr)rMr rrrrNrOrrr s zFile.to_info_dictt.Union[str, os.PathLike[str]]r"rcCs4|jdur|jSt|dkr"dSd|jvr0dSdS)N-FwT)rosfspathrrr"rrrresolve_lazy_flags  zFile.resolve_lazy_flagos.PathLike[str]rr r!c Cst|r |Std|}z||}|rlt||j|j|j|jd}|durV| |j ttj tj |WSt ||j|j|j|jd\}}|dur|r| t|jn| t|j|WSty}z,|dt|d|j||WYd}~n d}~00dS)Nr)r'z': ) _is_file_liker:castrr rrrrZ call_on_closeZclose_intelligentlyIOrBrr closeflushOSErrorr3r strerror) rr"r#r$rlfrZ should_closeerrrr&s,    z File.convertrr4cCsddlm}||ddgS)acReturn a special completion marker that tells the completion system to use the shell to provide file path completions. :param ctx: Invocation context for this command. :param param: The parameter that is requesting completion. :param incomplete: Value being completed. May be empty. .. versionadded:: 8.0 rrfiler)rr)rr$r#r5rrrrr6s zFile.shell_complete)rNrNF)rr7r8r9rrpathpathseprr:r;r?r=r@r<rLrArBr rUnionrr&rDr6rWrrrOrrs4    ,rzte.TypeGuard[t.IO[t.Any]]rcCst|dpt|dS)Nreadwrite)rrrrrrsrc seZdZUdZejjZej e e d<de e e e e e e ej ejeje d ddZeje ejfd fd d Zd d dddZd ej dej dd dddZdde ejddddZZS)PathaThe ``Path`` type is similar to the :class:`File` type, but returns the filename instead of an open file. Various checks can be enabled to validate the type of file and permissions. :param exists: The file or directory needs to exist for the value to be valid. If this is not set to ``True``, and the file does not exist, then all further checks are silently skipped. :param file_okay: Allow a file as a value. :param dir_okay: Allow a directory as a value. :param readable: if true, a readable check is performed. :param writable: if true, a writable check is performed. :param executable: if true, an executable check is performed. :param resolve_path: Make the value absolute and resolve any symlinks. A ``~`` is not expanded, as this is supposed to be done by the shell only. :param allow_dash: Allow a single dash as a value, which indicates a standard stream (but does not open it). Use :func:`~click.open_file` to handle opening this value. :param path_type: Convert the incoming path value to this type. If ``None``, keep Python's default, which is ``str``. Useful to convert to :class:`pathlib.Path`. .. versionchanged:: 8.1 Added the ``executable`` parameter. .. versionchanged:: 8.0 Allow passing ``path_type=pathlib.Path``. .. versionchanged:: 6.0 Added the ``allow_dash`` parameter. rFTN) exists file_okaydir_okaywritablereadable resolve_path allow_dash path_type executablec Cst||_||_||_||_||_| |_||_||_||_|jrN|jsNt d|_ n"|jrf|jsft d|_ n t d|_ dS)Nr directoryr) rrrrrrrrrrqr) rrrrrrrrrrrrrrL#s     z Path.__init__rcs0t}|j|j|j|j|j|j|jd|S)N)rrrrrr) rMr rrrrrrrrNrOrrr @s zPath.to_info_dictrz%t.Union[str, bytes, os.PathLike[str]]rcCsT|jdurPt||jsP|jtur*t|S|jtur>t|Std||S|S)Nr) rr^r?rfsdecoder_fsencoder:rrrrrcoerce_path_resultLs    zPath.coerce_path_resultrr r!c Cs|}|jo|jo|dv}|s|jr@ddl}t||}zt|}WnLt y|j sp| |YS| t dj|jt|d||Yn0|jst|jr| t dj|jt|d|||jst|jr| t dj|jt|d|||jrNt|tjsN| t dj|jt|d|||jrt|tjs| t dj|jt|d|||jrt|tjs| t d j|jt|d||| |S) N)-rrz#{name} {filename!r} does not exist.)rrz{name} {filename!r} is a file.z#{name} {filename!r} is a directory.z${name} {filename!r} is not readable.z${name} {filename!r} is not writable.z&{name} {filename!r} is not executable.)rrrpathlibrrrresolvestatrrrr3rqrrrtitler S_ISREGst_moderS_ISDIRraccessR_OKrW_OKrX_OK)rr"r#r$r-Zis_dashrstrrrr&Ysv  z Path.convertrr4cCs.ddlm}|jr|jsdnd}|||dgS)aReturn a special completion marker that tells the completion system to use the shell to provide path completions for only directories or any paths. :param ctx: Invocation context for this command. :param param: The parameter that is requesting completion. :param incomplete: Value being completed. May be empty. .. versionadded:: 8.0 rrrrr)rrrr)rr$r#r5rrrrrr6s zPath.shell_complete) FTTFTFFNF)rr7r8r9rrrrr:r;r?r=r<r@rrBrLrAr rr&rDr6rWrrrOrrsD      NrcseZdZdZejejejeje fddddZ ej e ejfdfdd Z ee dd d Zeedd d Zejejd ejdejdddZZS)TupleaThe default behavior of Click is to apply a type on a value directly. This works well in most cases, except for when `nargs` is set to a fixed count and different types should be used for different items. In this case the :class:`Tuple` type can be used. This type can only be used if `nargs` is set to a fixed number. For more information see :ref:`tuple-type`. This can be selected by using a Python tuple literal as a type. :param types: a list of types that should be used for the tuple items. N)typesrcCsdd|D|_dS)NcSsg|] }t|qSr) convert_typerstyrrrrruz"Tuple.__init__..)r)rrrrrrLszTuple.__init__rcs"t}dd|jD|d<|S)NcSsg|] }|qSr)r )rsr:rrrrruz&Tuple.to_info_dict..r)rMr rrNrOrrr s zTuple.to_info_dictcCsdddd|jDdS)Nrrcss|] }|jVqdSr%)rrrrrrruzTuple.name..r)rnrrHrrrrsz Tuple.namecCs t|jSr%)rrrHrrrrsz Tuple.arityrr r!cs\t|j}t|}||kr<|jtdd|j||ddtfddt|j|DS)Nz:{len_type} values are required, but {len_value} was given.z;{len_type} values are required, but {len_value} were given.)len_type len_value)r#r$c3s|]\}}||VqdSr%r)rsrrr2rrrruz Tuple.convert..)rrr3rrrtuplezip)rr"r#r$rrrr2rr&s  z Tuple.convert)rr7r8r9r:rCrrrBrrLrAr?r rIrr>rr@r&rWrrrOrr s (r )rdefaultrcCsd}|durb|durbt|ttfrV|r^|d}t|ttfrLttt|}q^t|}nt|}d}t|trtt|St|tr|S|tus|durtS|t urt S|t urt S|t urtS|rtSzt|trtd|dWntyYn0t|S)zFind the most appropriate :class:`ParamType` for the given Python type. If the type isn't provided, it can be inferred from a default value. FNrTz3Attempted to use an uninstantiated parameter type (z).)r^rrr}rr rr?rcr>rrrr<r issubclassAssertionErrorrrJ)rrZ guessed_typeitemrrrrs@      r)N)6rrr`typingr:rrrqrZ_compatrr exceptionsr utilsr r r TYPE_CHECKINGtyping_extensionstecorer rZshell_completionrrrErJrXr]rdrrrrrrrrrrrBrrr r@rrZrcrrrrrrrrsZ             kGV+y:2$G