o
    éË·eB  ã                   @   sP   d Z ddlZddlZddddddœZd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )zH
Helper functions to perform simple tasks for multiple areas of the API
é    NÚGETÚPOSTÚPATCHÚPUTÚDELETE)ÚgetÚcreateÚupdateÚcreate_or_updateÚdeletec                 C   s&   t | ƒ |  ¡  ¡ } t | ¡}| ¡ S )a  
    The MD5 hash of the lowercase version of the list member's email.
    Used as subscriber_hash

    :param member_email: The member's email address
    :type member_email: :py:class:`str`
    :returns: The MD5 hash in hex
    :rtype: :py:class:`str`
    )Úcheck_emailÚlowerÚencodeÚhashlibÚmd5Ú	hexdigest)Úmember_emailÚm© r   úI/var/www/ideatree/venv/lib/python3.10/site-packages/mailchimp3/helpers.pyÚget_subscriber_hash   s   

r   c                 C   s   t  d| ¡r| S t| ƒS )a˜  
    Check the passed value to see if it matches a 32 character hex number that
    MD5 generates as output, or compute that value assuming that the input is
    an email address.

    :param potential_hash: A value to be passed to any of the endpoints that
    expect an MD5 of an email address
    :type potential_hash: :py:class:`str`
    :returns: A valid MD5 hash in hex
    :rtype: :py:class:`str`
    z^[0-9a-f]{32}$)ÚreÚmatchr   )Úpotential_hashr   r   r   Úcheck_subscriber_hash!   s   r   c                 C   s   t  d| ¡s
tdƒ‚dS )aG  
    Function that verifies that the string passed is a valid email address.

    Regex for email validation based on MailChimp limits:
    http://kb.mailchimp.com/accounts/management/international-characters-in-mailchimp

    :param email: The potential email address
    :type email: :py:class:`str`
    :return: Nothing
    z	.+@.+\..+z*String passed is not a valid email addressN)r   r   Ú
ValueError)Úemailr   r   r   r   3   s   r   c                 C   s&   t  dt j¡}t  || ¡stdƒ‚dS )a  
    Function that verifies that the string passed is a valid url.

    Original regex author Diego Perini (http://www.iport.it)
    regex ported to Python by adamrofer (https://github.com/adamrofer)
    Used under MIT license.

    :param url:
    :return: Nothing
    u—  ^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-zÂ¡-ï¿¿0-9]-?)*[a-zÂ¡-ï¿¿0-9]+)(?:\.(?:[a-zÂ¡-ï¿¿0-9]-?)*[a-zÂ¡-ï¿¿0-9]+)*(?:\.(?:[a-zÂ¡-ï¿¿]{2,})))(?::\d{2,5})?(?:/\S*)?$z String passed is not a valid urlN)r   ÚcompileÚUNICODEr   r   )ÚurlÚ	URL_REGEXr   r   r   Ú	check_urlC   s   ír!   c                 C   sR   |   ¡ }| ¡ D ]\}}t|tƒr"t| |¡tƒr"||  |7  < q|||< q|S )a  
    Given two dicts, x and y, merge them into a new dict as a shallow copy.

    The result only differs from `x.update(y)` in the way that it handles list
    values when both x and y have list values for the same key. In which case
    the returned dictionary, z, has a value according to:
      z[key] = x[key] + z[key]

    :param x: The first dictionary
    :type x: :py:class:`dict`
    :param y: The second dictionary
    :type y: :py:class:`dict`
    :returns: The merged dictionary
    :rtype: :py:class:`dict`
    )ÚcopyÚitemsÚ
isinstanceÚlistr   )ÚxÚyÚzÚkeyÚvaluer   r   r   Úmerge_resultsg   s   
r+   )	Ú__doc__r   r   ÚHTTP_METHOD_ACTION_MATCHINGr   r   r   r!   r+   r   r   r   r   Ú<module>   s   û	$