a
    [Xh&                     @   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rdZW n e!y   dZY n0 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   T/var/www/viveiro_mudafortebrasil/venv/lib/python3.9/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| rl| tjj	7 } d}t| } |r| ds| d7 } | drd|  S d|  S dS )zVReturn file URL of `path`.

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

    /Tz///r   file://N)
isinstancer   strbytesdecoder#   endswithospathsepabspathisdirr	   r!   )r.   Zwants_trailing_slashr   r   r   path2urlE   s    





r2   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    r8   Fc                 C   s4   |  |d }|r0t|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    
rC   c                 C   sN   t |rt|S | r"tt| |S |r.t|S tjd| g|R   dS dS )zBLike urllib.urljoin, but warn if base_url is required but missing.z+Relative URI reference without a base URI: N)r8   r%   r   r   error)r@   r$   rA   contextZ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}|r|rzt|}W n ty   t	d	| Y n^0 zt|}W n ty   t	d
| Y n20 |j
r|dd |dd krd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``.

    r9   #r   r$   ZinternalNT)rA   zMalformed URL: %szMalformed base URL: %sZexternal)r:   r;   r!   lenr   rC   r   
ValueErrorr   warningfragment)r>   r?   r@   
attr_valueuriparsedZparsed_baser   r   r   get_link_attribute   s"    rO   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.

    )r8   r2   )stringr   r   r   
ensure_url   s    rQ   
   c                 C   s   t | r| dr"| dd } t| } tt| td||d}| }|	 |
 |d| d}|d}|d	krt|d
|d< nR|dkr| }zt||d< W q tjy   t|d|d< Y q0 n||d< |S td|  dS )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)timeoutrE   charset)redirected_url	mime_typeencodingfilenamezContent-Encodinggzipr   file_objdeflaterP   izNot an absolute URI: %rN)r3   r6   r!   splitr%   r
   r   HTTP_HEADERSinfogeturlget_content_type	get_paramget_filenamer:   r   readzlib
decompressrD   rI   )r$   rU   ssl_contextresponseZresponse_inforesultcontent_encodingdatar   r   r   default_url_fetcher   s4    %


rm   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   rn      s   rn   c                 c   s   z| |}W n< t yH } z$tt|j d| W Y d}~n
d}~0 0 |d| |dd d|v rz@|V  W z|d   W q t y   td|t	  Y q0 qz|d   W n$ t y   td|t	  Y n0 0 n|V  dS )z9Call an url_fetcher, fill in optional data, and clean up.z: NrW   rX   r\   z$Error when closing stream for %s:
%s)
	Exceptionrn   typer   
setdefaultr   r   rJ   	traceback
format_exc)Zurl_fetcherr$   rj   	exceptionr   r   r   fetch   s0    .rv   )F)rR   N)0ro   codecs
contextlibos.pathr-   resysrs   rf   r[   r   pathlibr   urllib.parser   r   r   r   urllib.requestr   r	   r
   r9   r   loggerr   compiler3   r4   getfilesystemencodingr#   lookupnameLookupErrorr_   r   r%   r2   r8   rC   r<   rO   rQ   rm   IOErrorrn   contextmanagerrv   r   r   r   r   <module>   sJ   





F