a
    ¥Q•hD  ã                   @   sF  d Z ddl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
Z
ddlZddlZd ZZdddddddd	d	d	d	d	ddd	d	dd
œZg d¢ZddlmZmZmZmZmZ ddlmZmZ dd„ ZG dd„ dƒZG dd„ dƒZG dd„ dƒZejdddddded	fdd„ƒZddl m!Z! ddl"m#Z#m$Z$m%Z%m&Z& ddl'm(Z(m)Z) dS )z~The Awesome Document Factory.

The public API is what is accessible from this "root" packages without
importing sub-modules.

é    N)Údatetime)ÚgetctimeÚgetmtime)ÚPath)Úurljoinz65.0ÚprintF)ÚstylesheetsÚ
media_typeÚattachmentsZpdf_identifierZpdf_variantZpdf_versionZ	pdf_formsZuncompressed_pdfZcustom_metadataZpresentational_hintsÚsrgbZoptimize_imagesZjpeg_qualityÚdpiZ
full_fontsZhintingÚcache)	ÚCSSÚDEFAULT_OPTIONSÚHTMLÚVERSIONÚ
AttachmentÚDocumentÚPageÚ__version__Údefault_url_fetcheré   )Úfetchr   Úpath2urlÚ
ensure_urlÚurl_is_absolute)ÚLOGGERÚPROGRESS_LOGGERc                 C   s>   t t|  d¡ƒdƒ}|dur:| dd¡ ¡ }|r:t||ƒS |S )zlReturn the base URL for the document.

    See https://www.w3.org/TR/html5/urls.html#document-base-url

    ÚbaseNÚhrefÚ )ÚnextÚiterÚgetÚstripr   )Zhtml_documentZfallback_base_urlZfirst_base_elementr   © r%   úL/var/www/sistema_ama/venv/lib/python3.9/site-packages/weasyprint/__init__.pyÚ_find_base_urlc   s    
r'   c                	   @   sZ   e Zd ZdZdddddddedf	dd„Zddd„Zd	d
„ Zdd„ Zddd„Z	ddd„Z
dS )r   a±  HTML document parsed by tinyhtml5.

    You can just create an instance with a positional argument:
    ``doc = HTML(something)``
    The class will try to guess if the input is a filename, an absolute URL,
    or a :term:`file object`.

    Alternatively, use **one** named argument so that no guessing is involved:

    :type filename: str or pathlib.Path
    :param filename:
        A filename, relative to the current directory, or absolute.
    :param str url:
        An absolute, fully qualified URL.
    :type file_obj: :term:`file object`
    :param file_obj:
        Any object with a ``read`` method.
    :param str string:
        A string of HTML source.

    Specifying multiple inputs is an error:
    ``HTML(filename="foo.html", url="localhost://bar.html")``
    will raise a :obj:`TypeError`.

    You can also pass optional named arguments:

    :param str encoding:
        Force the source character encoding.
    :type base_url: str or pathlib.Path
    :param base_url:
        The base used to resolve relative URLs (e.g. in
        ``<img src="../foo.png">``). If not provided, try to use the input
        filename, URL, or ``name`` attribute of
        :term:`file objects <file object>`.
    :type url_fetcher: :term:`callable`
    :param url_fetcher:
        A function or other callable with the same signature as
        :func:`default_url_fetcher` called to fetch external resources such as
        stylesheets and images. (See :ref:`URL Fetchers`.)
    :param str media_type:
        The media type to use for ``@media``. Defaults to ``'print'``.
        **Note:** In some cases like ``HTML(string=foo)`` relative URLs will be
        invalid if ``base_url`` is not provided.

    Nr   c
                 C   sþ   t  d|p|p|pt|ddƒ¡ t|tƒr2t|ƒ}t|||||||ƒ}
|
�l\}}}}t|tƒrntj|dd�}
n:ddi}|d ur†||d< |d ur–||d< tj|fi |¤Ž}
W d   ƒ n1 s¼0    Y  t	|
|ƒ| _
|| _|	| _tjj|
d d	�| _| jj| _d S )
Nz'Step 1 - Fetching and parsing HTML - %sÚnamezHTML stringF)Únamespace_html_elementsr)   Ztransport_encodingZoverride_encoding)Zcontent_language)r   ÚinfoÚgetattrÚ
isinstancer   ÚstrÚ_select_sourceÚ	tinyhtml5Úparser'   Úbase_urlÚurl_fetcherr	   Ú
cssselect2ZElementWrapperZfrom_html_rootZwrapper_elementZetree_element)ÚselfÚguessÚfilenameÚurlÚfile_objÚstringÚencodingr1   r2   r	   ÚresultÚsource_typeÚsourceÚprotocol_encodingÚkwargsr%   r%   r&   Ú__init__Ÿ   s4    
ý
ÿ
0ÿzHTML.__init__Fc                 C   s   |rt tgS t gS ©N)ÚHTML5_UA_STYLESHEETÚHTML5_UA_FORM_STYLESHEET)r4   Úformsr%   r%   r&   Ú_ua_stylesheets»   s    zHTML._ua_stylesheetsc                 C   s
   t  ¡ gS rA   )ÚHTML5_UA_COUNTER_STYLEÚcopy©r4   r%   r%   r&   Ú_ua_counter_styleÀ   s    zHTML._ua_counter_stylec                 C   s   t gS rA   )ÚHTML5_PH_STYLESHEETrH   r%   r%   r&   Ú_ph_stylesheetsÃ   s    zHTML._ph_stylesheetsc                 K   sH   t |ƒt tƒ D ]}t d|¡ qt ¡ }| |¡ |}t | |||¡S )að  Lay out and paginate the document, but do not (yet) export it.

        This returns a :class:`document.Document` object which provides
        access to individual pages and various meta-data.
        See :meth:`write_pdf` to get a PDF directly.

        :type font_config: :class:`text.fonts.FontConfiguration`
        :param font_config:
            A font configuration handling ``@font-face`` rules.
        :type counter_style: :class:`css.counters.CounterStyle`
        :param counter_style:
            A dictionary storing ``@counter-style`` rules.
        :param options:
            The ``options`` parameter includes by default the
            :data:`DEFAULT_OPTIONS` values.
        :returns: A :class:`document.Document` object.

        zUnknown rendering option: %s.)Úsetr   r   ÚwarningrG   Úupdater   Ú_render)r4   Úfont_configÚcounter_styleÚoptionsÚunknownÚnew_optionsr%   r%   r&   ÚrenderÆ   s    
zHTML.renderr   c                 K   s<   t  ¡ }| |¡ |}| j||fi |¤Žj|||fi |¤ŽS )aE  Render the document to a PDF file.

        This is a shortcut for calling :meth:`render`, then
        :meth:`Document.write_pdf() <document.Document.write_pdf>`.

        :type target:
            :class:`str`, :class:`pathlib.Path` or :term:`file object`
        :param target:
            A filename where the PDF file is generated, a file object, or
            :obj:`None`.
        :param float zoom:
            The zoom factor in PDF units per CSS units.  **Warning**:
            All CSS units are affected, including physical units like
            ``cm`` and named sizes like ``A4``.  For values other than
            1, the physical CSS units will thus be "wrong".
        :type finisher: :term:`callable`
        :param finisher:
            A finisher function or callable that accepts the document and a
            :class:`pydyf.PDF` object as parameters. Can be passed to perform
            post-processing on the PDF right before the trailer is written.
        :type font_config: :class:`text.fonts.FontConfiguration`
        :param font_config:
            A font configuration handling ``@font-face`` rules.
        :type counter_style: :class:`css.counters.CounterStyle`
        :param counter_style:
            A dictionary storing ``@counter-style`` rules.
        :param options:
            The ``options`` parameter includes by default the
            :data:`DEFAULT_OPTIONS` values.
        :returns:
            The PDF as :obj:`bytes` if ``target`` is not provided or
            :obj:`None`, otherwise :obj:`None` (the PDF is written to
            ``target``).

        )r   rG   rN   rU   Ú	write_pdf)r4   ÚtargetÚzoomZfinisherrP   rQ   rR   rT   r%   r%   r&   rV   à   s    %
ÿÿÿzHTML.write_pdf)F)NN)Nr   NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r@   rE   rI   rK   rU   rV   r%   r%   r%   r&   r   q   s   -þ


  ÿr   c                   @   s6   e Zd ZdZdddddddeddddddfdd„ZdS )r   a#  CSS stylesheet parsed by tinycss2.

    An instance is created in the same way as :class:`HTML`, with the same
    arguments.

    An additional argument called ``font_config`` must be provided to handle
    ``@font-face`` rules. The same ``text.fonts.FontConfiguration`` object
    must be used for different ``CSS`` objects applied to the same document.

    ``CSS`` objects have no public attributes or methods. They are only meant
    to be used in the :meth:`HTML.write_pdf` and :meth:`HTML.render` methods
    of :class:`HTML` objects.

    NFr   c              
   C   sî   t  d|p|pt|ddƒ¡ t||||||||	d�}|�R\}}}}|dkrR| ¡ }t|tƒrht |¡}ntj	|||d�\}}W d   ƒ n1 s�0    Y  || _
|pªt ¡ | _|d u rºg n|| _|d u rÌi n|}t|
|||| j| j||ƒ d S )Nz&Step 2 - Fetching and parsing CSS - %sr(   z
CSS string©r1   r2   Úcheck_css_mime_typer8   )Zenvironment_encodingr>   )r   r*   r+   r.   Úreadr,   r-   Útinycss2Zparse_stylesheetZparse_stylesheet_bytesr1   r3   ZMatcherÚmatcherÚ
page_rulesÚpreprocess_stylesheet)r4   r5   r6   r7   r8   r9   r:   r1   r2   Z_check_mime_typer	   rP   rQ   ra   rb   r;   r<   r=   r>   Ú
stylesheetr%   r%   r&   r@     s4    þ
ý
þ(þzCSS.__init__©rY   rZ   r[   r\   r   r@   r%   r%   r%   r&   r     s   ür   c                   @   s2   e Zd ZdZddddddedddddfdd„ZdS )r   a¿  File attachment for a PDF document.

    An instance is created in the same way as :class:`HTML`, except that the
    HTML specific arguments (``encoding`` and ``media_type``) are not
    supported.

    :param str name:
        The name of the attachment to be included in the PDF document.
        May be :obj:`None`.
    :param str description:
        A description of the attachment to be included in the PDF document.
        May be :obj:`None`.
    :type created: :obj:`datetime.datetime`
    :param created:
        Creation date and time. Default is current date and time.
    :type modified: :obj:`datetime.datetime`
    :param modified:
        Modification date and time. Default is current date and time.
    :param str relationship:
        A string that represents the relationship between the attachment and
        the PDF it is embedded in. Default is 'Unspecified', other common
        values are defined in ISO-32000-2:2020, 7.11.3.

    NZUnspecifiedc              	   C   sˆ   t |||||||d�| _|| _|	| _|| _d | _|
d u rT|rLt t|ƒ¡}
nt 	¡ }
|d u rx|rpt t
|ƒ¡}nt 	¡ }|
| _|| _d S )N)r1   r2   )r.   r=   r(   ÚdescriptionÚrelationshipÚmd5r   Úfromtimestampr   Únowr   ÚcreatedÚmodified)r4   r5   r6   r7   r8   r9   r1   r2   r(   rf   rk   rl   rg   r%   r%   r&   r@   T  s$    þzAttachment.__init__re   r%   r%   r%   r&   r   ;  s   ýr   c                 c   sZ  |durt |ƒ}dd„ | ||||fD ƒ}t|ƒdkrTd |¡p@d}	td|	› �ƒ‚�n| durÖt| dƒrld	}
n"t| tƒr|d
}
nt| ƒrŠd}
nd
}
tf |||dœ|
| i¤Ž}|�}|V  W d  ƒ n1 sÈ0    Y  �n€|du�r.|du rðt	|ƒ}t
|dƒ�}d	||dfV  W d  ƒ n1 �s 0    Y  �n(|du�rðt||ƒ�š}|�r~|d dk�r~t d|d |d ¡ dd|dfV  nP| d¡}|du �rž| d|¡}d|v �r¼d|d ||fV  nd	|d	 ||fV  W d  ƒ n1 �sä0    Y  nf|du�r:|du �r*t|ddƒ}|�r*| d¡�s*t |ƒ}d	||dfV  n|du�sHJ ‚d||dfV  dS )zCIf only one input is given, return it with normalized ``base_url``.Nc                 S   s   g | ]}|d ur|‘qS rA   r%   )Ú.0Úparamr%   r%   r&   Ú
<listcomp>v  s   ÿz"_select_source.<locals>.<listcomp>r   z, Únothingz!Expected exactly one source, got r_   r8   r6   r7   r]   ÚrbZ	mime_typeztext/cssz%Unsupported stylesheet type %s for %sZredirected_urlr9   r    r:   r(   ú<)r   ÚlenÚjoinÚ	TypeErrorÚhasattrr,   r   r   r.   r   Úopenr   r   Úerrorr#   r+   Ú
startswith)r5   r6   r7   r8   r9   r1   r2   r^   Zselected_paramsr=   Útype_r;   Zproto_encodingr(   r%   r%   r&   r.   n  sj    ÿ

þý(
2
þ



þ(

r.   )rc   )rF   rB   rC   rJ   )r   r   )*r\   Ú
contextlibr   Úos.pathr   r   Úpathlibr   Úurllib.parser   r3   r`   r/   r   r   r   Ú__all__Úurlsr   r   r   r   r   Úloggerr   r   r'   r   r   r   Úcontextmanagerr.   Úcssrc   ÚhtmlrF   rB   rC   rJ   Údocumentr   r   r%   r%   r%   r&   Ú<module>   sV   1ï .3þ?