U
    :vh
                     @   sX   d Z ddlZddlZddlmZ ddlmZ ddlmZ G dd dZ	G d	d
 d
Z
dS )a  
A wrapper class to allow rewinding/replaying changes made to a FPDF instance.

The contents of this module are internal to fpdf2, and not part of the public API.
They may change at any time without prior warning or any deprecation period,
in non-backward-compatible ways.
    N)deepcopy   )get_stack_level)FPDFExceptionc                   @   s2   e Zd ZdZdddZdd Zdd Zd	d
 ZdS )FPDFRecordera  
    The class is aimed to be used as wrapper around fpdf.FPDF:

        pdf = FPDF()
        recorder = FPDFRecorder(pdf)

    Its aim is dual:
      * allow to **rewind** to the state of the FPDF instance passed to its constructor,
        reverting all changes made to its internal state
      * allow to **replay** again all the methods calls performed
        on the recorder instance between its creation and the last call to rewind()

    Note that method can be called on a FPDFRecorder instance using its .pdf attribute
    so that they are not recorded & replayed later, on a call to .replay().

    Note that using this class means to duplicate the FPDF `bytearray` buffer:
    when generating large PDFs, doubling memory usage may be troublesome.
    Tc                 C   s(   || _ t| j j| _g | _|s$d| _d S )NF)pdfr   __dict___initial_callsaccept_page_break)selfr   r    r   1/tmp/pip-unpacked-wheel-dvf6lv8i/fpdf/recorder.py__init__$   s
    zFPDFRecorder.__init__c                 C   s$   t | j|}t|r t|| jS |S N)getattrr   callableCallRecorderr
   )r   nameattrr   r   r   __getattr__+   s    zFPDFRecorder.__getattr__c                 C   s   | j | j_t| jj| _ d S r   )r	   r   r   r   )r   r   r   r   rewind1   s    
zFPDFRecorder.rewindc                 C   s   | j D ]z}|\}}}z*|||}t|tjr<tjdt d W q tk
r~ } z"td| d| d| d|W 5 d }~X Y qX qg | _ d S )Nz[Detected usage of a context manager inside an unbreakable() section, which is not supported)
stacklevelzFailed to replay FPDF call: z(*z, **))	r
   
isinstancetypesGeneratorTypewarningswarnr   	Exceptionr   )r   callfuncargskwargsresulterrorr   r   r   replay5   s     



zFPDFRecorder.replayN)T)__name__
__module____qualname____doc__r   r   r   r&   r   r   r   r   r      s
   
r   c                   @   s   e Zd Zdd Zdd ZdS )r   c                 C   s   || _ || _d S r   )_funcr
   )r   r!   Zcallsr   r   r   r   H   s    zCallRecorder.__init__c                 O   s    | j | j||f | j||S r   )r
   appendr+   )r   r"   r#   r   r   r   __call__L   s    zCallRecorder.__call__N)r'   r(   r)   r   r-   r   r   r   r   r   G   s   r   )r*   r   r   copyr   deprecationr   errorsr   r   r   r   r   r   r   <module>   s   7