a
    [Xh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.1printF)Zstylesheets
media_typeattachmentsZpdf_identifierZpdf_variantZpdf_versionZ	pdf_formsZuncompressed_pdfZcustom_metadataZpresentational_hintsZsrgbZoptimize_imagesZjpeg_qualityZ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"   X/var/www/viveiro_mudafortebrasil/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 s0    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&   transport_encoding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encodingr0   r1   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)r3   Zformsr"   r"   r#   _ua_stylesheets   s    zHTML._ua_stylesheetsc                 C   s
   t  gS r@   )HTML5_UA_COUNTER_STYLEcopyr3   r"   r"   r#   _ua_counter_style   s    zHTML._ua_counter_stylec                 C   s   t gS r@   )HTML5_PH_STYLESHEETrF   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   warningrE   updater   Z_render)r3   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   rE   rL   rR   	write_pdf)r3   targetZzoomZfinisherrM   rN   rO   rQ   r"   r"   r#   rS      s    %
zHTML.write_pdf)F)NN)Nr   NNN)__name__
__module____qualname____doc__r   r?   rC   rG   rI   rR   rS   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 s0    Y  || _
|pt | _|d u rg n|| _|d u ri n|}t|
|||| j| j|| d S )Nz&Step 2 - Fetching and parsing CSS - %sr%   z
CSS stringr0   r1   check_css_mime_typer7   )Zenvironment_encodingr=   )r   r)   r*   r-   readr+   r,   tinycss2Zparse_stylesheetZparse_stylesheet_bytesr0   r2   ZMatchermatcher
page_rulespreprocess_stylesheet)r3   r4   r5   r6   r7   r8   r9   r0   r1   Z_check_mime_typer   rM   rN   r]   r^   r:   r;   r<   r=   Z
stylesheetr"   r"   r#   r?     s4    

(zCSS.__init__rU   rV   rW   rX   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)r0   r1   )r-   r<   r%   descriptionrelationshipmd5r   fromtimestampr   nowr   createdmodified)r3   r4   r5   r6   r7   r8   r0   r1   r%   ra   rf   rg   rb   r"   r"   r#   r?   T  s$    zAttachment.__init__r`   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rt| drld	}
n"t| tr|d
}
nt| rd}
nd
}
tf |||d|
| i}|}|V  W d   n1 s0    Y  n|dur.|du rt	|}t
|d}d	||dfV  W d   n1 s 0    Y  n(|durt||}|r~|d dkr~td|d |d  dd|dfV  nP|d}|du r|d|}d|v rd|d ||fV  nd	|d	 ||fV  W d   n1 s0    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 r@   r"   ).0paramr"   r"   r#   
<listcomp>v  s   z"_select_source.<locals>.<listcomp>r   z, Znothingz!Expected exactly one source, got r[   r7   r5   r6   rY   rbZ	mime_typeztext/cssz%Unsupported stylesheet type %s for %sZredirected_urlr8   r   r9   r%   <)r   lenjoin	TypeErrorhasattrr+   r   r   r-   r   openr   r   errorr    r*   
startswith)r4   r5   r6   r7   r8   r0   r1   rZ   Zselected_paramsr<   type_r:   Zproto_encodingr%   r"   r"   r#   r-   n  sj    

(
2




(

r-   )r_   )rD   rA   rB   rH   )r   r   )*rX   
contextlibr   os.pathr   r   pathlibr   urllib.parser   r2   r\   r.   r   r   r   __all__urlsr   r   r   r   r   loggerr   r   r$   r   r   r   contextmanagerr-   cssr_   htmlrD   rA   rB   rH   documentr   r   r"   r"   r"   r#   <module>   sV   1 .3?