o
    `jJ                     @   s   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 G dd dZdZe	dZ
dd ZG d	d
 d
ZG dd deZdd ZG dd dZG dd dZdS )    N)KafkaTimeoutErrorc                   @   sZ   e Zd ZdZdddZedd Zedd Zed	d
 Zedd Z	dd Z
dd ZdS )Timer)	_start_at
_expire_at_timeout_ms_error_messageNc                 C   sB   || _ |pt | _|d ur| j|d  | _ntd| _|| _d S )N  inf)r   time	monotonicr   r   floatr   )self
timeout_mserror_messagestart_at r   H/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/kafka/util.py__init__   s   

zTimer.__init__c                 C   s   t  | jkS N)r
   r   r   r   r   r   r   expired   s   zTimer.expiredc                 C   sJ   | j d u rd S | jtdkrtdS | jt  }|dk rdS t|d S )Nr	   r   r   )r   r   r   r
   r   int)r   	remainingr   r   r   r      s   
zTimer.timeout_msc                 C   s   | j }|d ur|d S d S Nr   r   )r   r   r   r   r   timeout_secs&   s   zTimer.timeout_secsc                 C   s   t dt | j  S r   )r   r
   r   r   r   r   r   r   
elapsed_ms+   s   zTimer.elapsed_msc                 C   s   | j rt| jd S r   )r   r   r   r   r   r   r   maybe_raise/   s   
zTimer.maybe_raisec                 C   s
   d| j  S )NzTimer(%s ms remaining)r   r   r   r   r   __str__3      
zTimer.__str__)NN)__name__
__module____qualname__	__slots__r   propertyr   r   r   r   r   r   r   r   r   r   r   
   s    
	



r      z^[a-zA-Z0-9._-]+$c                 C   s   | du rt dt| tst dt| dkrtd| dks#| dkr'tdt| tkr5td	t| t| sAtd
| dS )zE Ensures that the topic name is valid according to the kafka source. NzAll topics must not be NonezAll topics must be stringsr   z$All topics must be non-empty strings.z..z Topic name cannot be "." or ".."zKTopic name is illegal, it can't be longer than {0} characters, topic: "{1}"zeTopic name "{0}" is illegal, it contains a character other than ASCII alphanumerics, ".", "_" and "-")		TypeError
isinstancestrlen
ValueErrorTOPIC_MAX_LENGTHformatTOPIC_LEGAL_CHARSmatch)topicr   r   r   ensure_valid_topic_name:   s   

r1   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )
WeakMethodz
    Callable that weakly references a method and the object it is bound to. It
    is based on https://stackoverflow.com/a/24287465.

    Arguments:

        object_dot_method: A bound instance method (i.e. 'object.method').
    c                 C   s   z	t |j| _W n ty   t |j| _Y nw t|  | _z	t |j| _	W n ty:   t |j
| _	Y nw t| 	 | _d S r   )weakrefref__self__targetAttributeErrorim_selfid
_target_id__func__methodim_func
_method_id)r   object_dot_methodr   r   r   r   V   s   zWeakMethod.__init__c                 O   s   |   |  g|R i |S )zB
        Calls the method on target with args and kwargs.
        )r<   r6   r   argskwargsr   r   r   __call__b   s   zWeakMethod.__call__c                 C   s   t | jt | jA S r   )hashr6   r<   r   r   r   r   __hash__h   s   zWeakMethod.__hash__c                 C   s&   t |tsdS | j|jko| j|jkS )NF)r(   r2   r:   r>   )r   otherr   r   r   __eq__k   s   
zWeakMethod.__eq__N)r    r!   r"   __doc__r   rC   rE   rG   r   r   r   r   r2   M   s    r2   c                   @   s   e Zd ZdZdS )DictzpUtility class to support passing weakrefs to dicts

    See: https://docs.python.org/2/library/weakref.html
    N)r    r!   r"   rH   r   r   r   r   rI   q   s    rI   c                    s    fdd}t |  |S )Nc                    s@   | j   | g|R i |W  d    S 1 sw   Y  d S r   )_lockr@   funcr   r   wrapperz   s   $zsynchronized.<locals>.wrapper)	functoolsupdate_wrapper)rL   rM   r   rK   r   synchronizedy   s   rP   c                   @   s   e Zd Zdd Zdd ZdS )classpropertyc                 C   s
   || _ d S r   f)r   rS   r   r   r   r      r   zclassproperty.__init__c                 C   s
   |  |S r   rR   )r   objownerr   r   r   __get__   r   zclassproperty.__get__N)r    r!   r"   r   rV   r   r   r   r   rQ      s    rQ   c                   @   s$   e Zd Zedd Zedd ZdS )
EnumHelperc                 C   sr   t || r|S z| |W S  ty   Y nw z| t|  dd W S  ty8   td| j d| d w N-_zUnrecognized z: )r(   r+   r)   stripupperreplaceKeyErrorr    clsvalr   r   r   
build_from   s   

zEnumHelper.build_fromc                 C   sh   t || r|jS t |tr| |jS z| t| dd jW S  ty3   td| j d| d w rX   )	r(   valuer   r)   r\   r]   r^   r+   r    r_   r   r   r   	value_for   s   


zEnumHelper.value_forN)r    r!   r"   classmethodrb   rd   r   r   r   r   rW      s
    
rW   )binasciirN   rer
   r3   kafka.errorsr   r   r,   compiler.   r1   r2   dictrI   rP   rQ   rW   r   r   r   r   <module>   s    -
$