3
S^                 @   sd   d Z ddlZddlZddlZddlmZ dddZddd	Zd
d Zdd Z	dddZ
dddZdS )z&This module contains helper functions.    N   )NUMERIC_TYPESutf-8c             C   s   t | tr| S | j|S )zReturns 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
   @/tmp/pip-build-8app2_gc/mysql-connector-python/mysqlx/helpers.pyencode_to_bytes&   s    	r   c             C   s   t | tr| j|S | S )zReturns 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_bytes2   s    
r   c             C   s   t | tr| | S t| |S )zGet 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_attr?   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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   r   r   	bytearrayreplace)r   r
   r
   r   _escapeW   s$    
zescape.<locals>._escaper   c                s   g | ]} |qS r
   r
   ).0arg)r"   r
   r   
<listcomp>m   s    zescape.<locals>.<listcomp>r   )len)argsr
   )r"   r   escapeL   s    r(    c             C   s,   |dkrdj | jddS dj | j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_identifierq   s    r-   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 j  fdd}|S )zDecorate function.c                 sl   dj  jg}r"|jdj  r6|jdj  tj j}tjdj|t	tj
|j|jd  | |S )zWrapper function.

            Args:
                *args: Variable length argument list.
                **kwargs: Arbitrary keyword arguments.
            z'{}' is deprecatedz since version {}z. {}r)   )categoryfilenamelineno)r+   __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)r@   rC   )rA   rB   )r@   r   decorate   s    zdeprecated.<locals>.decorater
   )rB   rA   rF   r
   )rA   rB   r   
deprecated   s    rG   )r   )r   )r)   )NN)__doc__rD   r3   r6   compatr   r   r   r   r(   r-   rG   r
   r
   r
   r   <module>   s   

%
