o
    ]8                     @   sT   d Z ddlZddlZg dZdZdd Zdd Zd	d
 Zdd Z	dd Z
dd ZdS )z-
Utility functions used by the stem library.
    N)confZ
connectionenumlog	lru_cacheZordereddictprocZ	str_toolssystemZtermZ
test_toolsZ	tor_toolsdatetime_to_unixTc                 C   s   t sd}nt| rtd}nttt| }t| ttfr,| D ]
}|d t| }q|S t| trLt	| 
 D ]}|d t|d  t| |  }q7|S |t| 7 }|S )Nr   str   i   )
HASH_TYPES_is_strhashr	   type
isinstancetuplelistdictsortedkeys)valmy_hashvk r   4/usr/lib/python3/dist-packages/stem/util/__init__.py_hash_value0   s   	

"r   c                 C   s&   t j rt| ttfS t| ttfS )a  
  Check if a value is a string. This will be removed when we no longer provide
  backward compatibility for the Python 2.x series.

  :param object val: value to be checked

  :returns: **True** if the value is some form of string (unicode or bytes),
    and **False** otherwise
  )stemprereqis_python_3r   bytesr	   Zunicoder   r   r   r   r   N   s   
r   c                 C   s"   t j r
t| tS t| ttfS )a  
  Check if a value is an integer. This will be removed when we no longer
  provide backward compatibility for the Python 2.x series.

  :param object val: value to be checked

  :returns: **True** if the value is some form of integer (int or long),
    and **False** otherwise
  )r   r   r   r   intZlongr    r   r   r   _is_int_   s   

r"   c                 C   sB   t j r| tddd }|jd |j S | tddd  S )z
  Converts a utc datetime object to a unix timestamp.

  .. versionadded:: 1.5.0

  :param datetime timestamp: timestamp to be converted

  :returns: **float** for the unix timestamp of the given datetime object
  i     iQ )r   r   Z_is_python_26datetimeZdaysZsecondsZtotal_seconds)Z	timestampZdeltar   r   r   r   p   s   
r   c                 C   s   t | r| S tj stdtjjddstdddlm} ddlm}m	} ddl
m}m} t| ||frD|  j|jj|jjd	S t| ||frV| j|jj|jjd	S td
t| j )zE
  Normalizes X25509 and ED25519 keys into their public key bytes.
  z2Key normalization requires the cryptography moduleT)Zed25519z;Key normalization requires the cryptography ed25519 supportr   )serialization)Ed25519PrivateKeyEd25519PublicKey)X25519PrivateKeyX25519PublicKey)encodingformatzAKey must be a string or cryptographic public/private key (was %s))r   r   r   Zis_crypto_availableImportErrorZcryptography.hazmat.primitivesr%   Z1cryptography.hazmat.primitives.asymmetric.ed25519r&   r'   Z0cryptography.hazmat.primitives.asymmetric.x25519r(   r)   r   Z
public_keyZpublic_bytesZEncodingZRawZPublicFormat
ValueErrorr   __name__)keyr%   r&   r'   r(   r)   r   r   r   _pubkey_bytes   s(   
r0   c           	      O   s   | dd}| dd}t| dd}|r|dur|S |r!|| nd}|d ttt|  }|D ]}t| |}|d t| }q1|rIt| d| |S )a  
  Provide a hash value for the given set of attributes.

  :param Object obj: object to be hashed
  :param list attributes: attribute names to take into account
  :param bool cache: persists hash in a '_cached_hash' object attribute
  :param class parent: include parent's hash value
  cacheFparentNZ_cached_hashr   r
   )getgetattr__hash__r   r	   r   r   setattr)	objZ
attributeskwargsZ	is_cachedZparent_classZcached_hashr   attrr   r   r   r   
_hash_attr   s   

r:   )__doc__r$   Zstem.prereqr   __all__r   r   r   r"   r   r0   r:   r   r   r   r   <module>   s   !