o
    5ti	                     @   s   U d Z ddlZddlmZ ddlmZ ddlmZmZ ddl	m
Z
 dZeed	< d
Zeed< edeeddejZeed< deddfddZddededefddZdS )zA
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
    N)Final)validate_pathtype)ErrorReasonValidationError   )_preprocess   __MAX_SHEET_NAME_LENz[]:*?/\__INVALID_EXCEL_CHARS[s]__RE_INVALID_EXCEL_SHEET_NAME
sheet_namereturnc                 C   sj   t |  t| tkrtdtt| tjdt| }t	|}|dur3tdt
| tjddS )ah  
    :param str sheet_name: Excel sheet name to validate.
    :raises pathvalidate.ValidationError (ErrorReason.INVALID_CHARACTER):
        If the ``sheet_name`` includes invalid char(s):
        |invalid_excel_sheet_chars|.
    :raises pathvalidate.ValidationError (ErrorReason.INVALID_LENGTH):
        If the ``sheet_name`` is longer than 31 characters.
    z3sheet name is too long: expected<={:d}, actual={:d})descriptionreasonNz,invalid char found in the sheet name: '{:s}')r   lenr	   r   formatr   INVALID_LENGTHr   r   searchreescapegroupINVALID_CHARACTER)r   unicode_sheet_namematch r   R/home/ubuntu/.local/lib/python3.10/site-packages/pytablewriter/sanitizer/_excel.pyvalidate_excel_sheet_name   s$   

r    replacement_textc              
   C   sF   zt | }W n ty } zt|d}~ww t||}|dt S )aE  
    Replace invalid characters for an Excel sheet name within
    the ``sheet_name`` with the ``replacement_text``.
    Invalid characters are as follows:
    |invalid_excel_sheet_chars|.
    The ``sheet_name`` truncate to 31 characters
    (max sheet name length of Excel) from the head, if the length
    of the name is exceed 31 characters.

    :param str sheet_name: Excel sheet name to sanitize.
    :param str replacement_text: Replacement text.
    :return: A replacement string.
    :rtype: str
    :raises ValueError: If the ``sheet_name`` is an invalid sheet name.
    N)r   AttributeError
ValueErrorr   subr	   )r   r!   r   emodify_sheet_namer   r   r   sanitize_excel_sheet_name6   s   r'   )r    )__doc__r   typingr   pathvalidater   pathvalidate.errorr   r   _baser   r	   __annotations__r
   compiler   UNICODEr   strr   r'   r   r   r   r   <module>   s    