o
    hhD                  	   @   s  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i ddd	d
ddddddddddddddddddddddddddddddddi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66.0stylesheets
media_typeprintattachmentspdf_identifierpdf_variantpdf_version	pdf_formspdf_tagsFuncompressed_pdfcustom_metadatapresentational_hintssrgboptimize_imagesjpeg_qualitydpi
full_fonts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   )html_documentfallback_base_urlfirst_base_elementr+    r4   U/var/www/html/optinet_system/venv/lib/python3.10/site-packages/weasyprint/__init__.py_find_base_urlf   s   
r6   c                	   @   s^   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rt|}t|||||||}
|
6\}}}}t|tr7tj|dd}
nddi}|d urC||d< |d urK||d< tj|fi |}
W d    n1 s^w   Y  t	|
|| _
|| _|	| _tjj|
d d	| _| jj| _d S )
Nz'Step 1 - Fetching and parsing HTML - %snamezHTML stringF)namespace_html_elementsr8   transport_encodingoverride_encoding)content_language)r)   infogetattr
isinstancer   str_select_source	tinyhtml5parser6   base_urlurl_fetcherr   
cssselect2ElementWrapperfrom_html_rootwrapper_elementetree_element)selfguessfilenameurlfile_objstringencodingrC   rD   r   resultsource_typesourceprotocol_encodingkwargsr4   r4   r5   __init__   s8   



zHTML.__init__Fc                 C   s   |rt tgS t gS N)HTML5_UA_STYLESHEETHTML5_UA_FORM_STYLESHEET)rJ   formsr4   r4   r5   _ua_stylesheets   s   zHTML._ua_stylesheetsc                 C   s
   t  gS rW   )HTML5_UA_COUNTER_STYLEcopyrJ   r4   r4   r5   _ua_counter_style   s   
zHTML._ua_counter_stylec                 C   s   t gS rW   )HTML5_PH_STYLESHEETr^   r4   r4   r5   _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(   warningr]   updater   _render)rJ   font_configcounter_styleoptionsunknownnew_optionsr4   r4   r5   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   r]   rd   rk   	write_pdf)rJ   targetzoomfinisherrf   rg   rh   rj   r4   r4   r5   rl      s   %
zHTML.write_pdf)F)NN)Nr#   NNN)__name__
__module____qualname____doc__r"   rV   r[   r_   ra   rk   rl   r4   r4   r4   r5   r   t   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}|)\}}}}|dkr)| }t|tr4t|}n
tj	|||d\}}W d    n1 sHw   Y  || _
|pUt | _|d u r]g n|| _|d u rfi n|}t|
|||| j| j|| d S )Nz&Step 2 - Fetching and parsing CSS - %sr7   z
CSS stringrC   rD   check_css_mime_typerN   )environment_encodingrT   )r)   r<   r=   r@   readr>   r?   tinycss2parse_stylesheetparse_stylesheet_bytesrC   rE   Matchermatcher
page_rulespreprocess_stylesheet)rJ   rK   rL   rM   rN   rO   rP   rC   rD   _check_mime_typer   rf   rg   r|   r}   rQ   rR   rS   rT   
stylesheetr4   r4   r5   rV     s8   



zCSS.__init__rp   rq   rr   rs   r"   rV   r4   r4   r4   r5   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.

    NUnspecifiedc              	   C   s   t |||||||d| _|| _|	| _|| _d | _|
d u r*|r&tt|}
nt	 }
|d u r<|r8tt
|}nt	 }|
| _|| _d S )N)rC   rD   )r@   rS   r7   descriptionrelationshipmd5r   fromtimestampr   nowr   createdmodified)rJ   rK   rL   rM   rN   rO   rC   rD   r7   r   r   r   r   r4   r4   r5   rV   W  s$   
zAttachment.__init__r   r4   r4   r4   r5   r   >  s    r   c                 c   st   |dur	t |}dd | ||||fD }t|dkr)d|p!d}	td|	 | durkt| dr5d	}
nt| tr=d
}
n	t| rDd}
nd
}
td|||d|
| i}|}|V  W d   dS 1 sdw   Y  dS |dur|du rwt	|}t
|d}d	||dfV  W d   dS 1 sw   Y  dS |durt||Z}|r|d dkrtd|d |d  dd|dfV  n.|d}|du r|d|}d|v rd|d ||fV  nd	|d	 ||fV  W d   dS W d   dS W d   dS 1 sw   Y  dS |dur*|du r!t|dd}|r!|ds!t |}d	||dfV  dS |dus1J 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 rW   r4   ).0paramr4   r4   r5   
<listcomp>y  s
    z"_select_source.<locals>.<listcomp>r#   z, nothingz!Expected exactly one source, got rw   rN   rL   rM   rt   rb	mime_typeztext/cssz%Unsupported stylesheet type %s for %sredirected_urlrO   r,   rP   r7   <r4   )r&   lenjoin	TypeErrorhasattrr>   r   r'   r@   r%   openr$   r(   errorr/   r=   
startswith)rK   rL   rM   rN   rO   rC   rD   ru   selected_paramsrS   type_rQ   proto_encodingr7   r4   r4   r5   r@   q  sz   

""


"

r@   )r~   )r\   rX   rY   r`   )r   r    )*rs   
contextlibr   os.pathr   r   pathlibr   urllib.parser   rE   rx   rA   r   r!   r   __all__urlsr$   r"   r%   r&   r'   loggerr(   r)   r6   r   r   r   contextmanagerr@   cssr~   htmlr\   rX   rY   r`   documentr   r    r4   r4   r4   r5   <module>   s~    2	
 .3?