U
    ë‹×j  ã                   @   s.   d dl Z d dlZd dlmZ G dd„ dƒZdS )é    N)ÚLockc                   @   s6   e Zd Zddd„Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚHLSRegistryV2éx   é'  c                 C   s$   || _ || _i | _i | _tƒ | _d S ©N)ÚttlÚmax_recursosÚrecursosÚidentidadesr   Úlock)Úselfr   r   © r   úhermes_hls_registry_v2.pyÚ__init__   s
    zHLSRegistryV2.__init__c                    sZ   t   ¡ ‰ ‡ fdd„| j ¡ D ƒ}|D ]0}| j |¡}|d }| j |¡|kr$| j|= q$d S )Nc                    s    g | ]\}}ˆ |d  kr|‘qS )Úexpr   )Ú.0ÚidentificadorÚrecurso©Zahorar   r   Ú
<listcomp>   s   þz*HLSRegistryV2._limpiar.<locals>.<listcomp>Ú	identidad)Útimer	   ÚitemsÚpopr
   Úget)r   Z	expiradosr   r   r   r   r   r   Ú_limpiar   s    
þzHLSRegistryV2._limpiarc              
   C   sÌ   |st dƒ‚| j�° |  ¡  | j |¡}|rj|| jkrj| j| }||d< t ¡ | j |d< |W  5 Q R £ S t| jƒ| j	kr‚t
dƒ‚t d¡}||t ¡ | j dœ| j|< || j|< |W  5 Q R £ S Q R X d S )NzIdentidad HLS requeridaÚurlr   zLimite HLS alcanzadoé   )r   r   r   )Ú
ValueErrorr   r   r
   r   r	   r   r   Úlenr   ÚRuntimeErrorÚsecretsZtoken_urlsafe)r   r   r   Z	existenter   r   r   r   r   Ú	registrar   s&    

ý
zHLSRegistryV2.registrarc              
   C   sV   | j �F | j |¡}|s tdƒ‚t ¡ |d kr8tdƒ‚|d W  5 Q R £ S Q R X d S )NzRecurso desconocidor   zRecurso expirador   )r   r	   r   r   r   )r   r   r   r   r   r   Úresolver?   s    zHLSRegistryV2.resolverc              	   C   s   | j � |  ¡  W 5 Q R X d S r   )r   r   )r   r   r   r   ÚlimpiarK   s    zHLSRegistryV2.limpiarN)r   r   )Ú__name__Ú
__module__Ú__qualname__r   r   r"   r#   r$   r   r   r   r   r      s
   
 r   )r!   r   Z	threadingr   r   r   r   r   r   Ú<module>   s   