U WI@sbddlZddlZddlmZmZmZmZmZmZm Z ddl m Z ddl m Z mZGdddeZGdd d eZGd d d eZGd d d eZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGd d!d!eZd$d"d#ZeZeZ eZ!eZ"eZ#eZ$dS)%N) open_stream text_typefilename_to_uiget_filesystem_encoding get_streerror_get_argv_encodingPY2 BadParameter)safecallLazyFilec@sPeZdZdZdZdZdZdddZddZdd Z d d Z d d Z dddZ dS) ParamTypeaHelper for converting values through types. The following is necessary for a valid type: * it needs a name * it needs to pass through None unchanged * it needs to convert from a string * it needs to convert its result type through unchanged (eg: needs to be idempotent) * it needs to be able to deal with param and context being `None`. This can be the case when the object is used with prompt inputs. FNcCs|dk r||||SdSN)convertselfvalueparamctxr /types.py__call__$szParamType.__call__cCsdS)z>Returns the metavar default for this param if it provides one.Nrrrrrr get_metavar(szParamType.get_metavarcCsdS)ztOptionally might return extra information about a missing parameter. .. versionadded:: 2.0 Nrrrrrget_missing_message+szParamType.get_missing_messagecCs|S)ziConverts the value. This is not invoked for values that are `None` (the missing value). rrrrrr2szParamType.convertcCs|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. )splitenvvar_list_splitterrrvrrrsplit_envvar_value8szParamType.split_envvar_valuecCst|||ddS)z4Helper method to fail with an invalid value message.rrNr )rmessagerrrrrfailBszParamType.fail)NN)NN) __name__ __module__ __qualname____doc__ is_compositenamerrrrrr!r$rrrrr s   rc@seZdZdZeddZdS)CompositeParamTypeTcCs tdSr)NotImplementedErrorrrrrarityJszCompositeParamType.arityN)r%r&r'r)propertyr.rrrrr+Gsr+c@seZdZddZddZdS) FuncParamTypecCs|j|_||_dSr)r%r*func)rr1rrr__init__QszFuncParamType.__init__c Csfz ||WStk r`z t|}Wn$tk rLt|dd}YnX||||YnXdSNzutf-8replace)r1 ValueErrorr UnicodeErrorstrdecoder$rrrrrUs  zFuncParamType.convertN)r%r&r'r2rrrrrr0Osr0c@s eZdZdZddZddZdS)UnprocessedParamTypetextcCs|SrrrrrrrcszUnprocessedParamType.convertcCsdS)N UNPROCESSEDrr-rrr__repr__fszUnprocessedParamType.__repr__Nr%r&r'r*rr<rrrrr9`sr9c@s eZdZdZddZddZdS)StringParamTyper:c Cszt|trvt}z||}WnRtk rpt}||krlz||}Wn tk rj|dd}YnXYnX|S|Sr3) isinstancebytesrr8r6r)rrrrencZfs_encrrrrms zStringParamType.convertcCsdS)NSTRINGrr-rrrr<|szStringParamType.__repr__Nr=rrrrr>jsr>c@s<eZdZdZdZddZddZddZd d Zd d Z d S)ChoicezThe choice type allows a value to be checked against a fixed set of supported values. All of these values have to be strings. See :ref:`choice-opts` for an example. choicecCs ||_dSr)choices)rrErrrr2szChoice.__init__cCsdd|jS)Nz[%s]|joinrErrrrrszChoice.get_metavarcCsdd|jS)NzChoose from %s., rGrrrrrszChoice.get_missing_messagecCsn||jkr|S|dk rL|jdk rL||}|jD]}|||kr0|Sq0|d|d|jf||dS)Nz$invalid choice: %s. (choose from %s)rI)rEZtoken_normalize_funcr$rH)rrrrrDrrrrs    zChoice.convertcCsdt|jS)Nz Choice(%r))listrEr-rrrr<szChoice.__repr__N) r%r&r'r(r*r2rrrr<rrrrrCsrCc@s eZdZdZddZddZdS) IntParamTypeZintegerc Cs:z t|WSttfk r4|d|||YnXdS)Nz%s is not a valid integer)intr5r6r$rrrrrs zIntParamType.convertcCsdS)NINTrr-rrrr<szIntParamType.__repr__Nr=rrrrrKsrKc@s.eZdZdZdZd ddZddZd d ZdS) IntRangeaA parameter that works similar to :data:`click.INT` but restricts the value to fit into a range. The default behavior is to fail if the value falls outside the range, but it can also be silently clamped between the two edges. See :ref:`ranges` for an example. z integer rangeNFcCs||_||_||_dSr)minmaxclamp)rrOrPrQrrrr2szIntRange.__init__cCst||||}|jrJ|jdk r0||jkr0|jS|jdk rJ||jkrJ|jS|jdk r^||jksr|jdk r||jkr|jdkr|d||jf||n@|jdkr|d||jf||n|d||j|jf|||S)Nz-%s is bigger than the maximum valid value %s.z.%s is smaller than the minimum valid value %s.z)%s is not in the valid range of %s to %s.)rKrrQrOrPr$)rrrrr rrrrs@   zIntRange.convertcCsd|j|jfS)NzIntRange(%r, %r))rOrPr-rrrr<szIntRange.__repr__)NNF)r%r&r'r(r*r2rr<rrrrrNs  rNc@s eZdZdZddZddZdS) BoolParamTypeZbooleancCsHt|trt|S|}|dkr&dS|dkr2dS|d|||dS)N)true1ZyesyT)Zfalse0ZnonFz%s is not a valid boolean)r?boollowerr$rrrrrs zBoolParamType.convertcCsdS)NBOOLrr-rrrr<szBoolParamType.__repr__Nr=rrrrrRs rRc@s eZdZdZddZddZdS)FloatParamTypefloatc Cs:z t|WSttfk r4|d|||YnXdS)Nz&%s is not a valid floating point value)r\r6r5r$rrrrrs zFloatParamType.convertcCsdS)NFLOATrr-rrrr<szFloatParamType.__repr__Nr=rrrrr[sr[c@s eZdZdZddZddZdS)UUIDParameterTypeuuidc Cs\ddl}z$tr"t|tr"|d}||WSttfk rV|d|||YnXdS)Nrasciiz%s is not a valid UUID value) r_r r?rencodeUUIDr6r5r$)rrrrr_rrrrs  zUUIDParameterType.convertcCsdS)Nrbrr-rrrr<szUUIDParameterType.__repr__Nr=rrrrr^s r^c@s6eZdZdZdZejjZd ddZ d d Z d d Z dS)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. 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. filenamerNstrictFcCs"||_||_||_||_||_dSr)modeencodingerrorslazyatomic)rrgrhrirjrkrrrr2"s z File.__init__cCs.|jdk r|jS|dkrdSd|jkr*dSdS)N-FwT)rjrg)rrrrrresolve_lazy_flag*s  zFile.resolve_lazy_flagc Cszt|dst|dr|WS||}|r^t||j|j|j|jd}|dk rX||j|WSt ||j|j|j|jd\}}|dk r|r|t |j n|t |j |WSt tfk r}z"|dt|t|f||W5d}~XYnXdS)Nreadwrite)rkzCould not open file: %s: %s)hasattrrnr rgrhrirkZ call_on_closeZclose_intelligentlyrr closeflushIOErrorOSErrorr$rr)rrrrrjfZ should_closeerrrr3s:   z File.convert)reNrfNF) r%r&r'r(r*ospathpathseprr2rnrrrrrrcs  rcc@s2eZdZdZejjZd ddZddZ d d Z dS) PathaThe 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. .. versionchanged:: 6.0 `allow_dash` was added. :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. :param allow_dash: If this is set to `True`, a single dash to indicate standard streams is permitted. :param type: optionally a string type that should be used to represent the path. The default is `None` which means the return value will be either bytes or unicode depending on what makes most sense given the input data Click deals with. FTNc Csr||_||_||_||_||_||_||_||_|jrH|jsHd|_d|_ |jrb|jsbd|_d|_ n d|_d|_ dS)NfilercZ directoryZ Directoryryr{) exists file_okaydir_okaywritablereadable resolve_path allow_dashtyper* path_type) rr}r~rrrrrrrrrr2ts   z Path.__init__cCs>|jdk r:t||js:|jtkr.|t}n |t}|Sr)rr?rr8rrarrrrcoerce_path_results   zPath.coerce_path_resultc CsR|}|jo|jo|dk}|sH|jr0tj|}zt|}WnDtk r|jsb| |YS| d|j t |f||YnX|jst |jr| d|j t |f|||jst|jr| d|j t |f|||jrt|tjs| d|j t |f|||jrHt|tjsH| d|j t |f||| |S)N)-rlz%s "%s" does not exist.z%s "%s" is a file.z%s "%s" is a directory.z%s "%s" is not writable.z%s "%s" is not readable.)r~rrrxryrealpathstatrur}rr$rrS_ISREGst_moderS_ISDIRraccessW_OKrR_OK)rrrrr Zis_dashstrrrrsd  z Path.convert)FTTFTFFN) r%r&r'r(rxryrzrr2rrrrrrr{Vs r{c@s8eZdZdZddZeddZeddZdd Zd S) 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. cCsdd|D|_dS)NcSsg|] }t|qSr) convert_type.0tyrrr sz"Tuple.__init__..)types)rrrrrr2szTuple.__init__cCsdddd|jDdS)N< css|] }|jVqdSr)r*rrrr szTuple.name..>)rHrr-rrrr*sz Tuple.namecCs t|jSr)lenrr-rrrr.sz Tuple.aritycs:t|t|jkrtdtfddt|j|DS)NzLIt would appear that nargs is set to conflict with the composite type arity.c3s|]\}}||VqdSrr)rrxr"rrrsz Tuple.convert..)rr TypeErrortupleziprrr"rrsz Tuple.convertN) r%r&r'r(r2r/r*r.rrrrrrs   rcCsd}|dkr:|dk r:t|tr.ttt|}nt|}d}t|trLt|St|trZ|S|tksr|tksr|dkrvtS|t krt S|t kr|st S|t krtS|rtSzt|trtd|Wntk rYnXt|S)zMConverts a callable or python ty into the most appropriate param ty. FNTz7Attempted to use an uninstantiated parameter type (%s).)r?rmaprrrrr7rBrLrMrXrZr\r] issubclassAssertionErrorrr0)rdefaultZ guessed_typerrrrs8      r)N)%rxrZ_compatrrrrrrr exceptionsr Zutilsr r objectrr+r0r9r>rCrKrNrRr[r^rcr{rrr;rBrMr]rZrbrrrrs2$ = % 'Ng 6