o
    ,h                      @   sB   d dl Z d dlmZ d dlmZ G dd deZG dd dZdS )    N)Enum)Optionalc                   @   s    e Zd ZdZdZdZdZdZdS )UrlTypesz4
    Enum to represent different types of URLS.
    r            N)__name__
__module____qualname____doc__UNKNOWNURLINTERNALEXTERNAL r   r   D/var/www/html/rh/venv/lib/python3.10/site-packages/xlsxwriter/url.pyr      s    r   c                   @   s   e Zd ZdZdZdZdefddZdd Ze	d	e
d
ed  fddZed
efddZejdefddZed
efddZejdefddZdd Zdd Zdd Zd
efddZd
efddZeded
efdd Zeded
efd!d"Zd#S )$Urlz.
    A class to represent URLs in Excel.

    i      linkc                 C   s   t j| _|| _|| _|| _d| _d| _d| _d| _	d| _
|   t| j| jkr,tdt| j| jkr8tdt| j| jkrDtd|   d S )N Fr   z#URL exceeds Excel's maximum length.z5Anchor segment or url exceeds Excel's maximum length.z2Hyperlink tool tip exceeds Excel's maximum length.)r   r   
_link_type_original_url_link_relationship_link_text_tip_anchor_is_object_link
_rel_index
_parse_urllenMAX_URL_LEN
ValueErrorMAX_PARAMETER_LEN_escape_strings)selfr   r   r   r   __init__$   s"   zUrl.__init__c                 C   sP   d| j j d| j d| j d| j d| j d| j d| j d| j d	| j	 d
S )zF
        Return a string representation of the Url instance.

        z
Url:
  _link_type         = z
  _original_url      = z
  _link              = z
  _relationship_link = z
  _text              = z
  _tip               = z
  _anchor            = z
  _is_object_link    = z
  _rel_index         = 
)
r   namer   r   r   r   r   r   r   r   r%   r   r   r   __repr__<   s(   	
zUrl.__repr__optionsreturnc                 C   sJ   t |ts	td|d}t |tr#| |d }|dr#|d |_|S )aM  
        For backward compatibility, convert the 'url' key and 'tip' keys in an
        options dictionary to a Url object, or return the Url object if already
        an instance.

        Args:
            options (dict): A dictionary that may contain a 'url' key.

        Returns:
            url: A Url object or None.

        z-The 'options' parameter must be a dictionary.urltip)
isinstancedict	TypeErrorgetstrr   )clsr+   r-   r   r   r   from_optionsO   s   




zUrl.from_optionsc                 C      | j S )z5Get the alternative, user-friendly, text for the URL.r   r)   r   r   r   texti      zUrl.textvaluec                 C   
   || _ dS )z5Set the alternative, user-friendly, text for the URL.Nr7   r%   r:   r   r   r   r8   n      
c                 C   r6   )zGet the screen tip for the URL.r   r)   r   r   r   r.   s   r9   zUrl.tipc                 C   r;   )zSet the screen tip for the URL.Nr>   r<   r   r   r   r.   x   r=   c                 C   s  | j dr0tj| _| js| j | _d| j v r!| j dd\| _ | _| jr*| j	| _
dS | j | _
dS | j drJtj| _| jsH| j ddd| _dS dS | j drutj| _| j ddd| _
| j ddd| _ | j | _| jss| j| _dS dS | j ds| j drtj| _| j	dd	d}|}|d	dd}|ddd}|d
d}| |r|| _ nd	| | _ | js|| _d| j v r| j dd\| _ | _| jr| |r| j dd
| _
n|| _
n| j | _
| j
dr| j
ddd| _
dS dS td| j	 )z%Parse the URL and determine its type.)zhttp://zhttps://zftp://zftps://#r   zmailto:r   z	internal:zfile://z	external:zfile:////\z.\zUnknown URL type: N)r   
startswithr   r   r   r   splitr   r   r   r   replacer   r   _is_relative_pathr"   )r%   file_url	link_pathr   r   r   r   }   sZ   




zUrl._parse_urlc                 C   s"   d| _ | j| _|   |   dS )z
        Set the _is_object_link flag and re-parse the URL since the relationship
        link is different for object links.

        TN)r   r   r   r   r$   r)   r   r   r   _set_object_link   s   zUrl._set_object_linkc                 C   sL   | j tjkr| | j| _| | j| _| j tjkr$| jdd| _dS dS )z-Escape special characters in the URL strings.r?   z%23N)r   r   r   _escape_urlr   r   r   rD   r)   r   r   r   r$      s   zUrl._escape_stringsc                 C   r6   )z$Get the target for relationship IDs.)r   r)   r   r   r   _target   s   zUrl._targetc                 C   s   | j tjkrdS dS )z)Get the target mode for relationship IDs.r   External)r   r   r   r)   r   r   r   _target_mode   s   zUrl._target_moder-   c                 C   s.   |  drdS | d  r| d dkrdS dS )z"Check if a URL is a relative path.z\\Fr   r   :T)rB   isalphar-   r   r   r   rE      s
   
zUrl._is_relative_pathc                 C   sl   t d| r| S | dddddddd	d
dddddddddddddS )z#Escape special characters in a URL.z%[0-9a-fA-F]{2}%z%25"z%22 z%20<z%3c>z%3e[z%5b]z%5d^z%5e`z%60{z%7b}z%7d)researchrD   rO   r   r   r   rI      s   
zUrl._escape_urlN)r   r	   r
   r   r!   r#   r3   r&   r*   classmethodr0   r   r5   propertyr8   setterr.   r   rH   r$   rJ   rL   staticmethodboolrE   rI   r   r   r   r   r      s2    N
r   )r[   enumr   typingr   r   r   r   r   r   r   <module>   s
   
