
    k7iI                     `   % S r SSKrSSKrSSKrSSKrSSKrSSKJr  SSK	J
r
Jr  SSKJr  SSKJrJr  SSKJrJrJrJr  SS	KJrJrJrJr  SS
KJrJrJrJr  SSKJ r J!r!  SSK"J#r#J$r$  SSK%J&r&J'r'J(r(J)r)J*r*  SSK+J,r,J-r-  \R\                  " S\R^                  " \R`                  5      S S3\Rb                  5      r2\\3S'   \R\                  " S\R^                  " \Rh                  5      S S3\Rb                  5      r5\\3S'    " S S\5      r6 " S S\5      r7S\SSSS4S\#S\\$   S\8S\\8   S\\9   S\:S\\\9      S S4S! jjr;S\SSSS4S\#S\\$   S\8S\\8   S\\9   S\:S\\\9      S \:4S" jjr<          S)S\#S#\9S\\$   S\\8   S\\9   S\\:   S$\\-   S%\\-   S\\\9      S&\:S'\:S \#4S( jjr=g)*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                      ^  \ rS rSr         SS\S\\   S\\   S\\   S\\   S\\	\      S	\
S
\
S\\   SS4U 4S jjjrSS\S\S\4S jjrS\\   4S jrS\4S jrSrU =r$ )FilePathSanitizer   Nmax_lenfs_encodingplatformnull_value_handlerreserved_name_handleradditional_reserved_names	normalizevalidate_after_sanitize	validatorreturnc
                 j  > U	(       a  U	n
O[        [        UUSUUS9n
[        TU ]  UUU
UUUUUS9  U R	                  5       U l        [        U R                  UUUUU R                  US9U l	        Xpl
        U R                  SS9(       a  [        R                  U l        g [        R                  U l        g )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              P/home/james-whalen/.local/lib/python3.13/site-packages/pathvalidate/_filepath.pyr;   FilePathSanitizer.__init__   s     'O/''#*C!O 	#%1"7&?$; 	 		
 !% 9 9 ;!2LL#1"7&?]]$;"
 %d3!'!2!2D!*!5!5D    valuereplacement_textc                     [        XR                  SS9(       + S9  [        U5      nU R                  U5      u  pTU R                  R                  X$5      nU R                  (       a&  U(       a  [        R                  R!                  U5      nUn/ nU(       a  UR#                  U5        UR%                  SS5      R'                  S5       Hw  nU[(        ;   a  UR#                  U S35        M#  [+        U R,                  R/                  XS95      n	U	(       d  U(       d  UR#                  S5        Mf  UR#                  U	5        My     U R1                  5       R3                  U5      n U R4                  R7                  U5        U R8                  (       a  U R4                  R7                  U5        [        U[        5      (       a  [;        U5      $ U$ ! [         aP  nUR                  [        R
                  :X  a,  [        U[        5      (       a  e U R                  U5      s S nA$ e S nAff = f! [         a9  nUR                  [        R
                  :X  a  U R                  U5      n S nANS nAff = f)	NTr7   allow_whitespaces\/_)rL    )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`   FilePathSanitizer.sanitizeR   s   	e;K;K^b;K;c7cd "%="&"4"45E"F00445EX 0!ww//0@A)')$$U+#++D#6<<SAE11!((E7!5!&&///YO #(%,,R0$$_5 B 22499:KL	=OO$$^4
 ((OO$$^4eX&&'']  	xx;000eX..//22	H  	=xx;000!%!9!9!!<	=s<   G %H+ 
H(AH#H("H##H(+
I.5/I))I.c                 B    U R                  SS9(       a  [        $ [        $ )NTr7   )r@   r%   r$   rE   s    rH   r<   &FilePathSanitizer._get_sanitize_regexp   s    d3''rJ   c                 0    U R                  5       (       a  gg)NrP   rQ   )r@   ro   s    rH   __get_path_separator&FilePathSanitizer.__get_path_separator   s    rJ   )__fname_sanitizer__normalize__split_driver=   )	NNNNNTFN)rS   )__name__
__module____qualname____firstlineno__intr   r_   r   r    r   boolr   r;   r   r`   r   r<   ra   __static_attributes____classcell__rG   s   @rH   r'   r'      s     %)+/?CBF=A(-15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6f1h 1# 1x 1f gcl  c  rJ   r'   c                   `  ^  \ rS rSr% \R
                  " SR                  S \ 5       5      \R                  5      r	\
\S'   Sr\
\S'   \S\\S4   4U 4S	 jj5       r\S
SSSS4S\S\S\\   S\\   S\S\\\      SS4U 4S jjjrS\SS4S jrS\SS4S jrS\SS4S jrS\SS4S jrSrU =r$ )r9      |c              #   V   #    U  H  nS [         R                  " U5       S3v   M!     g7f)z^/$N)reescape).0patterns     rH   	<genexpr>FilePathValidator.<genexpr>   s%     U;T2bii()+;Ts   ')_RE_NTFS_RESERVED)rQ   :_MACOS_RESERVED_FILE_PATHSr2   .c                    > [         TU ]  n[        U R                  5       U R	                  5       U R                  5       /5      (       a  XR                  -   $ U R                  5       (       a  US-   $ U$ )N)rQ   )r:   reserved_keywordsany_is_universal	_is_posix	_is_macosr   	_is_linux)rE   common_keywordsrG   s     rH   r   #FilePathValidator.reserved_keywords   sc    '3""$dnn&68HIJJ"%D%DDD>>"V++rJ   rw   NTr5   r)   r*   r+   r6   r.   c           	         > [         TU ]  UUUUUUS9  [        UU R                  UUUUS9U l        U R                  SS9(       a  [        R                  U l        g [        R                  U l        g )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;   FilePathValidator.__init__   s}     	#)&? 	 	
 "3LL#)&?"
 d3!'!2!2D!*!5!5DrJ   rK   c           
         [        XR                  SS9(       + S9  U R                  U5        U R                  U5      u  p#U(       d  g [	        U5      n[        UR                  U R                  5      5      n[        R                  [        R                  [        R                  U R                  [        R                  U R                  [        R                  U[        R                   U0nXPR"                  :  a   [%        SU R"                  S SUS S3/40 UD6eXPR&                  :  a'  [%        SR)                  U R&                  U5      /40 UD6eU R+                  U5        UR-                  S	S
5      nUR/                  S
5       H-  nU(       a  US;   a  M  U R0                  R3                  U5        M/     U R                  SS9(       a  U R5                  U5        g U R7                  U5        g )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   rd   )_FilePathValidator__validate_win_filepath*_FilePathValidator__validate_unix_filepath)rE   rK   _drivetailrg   byte_ct
err_kwargsrk   s           rH   rd   FilePathValidator.validate   s   %7G7GZ^7G7_3_`e$))%0!$<&--d.?.?@A!;!;!!4==$$d&7&7##W 0

 \\!!7Q7GW^_`Vaagh 	  \\!!U\\g   	(()9:+33D#>%++C0EE[0""++E2	 1 d3(()9:))*:;rJ   c                    [         R                  " U5      n[        [        U5      5      n[	        U R                  5       =(       a    UU R                  5       =(       a    U/5      (       a  g U R                  5       (       a4  [	        X2/5      (       a#  [        SU R                  [        R                  S9  [        SU< SU R                  R                   S3S-   S-   U R                  [        R                  S9nU R                  S	S
9(       a	  U(       a  UeU R                  5       (       d*  [        R                  " U5      u  pVU(       a
  U(       a  Ueg g g )Nz8platform-independent absolute file path is not supported)r+   rT   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+   rT   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_objectrh   _tails          rH   r   "FilePathValidator.validate_abspath   s#    u-!&-0	  "0y$..2B2S|TUUC(A$B$BJ"55 %1%:Nt}}ObObNccefYZ/0 ]]11

 d3!!!,,U3LE   #u "rJ   rg   c                     [         R                  U5      nU(       a%  [        [        R                  " [        U5      S9US9eg )Ninvalid)rK   )r$   findallr   r   r   r   )rE   rg   matchs      rH   __validate_unix_filepath*FilePathValidator.__validate_unix_filepath  s>     (()9:")009NO&  rJ   c                 r   [         R                  U5      nU(       a4  [        [        R                  " [        U5      S9[        R                  US9eU R                  U5      u  p4U(       aM  U R                  R                  U5      nU(       a*  UR                  5       n[        SU S3SUU R                  S9eg g )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   rg   r   r   rK   match_reservedr   s          rH   __validate_win_filepath)FilePathValidator.__validate_win_filepath  s    $,,-=>")009NO!))&  **+;<!33::5AN . 4 4 6'&:;"'"/!]]	   rJ   )__fname_validatorrv   )rx   ry   rz   r{   r   compilerb   r   
IGNORECASEr   r   __annotations__r   propertytupler_   r   r   r|   r   r   r}   r   r;   r   rd   r   r   r   r~   r   r   s   @rH   r9   r9      s)   !zzU;TUU
 u  )32	5c? 	 	 '%)+/#=A66 6 c]	6
 <(6 6 $,HSM#:6 
6 6@.<h .<4 .<`!h !4 !@     rJ   r9   T	file_pathr+   r5   r)   r*   r6   r.   r2   c           	      H    [        UUUc  SOUUUUS9R                  U 5        g)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>`__
Nrw   r+   r5   r)   r*   r6   r.   )r9   rd   r   r+   r5   r)   r*   r6   r.   s          rH   validate_filepathr   3  s1    r o7%"; hyrJ   c           	      F    [        UUUc  SOUUUUS9R                  U 5      $ )a  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()`
rw   r   )r9   is_validr   s          rH   is_valid_filepathr   v  s5    0 o7%"; hyrJ   rL   r,   r-   r/   r0   c                     Ub0  [         R                  " S[        5        USL a  [        R                  n[        UUc  SOUUU	UUUU
S9R                  X5      $ )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`
zD'check_reserved' is deprecated. Use 'reserved_name_handler' instead.Frw   )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     sj    t !R	

 U"$7$=$=!o7-3"; 7	 hy+	,rJ   )
rS   NNNNNNNTF)>__doc__rA   os.pathrY   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   r|   r_   r}   r   r   r    rJ   rH   <module>r      s      	  $ "  " P P M M c c ; * b b @ **q83O3O)PQR(SST%UWYWaWab % b jj		(223A6a8"** e 
q) qh^ ^F (,"!!%9=@@|$@ @ c]	@
 #@ @  (6@ 
@J (,"!!%9=|$  c]	
 #   (6 
H '+!!%%);?>B9=$)l,l,l, |$l, c]	l,
 #l, TNl, !!78l, $$:;l,  (6l, l, "l, l,rJ   