3
Wz)                 @   s|  d Z ddlmZm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ZddlmZ ddlmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z" ej#d dkre	j$d	d
 ne	j$dd ej%dZ&ej%dZ'dd Z(dd Z)dd Z*dd Z+dd Z,dd Z-dd Z.dd Z/dd  Z0d!d" Z1ed#d$Z2d%d& Z3G d'd( d(e4Z5e
j6d)d* Z7dS )+z
    weasyprint.utils
    ----------------

    Various utility functions and classes.

    :copyright: Copyright 2011-2014 Simon Sapin and contributors, see AUTHORS.
    :license: BSD, see LICENSE for details.

    )divisionunicode_literalsN   )VERSION_STRING)LOGGER)urljoinurlsplitquoteunquoteunquote_to_bytesurlopenurllib_get_content_typeurllib_get_charseturllib_get_filenameRequestparse_emailpathname2urlunicodebase64_decodeStreamingGzipFileFILESYSTEM_ENCODING   zimage/svg+xmlz.svgs   image/svg+xmls   .svgz^([a-zA-Z][a-zA-Z0-9.+-]+):s   ^([a-zA-Z][a-zA-Z0-9.+-]+):c             C   s2   | j dr| S | j| j dr tnd} t| ddS )zmTurn an IRI that can contain any Unicode character into an ASCII-only
    URI that conforms to RFC 3986.
    zdata:zfile:zutf-8s   /:?#[]@!$&'()*+,;=~%)safe)
startswithencoder   r	   )url r   O/var/www/html/enquirykeeper_venv/lib/python3.6/site-packages/weasyprint/urls.py
iri_to_uri2   s
    
r   c             C   sJ   t jj| } t jj| r$| t jj7 } t| } | jdr>d|  S d|  S dS )zReturn file URL of `path`z///zfile:zfile://N)ospathabspathisdirsepr   r   )r    r   r   r   path2urlE   s    
r$   c             C   s   t t| trtntj| S )N)bool
isinstancer   UNICODE_SCHEME_REBYTES_SCHEME_REmatch)r   r   r   r   url_is_absoluteU   s    r*   c             C   s   | j  jjS )u   Return the URL associated with a lxml document.

    This is the same as the HtmlElement.base_url property, but dont’t want
    to require HtmlElement.

    )ZgetroottreeZdocinfoURL)elementr   r   r   element_base_url[   s    r-   c             C   s4   | j |dj }|r0tt| |d| j||| jS dS )zGet 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.

    Otherwise, return an absolute URI.

     z<%s %s="%s"> at line %sN)getstripurl_joinr-   tagZ
sourceline)r,   	attr_namevaluer   r   r   get_url_attributee   s
    
r5   c             G   s>   t |rt|S | r"tt| |S tjd| f|  dS dS )zgLike urllib.urljoin, but issue a warning and return None if base_url
    is required but missing.

    z+Relative URI reference without a base URI: N)r*   r   r   r   warning)Zbase_urlr   contextargsr   r   r   r1   w   s    r1   c             C   s   | j |dj }|jdr:t|dkr:dt|dd fS t| |}|rt| }|rt|}|dd t|dd krdt|jfS d|fS dS )	zZReturn ('external', absolute_uri) or
    ('internal', unquoted_fragment_id) or None.

    r.   #r   ZinternalNZexternalr:   )	r/   r0   r   lenr
   r5   r-   r   fragment)r,   r3   Z
attr_valueZuriZdocument_urlparsedr   r   r   get_link_attribute   s    
r>   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.

    )r*   r$   )stringr   r   r   
ensure_url   s    r@   c             C   s(   dt | d  }|r | d| 7 } t| S )ai  Decode base64, padding being optional.

    "From a theoretical point of view, the padding character is not needed,
     since the number of missing bytes can be calculated from the number
     of Base64 digits."

    https://en.wikipedia.org/wiki/Base64#Padding

    :param data: Base64 data as an ASCII byte string
    :returns: The decoded byte string.

          =)r;   r   )datamissing_paddingr   r   r   safe_base64_decode   s    rE   c       	      C   s   y| j dd\}}W n tk
r0   tdY nX |dd }|r|jd}|dkrd||d kr|d| }||d d }n|}d	}td
| }|j }|j }nd}d}d	}t|}|dkrt|}t	|||| dS )u   Decode URLs with the 'data' scheme. urllib can handle them
    in Python 2, but that is broken in Python 3.

    Inspired from Python 2.7.2’s urllib.py.

    ,r   zbad data URL   N;r   =r.   zContent-type: z
text/plainzUS-ASCIIbase64)r?   	mime_typeencodingredirected_url)
split
ValueErrorIOErrorrfindr   get_content_typeget_content_charsetr   rE   dict)	r   headerrC   Zsemicontent_typerL   messagerK   charsetr   r   r   open_data_url   s.    

rY   zgzip, deflate)z
User-AgentzAccept-Encodingc             C   s  | j  jdrt| S tj| rt| } tt| td}t	|j
 t|t|t|d}|j jd}|dkrtdkrtjtj|j dj |d< |j  nt|d|d	< nX|d
kr |j }ytj||d< W n( tjk
 r   tj|d|d< Y nX n||d	< |S td|  dS )uF  Fetch an external resource such as an image or stylesheet.

    Another callable with the same signature can be given as the
    :obj:`url_fetcher` argument to :class:`HTML` or :class:`CSS`.
    (See :ref:`url-fetchers`.)

    :type url: Unicode string
    :param url: The URL of the resource to fetch
    :raises: any exception to indicate failure. Failures are logged
        as warnings, with the string representation of the exception
        in the message.
    :returns: In case of success, a dict with the following keys:

        * One of ``string`` (a byte string) or ``file_obj``
          (a file-like object)
        * Optionally: ``mime_type``, a MIME type extracted eg. 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 eg. from a
          *charset* parameter in a *Content-Type* header
        * Optionally: ``redirected_url``, the actual URL of the resource
          in case there were eg. 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 responsability
        to call ``file_obj.close()``.

    zdata:)headers)rM   rK   rL   filenamezContent-EncodinggzipN)fileobjr?   file_objdeflate   zNot an absolute URI: %ri)lowerr   rY   r'   r)   r   r   r   HTTP_HEADERSrT   geturlr   r   r   infor/   r   r\   ZGzipFileioBytesIOreadclosezlib
decompresserrorrO   )r   responseresultZcontent_encodingrC   r   r   r   default_url_fetcher   s2    

rn   c               @   s   e Zd ZdZdS )URLFetchingErrorz)Some error happened when fetching an URL.N)__name__
__module____qualname____doc__r   r   r   r   ro   $  s   ro   c          "   c   s   y| |}W nL t k
rX } z0t|j}t|}t|rBd||f n|W Y dd}~X nX |jd| |jdd d|krz
|V  W dy|d j  W n& t k
r   tjd|t	j
  Y nX X n|V  dS )z9Call an url_fetcher, fill in optional data, and clean up.z%s: %sNrM   rK   r^   z$Error when closing stream for %s:
%s)	Exceptiontyperp   strro   
setdefaultrh   r   r6   	traceback
format_exc)Zurl_fetcherr   rm   excnamer4   r   r   r   fetch(  s"    
*
r|   )8rs   
__future__r   r   re   resysos.pathr   Z	mimetypes
contextlibr\   ri   rx   r.   r   loggerr   compatr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   version_infoZadd_typecompiler'   r(   r   r$   r*   r-   r5   r1   r>   r@   rE   rY   rb   rn   rP   ro   contextmanagerr|   r   r   r   r   <module>   sB   H	



*>