o
    Zh0h&                     @   sl  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 ddlmZ dd	lmZ ed
ZedZe Zzeej dkredZW n e!yq   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) Z/dS ),z9Various utility functions and classes for URL management.    N)GzipFile)Path)quoteunquoteurljoinurlsplit)Requestpathname2url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   E/var/www/html/rh/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   






r3   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   r9   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   
rD   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)r9   r%   r   r   error)rA   r$   rB   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)rB   zMalformed URL: %szMalformed base URL: %sexternal)r;   r<   r!   lenr   rD   r   
ValueErrorr   warningfragment)r?   r@   rA   
attr_valueuriparsedparsed_baser   r   r   get_link_attribute   s(   rT   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.

    )r9   r3   )stringr   r   r   
ensure_url   s   rV   
   c                 C   s   t | rr| dr| dd } t| } tt| td||d}| }|	 |
 |d| d}|d}|d	krFt|d
|d< |S |dkrl| }z
t||d< W |S  tjyk   t|d|d< Y |S w ||d< |S td|  )u  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.
    :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

        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   )headers)timeoutrF   charset)redirected_url	mime_typeencodingfilenamezContent-Encodinggzipr   file_objdeflaterU   izNot an absolute URI: %r)r4   r7   r!   splitr%   r
   r   HTTP_HEADERSinfogeturlget_content_type	get_paramget_filenamer;   r   readzlib
decompressrE   rM   )r$   rZ   ssl_contextresponseresponse_inforesultcontent_encodingdatar   r   r   default_url_fetcher   s<   
%


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]   ra   z$Error when closing stream for %s:
%s)
	Exceptionrt   typer   
setdefaultr   r   rN   	traceback
format_exc)url_fetcherr$   rp   	exceptionr   r   r   fetch   s:   

r}   )F)rW   N)0ru   codecs
contextlibos.pathr-   resysry   rk   r`   r   pathlibr   urllib.parser   r   r   r   urllib.requestr   r	   r
   r:   r   loggerr   compiler4   r5   getfilesystemencodingr#   lookupnameLookupErrorrd   r   r%   r3   r9   rD   r=   rT   rV   rs   IOErrorrt   contextmanagerr}   r   r   r   r   <module>   sP    




F