
    <iI                        U d Z ddlZddlZddlZddlZddlZddlmZ ddl	m
Z
mZ ddlmZ ddlmZmZ ddlmZmZmZmZ dd	lmZmZmZmZ dd
lmZmZmZmZ ddlm Z m!Z! ddl"m#Z#m$Z$ ddl%m&Z&m'Z'm(Z(m)Z)m*Z* ddl+m,Z,m-Z-  ej.        d ej/        ej0                  ddej1                  Z2ee3d<    ej.        d ej/        ej4                  ddej1                  Z5ee3d<    G d de          Z6 G d de          Z7deddddfde#dee$         de8dee8         dee9         de:deee9                  d dfd!Z;deddddfde#dee$         de8dee8         dee9         de:deee9                  d e:fd"Z<	 	 	 	 	 	 	 	 	 	 d+de#d%e9dee$         dee8         dee9         dee:         d&ee-         d'ee-         deee9                  d(e:d)e:d e#fd*Z=dS ),zA
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
    N)Sequence)PathPurePath)Pattern)FinalOptional   )AbstractSanitizerAbstractValidatorBaseFileBaseValidator)findall_to_stris_nt_abspathto_strvalidate_pathtype)_NTFS_RESERVED_FILE_NAMESDEFAULT_MIN_LENINVALID_CHAR_ERR_MSG_TMPLPlatform)FileNameSanitizerFileNameValidator)PathTypePlatformType)ErrorAttrKeyErrorReasonInvalidCharErrorReservedNameErrorValidationError)ReservedNameHandlerValidationErrorHandler[s]_RE_INVALID_PATH_RE_INVALID_WIN_PATHc                        e Zd Z	 	 	 	 	 	 	 	 	 ddedee         dee         dee         d	ee         d
eee                  de	de	dee
         ddf fdZddededefdZdee         fdZdefdZ xZS )FilePathSanitizerNTFmax_lenfs_encodingplatformnull_value_handlerreserved_name_handleradditional_reserved_names	normalizevalidate_after_sanitize	validatorreturnc
           
         |	r|	}
nt          t          ||d||          }
t                                          |||
|||||           |                                 | _        t          | j        ||||| j        |          | _	        || _
        |                     d          rt          j        | _        d S t          j        | _        d S )NTmin_lenr)   r*   check_reservedr.   r+   )r)   r*   r1   r,   r-   r.   r+   r0   )r)   r*   r,   r-   r.   r+   r0   include_universal)FilePathValidatorr   super__init___get_sanitize_regexp_sanitize_regexpr   r)   r+   #_FilePathSanitizer__fname_sanitizer_FilePathSanitizer__normalize_is_windowsntpath
splitdrive_FilePathSanitizer__split_drive	posixpath)selfr)   r*   r+   r,   r-   r.   r/   r0   r1   fpath_validator	__class__s              /Users/kimhansen/Desktop/03 Workspace/ceo-agents/chl-effectiveness/mcp-servers/whoop/.venv/lib/python3.11/site-packages/pathvalidate/_filepath.pyr;   zFilePathSanitizer.__init__   s      
	'OO/''#*C!  O 	#%1"7&?$; 	 		
 		
 		
 !% 9 9 ; ;!2L#1"7&?]$;"
 "
 "
 %d33 	6!'!2D!*!5D     valuereplacement_textc                    	 t          ||                     d                      nX# t          $ rK}|j        t          j        k    r0t          |t                    r |                     |          cY d }~S  d }~ww xY wt          |          }| 
                    |          \  }}| j                            ||          }| j        r!|rt          j                            |          }|}g }|r|                    |           |                    dd                              d          D ]|}|t(          v r|                    | d           $t+          | j                            ||                    }	|	s|s|                    d           g|                    |	           }|                                                     |          }	 | j                            |           nA# t          $ r4}|j        t          j        k    r|                     |          }Y d }~nd }~ww xY w| j        r| j                            |           t          |t                    rt;          |          S |S )	NTr7   allow_whitespaces\/_)rL   rJ   )r   r@   r   reasonr   	NULL_NAME
isinstancer   _null_value_handlerr   rC   r=   subr?   ospathnormpathappendreplacesplitr   strr>   sanitize&_FilePathSanitizer__get_path_separatorjoin
_validatorvalidate_validate_after_sanitizer   )
rE   rK   rL   eunicode_filepathdrivesanitized_pathsanitized_entriesentrysanitized_entrys
             rH   r_   zFilePathSanitizer.sanitizeR   s   	e4;K;K^b;K;c;c7cddddd 	 	 	x;000eX.. //22222222	 "%=="&"4"45E"F"F0445EGWXX 	B 0 	B!w//0@AA)') 	,$$U+++#++D#66<<SAA 	6 	6E111!((E555!&//HX/YY O # ( 1%,,R000$$_5555224499:KLL	=O$$^4444 	= 	= 	=x;000!%!9!9!!<!<	= ( 	5O$$^444eX&& 	('''s8   &) 
A>?A92A>8A99A>G/ /
H-9*H((H-c                 J    |                      d          rt          S t          S )NTr7   )r@   r%   r$   rE   s    rH   r<   z&FilePathSanitizer._get_sanitize_regexp   s'    d33 	(''rI   c                 2    |                                  rdS dS )NrP   rQ   )r@   rm   s    rH   __get_path_separatorz&FilePathSanitizer.__get_path_separator   s      	4srI   )	r(   NNNNNTFN)rJ   )__name__
__module____qualname__intr   r^   r   r    r   boolr   r;   r   r_   r   r<   r`   __classcell__rG   s   @rH   r'   r'      sO        %)+/?CBF=A(-1516 1616 c]16 <(	16
 %%;<16  ((>?16 $,HSM#:16 16 "&16 -.16 
16 16 16 16 16 16f1 1h 1# 1x 1 1 1 1f gcl        c        rI   r'   c                   j    e Zd ZU  ej        d                    d eD                       ej                  Ze	e
d<   dZe	e
d<   edeedf         f fd            Zed	d
d
dd
fdededee         dee         dedeee                  dd
f fdZdedd
fdZdedd
fdZdedd
fdZdedd
fdZ xZS )r9   |c              #   F   K   | ]}d t          j        |           dV  dS )z^/$N)reescape).0patterns     rH   	<genexpr>zFilePathValidator.<genexpr>   s8      UU+bi((+++UUUUUUrI   _RE_NTFS_RESERVED)rQ   :_MACOS_RESERVED_FILE_PATHSr2   .c                    t                      j        }t          |                                 |                                 |                                 g          r
|| j        z   S |                                 r|dz   S |S )N)rQ   )r:   reserved_keywordsany_is_universal	_is_posix	_is_macosr   	_is_linux)rE   common_keywordsrG   s     rH   r   z#FilePathValidator.reserved_keywords   sx    ''3""$$dnn&6&68H8HIJJ 	E"T%DDD>> 	,"V++rI   r(   NTr5   r)   r*   r+   r6   r.   c                    t                                          ||||||           t          || j        ||||          | _        |                     d          rt          j        | _        d S t          j        | _        d S )Nr4   Tr7   )
r:   r;   r   r)   #_FilePathValidator__fname_validatorr@   rA   rB   _FilePathValidator__split_driverD   )rE   r5   r)   r*   r+   r6   r.   rG   s          rH   r;   zFilePathValidator.__init__   s     	#)&? 	 	
 	
 	
 "3L#)&?"
 "
 "
 d33 	6!'!2D!*!5DrI   rK   c           
         t          ||                     d                      |                     |           |                     |          \  }}|sd S t	          |          }t          |                    | j                            }t          j	        t          j        t          j        | j        t          j        | j        t          j        |t          j        |i}|| j        k    rt%          d| j        dd|ddgfi ||| j        k     r't%          d                    | j        |          gfi ||                     |           |                    d	d
          }|                    d
          D ]#}|r|dv r	| j                            |           $|                     d          r|                     |           d S |                     |           d S )NTr7   rN   z!file path is too long: expected<=dz bytes, actual=z bytesz?file path is too short: expected>={:d} bytes, actual={:d} bytesrP   rQ   ).z..)r   r@   validate_abspathr   r   lenencode_fs_encodingr   REASONr   INVALID_LENGTHPLATFORMr+   FS_ENCODING
BYTE_COUNTVALUEr)   r   r5   format_validate_reserved_keywordsr\   r]   r   rc   )_FilePathValidator__validate_win_filepath*_FilePathValidator__validate_unix_filepath)rE   rK   _drivetailrf   byte_ct
err_kwargsrj   s           rH   rc   zFilePathValidator.validate   s0   %t7G7GZ^7G7_7_3_````e$$$))%00 	F!$<<&--d.?@@AA!;!4=$d&7#W 0

 T\!!!hhhhW^hhhh  	   T\!!!U\\g      	(()9:::+33D#>>%++C00 	3 	3E E[00"++E2222d33 	<(()9:::::))*:;;;;;rI   c                 v   t          j        |          }t          t          |                    }t	          |                                 o||                                 o|g          rd S |                                 r2t	          ||g          r!t          d| j	        t          j                   t          d|d| j	        j         ddz   dz   | j	        t          j                  }|                     d	
          r|r||                                 st          j        |          \  }}|r|r|d S d S d S )Nz8platform-independent absolute file path is not supported)r+   rS   zan invalid absolute file path (z) for the platform (z).zE to avoid the error, specify an appropriate platform corresponding toz the path format or 'auto'.)descriptionr+   rS   Tr7   )rD   isabsr   r   r   r@   r   r   r   r+   r   MALFORMED_ABS_PATHrK   rA   rB   )rE   rK   is_posix_abs	is_nt_abs
err_objectrg   _tails          rH   r   z"FilePathValidator.validate_abspath   s}    u--!&--00	  ""0y$..2B2B2S|TUU 	F 	CL(A$B$B 	J"5    %f%fft}ObfffYZ/0 ]1
 
 

 d33 	 	!! 	!!,U33LE5 ! !  	! 	!! ! ! !rI   rf   c                     t                               |          }|r1t          t          j        t          |                    |          d S )Ninvalid)rK   )r$   findallr   r   r   r   )rE   rf   matchs      rH   __validate_unix_filepathz*FilePathValidator.__validate_unix_filepath  s[     (()9:: 	")09N9NOOO&   	 	rI   c                    t                               |          }|r<t          t          j        t          |                    t          j        |          |                     |          \  }}|rL| j	        
                    |          }|r2|                                }t          d| dd|| j                  d S d S )Nr   )r+   rK   'z' is a reserved nameF)reusable_namereserved_namer+   )r%   r   r   r   r   r   r   WINDOWSr   r   searchgroupr   r+   )rE   rf   r   r   rK   match_reservedr   s          rH   __validate_win_filepathz)FilePathValidator.__validate_win_filepath  s    $,,-=>> 	")09N9NOOO!)&    **+;<< 		!3::5AAN  . 4 4 6 6';;;;"'"/!]	   			 		 rI   )rp   rq   rr   r{   compilera   r   
IGNORECASEr   r   __annotations__r   propertytupler^   r   r   rs   r   r   rt   r   r;   r   rc   r   r   r   ru   rv   s   @rH   r9   r9      s        )rzUU;TUUUUU
   u    )3222	5c? 	 	 	 	 	 X	 '%)+/#=A6 66 6 c]	6
 <(6 6 $,HSM#:6 
6 6 6 6 6 6@.<h .<4 .< .< .< .<`!h !4 ! ! ! !@              rI   r9   T	file_pathr+   r5   r)   r*   r6   r.   r2   c                 ^    t          |||dn||||                              |            dS )a  Verifying whether the ``file_path`` is a valid file path or not.

    Args:
        file_path (PathType):
            File path to be validated.
        platform (Optional[PlatformType], optional):
            Target platform name of the file path.

            .. include:: platform.txt
        min_len (int, optional):
            Minimum byte length of the ``file_path``. The value must be greater or equal to one.
            Defaults to ``1``.
        max_len (Optional[int], optional):
            Maximum byte length of the ``file_path``. If the value is |None| or minus,
            automatically determined by the ``platform``:

                - ``Linux``: 4096
                - ``macOS``: 1024
                - ``Windows``: 260
                - ``universal``: 260
        fs_encoding (Optional[str], optional):
            Filesystem encoding that is used to calculate the byte length of the file path.
            If |None|, get the encoding from the execution environment.
        check_reserved (bool, optional):
            If |True|, check the reserved names of the ``platform``.
            Defaults to |True|.
        additional_reserved_names (Optional[Sequence[str]], optional):
            Additional reserved names to check.

    Raises:
        ValidationError (ErrorReason.INVALID_CHARACTER):
            If the ``file_path`` includes invalid char(s):
            |invalid_file_path_chars|.
            The following characters are also invalid for Windows platforms:
            |invalid_win_file_path_chars|
        ValidationError (ErrorReason.INVALID_LENGTH):
            If the ``file_path`` is longer than ``max_len`` characters.
        ValidationError:
            If ``file_path`` includes invalid values.

    Example:
        :ref:`example-validate-file-path`

    See Also:
        `Naming Files, Paths, and Namespaces - Win32 apps | Microsoft Docs
        <https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file>`__
    Nr(   r+   r5   r)   r*   r6   r.   )r9   rc   r   r+   r5   r)   r*   r6   r.   s          rH   validate_filepathr   3  sI    r o7%";   hyrI   c                 Z    t          |||dn||||                              |           S )a3  Check whether the ``file_path`` is a valid name or not.

    Args:
        file_path:
            A filepath to be checked.
        platform:
            Target platform name of the file path.

    Example:
        :ref:`example-is-valid-filepath`

    See Also:
        :py:func:`.validate_filepath()`
    Nr(   r   )r9   is_validr   s          rH   is_valid_filepathr   v  sD    0 o7%";   hyrI   rJ   FrL   r,   r-   r/   r0   c           
          |*t          j        dt                     |du rt          j        }t          ||dn|||	||||
                              | |          S )a  Make a valid file path from a string.

    To make a valid file path, the function does the following:

        - Replace invalid characters for a file path within the ``file_path``
          with the ``replacement_text``. Invalid characters are as follows:

            - unprintable characters
            - |invalid_file_path_chars|
            - for Windows (or universal) only: |invalid_win_file_path_chars|

        - Replace a value if a sanitized value is a reserved name by operating systems
          with a specified handler by ``reserved_name_handler``.

    Args:
        file_path:
            File path to sanitize.
        replacement_text:
            Replacement text for invalid characters.
            Defaults to ``""``.
        platform:
            Target platform name of the file path.

            .. include:: platform.txt
        max_len:
            Maximum byte length of the file path.
            Truncate the path if the value length exceeds the `max_len`.
            If the value is |None| or minus, ``max_len`` will automatically determined by the ``platform``:

                - ``Linux``: 4096
                - ``macOS``: 1024
                - ``Windows``: 260
                - ``universal``: 260
        fs_encoding:
            Filesystem encoding that is used to calculate the byte length of the file path.
            If |None|, get the encoding from the execution environment.
        check_reserved:
            [Deprecated] Use 'reserved_name_handler' instead.
        null_value_handler:
            Function called when a value after sanitization is an empty string.
            You can specify predefined handlers:

                - :py:func:`.handler.NullValueHandler.return_null_string`
                - :py:func:`.handler.NullValueHandler.return_timestamp`
                - :py:func:`.handler.raise_error`

            Defaults to :py:func:`.handler.NullValueHandler.return_null_string` that just return ``""``.
        reserved_name_handler:
            Function called when a value after sanitization is one of the reserved names.
            You can specify predefined handlers:

                - :py:meth:`~.handler.ReservedNameHandler.add_leading_underscore`
                - :py:meth:`~.handler.ReservedNameHandler.add_trailing_underscore`
                - :py:meth:`~.handler.ReservedNameHandler.as_is`
                - :py:func:`~.handler.raise_error`

            Defaults to :py:func:`.handler.add_trailing_underscore`.
        additional_reserved_names:
            Additional reserved names to sanitize.
            Case insensitive.
        normalize:
            If |True|, normalize the the file path.
        validate_after_sanitize:
            Execute validation after sanitization to the file path.

    Returns:
        Same type as the argument (str or PathLike object):
            Sanitized filepath.

    Raises:
        ValueError:
            If the ``file_path`` is an invalid file path.

    Example:
        :ref:`example-sanitize-file-path`
    NzD'check_reserved' is deprecated. Use 'reserved_name_handler' instead.Fr(   )r+   r)   r*   r/   r,   r-   r.   r0   )warningswarnDeprecationWarningr   as_isr'   r_   )r   rL   r+   r)   r*   r6   r,   r-   r.   r/   r0   s              rH   sanitize_filepathr     s    t !R	
 	
 	

 U""$7$=!o7-3"; 7	 	 	 hy*++	,rI   )
rJ   NNNNNNNTF)>__doc__rA   os.pathrX   rD   r{   r   collections.abcr   pathlibr   r   r   typingr   r   _baser
   r   r   r   _commonr   r   r   r   _constr   r   r   r   	_filenamer   r   _typesr   r   errorr   r   r   r   r   handlerr   r    r   r|   _INVALID_PATH_CHARSUNICODEr$   r   _INVALID_WIN_PATH_CHARSr%   r'   r9   rs   r^   rt   r   r   r    rI   rH   <module>r      s           				  $ $ $ $ $ $ " " " " " " " "       " " " " " " " " P P P P P P P P P P P P M M M M M M M M M M M M c c c c c c c c c c c c ; ; ; ; ; ; ; ; * * * * * * * * b b b b b b b b b b b b b b @ @ @ @ @ @ @ @ %"*%U83O)P)P%U%U%U%UWYWabb % b b b(bj8		(2338888"*  e   
q q q q q) q q qh^ ^ ^ ^ ^ ^ ^ ^F (,"!!%9=@ @@|$@ @ c]	@
 #@ @  (6@ 
@ @ @ @J (,"!!%9= |$  c]	
 #   (6 
   H '+!!%%);?>B9=$)l, l,l,l, |$l, c]	l,
 #l, TNl, !!78l, $$:;l,  (6l, l, "l, l, l, l, l, l, l,rI   