o
    ½ÙøioG  ã                   @   sØ  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	Z	ddl
mZ ddlmZ ddlmZm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 d	dlmZ e d¡Ze d¡Z e !¡ Z"ze #e"¡j$dkrsdZ"W n e%y   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*d6dd„Z+dd „ Z,d!d"„ Z-d#d$„ Z.d%d&„ Z/		d7d(d)„Z0ej1		d8d*d+„ƒZ2G d,d-„ d-e3ƒZ4G d.d/„ d/e5ƒZ6G d0d1„ d1ej7ƒZ8G d2d3„ d3ƒZ9ej1d4d5„ ƒZ:dS )9z9Various utility functions and classes for URL management.é    N)ÚEmailMessage)ÚGzipFile)ÚBytesIOÚStringIO)ÚPath)Úrequest)ÚquoteÚunquoteÚurljoinÚurlsplité   )Ú__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   ú\/home/develop/edictos_py/central_edictos/env/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   Úseekable3   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_uri7   s   
r(   c                 C   s¤   t | tƒr
t| ƒ} n
t | tƒr|  t¡} |  tjj	df¡}tj 
| ¡} |s+tj | ¡r3| tjj	7 } d}t | ¡} |rC|  d¡sC| d7 } |  d¡rMd| › �S d| › �S )zVReturn file URL of `path`.

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

    ú/Tz///r!   zfile://)Ú
isinstancer   ÚstrÚbytesÚdecoder%   ÚendswithÚosÚpathÚsepÚabspathÚisdirr   Úpathname2urlr#   )r0   Úwants_trailing_slashr   r   r   Úpath2urlG   s   







r6   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_absolutef   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_attributel   s   
þÿrG   c                 C   sL   |   d¡rdt| dd… ƒfS t| ƒrdt| ƒfS |r$dtt|| ƒƒfS dS )z.Get tuple describing internal or external URI.ú#Úinternalr   NÚexternal)r#   r	   r<   r(   r
   )r'   rD   r   r   r   Úget_url_tuple   s   
ÿrK   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)rD   r'   rE   Ú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 )z˜Get the URL value of an element attribute.

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

    r=   rH   r   r'   rI   NT)rE   zMalformed URL: %szMalformed base URL: %séÿÿÿÿrJ   )r>   r?   r#   Úlenr	   rG   r   Ú
ValueErrorr   ÚwarningÚfragment)rB   rC   rD   Ú
attr_valueÚuriÚparsedÚparsed_baser   r   r   Úget_link_attribute˜   s(   ÿÿñrX   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.

    )r<   r6   )Ústringr   r   r   Ú
ensure_url¶   s   rZ   é
   c                 C   s*   t jdtd� t||||dd�}| | ¡S )z~Fetch an external resource such as an image or stylesheet.

    This function is deprecated, use ``URLFetcher`` instead.

    a-  default_url_fetcher is deprecated and will be removed in WeasyPrint 69.0, please use URLFetcher instead. For security reasons, HTTP redirects are not supported anymore with default_url_fetcher, but are with URLFetcher.

See https://doc.courtbouillon.org/weasyprint/stable/first_steps.html#url-fetchers©ÚcategoryF)Úallow_redirects)ÚwarningsÚwarnÚDeprecationWarningÚ
URLFetcherÚfetch)r'   ÚtimeoutÚssl_contextÚhttp_headersÚallowed_protocolsÚfetcherr   r   r   Údefault_url_fetcherÀ   s   û
ÿ
ri   c                 c   sr  � |dur	t |ƒ}|du rtƒ }dd„ | ||||fD ƒ}t|ƒdkr0d |¡p(d}	td|	› �ƒ‚| durz|||dœ}
t| d	ƒrD| |
d
< nt| tƒrN| |
d< nt| ƒrW| |
d< n| |
d< t	di |
¤Ž}|�}|V  W d  ƒ dS 1 ssw   Y  dS |dur¥|du r†t
|ƒ}t|dƒ�}||ddfV  W d  ƒ dS 1 sžw   Y  dS |dur÷t||ƒ�?}|rÍ|jdkrÍt d|j› d|j› �¡ tdƒ|ddfV  n|du rÔ|j}|||j|jfV  W d  ƒ dS W d  ƒ dS 1 sðw   Y  dS |du�r|du �rt|ddƒ}|�r| d¡�st |ƒ}||ddfV  dS t|tƒ�r.t|ƒ|ddfV  dS t|ƒ|ddfV  dS )z†If only one input is given, return it.

    Yield a file object, the base url, the protocol encoding and the protocol mime-type.

    Nc                 S   s   g | ]}|d ur|‘qS r   r   )Ú.0Úparamr   r   r   Ú
<listcomp>ß   s
    ÿz!select_source.<locals>.<listcomp>r   z, Únothingz!Expected exactly one source, got )rD   Úurl_fetcherÚcheck_css_mime_typeÚreadÚfile_objÚfilenamer'   Úrbztext/csszUnsupported stylesheet type z for r=   Únameú<r   )rZ   rb   rP   ÚjoinÚ	TypeErrorÚhasattrr*   r   r<   Úselect_sourcer6   Úopenrc   Úcontent_typer   rL   r'   r   ÚcharsetÚgetattrr#   r+   r   )Úguessrr   r'   rq   rY   rD   rn   ro   Úselected_paramsÚsourceÚkwargsÚresultÚresponsert   r   r   r   ry   Ò   sr   €ÿý




"ÿ"ÿ
ÿÿ÷"û


ry   c                   @   ó   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   r…     ó    r…   c                   @   r„   )ÚFatalURLFetchingErrorzESome error happened when fetching an URL and must stop the rendering.Nr†   r   r   r   r   r‰     rˆ   r‰   c                       sF   e Zd ZdZ		d‡ fdd„	Zd‡ fdd	„	Zdd
d„Zdd„ Z‡  ZS )rb   a`  Fetcher of external resources such as images or stylesheets.

    :param int timeout: The number of seconds before HTTP requests are dropped.
    :param ssl.SSLContext ssl_context: An SSL context used for HTTPS requests.
    :param dict http_headers: Additional HTTP headers used for HTTP requests.
    :type allowed_protocols: :term:`sequence`
    :param allowed_protocols: A set of authorized protocols, :obj:`None` means all.
    :param bool allow_redirects: Whether HTTP redirects must be followed.
    :param bool fail_on_errors: Whether HTTP errors should stop the rendering.

    Another class inheriting from this class, with a ``fetch`` method that has a
    compatible signature, can be given as the ``url_fetcher`` argument to
    :class:`weasyprint.HTML` or :class:`weasyprint.CSS`.

    See :ref:`URL Fetchers` for more information and examples.

    r[   NTFc           
         s–   t ƒ  ¡  t ¡ t ¡ t ¡ t ¡ t ¡ t ¡ t 	¡ t 
¡ tj|d�g	}|r-| t ¡ ¡ |D ]}	|  |	¡ q/|| _i t¥|p@i ¥| _|| _|| _d S )N)rM   )Úsuperr   r   ÚProxyHandlerÚUnknownHandlerÚHTTPHandlerÚHTTPDefaultErrorHandlerÚ
FTPHandlerÚFileHandlerÚHTTPErrorProcessorÚDataHandlerÚHTTPSHandlerÚappendÚHTTPRedirectHandlerÚadd_handlerÚ_timeoutÚHTTP_HEADERSÚ_http_headersÚ_allowed_protocolsÚ_fail_on_errors)
r   rd   re   rf   rg   r^   Úfail_on_errorsr�   ÚhandlersÚhandler©Ú	__class__r   r   r   0  s   

ü
zURLFetcher.__init__c           
         s"  t  |¡ }std|› �ƒ‚|d  ¡ }| jdur%|| jvr%td|› �ƒ‚|dkr0| d¡d }t|ƒ}i | j¥|p;i ¥}tj	||d�}t
ƒ j|| jd	�}|}d
|jv r‡|jd
 }|jd
= |dkrgt|d�}n |dkr‡| ¡ }	zt |	¡}W n tjy†   t |	d¡}Y nw t|j||j|jƒS )aE  Fetch a given URL.

        :returns: A :obj:`URLFetcherResponse` instance.
        :raises: An exception indicating failure, e.g. :obj:`ValueError` on
            syntactically invalid URL. All exceptions are catched internally by
            WeasyPrint, except when they inherit from :obj:`FatalURLFetchingError`.

        zNot an absolute URI: r   NzURI uses disallowed protocol: Úfileú?r   ©Úheaders)rd   zContent-EncodingÚgzipr   Údeflateiñÿÿÿ)r7   r:   rQ   Úlowerrš   Úsplitr(   r™   r   ÚRequestrŠ   rz   r—   r¤   r   rp   ÚzlibÚ
decompressrL   ÚURLFetcherResponser'   Ústatus)
r   r'   r¤   r:   r;   Úhttp_requestrƒ   ÚbodyÚcontent_encodingÚdatarŸ   r   r   rc   C  s4   




þzURLFetcher.fetchc                 C   s&   t |tjƒr|  |j|j¡S |  |¡S r   )r*   r   r©   rc   Úfull_urlr¤   )r   r'   r±   rd   r   r   r   rz   t  s   
zURLFetcher.openc                 C   s
   |   |¡S r   )rc   )r   r'   r   r   r   Ú__call__y  s   
zURLFetcher.__call__)r[   NNNTFr   )NN)	r   r   r    r‡   r   rc   rz   r³   Ú__classcell__r   r   rŸ   r   rb     s    ÿ
1rb   c                   @   sr   e Zd ZdZddd„Zdd„ Zdd	„ Zed
d„ ƒZedd„ ƒZ	edd„ ƒZ
dd„ Zdd„ Zedd„ ƒZdd„ ZdS )r¬   u  The HTTP response of an URL fetcher.

    :param str url: The URL of the HTTP response.
    :type body: :class:`str`, :class:`bytes` or :term:`file object`
    :param body: The body of the HTTP response.
    :type headers: dict or email.message.EmailMessage
    :param headers: The headers of the HTTP response.
    :param int status: The status of the HTTP response.

    Has the same interface as :class:`urllib.response.addinfourl`.

    If a :term:`file object` is given for the body, it is the callerâ€™s responsibility to
    call ``close()`` on it. 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.

    NéÈ   c              	   K   sª   || _ || _t|tƒr|| _ntƒ | _|pi  ¡ D ]\}}z|| j|< W q ty-   Y qw t|dƒr8|| _d S t|t	ƒrN| j 
dd¡ t| d¡ƒ| _d S t|ƒ| _d S )Nrp   r|   r   )r'   r­   r*   r   r¤   ÚitemsrQ   rx   Ú	_file_objr+   Ú	set_paramr   r$   )r   r'   r¯   r¤   r­   r�   ÚkeyrF   r   r   r   r   �  s"   
ÿ


zURLFetcherResponse.__init__c                 O   s   | j j|i |¤ŽS r   )r·   rp   )r   Úargsr�   r   r   r   rp   ¥  s   zURLFetcherResponse.readc              	   C   s:   z| j  ¡  W d S  ty   t d| jt ¡ ¡ Y d S w )Nz$Error when closing stream for %s:
%s)r·   r   Ú	Exceptionr   rR   r'   Ú	tracebackÚ
format_excr   r   r   r   r   ¨  s   

þýzURLFetcherResponse.closec                 C   s,   | j  d¡rt | j  d¡d  d¡¡S d S )Nr!   r¢   r   )r'   r#   r   Úurl2pathnamer¨   Úremoveprefixr   r   r   r   r0   ²  s   ÿzURLFetcherResponse.pathc                 C   s
   | j  ¡ S r   )r¤   Úget_content_typer   r   r   r   r{   ·  s   
zURLFetcherResponse.content_typec                 C   s   | j  d¡S )Nr|   )r¤   Ú	get_paramr   r   r   r   r|   »  s   zURLFetcherResponse.charsetc                 C   ó   | j S r   r&   r   r   r   r   Úgeturl¿  ó   zURLFetcherResponse.geturlc                 C   rÂ   r   r£   r   r   r   r   ÚinfoÂ  rÄ   zURLFetcherResponse.infoc                 C   rÂ   r   ©r­   r   r   r   r   ÚcodeÅ  s   zURLFetcherResponse.codec                 C   rÂ   r   rÆ   r   r   r   r   ÚgetcodeÉ  rÄ   zURLFetcherResponse.getcode)NNrµ   )r   r   r    r‡   r   rp   r   Úpropertyr0   r{   r|   rÃ   rÅ   rÇ   rÈ   r   r   r   r   r¬   }  s     





r¬   c              
   c   s  � z| |ƒ}W n' t y. } zt| ddƒrtd|› d�ƒ|‚tt|ƒj› d|› �ƒ‚d}~ww t|tƒrstj	dt
d� d	|vrG| d
|¡|d	< | d| d¡¡|d< | dd¡}| d¡ }rf|d|› �7 }d|i|d< tdi |¤Ž}t|tƒs|J dƒ‚z
|V  W | ¡  dS | ¡  w )aD  Fetch an ``url`` with ```url_fetcher``, fill in optional data, and clean up.

    Fatal errors must raise a ``FatalURLFetchingError`` that stops the rendering. All
    other exceptions are catched and raise an ``URLFetchingError``, that is usually
    catched by the code that fetches the resource and emits a warning.

    r›   FzError fetching "ú"z: NzReturning dicts in URL fetchers is deprecated and will be removed in WeasyPrint 69.0, please return URLFetcherResponse instead.r\   r'   Úredirected_urlrq   rY   r¯   Ú	mime_typezapplication/octet-streamÚencodingz
; charset=zContent-Typer¤   zFURL fetcher must return either a dict or a URLFetcherResponse instancer   )r»   r}   r‰   r…   Útyper   r*   Údictr_   r`   ra   r>   r¬   r   )rn   r'   ÚresourceÚ	exceptionr{   r|   r   r   r   rc   Í  s8   €	€ý
ýÿrc   )F)r[   NNN)NNNNNNNF);r‡   ÚcodecsÚ
contextlibÚos.pathr/   ÚreÚsysr¼   r_   rª   Úemail.messager   r¥   r   Úior   r   Úpathlibr   Úurllibr   Úurllib.parser   r	   r
   r   r=   r   Úloggerr   Úcompiler7   r8   Úgetfilesystemencodingr%   Úlookuprt   ÚLookupErrorr˜   r   r(   r6   r<   rG   rK   r@   rX   rZ   ri   Úcontextmanagerry   ÚIOErrorr…   ÚBaseExceptionr‰   ÚOpenerDirectorrb   r¬   rc   r   r   r   r   Ú<module>   sj    

€ÿý



ÿÿB`P