U
    c                     @   sb   d dl Z d dlmZmZ d dlmZmZmZ d dlmZ ddgZ	e
e
dddZG d	d dZdS )
    N)datetimetimezone)DictOptionalUnion)core
POEManagerdigest_for_poe)datareturnc                 C   s   t |  S N)hashlibsha256digest)r
    r   A/tmp/pip-unpacked-wheel-rwcmptg8/pyhanko_certvalidator/ltv/poe.pyr	   
   s    c                   @   s   e Zd ZdZdee dddZdeee	j
f ee edddZdeee ed	d
dZdd Zeee	j
f edddZdd Zdd ZdS )r   z~
    Class to manage proof-of-existence (POE) claims.

    :param current_dt_override:
        Override the current time.
    Ncurrent_dt_overridec                 C   s   i | _ || _d S r   )_poes_current_dt_override)selfr   r   r   r   __init__   s    zPOEManager.__init__)r
   dtr   c                 C   s(   t |tjr| }t|}| ||S )a9  
        Register a new POE claim if no POE for an earlier time is available.

        :param data:
            Data to register a POE claim for.
        :param dt:
            The POE time to register. If ``None``, assume the current time.
        :return:
            The oldest POE datetime available.
        )
isinstancer   	Asn1Valuedumpr	   register_by_digest)r   r
   r   r   r   r   r   register   s    zPOEManager.register)r   r   r   c                 C   sV   |p| j pttj}z| j| }||kr0|W S W n tk
rF   Y nX || j|< |S )aQ  
        Register a new POE claim if no POE for an earlier time is available.

        :param digest:
            SHA-256 digest of the data to register a POE claim for.
        :param dt:
            The POE time to register. If ``None``, assume the current time.
        :return:
            The oldest POE datetime available.
        )r   r   nowr   utcr   KeyError)r   r   r   Zcur_poer   r   r   r   ,   s    


zPOEManager.register_by_digestc                 C   s   t | j S )z
        Iterate over the current earliest known POE for all items currently
        being managed.

        Returns an iterator with ``(digest, poe_dt)`` pairs.
        )iterr   items)r   r   r   r   __iter__C   s    zPOEManager.__iter__)itemr   c                 C   s   | j |ddS )a  
        Return the earliest available POE for an item.

        .. note::
            This is an alias for :meth:`register` with `dt=None`, and hence
            will register the current time as the POE time for the given item.
            This side effect is intentional.

        :param item:
            Item to get the current POE time for.
        :return:
            A datetime object representing the earliest available POE for the
            item.
        N)r   )r   )r   r$   r   r   r   __getitem__L   s    zPOEManager.__getitem__c                 C   s0   t |tstt|D ]\}}| || qdS )ze
        Combine data in another POE manager with the POEs managed by this
        instance.
        N)r   r   	TypeErrorr!   r   )r   otherr   r   r   r   r   __ior__]   s    
zPOEManager.__ior__c                 C   s   t | jd}t| j|_|S )Nr   )r   r   dictr   )r   Znew_instancer   r   r   __copy__g   s    zPOEManager.__copy__)N)N)N)__name__
__module____qualname____doc__r   r   r   r   bytesr   r   r   r   r#   r%   r(   r*   r   r   r   r   r      s$       	
)r   r   r   typingr   r   r   Z
asn1cryptor   __all__r/   r	   r   r   r   r   r   <module>   s   