
    Ki                        d dl Z d dlmZ d dlmZ dZdZdZdZdZ	d	Z
d
Ze	e
z   ez   ZefdededefdZ G d dee      ZdedededefdZedeeej$                  efdedededz  dededededefd       Zedededefd       Zy)    N)Enum)bear_enforce      -_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789valuesizereturnc                 j    t        j                  | j                               j                         d| S )z&Generate a hash fragment of the value.N)hashlibsha256encode	hexdigestr   r   s     k/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/key_value/shared/utils/sanitize.pygenerate_hash_fragmentr      s)     >>%,,.)335et<<    c                       e Zd ZdZdZdZy)HashFragmentModealwaysneveronly_if_changedN)__name__
__module____qualname__ALWAYSNEVERONLY_IF_CHANGED r   r   r   r      s    FE'Or   r   allowed_charactersreplace_withc                 j    d}d}| D ]  }||v r||z  }d}|r||z  }d} t        |      dk(  r|ry|S )a  Replace characters in a string. If multiple characters are in a row that are not allowed, only
    the first one will be replaced. The rest will be removed. If all characters are not allowed, an
    empty string will be returned.

    Args:
        value: The value to replace characters in.
        to_replace: The characters to replace.
        replace_with: The characters to replace with.
     FT   )len)r   r$   r%   	new_valuelast_char_was_replacedchars         r   sanitize_characters_in_stringr-   "   sg     I" 	*%%I%*"%%I%)"	* 9~5r   
max_lengthreplacement_characterhash_fragment_separatorhash_fragment_modehash_fragment_lengthc                    |t         k  rdt          }t        |      ||dz  kD  rd}t        |      t        | |      }t        |      t        |      z   }	|rt	        | ||      n| }
|t
        j                  k(  r||	z
  }|
d| }
|
s|S |
|z   |z   S |t
        j                  k(  r"|
d| }
| |
k(  r| S ||	z
  }|
d| }
|
s|S |
|z   |z   S |
sd}t        |      |
S )a  Sanitize the value, replacing characters and optionally adding a fragment a hash of the value if requested.

    If the entire value is sanitized and hash_fragment_mode is HashFragmentMode.ALWAYS or HashFragmentMode.ONLY_IF_CHANGED,
    the value returned will be the hash fragment only.

    If the entire value is sanitized and hash_fragment_mode is HashFragmentMode.NEVER, an error will be raised.

    Args:
        value: The value to sanitize.
        allowed_characters: The allowed characters in the value.
        max_length: The maximum length of the value (with the hash fragment added).
        hash_fragment_separator: The separator to add between the value and the hash fragment.
        hash_fragment_mode: The mode to add the hash fragment.
    z,max_length must be greater than or equal to    zEhash_fragment_length must be less than or equal to half of max_lengthr   )r   r$   r%   NzKEntire value was sanitized and hash_fragment_mode is HashFragmentMode.NEVER)MINIMUM_MAX_LENGTH
ValueErrorr   r)   r-   r   r    r"   )r   r.   r$   r/   r0   r1   r2   msghash_fragmenthash_fragment_size_requiredsanitized_valueactual_max_lengths               r   sanitize_stringr<   @   s9   0 &&<=O<PQojAo-Uo/eBVWM'*+B'Cc-FX'X  	&EFXg|}  -444&)DD)*<+<=  !88=HH-===)+:6O#L&)DD)*<+<=  !88=HH[or   c                     |t         k  rdt          }t        |      t        |       |k  r| S | d|dz
   }t        j                  | j                               j                         }|dd }||z   S )a  Hash part of the value if it exceeds the maximum length. This operation
    will truncate the value to the maximum length minus 8 characters and will swap
    the last 8 characters with the first 8 characters of the generated hash.

    Args:
        value: The value to hash.
        max_length: The maximum length of the value. Must be greater than 32.

    Returns:
        The hashed value if the value exceeds the maximum length, otherwise the original value.
    z max_length must be greater than Nr   )r5   r6   r)   r   r   r   r   )r   r.   r7   truncated_valuehash_of_valuefirst_eight_of_hashs         r   hash_excess_lengthrA      s}     ''01C0DEo
5zZ,j1n-ONN5<<>2<<>M'+000r   )r   enumr   )key_value.shared.type_checking.bear_sprayr   r5   DEFAULT_HASH_FRAGMENT_SIZEDEFAULT_HASH_FRAGMENT_SEPARATORDEFAULT_REPLACEMENT_CHARACTERLOWERCASE_ALPHABETUPPERCASE_ALPHABETNUMBERSALPHANUMERIC_CHARACTERSstrintr   r   r-   r"   r<   rA   r#   r   r   <module>rM      sL     B  "%  # 1 1 
,/AAGK 
 +==
= 	=(sD ( # UX ]` <  &*!>#B+;+K+K :GGG d
G 	G
 !G )G G 	G GT 1c 1s 1s 1 1r   