
    ,g&                        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j4                  d
      Z ej4                  d      Z ej:                         Z	  ej>                  e      j@                  dk(  rdZde dddZ" G d de
      Z#d Z$d Z%d Z&ddZ'd Z(d Z)d Z*d dZ+ G d de,      Z-ej\                  d        Z/y# e!$ r dZY Uw xY w)!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                       e Zd Zd Zd Zd Zy)StreamingGzipFilec                 @    t        j                  | |       || _        y )Nfileobj)r   __init__fileobj_to_close)selfr   s     g/home/viktor/gitlab-persoonlijk/factuur-applicatie/venv/lib/python3.12/site-packages/weasyprint/urls.pyr   zStreamingGzipFile.__init__(   s    $0 '    c                 b    t        j                  |        | j                  j                          y )N)r   closer   r   s    r   r   zStreamingGzipFile.close,   s     t##%r   c                      y)NF r   s    r   seekablezStreamingGzipFile.seekable0   s    r   N)__name__
__module____qualname__r   r   r!   r    r   r   r   r   '   s    (&r   r   c                     | j                  d      r| S | j                  | j                  d      rt        nd      } t        | 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   )urls    r   
iri_to_urir,   4   sC    
~~g

**"~~g6GEC 233r   c                 6   t        | t              rt        |       } n%t        | t              r| j	                  t
              } | j                  t        j                  j                        xs | j                  d      }t        j                  j                  |       } |st        j                  j                  |       r| t        j                  j                  z  } d}t        |       } |r| j                  d      s| dz  } | j                  d      rd|  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(   )r6   wants_trailing_slashs     r   path2urlr;   E   s     $4y	D%	 {{./==5Ks9K77??4 Drww}}T2 	#DDMM#$6u tf~r   c                 n    t        | t              rt        nt        }t	        |j                  |             S )z4Return whether an URL (bytes or string) is absolute.)r0   r1   UNICODE_SCHEME_REBYTES_SCHEME_REboolmatch)r+   schemes     r   url_is_absoluterB   d   s'    ",S#"6OFS!""r   c           	          | j                  |d      j                         }|r t        |xs d||d| j                  ||f      S y)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values        r   get_url_attributerN   j   sM     KK	2&,,.ENE>>[[)U+- 	- r   c                     t        |      rt        |      S | rt        t        | |            S |rt        |      S t        j                  d| g|  y)zBLike urllib.urljoin, but warn if base_url is required but missing.z+Relative URI reference without a base URI: N)rB   r,   r   r   error)rK   r+   rL   contextcontext_argss        r   rG   rG   }   sY    s#	'(C011	#9'C		 r   c                    | j                  |d      j                         }|j                  d      r t        |      dkD  rddt	        |dd       ffS t        | ||d      }|rP|rH	 t        |      }	 t        |      }|j                  r$|dd	 |dd	 k(  rddt	        |j                        ffS dd|ffS y# t        $ r t        j                  d
|       Y (w xY w# t        $ r t        j                  d|       Y Mw xY w)zGet the URL value of an element attribute.

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

    rD   #r   r+   internalNT)rL   zMalformed base URL: %szMalformed URL: %sexternal)rE   rF   r(   lenr   rN   r   fragment
ValueErrorr   warning)rI   rJ   rK   
attr_valueuriparsedparsed_bases          r   get_link_attributer`      s    Y+113JS!c*o&9
GJqrN$;<==
GY
NC
O!#O"*8"4K
 6#2;+cr:J+J %
GFOO4L'MNN
C())  " GNN#;XFG  92C89s$   &C 2B4 4CCC;:C;c                 2    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.

    )rB   r;   )strings    r   
ensure_urlrc      s     %V,6B(62BBr   c                    t         j                  |       r| j                  d      r| j                  d      d   } t	        |       } t        t        | t              ||      }|j                         }|j                         |j                         |j                  d      |j                         d}|j                  d      }|d	k(  rt        |
      |d<   |S |dk(  r+|j                         }	 t!        j"                  |      |d<   |S ||d<   |S t'        d| z        # t         j$                  $ r t!        j"                  |d      |d<   Y |S w xY w)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)timeoutrQ   charset)redirected_url	mime_typeencodingfilenamezContent-Encodinggzipr   file_objdeflaterb   izNot an absolute URI: %r)r=   r@   r(   splitr,   r   r	   HTTP_HEADERSinfogeturlget_content_type	get_paramget_filenamerE   r   readzlib
decompressrP   rZ   )r+   rg   ssl_contextresponseresponse_inforesultcontent_encodingdatas           r   default_url_fetcherr      sP   J s#>>)$))C.#CoC.! !&oo/&779%//	:%224	
 ),,-?@v%!28!DF:  *==?D>#'??4#8x   "*F:2S899 :: >#'??4#=x  >s   0D ,EEc                       e Zd ZdZy)URLFetchingErrorz)Some error happened when fetching an URL.N)r"   r#   r$   __doc__r    r   r   r   r      s    3r   r   c           	   #   $  K   	  | |      }|j	                  d|       |j	                  dd       d|v r	 | 	 |d   j                          y| y# t         $ r(}t        t        |      j                   d|       d}~ww xY w# t         $ r, t        j                  d|t        j                                Y yw xY w# 	 |d   j                          w # t         $ r, t        j                  d|t        j                                Y w w xY wxY ww)z9Call an url_fetcher, fill in optional data, and clean up.z: Nri   rj   rn   z$Error when closing stream for %s:
%s)
	Exceptionr   typer"   
setdefaultr   r   r[   	traceback
format_exc)url_fetcherr+   r}   	exceptions       r   fetchr      s    KS! &,
k4(V
	1L1z"((* #  K$y/":":!;2i[IJJK  1 ;--/111z"((* 1 ;--/11s   DA (DB? B D	B#A??BD2B<9D;B<<D?DCD2D
D	D

DD)F)
   N)0r   codecs
contextlibos.pathr5   resysr   rx   rm   r   pathlibr   urllib.parser   r   r   r   urllib.requestr	   r
   r   rD   r   loggerr   compiler=   r>   getfilesystemencodingr*   lookupnameLookupErrorrq   r   r,   r;   rB   rN   rG   r`   rc   r   IOErrorr   contextmanagerr   r    r   r   <module>r      s    ?    	 
     : : 9 9  
 BJJ<= "**;< 0c//1 "v}}()..'9%
  }-&
 
4" >#-&*<CC:L4w 4  E  "!"s   !C0 0C:9C: