o
    h(                     @   sp  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ZddlZddl	m
Z
 ddl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 dd	lmZ ed
ZedZe Zze ej!dkrgdZW n e"ys   dZY nw de dddZ#G dd de
Z$dd Z%dd Z&dd Z'd*ddZ(dd Z)dd  Z*d!d" Z+d+d$d%Z,G d&d' d'e-Z.ej/d(d) Z0dS ),z9Various utility functions and classes for URL management.    N)GzipFile)Path)quoteunquoteurljoinurlsplit)Requestpathname2urlurl2pathnameurlopen   )__version__)LOGGERz^([a-zA-Z][a-zA-Z0-9.+-]+):s   ^([a-zA-Z][a-zA-Z0-9.+-]+):asciiutf-8zWeasyPrint z*/*zgzip, deflate)z
User-AgentAcceptzAccept-Encodingc                   @   s$   e Zd Zdd Zdd Zdd ZdS )StreamingGzipFilec                 C   s   t j| |d || _d S )Nfileobj)r   __init__fileobj_to_close)selfr    r   Q/var/www/html/optinet_system/venv/lib/python3.10/site-packages/weasyprint/urls.pyr   (   s   
zStreamingGzipFile.__init__c                 C   s   t |  | j  d S )N)r   closer   r   r   r   r   r   ,   s   
zStreamingGzipFile.closec                 C   s   dS )NFr   r   r   r   r   seekable0   s   zStreamingGzipFile.seekableN)__name__
__module____qualname__r   r   r   r   r   r   r   r   '   s    r   c                 C   s2   |  dr| S | |  drtnd} t| ddS )zDTurn a Unicode IRI into an ASCII-only URI that conforms to RFC 3986.zdata:file:r   s   /:?#[]@!$&'()*+,;=~%)safe)
startswithencodeFILESYSTEM_ENCODINGr   )urlr   r   r   
iri_to_uri4   s   
r&   c                 C   s   t | tr
t| } n
t | tr| t} | tjj	p| d}tj
| } |s.tj| r6| tjj	7 } d}t| } |rE| dsE| d7 } | drOd|  S d|  S )zVReturn file URL of `path`.

    Accepts 'str', 'bytes' or 'Path', returns 'str'.

    /Tz///r    file://)
isinstancer   strbytesdecoder$   endswithospathsepabspathisdirr	   r"   )r/   wants_trailing_slashr   r   r   path2urlE   s   






r4   c                 C   s    t | trtnt}t|| S )z4Return whether an URL (bytes or string) is absolute.)r)   r*   UNICODE_SCHEME_REBYTES_SCHEME_REboolmatch)r%   schemer   r   r   url_is_absoluted   s   r:   Fc                 C   s4   |  |d }|rt|pd||d| j||fS dS )a'  Get the URI corresponding to the ``attr_name`` attribute.

    Return ``None`` if:

    * the attribute is empty or missing or,
    * the value is a relative URI but the document has no base URI and
      ``allow_relative`` is ``False``.

    Otherwise return an URI, absolute if possible.

     z<%s %s="%s">N)getstripurl_jointag)element	attr_namebase_urlallow_relativevaluer   r   r   get_url_attributej   s   
rE   c                 C   sJ   t |rt|S | rtt| |S |rt|S tjd| g|R   dS )zBLike urllib.urljoin, but warn if base_url is required but missing.z+Relative URI reference without a base URI: N)r:   r&   r   r   error)rB   r%   rC   contextcontext_argsr   r   r   r>   }   s   r>   c                 C   s   |  |d }|drt|dkrddt|dd ffS t| ||dd}|ru|rozt|}W n ty@   t	d	| Y n/w zt|}W n tyV   t	d
| Y nw |j
ro|dd |dd kroddt|j
ffS dd|ffS dS )zGet the URL value of an element attribute.

    Return ``('external', absolute_uri)``, or ``('internal',
    unquoted_fragment_id)``, or ``None``.

    r;   #r   r%   internalNT)rC   zMalformed URL: %szMalformed base URL: %sexternal)r<   r=   r"   lenr   rE   r   
ValueErrorr   warningfragment)r@   rA   rB   
attr_valueuriparsedparsed_baser   r   r   get_link_attribute   s(   rU   c                 C   s   t | r| S t| S )zGet a ``scheme://path`` URL from ``string``.

    If ``string`` looks like an URL, return it unchanged. Otherwise assume a
    filename and convert it to a ``file://`` URL.

    )r:   r4   )stringr   r   r   
ensure_url   s   rW   
   c           	      C   s  t | r| dr| dd } t| d}nd}t| } |dur*i t|}nt}tt	| |d||d}|j
|j |jd|j |d	}|jd
}|dkr]t|d|d< |S |dkr| }z
t||d< W |S  tjy   t|d|d< Y |S w ||d< |S td|  )uP  Fetch an external resource such as an image or stylesheet.

    Another callable with the same signature can be given as the
    ``url_fetcher`` argument to :class:`HTML` or :class:`CSS`.
    (See :ref:`URL Fetchers`.)

    :param str url:
        The URL of the resource to fetch.
    :param int timeout:
        The number of seconds before HTTP requests are dropped.
    :param ssl.SSLContext ssl_context:
        An SSL context used for HTTP requests.
    :param dict http_headers:
        Additional HTTP headers used for HTTP requests.
    :raises: An exception indicating failure, e.g. :obj:`ValueError` on
        syntactically invalid URL.
    :returns: A :obj:`dict` with the following keys:

        * One of ``string`` (a :obj:`bytestring <bytes>`) or ``file_obj``
          (a :term:`file object`).
        * Optionally: ``mime_type``, a MIME type extracted e.g. from a
          *Content-Type* header. If not provided, the type is guessed from the
          file extension in the URL.
        * Optionally: ``encoding``, a character encoding extracted e.g. from a
          *charset* parameter in a *Content-Type* header
        * Optionally: ``redirected_url``, the actual URL of the resource
          if there were e.g. HTTP redirects.
        * Optionally: ``filename``, the filename of the resource. Usually
          derived from the *filename* parameter in a *Content-Disposition*
          header.
        * Optionally: ``path``, the path of the resource if it is stored on the
          local filesystem.

        If a ``file_obj`` key is given, it is the caller’s responsibility
        to call ``file_obj.close()``. The default function used internally to
        fetch data in WeasyPrint tries to close the file object after
        retreiving; but if this URL fetcher is used elsewhere, the file object
        has to be closed manually.

    r(   ?r   r    N)headers)timeoutrG   charset)redirected_url	mime_typeencodingfilenamer/   zContent-Encodinggzipr   file_objdeflaterV   izNot an absolute URI: %r)r5   r8   r"   splitr
   removeprefixr&   HTTP_HEADERSr   r   r%   rZ   get_content_type	get_paramget_filenamer<   r   readzlib
decompressrF   rN   )	r%   r[   ssl_contexthttp_headersr/   responseresultcontent_encodingdatar   r   r   default_url_fetcher   sF   
)


rs   c                   @   s   e Zd ZdZdS )URLFetchingErrorz)Some error happened when fetching an URL.N)r   r   r   __doc__r   r   r   r   rt     s    rt   c                 c   s    z| |}W n t y } ztt|j d| d}~ww |d| |dd d|v rmz!|V  W z	|d   W dS  t yQ   td|t	  Y dS w z|d   W w  t yl   td|t	  Y w w |V  dS )z9Call an url_fetcher, fill in optional data, and clean up.z: Nr]   r^   rb   z$Error when closing stream for %s:
%s)
	Exceptionrt   typer   
setdefaultr   r   rO   	traceback
format_exc)url_fetcherr%   rp   	exceptionr   r   r   fetch	  s:   

r}   )F)rX   NN)1ru   codecs
contextlibos.pathr.   resysry   rk   ra   r   pathlibr   urllib.parser   r   r   r   urllib.requestr   r	   r
   r   r;   r   loggerr   compiler5   r6   getfilesystemencodingr$   lookupnameLookupErrorrf   r   r&   r4   r:   rE   r>   rU   rW   rs   IOErrorrt   contextmanagerr}   r   r   r   r   <module>   sP    




Q