ó
º9—^c           @   sd   d  Z  d d l Z d d l Z d d l Z d d l m Z d d l m Z d e f d „  ƒ  YZ	 d S(   s;   Parse data received from the SendGrid Inbound Parse webhookiÿÿÿÿN(   t	   iteritems(   t   secure_filenamet   Parsec           B   sz   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e d „  ƒ Z	 e d „  ƒ Z
 e d „  ƒ Z e d	 „  ƒ Z RS(
   c         C   sA   | j  |  _ | |  _ | j d t ƒ | j |  _ | j |  _ d  S(   Nt   as_text(	   t   keyst   _keyst   _requestt   get_datat   Truet   formt   _payloadt   datat   _raw_payload(   t   selft   configt   request(    (    s@   /tmp/pip-unpacked-wheel-v7av_c/sendgrid/helpers/inbound/parse.pyt   __init__   s
    	c         C   sA   i  } x4 |  j  D]) } | |  j k r |  j | | | <q q W| S(   sd   
        Return a dictionary of key/values in the payload received from
        the webhook
        (   R   t   payload(   R   t
   key_valuest   key(    (    s@   /tmp/pip-unpacked-wheel-v7av_c/sendgrid/helpers/inbound/parse.pyR      s
    c         C   s1   d |  j  k r) t j |  j  d ƒ } | Sd Sd S(   s·   
        This only applies to raw payloads:
        https://sendgrid.com/docs/Classroom/Basics/Inbound_Parse_Webhook/setting_up_the_inbound_parse_webhook.html#-Raw-Parameters
        t   emailN(   R   R   t   message_from_stringt   None(   R   t	   raw_email(    (    s@   /tmp/pip-unpacked-wheel-v7av_c/sendgrid/helpers/inbound/parse.pyt   get_raw_email   s    c         C   sX   d } d |  j k r* |  j |  j ƒ } n  |  j ƒ  } | d k	 rT |  j | ƒ } n  | S(   s‘   Returns an object with:
        type = file content type
        file_name = the name of the file
        contents = base64 encoded file contentss   attachment-infoN(   R   R   t   _get_attachmentsR   R   t   _get_attachments_raw(   R   t   attachmentsR   (    (    s@   /tmp/pip-unpacked-wheel-v7av_c/sendgrid/helpers/inbound/parse.pyR   (   s    c         C   sŽ   g  } x t  | j ƒ D]p \ } } i  } | j d k r t | j ƒ } | j | d <| | d <t j | j ƒ  ƒ | d <| j	 | ƒ q q W| S(   Nt   fdopens   <fdopen>t   typet	   file_namet   contents(   NR   s   <fdopen>(
   R    t   filest   filenameR   R   t   content_typet   base64t	   b64encodet   readt   append(   R   R   R   t   _t   filestoraget
   attachmentR!   (    (    s@   /tmp/pip-unpacked-wheel-v7av_c/sendgrid/helpers/inbound/parse.pyR   6   s    
c         C   sÕ   g  } d } xÂ | j  ƒ  D]´ } i  } | j ƒ  d k r= q n  | j ƒ  } | s† t j | j ƒ  ƒ } | ss d } n  d | | f } n  | d 7} | j ƒ  | d <| | d <| j d t ƒ | d <| j | ƒ q W| S(	   Ni   t	   multiparts   .bins   part-%03d%sR   R   t   decodeR   (	   t   walkt   get_content_maintypet   get_filenamet	   mimetypest   guess_extensiont   get_content_typet   get_payloadt   FalseR&   (   R   R   R   t   countert   partR)   R!   t   ext(    (    s@   /tmp/pip-unpacked-wheel-v7av_c/sendgrid/helpers/inbound/parse.pyR   B   s$    	

c         C   s   |  j  S(   N(   R   (   R   (    (    s@   /tmp/pip-unpacked-wheel-v7av_c/sendgrid/helpers/inbound/parse.pyR   V   s    c         C   s   |  j  S(   N(   R   (   R   (    (    s@   /tmp/pip-unpacked-wheel-v7av_c/sendgrid/helpers/inbound/parse.pyR   Z   s    c         C   s   |  j  S(   N(   R
   (   R   (    (    s@   /tmp/pip-unpacked-wheel-v7av_c/sendgrid/helpers/inbound/parse.pyR   ^   s    c         C   s   |  j  S(   N(   R   (   R   (    (    s@   /tmp/pip-unpacked-wheel-v7av_c/sendgrid/helpers/inbound/parse.pyt   raw_payloadb   s    (   t   __name__t
   __module__R   R   R   R   R   R   t   propertyR   R   R   R7   (    (    (    s@   /tmp/pip-unpacked-wheel-v7av_c/sendgrid/helpers/inbound/parse.pyR   	   s   						(
   t   __doc__R#   R   R/   t   sixR    t   werkzeug.utilsR   t   objectR   (    (    (    s@   /tmp/pip-unpacked-wheel-v7av_c/sendgrid/helpers/inbound/parse.pyt   <module>   s   