o
    vh                     @   s^   d dl Z d dlmZ d dlmZmZ d dlmZ eddG dd dZG d	d
 d
e j	Z
dS )    N)	dataclass)DictType)miscT)frozenc                   @   s$   e Zd ZU dZeed< 	 eed< dS )SerialisedCredentialz*
    A credential in serialised form.
    credential_typedataN)__name__
__module____qualname____doc__str__annotations__bytes r   r   ^/var/www/html/hyperkenya/venv/lib/python3.10/site-packages/pyhanko/pdf_utils/crypt/cred_ser.pyr      s   
 r   c                   @   s   e Zd ZU dZe Zeeed  f e	d< e
defddZeded  fddZdefd	d
Ze
defddZededd fddZdefddZdS )SerialisableCredentialzA
    Class representing a credential that can be serialised.
    ._SerialisableCredential__registered_subclassesreturnc                 C      t )z
        Get the type name of the credential, which will be embedded into
        serialised values and used on deserialisation.
        NotImplementedErrorclsr   r   r   get_name%   s   zSerialisableCredential.get_namer   c                 C   s   | t j|  < | S )a  
        Register a subclass into the credential serialisation registry, using
        the name returned by :meth:`get_name`. Can be used as a class decorator.

        :param cls:
            The subclass.
        :return:
            The subclass.
        )r   r   r   r   r   r   r   register-   s   zSerialisableCredential.registerc                 C   r   )z
        Serialise a value to raw binary data. To be overridden by subclasses.

        :return:
            A byte string
        :raises misc.PdfWriteError:
            If a serialisation error occurs.
        r   selfr   r   r   
_ser_value;   s   	z!SerialisableCredential._ser_valuer	   c                 C   r   )aH  
        Deserialise a value from raw binary data.

        :param data:
            The data to deserialise.
        :return:
            The deserialised value (an instance of a subclass of
            :class:`.SerialisableCredential`)
        :raises misc.PdfReadError:
            If a deserialisation error occurs.
        r   )r   r	   r   r   r   _deser_valueF   s   z#SerialisableCredential._deser_value	ser_valuec                 C   sB   | j }ztj| }W n ty   td| dw || jS )a  
        Deserialise a :class:`.SerialisedCredential` value by looking up
        the proper subclass of :class:`.SerialisableCredential` and invoking
        its deserialisation method.

        :param ser_value:
            The value to deserialise.
        :return:
            The deserialised credential.
        :raises misc.PdfReadError:
            If a deserialisation error occurs.
        z3Failed to deserialise credential: credential type 'z' not known.)r   r   r   KeyErrorr   PdfReadErrorr    r	   )r!   	cred_typer   r   r   r   deserialiseV   s   z"SerialisableCredential.deserialisec                 C   s   t | j |  dS )z
        Serialise a value to an annotated :class:`.SerialisedCredential` value.

        :return:
            A :class:`.SerialisedCredential` value.
        :raises misc.PdfWriteError:
            If a serialisation error occurs.
        )r   r	   )r   	__class__r   r   r   r   r   r   	serialisep   s   	z SerialisableCredential.serialiseN)r
   r   r   r   dictr   r   r   r   r   classmethodr   staticmethodr   r   r   r    r   r%   r'   r   r   r   r   r      s"   
 r   )abcdataclassesr   typingr   r   pyhanko.pdf_utilsr   r   ABCr   r   r   r   r   <module>   s    