HEX
Server: Apache
System: Linux srv1.prosuiteplus.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: prosuiteplus (1001)
PHP: 8.3.20
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/pikepdf/__pycache__/_methods.cpython-38.pyc
U

~^=T�@s2dZddlZddlmZddlmZddlmZddlm	Z	m
Z
ddlmZdd	l
mZmZmZmZmZmZmZdd
lmZmZmZmZddlmZmZmZgZdd
�Zdd�Z dd�Z!ee�Gdd�d��Z"ee�Gdd�d��Z#ee�Gdd�d��Z$dd�Z%ee�Gdd�d��Z&ee�Gdd�d��Z'dS)z�
In several cases the implementation of some higher levels features might as
well be in Python. Fortunately we can attach Python methods to C++ class
bindings after the fact.

We can also move the implementation to C++ if desired.
�N)�
namedtuple��KeysView)�BytesIO)�PIPE�run)�NamedTemporaryFile�)�Array�
Dictionary�Name�Object�Page�Pdf�Stream)�PdfError�StreamParser�Token�_ObjectMapping)�EncryptionInfo�PdfMetadata�PermissionscCs|fdd�}|S)atAttach methods of a Python support class to an existing class

    This monkeypatches all methods defined in the support class onto an
    existing class. Example:

    .. code-block:: python

        @augments(ClassDefinedInCpp)
        class SupportClass:
            def foo(self):
                pass

    The Python method 'foo' will be monkeypatched on ClassDefinedInCpp. SupportClass
    has no meaning on its own and should not be used, but gets returned from
    this function so IDE code inspection doesn't get too confused.

    We don't subclass because it's much more convenient to monkeypatch Python
    methods onto the existing Python binding of the C++ class. For one thing,
    this allows the implementation to be moved from Python to C++ or vice
    versa. It saves having to implement an intermediate Python subclass and then
    ensures that the C++ superclass never 'leaks' to pikepdf users. Finally,
    wrapper classes and subclasses can become problematic if the call stack
    crosses the C++/Python boundary multiple times.

    Any existing methods may be used, regardless of whether they defined
    elsewhere in the support class or in the target class.

    The target class does not have to be C++ or derived from pybind11.

    THIS DOES NOT work for static methods or class methods. pybind11 does not seem
    to support this sort of runtime modification.
    cSsht�|�D]J\}}t�|�r>|j�|j|j�|_t|||�q
t�|�r
t|||�q
dd�}||_|S)NcSst|jjd��dS)Nz	.__init__)�NotImplementedError�	__class__�__name__��self�r�2/usr/lib/python3/dist-packages/pikepdf/_methods.py�
block_initKsz3augments.<locals>.class_augment.<locals>.block_init)	�inspectZ
getmembersZ
isfunction�__qualname__�replacer�setattrZisdatadescriptor�__init__)�cls�cls_cpp�name�memberrrrr�
class_augmentAs
�
zaugments.<locals>.class_augmentr)r&r)rrr�augmentss"r*cCs6t��}|j�|�t�}|�|�|�d�|��S)z<Construct a single page PDF from the provided page in memoryr)r�new�pages�appendr�save�seek�read)�pageZpdf�biorrr�_single_page_pdfUs

r3c
Csttdd��`}|�|�|�d�|��tdd|dd|jgttd�}|jrXt|j�	���|j
W5QR�SQRXd	S)
z8Use mupdf draw to rasterize the PDF in the memory bufferz.pdf)�suffixrZmudrawz-Fz-o�-)�stdout�stderrN)r�writer/�flushrr'rr7�RuntimeError�decoder6)�bufferZfmtZtmp_in�procrrr�_mudraw_s

�r>c@s6eZdZdd�Zdd�Zdd�Zdddd	�d
d�ZdS)�
Extend_ObjectcKs8t|t�r4tj|kr4|jtjkr4t|�j||f|�SdS)z�Present options to IPython for rich display of this object

        See https://ipython.readthedocs.io/en/stable/config/integrating.html#rich-display
        N)�
isinstancerr�Typer�_repr_mimebundle_)r�include�exclude�kwargsrrrrBps��
�zExtend_Object._repr_mimebundle_cCs*t|t�r|��St|t�r&|j��SdS�N)r@r�keysrZstream_dictrrrr�_ipython_key_completions_~s



z'Extend_Object._ipython_key_completions_cCsFt|���t|���}|��D]}||||<q |D]
}||=q6dS)a}Copy all items from other without making a new object.

        Particularly when working with pages, it may be desirable to remove all
        of the existing page's contents and emplace (insert) a new page on top
        of it, in a way that preserves all links and references to the original
        page. (Or similarly, for other Dictionary objects in a PDF.)

        When a page is assigned (``pdf.pages[0] = new_page``), only the
        application knows if references to the original the original page are
        still valid. For example, a PDF optimizer might restructure a page
        object into another visually similar one, and references would be valid;
        but for a program that reorganizes page contents such as a N-up
        compositor, references may not be valid anymore.

        This method takes precautions to ensure that child objects in common
        with ``self`` and ``other`` are not inadvertently deleted.

        Example:
            >>> pdf.pages[0].objgen
            (16, 0)
            >>> pdf.pages[0].emplace(pdf.pages[1])
            >>> pdf.pages[0].objgen
            (16, 0)  # Same object
        N)�setrG)r�otherZdel_keys�krrr�emplace�s
zExtend_Object.emplaceNT)�filter�decode_parms�
type_checkcCs
|r�|dk	r�t|t�rt|�}|��}t|t�r:t|�}n|dkrLtg�}n|��}tdd�|D��sntd��tdd�|D��s�td��t|�dkr�t|�t|�kr�td�t	|�t	|����t|�d	kr�|d}t|�dkr�d}nt|�d	kr�|d}|j
|||d
�dS)a�
        Replace stream object's data with new (possibly compressed) `data`.

        `filter` and `decode_parms` specify that compression that is present on
        the input `data`.

        When writing the PDF in :meth:`pikepdf.Pdf.save`,
        pikepdf may change the compression or apply compression to data that was
        not compressed, depending on the parameters given to that function. It
        will never change lossless to lossy encoding.

        PNG and TIFF images, even if compressed, cannot be directly inserted
        into a PDF and displayed as images.

        Args:
            data (bytes): the new data to use for replacement
            filter (pikepdf.Name or pikepdf.Array): The filter(s) with which the
                data is (already) encoded
            decode_parms (pikepdf.Dictionary or pikepdf.Array): Parameters for the
                filters with which the object is encode
            type_check (bool): Check arguments; use False only if you want to
                intentionally create malformed PDFs.

        If only one `filter` is specified, it may be a name such as
        `Name('/FlateDecode')`. If there are multiple filters, then array
        of names should be given.

        If there is only one filter, `decode_parms` is a Dictionary of
        parameters for that filter. If there are multiple filters, then
        `decode_parms` is an Array of Dictionary, where each array index
        is corresponds to the filter.
        Ncss|]}t|t�VqdSrF)r@r��.0�itemrrr�	<genexpr>�sz&Extend_Object.write.<locals>.<genexpr>z=filter must be: pikepdf.Name or pikepdf.Array([pikepdf.Name])css |]}t|t�p|dkVqdSrF)r@rrPrrrrS�szOdecode_parms must be: pikepdf.Dictionary or pikepdf.Array([pikepdf.Dictionary])rz@filter ({}) and decode_parms ({}) must be arrays of  same lengthr	)rMrN)r@�listr
Z
wrap_in_array�all�	TypeError�len�
ValueError�format�reprZ_write)r�datarMrNrOrrrr8�sF"



������zExtend_Object.write)r�
__module__r!rBrHrLr8rrrrr?nsr?c@s|eZdZdd�Zddd�Zdd�Zd	d
�dd�Zd
d�Zdd�Zdd�Z	e
dd��Ze
dd��Zdd�Z
ddd�dd�ZdS)�
Extend_PdfcKs*t�}|�|�|�d�d|��i}|S)z�
        Present options to IPython or Jupyter for rich display of this object

        See https://ipython.readthedocs.io/en/stable/config/integrating.html#rich-display
        r�application/pdf)rr.r/r0)rZ_kwargsr2r[rrrrB�s


zExtend_Pdf._repr_mimebundle_TFcCst||||d�S)a�
        Open the PDF's XMP metadata for editing

        Recommend for use in a ``with`` block. Changes are committed to the
        PDF when the block exits. (The ``Pdf`` must still be opened.)

        Example:
            >>> with pdf.open_metadata() as meta:
                    meta['dc:title'] = 'Set the Dublic Core Title'
                    meta['dc:description'] = 'Put the Abstract here'

        Args:
            set_pikepdf_as_editor (bool): Update the metadata to show that this
                version of pikepdf is the most recent software to modify the metadata.
                Recommended, except for testing.

            update_docinfo (bool): Update the deprecated PDF DocumentInfo block
                to be consistent with XMP.

            strict (bool): If ``False`` (the default), we aggressively attempt
                to recover from any parse errors in XMP, and if that fails we
                overwrite the XMP with an empty XMP record.  If ``True``, raise
                errors when either metadata bytes are not valid and well-formed
                XMP (and thus, XML). Some trivial cases that are equivalent to
                empty or incomplete "XMP skeletons" are never treated as errors,
                and always replaced with a proper empty XMP block. Certain
                errors may be logged.

        Returns:
            pikepdf.models.PdfMetadata
        )Zpikepdf_markZsync_docinfoZoverwrite_invalid_xml)r)rZset_pikepdf_as_editorZupdate_docinfo�strictrrr�
open_metadata�s"�zExtend_Pdf.open_metadatacCs
t||�S)z�
        Create a new pikepdf.Stream object that is attached to this PDF.

        Args:
            data (bytes): Binary data for the stream object
        )r)rr[rrr�make_stream'szExtend_Pdf.make_stream)idi)�	page_sizecCsr|D]"}d|krdksntd��qttjtdd|d|dg�|�d�t�d�}|�|�}|j|dd	�|S)
a�
        Add a blank page to this PD. If pages already exist, the page will be added to
        the end. Pages may be reordered using ``Pdf.pages``.

        The caller may add content to the page by modifying its objects after creating
        it.

        Args:
            page_size (tuple): The size of the page in PDF units (1/72 inch or 0.35mm).
                Default size is set to a US Letter 8.5" x 11" page.
        �i@8z/Page size must be between 3 and 14400 PDF unitsrr	�)rAZMediaBoxZContentsZ	ResourcesF)�first)rXrrrr
ra�
make_indirectZ	_add_page)rrbZdimZ	page_dictr1rrr�add_blank_page0s
�
zExtend_Pdf.add_blank_pagecCs*d}|jrd|j}nd}|�||�dS)aE
        Close a Pdf object and release resources acquired by pikepdf.

        If pikepdf opened the file handle it will close it (e.g. when opened with a file
        path). If the caller opened the file for pikepdf, the caller close the file.

        pikepdf lazily loads data from PDFs, so some :class:`pikepdf.Object` may
        implicitly depend on the :class:`pikepdf.Pdf` being open. This is always the
        case for :class:`pikepdf.Stream` but can be true for any object. Do not close
        the `Pdf` object if you might still be accessing content from it.

        When an ``Object`` is copied from one ``Pdf`` to another, the ``Object`` is copied into
        the destination ``Pdf`` immediately, so after accessing all desired information
        from the source ``Pdf`` it may be closed.

        Caution:
            Closing the ``Pdf`` is currently implemented by resetting it to an empty
            sentinel. It is currently possible to edit the sentinel as if it were a live
            object. This behavior should not be relied on and is subject to change.

        s�%PDF-1.3
1 0 obj
<< /Type /Catalog /Pages 2 0 R >>
endobj
2 0 obj
<< /Type /Pages /Kids [] /Count 0 >>
endobj
xref
0 3
0000000000 65535 f 
0000000009 00000 n 
0000000058 00000 n 
trailer << /Size 3 /Root 1 0 R >>
startxref
110
%%EOF
z
closed file: z
closed objectN)�filenameZ_process)rZ	EMPTY_PDFZdescriptionrrr�closeJs�zExtend_Pdf.closecCs|SrFrrrrr�	__enter__|szExtend_Pdf.__enter__cCs|��dSrF)ri)r�exc_type�	exc_value�	tracebackrrr�__exit__szExtend_Pdf.__exit__cCs.i}t��D]}t|d|�||<qtf|�S)a�
        Report permissions associated with this PDF.

        By default these permissions will be replicated when the PDF is
        saved. Permissions may also only be changed when a PDF is being saved,
        and are only available for encrypted PDFs. If a PDF is not encrypted,
        all operations are reported as allowed.

        pikepdf has no way of enforcing permissions.

        Returns:
            pikepdf.models.Permissions
        Z_allow_)rZfields�getattr)rZresultsZfieldrrr�allow�szExtend_Pdf.allowcCs
t|j�S)z�
        Report encryption information for this PDF.

        Encryption settings may only be changed when a PDF is saved.

        Returns: pikepdf.models.EncryptionInfo
        )rZ_encryption_datarrrr�
encryption�s	zExtend_Pdf.encryptioncCs�Gdd�dt�}g}z|��Wn0tk
rP}z|�t|��W5d}~XYnX|�}|jD]L}t|�}z|�|�Wq^tk
r�}z|�t|��W5d}~XYq^Xq^|��D]}|�d|�q�|S)z�
        Check if PDF is well-formed.  Similar to ``qpdf --check``.

        Returns:
            list of strings describing errors of warnings in the PDF
        cs,eZdZ�fdd�Zdd�Zdd�Z�ZS)z*Extend_Pdf.check.<locals>.DiscardingParsercst���dSrF)�superr$r�rrrr$�sz3Extend_Pdf.check.<locals>.DiscardingParser.__init__cSsdSrFr)r�objrrr�
handle_object�sz8Extend_Pdf.check.<locals>.DiscardingParser.handle_objectcSsdSrFrrrrr�
handle_eof�sz5Extend_Pdf.check.<locals>.DiscardingParser.handle_eof)rr\r!r$rurv�
__classcell__rrrsr�DiscardingParser�srxNz	WARNING: )	rZ_decode_all_streams_and_discardrr-�strr,rZparse_contentsZget_warnings)rrxZproblems�eZdiscarding_parserZ
basic_pager1Zwarningrrr�check�s 
 
"zExtend_Pdf.checkN�)�mime�descc	Cs�d|jkr|�t��|j_d|jkr6|�t��|jj_d|jjjkrRt�|jjj_d|ksbd|krjtd��|s�ddlm}||�\}}|s�d}t	||�}t
d|�|_tt
j|||td	|i�d
��}t||�|�g�|jjj_d|jkr�t
j
|j_dS)
a
        Attach a file to this PDF

        Args:
            basename (str): The basename (filename withouth path) to name the
                file. Not necessarily the name of the file on disk. Will be s
                hown to the user by the PDF viewer. filebytes (bytes): The file
                contents.

            mime (str or None): A MIME type for the filebytes. If omitted, we try
                to guess based on the standard library's
                :func:`mimetypes.guess_type`. If this cannot be determined, the
                generic value `application/octet-stream` is used. This value is
                used by PDF viewers to decide how to present the information to
                the user.

            desc (str): A extended description of the file contents. PDF viewers
                also display this information to the user. In Acrobat DC this is
                hidden in a context menu.

        The PDF will also be modified to request the PDF viewer to display the
        list of attachments when opened, as opposed to other viewing modes. Some
        PDF viewers will not make it obvious to the user that attachments are
        present unless this is done. This behavior may be overridden by changing
        ``pdf.Root.PageMode`` to some other valid value.

        z/Namesz/EmbeddedFiles�/�\z)basename should be a basename (no / or \)r)�
guess_typezapplication/octet-stream�/F)z/Typer�z/UFz/Descz/EFz	/PageModeN)ZRootrfrZNamesZ
EmbeddedFilesr
rXZ	mimetypesr�rrZSubtypeZFilespecZUseAttachmentsZPageMode)	r�basenameZ	filebytesr}r~r�Z	_encodingZ
filestreamZfilespecrrr�_attach�s8



��
�
zExtend_Pdf._attach)TTF)rr\r!rBr`rargrirjrn�propertyrprqr{r�rrrrr]�s �
)	2


'r]c@s.eZdZdd�Zd
dd�Zdd�Zdd	�ZdS)�Extend_ObjectMappingcCs,z||Wntk
r"YdSXdSdS)NFT��KeyError)r�keyrrr�__contains__s
z!Extend_ObjectMapping.__contains__NcCs(z
||WStk
r"|YSXdSrFr�)rr��defaultrrr�gets
zExtend_ObjectMapping.getcCst|�SrFrrrrrrGszExtend_ObjectMapping.keyscCsdd�|��D�S)Ncss|]\}}|VqdSrFr)rQZ_k�vrrrrS"sz.Extend_ObjectMapping.values.<locals>.<genexpr>)�itemsrrrr�values!szExtend_ObjectMapping.values)N)rr\r!r�r�rGr�rrrrr�s
r�cCs`z|jrWdSWntk
r$YnXzt|�}|jr<WdSWntk
rRYnXtd��dS)NTzobject is not a rectangle)Zis_rectangle�AttributeErrorr
�	ExceptionrX)rtZpdfobjrrr�check_is_box%s

r�c@sjeZdZedd��Zejdd��Zedd��Zejdd��Zedd��Zejd	d��Zd
d�Zdd
�Z	dS)�Extend_PagecCs
|�d�S�NT)Z
_get_mediaboxrrrr�mediabox8szExtend_Page.mediaboxcCst|�||jd<dS)Nz	/MediaBox�r�rt�r�valuerrrr�<scCs
|�d�Sr�)Z_get_cropboxrrrr�cropboxAszExtend_Page.cropboxcCst|�||jd<dS)Nz/CropBoxr�r�rrrr�EscCs
|�d�Sr�)Z_get_trimboxrrrr�trimboxJszExtend_Page.trimboxcCst|�||jd<dS)Nz/TrimBoxr�r�rrrr�NscCst|j��dd�S)Nrr)rZrtr"rrrr�__repr__SszExtend_Page.__repr__c	s�i}ddh}�r"�fdd�|D�}�r8�fdd�|D�}t|j�}d|krR||d<d|kr�zt|d�|d<Wnttfk
r�YnX|S)Nr^z	image/pngcsh|]}|�kr|�qSrr�rQrK)rCrr�	<setcomp>Zsz0Extend_Page._repr_mimebundle_.<locals>.<setcomp>csh|]}|�kr|�qSrrr�)rDrrr�\sZpng)r3rtr>�FileNotFoundErrorr:)rrCrDrEr[ZbundleZpagedatar)rDrCrrBVs
zExtend_Page._repr_mimebundle_N)
rr\r!r�r��setterr�r�r�rBrrrrr�6s





r�c@seZdZdd�ZdS)�Extend_TokencCsd�|j|j�S)Nzpikepdf.Token({}, {}))rYZtype_Z	raw_valuerrrrr�jszExtend_Token.__repr__N)rr\r!r�rrrrr�hsr�)(�__doc__r �collectionsrZcollections.abcr�ior�
subprocessrrZtempfilerr|r
rrr
rrrZ_qpdfrrrrZmodelsrrr�__all__r*r3r>r?r]r�r�r�r�rrrr�<module>s4$6
!1