o
    Óó·i1  ã                   @   sP  U d Z ddlZddlZddlZddlZddlZddlmZ	 ddl
mZ ddlmZmZ ddlmZ i Zeeejf ed< ejZdd	„ Zd
d„ Zdd„ Ze eje¡ dd„ Zdd„ Ze eje¡ dd„ Zdd„ Ze eje¡ dd„ Z dd„ Z!dd„ Z"dd„ Z#G dd„ dƒZ$i a%ee&d f ed!< i a'd"d#„ Z(d$d%„ Z)d&d'„ Z*G d(d „ d ƒZ+dS ))z(
Different styles of persisted objects.
é    N)ÚStringIO)ÚDict)ÚlogÚreflect)Ú_PYPYÚ
oldModulesc                 C   s   t | j| j| jjffS )z3support function for copy_reg to pickle method refsN)ÚunpickleMethodÚ__name__Ú__self__Ú	__class__)Úmethod© r   ú^C:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/persisted/styles.pyÚpickleMethod   s   þr   c                 C   s   t | |ƒ}|S )a³  
    Retrieve the function object implementing a method name given the class
    it's on and a method name.

    @param classObject: A class to retrieve the method's function from.
    @type classObject: L{type}

    @param methodName: The name of the method whose function to retrieve.
    @type methodName: native L{str}

    @return: the function object corresponding to the given method name.
    @rtype: L{types.FunctionType}
    N)Úgetattr)ZclassObjectÚ
methodNameZmethodObjectr   r   r   Ú_methodFunction"   s   
r   c              	   C   sŠ   |du r	t || ƒS zt|| ƒ}W n& ty6   t d| d|¡ |dus'J dƒ‚|j|u r-‚ t| ||jƒ Y S w d}tj||g|¢R Ž }|S )aS  
    Support function for copy_reg to unpickle method refs.

    @param im_name: The name of the method.
    @type im_name: native L{str}

    @param im_self: The instance that the method was present on.
    @type im_self: L{object}

    @param im_class: The class where the method was declared.
    @type im_class: L{type} or L{None}
    NZMethodznot on classz'No recourse: no instance to guess from.r   )	r   r   ÚAttributeErrorr   Úmsgr   r   ÚtypesÚ
MethodType)Zim_nameZim_selfZim_classZmethodFunctionZ
maybeClassÚboundr   r   r   r   4   s   

ø
r   c                 C   s4   | j dkrtd| › �ƒ‚ttd | j| jg¡gƒfS )a—  
    Reduce, in the sense of L{pickle}'s C{object.__reduce__} special method, a
    function object into its constituent parts.

    @param f: The function to reduce.
    @type f: L{types.FunctionType}

    @return: a 2-tuple of a reference to L{_unpickleFunction} and a tuple of
        its arguments, a 1-tuple of the function's fully qualified name.
    @rtype: 2-tuple of C{callable, native string}
    z<lambda>zCannot pickle lambda function: Ú.N)r	   Ú_UniversalPicklingErrorÚ_unpickleFunctionÚtupleÚjoinÚ
__module__Ú__qualname__)Úfr   r   r   Ú_pickleFunctionW   s   
r    c                 C   s   ddl m} || ƒS )aù  
    Convert a function name into a function by importing it.

    This is a synonym for L{twisted.python.reflect.namedAny}, but imported
    locally to avoid circular imports, and also to provide a persistent name
    that can be stored (and deprecated) independently of C{namedAny}.

    @param fullyQualifiedName: The fully qualified name of a function.
    @type fullyQualifiedName: native C{str}

    @return: A function object imported from the given location.
    @rtype: L{types.FunctionType}
    r   )ÚnamedAnyN)Ztwisted.python.reflectr!   )ZfullyQualifiedNamer!   r   r   r   r   h   s   r   c                 C   s   t | jffS )z3support function for copy_reg to pickle module refsN)ÚunpickleModuler	   )Úmoduler   r   r   ÚpickleModule~   s   r$   c                 C   s6   | t v rt d|  ¡ t |  } t | ¡ t| i i dƒS )z5support function for copy_reg to unpickle module refszModule has moved: %sÚxN)r   r   r   Ú
__import__)Únamer   r   r   r"   ƒ   s
   
r"   c                 C   s   	 t |  ¡ |  ¡ ffS )zá
    Reduce the given cStringO.

    This is only called on Python 2, because the cStringIO module only exists
    on Python 2.

    @param stringo: The string output to pickle.
    @type stringo: C{cStringIO.OutputType}
    N)ÚunpickleStringOÚgetvalueÚtell)Zstringor   r   r   ÚpickleStringO�   s   
r+   c                 C   s   t ƒ }| | ¡ | |¡ |S )aÚ  
    Convert the output of L{pickleStringO} into an appropriate type for the
    current python version.  This may be called on Python 3 and will convert a
    cStringIO into an L{io.StringIO}.

    @param val: The content of the file.
    @type val: L{bytes}

    @param sek: The seek position of the file.
    @type sek: L{int}

    @return: a file-like object which you can write bytes to.
    @rtype: C{cStringIO.OutputType} on Python 2, L{io.StringIO} on Python 3.
    N)Ú
_cStringIOÚwriteÚseek©ÚvalZsekr%   r   r   r   r(   �   s   

r(   c                 C   s   t |  ¡ |  ¡ ffS )aQ  
    Reduce the given cStringI.

    This is only called on Python 2, because the cStringIO module only exists
    on Python 2.

    @param stringi: The string input to pickle.
    @type stringi: C{cStringIO.InputType}

    @return: a 2-tuple of (C{unpickleStringI}, (bytes, pointer))
    @rtype: 2-tuple of (function, (bytes, int))
    N)ÚunpickleStringIr)   r*   )Zstringir   r   r   ÚpickleStringI²   s   r2   c                 C   s   t | ƒ}| |¡ |S )aß  
    Convert the output of L{pickleStringI} into an appropriate type for the
    current Python version.

    This may be called on Python 3 and will convert a cStringIO into an
    L{io.StringIO}.

    @param val: The content of the file.
    @type val: L{bytes}

    @param sek: The seek position of the file.
    @type sek: L{int}

    @return: a file-like object which you can read bytes from.
    @rtype: C{cStringIO.OutputType} on Python 2, L{io.StringIO} on Python 3.
    N)r,   r.   r/   r   r   r   r1   Â   s   
r1   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú	Ephemeralzh
    This type of object is never persisted; if possible, even references to it
    are eliminated.
    c                 C   s   t dfS )zu
        Serialize any subclass of L{Ephemeral} in a way which replaces it with
        L{Ephemeral} itself.
        r   N)r3   )Úselfr   r   r   Ú
__reduce__Þ   s   zEphemeral.__reduce__c                 C   sJ   t  d|  ¡ ts#dd l}t|dd ƒr#| | ¡D ]
}t  d|› �¡ qd S )Nz!WARNING: serializing ephemeral %sr   Úget_referrersz referred to by )r   r   r   Úgcr   r6   )r4   r7   Úrr   r   r   Ú__getstate__å   s   zEphemeral.__getstate__c                 C   s   t  d| j ¡ t| _d S )Nz#WARNING: unserializing ephemeral %s)r   r   r   r3   ©r4   Ústater   r   r   Ú__setstate__ï   s   
zEphemeral.__setstate__N)r	   r   r   Ú__doc__r5   r9   r<   r   r   r   r   r3   Ø   s
    
r3   Ú	VersionedÚversionedsToUpgradec                  C   s&   t t ¡ ƒD ]} t| ƒ qi ai ad S ©N)Úlistr?   ÚvaluesÚrequireUpgradeÚupgraded)Z	versionedr   r   r   Ú	doUpgradeø   s   
rE   c                 C   s4   t | ƒ}|tv r|tvrdt|< |  ¡  | S dS dS )z?Require that a Versioned instance be upgraded completely first.é   N)Úidr?   rD   ÚversionUpgrade)ÚobjZobjIDr   r   r   rC      s   ýrC   c                 C   s@   | t g}t | ¡D ]}||vrt|t ƒr| |¡ q	|dd… S )z±
    Get all of the parent classes of C{c}, not including C{c} itself, which are
    strict subclasses of L{Versioned}.

    @param c: a class
    @returns: list of classes
    é   N)r>   ÚinspectÚgetmroÚ
issubclassÚappend)ÚcÚlÚbr   r   r   Ú_aybabtu	  s   

€rR   c                   @   s2   e Zd ZdZdZdZdd„ Zddd„Zd	d
„ ZdS )r>   a²  
    This type of object is persisted with versioning information.

    I have a single class attribute, the int persistenceVersion.  After I am
    unserialized (and styles.doUpgrade() is called), self.upgradeToVersionX()
    will be called for each version upgrade I must undergo.

    For example, if I serialize an instance of a Foo(Versioned) at version 4
    and then unserialize it when the code is at version 9, the calls::

      self.upgradeToVersion5()
      self.upgradeToVersion6()
      self.upgradeToVersion7()
      self.upgradeToVersion8()
      self.upgradeToVersion9()

    will be made.  If any of these methods are undefined, a warning message
    will be printed.
    r   r   c                 C   s   | t t| ƒ< || _d S r@   )r?   rG   Ú__dict__r:   r   r   r   r<   3  s   
zVersioned.__setstate__Nc                 C   s€   t   |p| j¡}t| jƒ}| ¡  | | j¡ |D ]$}d|jv r-|jD ]	}||v r,||= q#d|jv r=|j|t 	|¡› d�< q|S )z8Get state, adding a version number to it on its way out.ÚpersistenceForgetsÚpersistenceVersionz.persistenceVersionN)
ÚcopyrS   rR   r   ÚreverserN   rT   rU   r   Úqual)r4   ÚdictÚdctÚbasesÚbaseZslotr   r   r   r9   7  s"   


€
þÿ€zVersioned.__getstate__c           
      C   sZ  t | jƒ}| ¡  | | j¡ d| jv rB| jd }| jd= d}d}|D ]}d|jvr+q#|j|kr5|}|j}q#|rB|| jdt |¡ < |D ]f}t|j	vrQd|jvrQqD|j}dt |¡ }| j 
|¡pbd}|ri| j|= ||ksqJ dƒ‚||k rª|d }|j 
d| d¡}	|	r�t dt |¡t | j¡t| ƒ|f ¡ |	| ƒ n	t d	 ||¡¡ ||k suqDdS )
z (internal) Do a version upgrade.rU   r   Nz%s.persistenceVersionz"Sorry, can't go backwards in time.rF   zupgradeToVersion%sz'Upgrading %s (of %s @ %s) to version %sz(Warning: cannot upgrade {} to version {})rR   r   rW   rN   rS   rU   r   rX   r>   Ú	__bases__Úgetr   r   rG   Úformat)
r4   r[   ZpverZhighestVersionZhighestBaser\   ZcurrentVersZpverNameZpersistVersr   r   r   r   rH   H  sl   


	

€þÿÿþ
üÿÿ
	ÿÿñ€ózVersioned.versionUpgrader@   )	r	   r   r   r=   rU   rT   r<   r9   rH   r   r   r   r   r>     s    
),r=   rV   ÚcopyregÚcopy_regrK   Úpickler   Úior   r,   Útypingr   Ztwisted.pythonr   r   Ztwisted.python.compatr   r   ÚstrÚ
ModuleTypeÚ__annotations__ÚPicklingErrorr   r   r   r   r   r    r   ÚFunctionTyper$   r"   r+   r(   r2   r1   r3   r?   ÚintrD   rE   rC   rR   r>   r   r   r   r   Ú<module>   sB    		