HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/env/lib/python3.10/site-packages/weasyprint/__pycache__/urls.cpython-310.pyc
o

&we�%�@sldZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
ddlmZddl
mZmZmZmZddlmZmZmZddlmZdd	lmZe�d
�Ze�d�Ze��Zze�e�j dkred
ZWne!yqd
ZYnwde��ddd�Z"Gdd�de
�Z#dd�Z$dd�Z%dd�Z&d*dd�Z'dd�Z(dd �Z)d!d"�Z*d+d$d%�Z+Gd&d'�d'e,�Z-ej.d(d)��Z/dS),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-8zWeasyPrint z*/*z
gzip, deflate)z
User-Agent�AcceptzAccept-Encodingc@s$eZdZdd�Zdd�ZdZZdS)�StreamingGzipFilecCstj||d�||_dS)N��fileobj)r�__init__�fileobj_to_close)�selfr�r�H/home/arjun/projects/env/lib/python3.10/site-packages/weasyprint/urls.pyr(s
zStreamingGzipFile.__init__cCst�|�|j��dS)N)r�closer)rrrrr,s
zStreamingGzipFile.closeN)�__name__�
__module__�__qualname__rr�seek�tellrrrrr'srcCs2|�d�r|S|�|�d�rtnd�}t|dd�S)zDTurn a Unicode IRI into an ASCII-only URI that conforms to RFC 3986.zdata:�file:rs/:?#[]@!$&'()*+,;=~%)�safe)�
startswith�encode�FILESYSTEM_ENCODINGr)�urlrrr�
iri_to_uri4s
�r%cCs�t|t�r
t|�}n
t|t�r|�t�}|�tjj	�p|�d�}tj�
|�}|s.tj�|�r6|tjj	7}d}t|�}|rE|�d�sE|d7}|�
d�rOd|��Sd|��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_slashrrr�path2urlEs






r3cCs t|t�rtnt}t|�|��S)z4Return whether an URL (bytes or string) is absolute.)r(r)�UNICODE_SCHEME_RE�BYTES_SCHEME_RE�bool�match)r$�schemerrr�url_is_absolutedsr9FcCs4|�|d���}|rt|pd||d|j||f�SdS)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�valuerrr�get_url_attributejs
��rDcCsJt|�rt|�S|rtt||��S|rt|�Stjd|��g|�R�dS)zBLike urllib.urljoin, but warn if base_url is required but missing.z+Relative URI reference without a base URI: N)r9r%rr
�error)rAr$rB�context�context_argsrrrr=}s��r=cCs�|�|d���}|�d�rt|�dkrddt|dd��ffSt|||dd�}|rO|rIt|�}|jrI|dd	�t|�dd	�krIddt|j�ffSdd
|ffSdS)z�Get the URL value of an element attribute.

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

    r:�#rr$�internalNT)rB����external)r;r<r!�lenrrDr�fragment)r?r@rA�
attr_value�uri�parsedrrr�get_link_attribute�s"�rQcCst|�r|St|�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.

    )r9r3)�stringrrr�
ensure_url�srS�
cCs�t�|�rr|�d�r|�d�d}t|�}tt|td�||d�}|��}|�	�|�
�|�d�|��d�}|�
d�}|d	krFt|d
�|d<|S|dkrl|��}z
t�|�|d
<W|Stjykt�|d�|d
<Y|Sw||d<|Std|��)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�filenamezContent-Encoding�gzipr�file_obj�deflaterRi�zNot an absolute URI: %r)r4r7r!�splitr%r
r�HTTP_HEADERS�info�geturl�get_content_type�	get_param�get_filenamer;r�read�zlib�
decompressrE�
ValueError)r$rW�ssl_context�response�
response_info�result�content_encoding�datarrr�default_url_fetcher�s<
%
��

���rqc@seZdZdZdS)�URLFetchingErrorz)Some error happened when fetching an URL.N)rrr�__doc__rrrrrr�srrccs��z||�}Wnty}ztt|�j�d|����d}~ww|�d|�|�dd�d|vrmz!|VWz	|d��WdStyQt�d|t�	��YdSwz|d��Wwtylt�d|t�	��Yww|VdS)z9Call an url_fetcher, fill in optional data, and clean up.z: NrYrZr^z$Error when closing stream for %s:
%s)
�	Exceptionrr�typer�
setdefaultrr
�warning�	traceback�
format_exc)�url_fetcherr$rn�	exceptionrrr�fetch�s:���
�����
r|)F)rTN)0rs�codecs�
contextlib�os.pathr-�re�sysrxrhr]r�pathlibr�urllib.parserrrr�urllib.requestrr	r
r:r�loggerr
�compiler4r5�getfilesystemencodingr#�lookup�name�LookupErrorrarr%r3r9rDr=rQrSrq�IOErrorrr�contextmanagerr|rrrr�<module>sP

���



F