U a/@s$ddlZddlZddlZddlmZddlmZddlmZddlm Z ddlm Z ddlm Z dd l m Z dd lmZdd lmZejrddlZdd lmZdd lmZddlmZGdddZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZ 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(Gd/d0d0eZ)Gd1d2d2eZ*d6ej+ej,ej+ej,ed3d4d5Z-eZ.eZ/e"Z0e$Z1e&Z2e'Z3dS)7N)datetime)gettext)ngettext)_get_argv_encoding)get_filesystem_encoding) open_stream BadParameter)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_splitterreturncCs.t|jdd}|dd}||jdS)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 ParameterType) param_typer)type__name__ partitionr)selfrrLC:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-s9fh6ldx\click\types.py to_info_dict6s zParamType.to_info_dictrr valueparamctxrcCs|dk r||||SdSN)convertrr!r"r#rrr__call__DszParamType.__call__r"rcCsdS)z>Returns the metavar default for this param if it provides one.Nrrr"rrr get_metavarMszParamType.get_metavarcCsdS)ztOptionally might return extra information about a missing parameter. .. versionadded:: 2.0 Nrr)rrrget_missing_messagePszParamType.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%WszParamType.convertrvrcCs|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. )splitrrr-rrrsplit_envvar_valueoszParamType.split_envvar_valuez t.NoReturn)messager"r#rcCst|||ddS)z4Helper method to fail with an invalid value message.r#r"Nr )rr2r"r#rrrfailyszParamType.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"r6rrrshell_completeszParamType.shell_complete)NN)NN)r __module__ __qualname____doc__rtClassVarbool__annotations__rintstrrOptionalDictAnyrr'r*r+r%Sequencer1r4Listr7rrrrrsF     rc@s"eZdZdZeedddZdS)CompositeParamTypeTrcCs tdSr$NotImplementedErrorrrrrrszCompositeParamType.arityN)rr8r9rpropertyr?rrrrrrFsrFcsreZdZejejgejfddddZejeejfdfdd Z ejej dej d ejd d d Z Z S) FuncParamTypeN)funcrcCs|j|_||_dSr$)rrrL)rrLrrr__init__szFuncParamType.__init__rcst}|j|d<|S)NrL)superrrLrZ info_dict __class__rrrs  zFuncParamType.to_info_dictrr r c Csbz ||WStk r\z t|}Wn tk rH|dd}YnX||||YnXdSNzutf-8replace)rL ValueErrorr@ UnicodeErrordecoder4r&rrrr%s  zFuncParamType.convert) rr8r9r;CallablerCrMrBr@rrAr% __classcell__rrrPrrKs rKc@sBeZdZdZejejdejdejdddZeddd Z d S) UnprocessedParamTypetextrr r cCs|Sr$rr&rrrr%szUnprocessedParamType.convertrcCsdS)N UNPROCESSEDrrIrrr__repr__szUnprocessedParamType.__repr__N rr8r9rr;rCrAr%r@r\rrrrrYs rYc@sBeZdZdZejejdejdejdddZeddd Z d S) StringParamTyperZrr r c Cst|trt}z||}Wn`tk r~t}||krnz||}Wqztk rj|dd}YqzXn |dd}YnX|St|SrR) isinstancebytesrrVrUrr@)rr!r"r#encZfs_encrrrr%s zStringParamType.convertrcCsdS)NSTRINGrrIrrrr\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$)rerf)rrerfrrrrMszChoice.__init__rcs"t}|j|d<|j|d<|S)Nrerf)rNrrerfrOrPrrrs   zChoice.to_info_dictrr(cCs4d|j}|jr(|jdkr(d|dSd|dS)N|Zargument{}[])joinrerequiredZparam_type_name)rr" choices_strrrrr*s  zChoice.get_metavarcCstdjd|jdS)NzChoose from: {choices}z, )re)_formatrlrer)rrrr+szChoice.get_missing_messager r cs|}dd|jD}dk rFjdk rF|}fdd|D}|jsf|}dd|D}||krv||Sdtt|j}|t ddt |jj |||d|dS) NcSsi|] }||qSrr).0rdrrr sz"Choice.convert..csi|]\}}||qSr)token_normalize_funcrqZ normed_choiceoriginalr#rrrrscSsi|]\}}||qSr)casefoldrtrrrrrs, z{value!r} is not {choice}.z"{value!r} is not one of {choices}.)r!rdre) rersitemsrfrwrlmapreprr4rlenrp)rr!r"r#Z normed_valueZnormed_choicesrnrrvrr%s8  zChoice.convertcCsdt|jdS)NzChoice())listrerIrrrr\*szChoice.__repr__rr5cs^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$) startswithrqcr6rr =s z(Choice.shell_complete..c3s |]}|r|VqdSr$)lowerrrrrrr@scsg|] }|qSrrrrrr Bsz)Choice.shell_complete..)click.shell_completionrrzr@rerfr)rr#r"r6Z str_choicesmatchedr)rr6rr7-s  zChoice.shell_complete)T)rr8r9r:rr;rDr@r=rMrBrCrr*r+rAr%r\rEr7rXrrrPrrcs"  &rccseZdZdZdZdejejedddZ 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 dddg|_dS)Nz%Y-%m-%dz%Y-%m-%dT%H:%M:%Sz%Y-%m-%d %H:%M:%Sr)rrrrrrM\szDateTime.__init__rcst}|j|d<|S)Nr)rNrrrOrPrrr_s  zDateTime.to_info_dictrr(cCsdd|jdS)Nrjrgrk)rlrr)rrrr*dszDateTime.get_metavar)r!rprcCs*zt||WStk r$YdSXdSr$)rstrptimerT)rr!rprrr_try_to_convert_dategszDateTime._try_to_convert_dater r cCstt|tr|S|jD] }|||}|dk r|Sqdtt|j}|tddt |jj |||d||dS)Nrxz-{value!r} does not match the format {format}.z/{value!r} does not match the formats {formats}.)r!rpr) r_rrrrlrzr{r4rr|rp)rr!r"r#rpZ convertedZ formats_strrrrr%ms(    zDateTime.convertcCsdS)NrrrIrrrr\szDateTime.__repr__)N)rr8r9r:rr;rArDr@rMrBrCrr*rrr%r\rXrrrPrrEs rc@sBeZdZUejejed<ejejdejdejdddZ dS)_NumberParamTypeBase _number_classrr r c CsDz ||WStk r>|tdj||jd||YnXdS)Nz'{value!r} is not a valid {number_type}.)r! number_type)rrTr4rorprr&rrrr%s z_NumberParamTypeBase.convertN) rr8r9r;r<Typer>rCrAr%rrrrrs rcseZdZdejeejeeeeddddZeje ej fdfdd Z ej ejd ejd ej d fd d Z edeedddZ e dddZe dddZZS)_NumberRangeBaseNFminmaxmin_openmax_openclamprcCs"||_||_||_||_||_dSr$rrrrrrrrrrrrrrrMs z_NumberRangeBase.__init__rcs,t}|j|j|j|j|j|jd|S)Nr)rNrupdaterrrrrrOrPrrrs z_NumberRangeBase.to_info_dictrr r csddl}t|||}|jdk o:|jr.|jn|j||j}|jdk o^|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)operatorrNr%rrleltrrgegtr_clampr4rorp_describe_range)rr!r"r#rr-Zlt_minZgt_maxrPrrr%s6  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. NrGrrrrrrrrsz_NumberRangeBase._clampcCs|jdkr(|jrdnd}d||jS|jdkrP|jrz>=)rrrr)ropZlopZroprrrrs  z _NumberRangeBase._describe_rangecCs.|jr dnd}dt|jd||dS)Nz clampedr.r r)rrrr)rrrrrr\sz_NumberRangeBase.__repr__)NNFFF)rr8r9r;rAfloatr=rMrBr@rCrr%rrr\rXrrrPrrs,   rc@s"eZdZdZeZedddZdS) IntParamTypeintegerrcCsdS)NINTrrIrrrr\szIntParamType.__repr__N)rr8r9rr?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$rrrrrr szIntRange._clampN)rr8r9r:rr?r=rrrrrrsrc@s"eZdZdZeZedddZdS)FloatParamTyperrcCsdS)NFLOATrrIrrrr\szFloatParamType.__repr__N)rr8r9rrrr@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.)rNrM TypeErrorrrPrrrM,s zFloatRange.__init__rrcCs|s|StddS)Nr) RuntimeErrorrrrrr;szFloatRange._clamp)NNFFF) rr8r9r:rr;rArr=rMrrXrrrPrrs rc@sBeZdZdZejejdejdejdddZeddd Z d S) BoolParamTypebooleanrr r cCsR|dkrt|S|}|dkr(dS|dkr4dS|tdj|d||dS)N>FT>truer;yon1yesT>fnofalse0noffFz!{value!r} is not a valid boolean.r!)r=striprr4rorp)rr!r"r#Znormrrrr%Hs zBoolParamType.convertrcCsdS)NBOOLrrIrrrr\ZszBoolParamType.__repr__Nr]rrrrrEs rc@sBeZdZdZejejdejdejdddZeddd Z d S) UUIDParameterTypeuuidrr r cCs`ddl}t||jr|S|}z ||WStk rZ|tdj|d||YnXdS)Nrz{value!r} is not a valid UUID.r)rr_UUIDrrTr4rorp)rr!r"r#rrrrr%as  zUUIDParameterType.convertrcCsdS)NrrrIrrrr\rszUUIDParameterType.__repr__Nr]rrrrr^s rcseZdZdZdZejjZde e j e e j e e j e e dddd Z e je e jfd fd d Ze je d ddZe je j de j de jdddZdde e jddddZZS)FileaDeclares 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. Starting with Click 2.0, 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. filenamerNstrictF)modeencodingerrorslazyatomicrcCs"||_||_||_||_||_dSr$)rrrrr)rrrrrrrrrrMs z File.__init__rcs t}|j|j|jd|S)N)rr)rNrrrrrOrPrrrs zFile.to_info_dict)r!rcCs.|jdk r|jS|dkrdSd|jkr*dSdS)N-FwT)rr)rr!rrrresolve_lazy_flags  zFile.resolve_lazy_flagrr r c Cszt|dst|dr|WS||}|rhttjt||j|j|j|j d}|dk rb| |j |WSt ||j|j|j|j d\}}|dk r|r| t |jn| t |j|WStk r}z$|t|d|j||W5d}~XYnXdS)Nreadwrite)rz: )hasattrrr;castIOr rrrrZ call_on_closeZclose_intelligentlyrr closeflushOSErrorr4osfsdecodestrerror)rr!r"r#rrZ should_closeerrrr%s@   z File.convertrr5cCsddlm}||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"r6rrrrr7s zFile.shell_complete)rNrNF)rr8r9r:rrpathpathseprr@r;rAr=rMrBrCrrr%rEr7rXrrrPrrvs8   *rc seZdZdZejjZdeeeeeeee j e j dddZ e j ee jfdfd d Ze je jd d d Ze je j de j de jdddZddee jddddZZS)PathaCThe path type is similar to the :class:`File` type but it performs different checks. First of all, instead of returning an open file handle it returns just the filename. Secondly, it can perform various basic checks about what the file or directory should be. :param exists: if set to true, the file or directory needs to exist for this value to be valid. If this is not required and a file does indeed not exist, then all further checks are silently skipped. :param file_okay: controls if a file is a possible value. :param dir_okay: controls if a directory is a possible value. :param writable: if true, a writable check is performed. :param readable: if true, a readable check is performed. :param resolve_path: if this is true, then the path is fully resolved before the value is passed onwards. This means that it's absolute and symlinks are resolved. It will not expand a tilde-prefix, as this is supposed to be done by the shell only. :param allow_dash: If this is set to `True`, a single dash to indicate standard streams is permitted. :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.0 Allow passing ``type=pathlib.Path``. .. versionchanged:: 6.0 Added the ``allow_dash`` parameter. FTN)exists file_okaydir_okaywritablereadable resolve_path allow_dash path_typec Csn||_||_||_||_||_||_||_||_|jrH|jsHtd|_ n"|jr`|js`td|_ n td|_ dS)Nr directoryr) rrrrrrrrror) rrrrrrrrrrrrrM s     z Path.__init__rcs0t}|j|j|j|j|j|j|jd|S)N)rrrrrr) rNrrrrrrrrrOrPrrr's zPath.to_info_dictr,cCsP|jdk rLt||jsL|jtkr,t|}n |jtkrBt|}n ||}|Sr$)rr_r@rrr`fsencoder0rrrcoerce_path_result3s     zPath.coerce_path_resultrr r c Cs|}|jo|jo|dk}|s|jrFtj|r:t|}tj|}zt|}WnPt k r|j sx| |YS| t dj|jt|d||YnX|jst|jr| t dj|jt|d|||jst|jr| t dj|jt|d|||jr^t|tjs^| t dj|jt|d|||jrt|tjs| t dj|jt|d||| |S)N)-rz#{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 writable.z${name} {filename!r} is not readable.)rrrrrislinkreadlinkrealpathstatrrrr4rorprtitlerS_ISREGst_moderS_ISDIRraccessW_OKrR_OK)rr!r"r#r-Zis_dashstrrrr%>sr    z Path.convertrr5cCs.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"r6rrrrrr7}s zPath.shell_complete)FTTFTFFN)rr8r9r:rrrrr=r;rArrMrBr@rCrrr%rEr7rXrrrPrrs@    @rcseZdZdZejejejefddddZ ej e ej fdfdd Z ee dd d Zeedd d Zej ejd ejdej dddZZS)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_typerqtyrrrrsz"Tuple.__init__..)r)rrrrrrMszTuple.__init__rcs"t}dd|jD|d<|S)NcSsg|] }|qSr)r)rqr;rrrrsz&Tuple.to_info_dict..r)rNrrrOrPrrrs zTuple.to_info_dictcCsdddd|jDdS)Nrrcss|] }|jVqdSr$)rrrrrrszTuple.name..r)rlrrIrrrrsz Tuple.namecCs t|jSr$)r|rrIrrrrsz 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)rqrrr3rrrsz Tuple.convert..)r|rr4rrptuplezip)rr!r"r#rrrr3rr%s   z Tuple.convert)rr8r9r:r;rDUnionrrrMrBr@rCrrJrr?rrAr%rXrrrPrrs "r)rdefaultrcCsd}|dkrb|dk rbt|ttfrV|r^|d}t|ttfrLttt|}q^t|}nt|}d}t|trtt|St|tr|S|tks|dkrtS|t krt S|t krt S|t krtS|rtSzt|trtd|dWntk rYnXt|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_r r~rzrrrr@rbr?rrrr=r issubclassAssertionErrorrrK)rr Z guessed_typeitemrrrrs@     r)N)4rrtypingr;rrror_compatrrr exceptionsr utilsr r TYPE_CHECKINGZtyping_extensionstecorer rZshell_completionrrrFrKrYr^rcrrrrrrrrrrrrrArCrr[rbrrrrrrrrsT            ~ kCV+t'2$G