a
    còfj  ã                   @   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 ddl	m
Z
 eefZeeejfZddd„Zdd	d
„Zdd„ Zdd„ Zddd„Zddd„Zdd„ Zdd„ ZdS )z&This module contains helper functions.é    Né   )ÚTLS_CIPHER_SUITESÚTLS_VERSIONS)ÚInterfaceErrorúutf-8c                 C   s   t | tƒr| S |  |¡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©ÚvalueÚencoding© r   úK/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/mysqlx/helpers.pyÚencode_to_bytes-   s    	r   c                 C   s   t | tƒr|  |¡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   Údecoder
   r   r   r   Údecode_from_bytes9   s    
r   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)ÚobjÚkeyr   r   r   Úget_item_or_attrF   s    
r   c                     s2   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.
    c                 S   s¾   | du r| S t | tƒr| S t | ttfƒrr|  dd¡} |  dd¡} |  dd¡} |  dd	¡} |  d
d¡} |  dd¡} nH|  dd¡} |  dd¡} |  dd¡} |  dd¡} |  dd¡} |  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   Ú_escape^   s$    
zescape.<locals>._escaper   c                    s   g | ]}ˆ |ƒ‘qS r   r   )Ú.0Úarg©r&   r   r   Ú
<listcomp>t   ó    zescape.<locals>.<listcomp>r   )Úlen)Úargsr   r)   r   ÚescapeS   s    r.   Ú c                 C   s,   |dkrd  |  dd¡¡S 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_QUOTESz"{0}"r!   z""z`{0}`ú`z``)Úformatr%   )Ú
identifierZsql_moder   r   r   Úquote_identifierx   s    r3   c                    s   ‡ ‡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.

    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
    c                    s   t  ˆ ¡‡ ‡‡fdd„ƒ}|S )zDecorate function.c                     sp   d  ˆ j¡g}ˆr"| d  ˆ¡¡ ˆr6| d  ˆ¡¡ t ¡ j}tjd |¡t	t 
|j¡|jd� ˆ | i |¤ŽS )zžWrapper function.

            Args:
                *args: Variable length argument list.
                **kwargs: Arbitrary keyword arguments.
            z'{}' is deprecatedz since version {}z. {}r/   )ÚcategoryÚfilenameÚlineno)r1   Ú__name__ÚappendÚinspectÚcurrentframeÚf_backÚwarningsÚwarn_explicitÚjoinÚDeprecationWarningÚgetfileÚf_codeÚf_lineno)r-   ÚkwargsÚmessageÚframe)ÚfuncÚreasonÚversionr   r   Úwrapper›   s    

ýz-deprecated.<locals>.decorate.<locals>.wrapper)Ú	functoolsÚwraps)rF   rI   ©rG   rH   )rF   r   Údecorate™   s    zdeprecated.<locals>.decorater   )rH   rG   rM   r   rL   r   Ú
deprecatedˆ   s    rN   c                 C   sz   g }i }t t | ¡d ƒD ]}| tt|  ¡ q|D ]>}d|v rN| |¡ q6||v rf| || ¡ q6td |¡ƒ‚q6|S )zã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.
    r   ú-z5The '{}' in cipher suites is not a valid cipher suite)Úranger   ÚindexÚupdater   r8   r   r1   )Ztls_versionZcipher_suites_namesZtranslated_namesZcipher_suitesrQ   Únamer   r   r   Úiani_to_openssl_cs_name²   s    ÿrT   c                 C   s   t  | ¡ d¡S )z³Return the hexadecimal representation of the binary data.

    Args:
        data (str): The binary data.

    Returns:
        bytes: The hexadecimal representation of data.
    r   )ÚbinasciiÚhexlifyr   )Údatar   r   r   rV   Ì   s    	rV   )r   )r   )r/   )NN)Ú__doc__rU   ÚdecimalrJ   r9   r<   Ú	constantsr   r   Úerrorsr   r$   r   Z
BYTE_TYPESÚintÚfloatÚDecimalr#   r   r   r   r.   r3   rN   rT   rV   r   r   r   r   Ú<module>   s"   

%

*