3
Šãde{  ã               @   s<  d Z 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mZm	Z	m
Z
mZ ddlmZmZ ddlmZ ddlmZmZ eefZeeejfZd!eeed	œd
d„Zd"eeed	œdd„Zeeedœdd„Zeeee	e f dœdd„Z d#eeedœdd„Z!d$e
e e
e edœdd„Z"ee	e e	e dœdd„Z#eedœdd „Z$dS )%z&This module contains helper functions.é    N)ÚAnyÚCallableÚListÚOptionalÚUnioné   )ÚTLS_CIPHER_SUITESÚTLS_VERSIONS)ÚInterfaceError)ÚEscapeTypesÚ
StrOrBytesúutf-8)ÚvalueÚencodingÚreturnc             C   s   t | tƒr| S | j|ƒS )zÄReturns an encoded version of the string as a bytes object.

    Args:
        encoding (str): The encoding.

    Resturns:
        bytes: The encoded version of the string as a bytes object.
    )Ú
isinstanceÚbytesÚencode)r   r   © r   úE/var/www/agendate/envp3/lib/python3.6/site-packages/mysqlx/helpers.pyÚencode_to_bytes/   s    	r   c             C   s   t | tƒr| j|ƒS | S )zÍReturns a string decoded from the given bytes.

    Args:
        value (bytes): The value to be decoded.
        encoding (str): The encoding.

    Returns:
        str: The value decoded from bytes.
    )r   r   Údecode)r   r   r   r   r   Údecode_from_bytes;   s    
r   )ÚobjÚkeyr   c             C   s   t | tƒr| | S t| |ƒS )zÅGet item from dictionary or attribute from object.

    Args:
        obj (object): Dictionary or object.
        key (str): Key.

    Returns:
        object: The object for the provided key.
    )r   ÚdictÚgetattr)r   r   r   r   r   Úget_item_or_attrH   s    
r   )Úargsr   c                 s:   t t dœdd„‰ t| ƒdkr.‡ fdd„| D ƒS ˆ | d ƒS )a
  Escapes special characters as they are expected to be when MySQL
    receives them.
    As found in MySQL source mysys/charset.c

    Args:
        value (object): Value to be escaped.

    Returns:
        str: The value if not a string, or the escaped string.
    )r   r   c             S   s¾   | dkr| S t | tƒr| S t | ttfƒrr| jddƒ} | jddƒ} | jddƒ} | jdd	ƒ} | jd
dƒ} | jddƒ} nH| jddƒ} | jddƒ} | jddƒ} | jddƒ} | jddƒ} | jddƒ} | S )zEscapes special characters.Nó   \s   \\ó   
s   \nó   s   \ró   's   \'ó   "s   \"ó   s   \ú\z\\Ú
z\núz\rú'z\'ú"z\"úz\)r   ÚNUMERIC_TYPESr   Ú	bytearrayÚreplace)r   r   r   r   Ú_escapea   s$    
zescape.<locals>._escaper   c                s   g | ]}ˆ |ƒ‘qS r   r   )Ú.0Úarg)r.   r   r   ú
<listcomp>x   s    zescape.<locals>.<listcomp>r   )r   Úlen)r   r   )r.   r   ÚescapeU   s    r3   Ú )Ú
identifierÚsql_moder   c             C   s8   |dkr | j ddƒ}d|› d�S | j ddƒ}d|› d�S )ac  Quote the given identifier with backticks, converting backticks (`)
    in the identifier name with the correct escape sequence (``) unless the
    identifier is quoted (") as in sql_mode set to ANSI_QUOTES.

    Args:
        identifier (str): Identifier to quote.

    Returns:
        str: Returns string with the identifier quoted with backticks.
    ZANSI_QUOTESr)   z""ú`z``)r-   )r5   r6   Úquotedr   r   r   Úquote_identifier|   s
    r9   )ÚversionÚreasonr   c                s   t t dœ‡ ‡fdd„}|S )a  This is a decorator used to mark functions as deprecated.

    Args:
        version (Optional[string]): Version when was deprecated.
        reason (Optional[string]): Reason or extra information to be shown.

    Returns:
        Callable: A decorator used to mark functions as deprecated.

    Usage:

    .. code-block:: python

       from mysqlx.helpers import deprecated

       @deprecated('8.0.12', 'Please use other_function() instead')
       def deprecated_function(x, y):
           return x + y
    )Úfuncr   c                s(   t jˆ ƒtttdœ‡ ‡‡fdd„ƒ}|S )zDecorate function.)r   Úkwargsr   c                 sn   dˆ j › d�g}ˆr$|jdˆ› �ƒ ˆr8|jdˆ› �ƒ tjƒ j}tjdj|ƒttj	|j
ƒ|jd� ˆ | |ŽS )zžWrapper function.

            Args:
                *args: Variable length argument list.
                **kwargs: Arbitrary keyword arguments.
            r(   z' is deprecatedz since version z. r4   )ÚcategoryÚfilenameÚlineno)Ú__name__ÚappendÚinspectÚcurrentframeÚf_backÚwarningsÚwarn_explicitÚjoinÚDeprecationWarningÚgetfileÚf_codeÚf_lineno)r   r=   ÚmessageÚframe)r<   r;   r:   r   r   Úwrapper¦   s    


z-deprecated.<locals>.decorate.<locals>.wrapper)Ú	functoolsÚwrapsr   r   )r<   rO   )r;   r:   )r<   r   Údecorate£   s    zdeprecated.<locals>.decorate)r   )r:   r;   rR   r   )r;   r:   r   Ú
deprecatedŽ   s    rS   )Útls_versionÚcipher_suites_namesr   c             C   s„   g }i }x,t tj| ƒd ƒD ]}|jtt|  ƒ qW xH|D ]@}d|krT|j|ƒ q<||krl|j|| ƒ q<td|› d�ƒ‚q<W |S )a  Translates a cipher suites names list; from IANI names to OpenSSL names.

    Args:
        TLS_version (str): The TLS version to look at for a translation.
        cipher_suite_names (list): A list of cipher suites names.

    Returns:
        List[str]: List of translated names.
    r   ú-zThe 'z.' in cipher suites is not a valid cipher suite)Úranger	   ÚindexÚupdater   rB   r
   )rT   rU   Ztranslated_namesZcipher_suitesrX   Únamer   r   r   Úiani_to_openssl_cs_nameÁ   s    
r[   )Údatar   c             C   s   t j| ƒjdƒS )z»Return the hexadecimal representation of the binary data.

    Args:
        data (bytes): The binary data.

    Returns:
        str: The decoded hexadecimal representation of data.
    zutf-8)ÚbinasciiÚhexlifyr   )r\   r   r   r   r^   á   s    	r^   )r   )r   )r4   )NN)%Ú__doc__r]   ÚdecimalrP   rC   rF   Útypingr   r   r   r   r   Ú	constantsr   r	   Úerrorsr
   Útypesr   r   r,   r   Z
BYTE_TYPESÚintÚfloatÚDecimalr+   Ústrr   r   Úobjectr   r3   r9   rS   r[   r^   r   r   r   r   Ú<module>   s(   '4