a
    ¥Q•hè&  ã                   @   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rÊd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   úH/var/www/sistema_ama/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¡ržd| › �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ê|rÞz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rÞddt|j
ƒffS dd|ffS dS )z˜Get the URL value of an element attribute.

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

    r9   ú#r   r$   ÚinternalNT)rA   zMalformed URL: %szMalformed base URL: %séÿÿÿÿZexternal)r:   r;   r!   Úlenr   rC   r   Ú
ValueErrorr   ÚwarningÚfragment)r>   r?   r@   Z
attr_valueÚuriÚparsedZparsed_baser   r   r   Úget_link_attributeŒ   s"    rO   c                 C   s   t | ƒr| S t| ƒS )z²Get 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rŠt|d
�|d< nR|dkrÔ| ¡ }zt |¡|d< W qÜ tjyÐ   t |d¡|d< Y qÜ0 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   iñÿÿÿz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   rJ   )r$   rU   Ússl_contextÚresponseZresponse_infoÚresultZcontent_encodingÚdatar   r   r   Údefault_url_fetcher´   s4    %

þü
rl   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   rm   ú   s   rm   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 ræz@|V  W z|d  ¡  W qä t y¨   t d|t 	¡ ¡ Y qä0 qìz|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)
Ú	Exceptionrm   Útyper   Ú
setdefaultr   r   rK   Ú	tracebackÚ
format_exc)Úurl_fetcherr$   rj   Ú	exceptionr   r   r   Úfetchþ   s0    .þûþrv   )F)rR   N)0rn   ÚcodecsÚ
contextlibÚos.pathr-   ÚreÚsysrr   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   rl   ÚIOErrorrm   Úcontextmanagerrv   r   r   r   r   Ú<module>   sJ   


ý


F