³ò
Je|Mc           @   s,   d  Z  d d k Z d e f d „  ƒ  YZ d S(   s^   MIME Type helpers

This helper depends on the WebOb package, and has optional Pylons support.
iÿÿÿÿNt	   MIMETypesc           B   sY   e  Z d  Z h  Z d „  Z e e ƒ Z d „  Z e e ƒ Z d „  Z d „  Z d „  Z	 RS(   s   MIMETypes registration mapping
    
    The MIMETypes object class provides a single point to hold onto all
    the registered mimetypes, and their association extensions. It's
    used by the mimetypes method to determine the appropriate content
    type to return to a client.
    
    c         C   s   t  i ƒ  d S(   sÕ   Loads a default mapping of extensions and mimetypes
        
        These are suitable for most web applications by default. 
        Additional types can be added by using the mimetypes module.
        
        N(   t	   mimetypest   init(   t   cls(    (    se   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/mimehelper.pyR      s    c         C   s.   d | j o t  d ƒ ‚ n | |  i | <d S(   sã   Create a MIMEType alias to a full mimetype.

        Examples:

        - ``add_alias('html', 'text/html')``
        - ``add_alias('xml', 'application/xml')``

        An ``alias`` may not contain the ``/`` character.

        t   /s$   MIMEType aliases may not contain '/'N(   t
   ValueErrort   aliases(   R   t   aliast   mimetype(    (    se   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/mimehelper.pyt	   add_alias   s    c         C   s   | |  _  d S(   s;   ``environ`` is the WSGI environment of the current request.N(   t   env(   t   selft   environ(    (    se   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/mimehelper.pyt   __init__,   s    c         C   s+   d |  i  j o | |  i  d i _ n | S(   Ns   pylons.pylons(   R
   t   responset   content_type(   R   R   (    (    se   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/mimehelper.pyt   _set_response_content_type0   s    c         C   su  d d k  } | t i j o t i | } n |  i d } t i | ƒ d } d } t } t | i	 d ƒ ƒ d j o. | i	 d ƒ d } d | j o
 t
 } q£ n d |  i j o  | i i d	 |  i d ƒ } n | t j oD | d j o |  i | ƒ Sq$| | j o |  i | ƒ Sq$t Sn | | j o |  i | ƒ Sn0 | d j o | | j o |  i | ƒ Sn t Sd S(
   s…  Check the PATH_INFO of the current request and client's HTTP Accept 
        to attempt to use the appropriate mime-type.

        If a content-type is matched, return the appropriate response
        content type, and if running under Pylons, set the response content
        type directly. If a content-type is not matched, return ``False``.
                
        This works best with URLs that end in extensions that differentiate
        content-type. Examples: ``http://example.com/example``, 
        ``http://example.com/example.xml``, ``http://example.com/example.csv``
                
        Since browsers generally allow for any content-type, but should be
        sent HTML when possible, the html mimetype check should always come
        first, as shown in the example below.
        
        Example::
        
            # some code likely in environment.py
            MIMETypes.init()
            MIMETypes.add_alias('html', 'text/html')
            MIMETypes.add_alias('xml', 'application/xml')
            MIMETypes.add_alias('csv', 'text/csv')
            
            # code in a Pylons controller
            def somaction(self):
                # prepare a bunch of data
                # ......
                
                # prepare MIMETypes object
                m = MIMETypes(request.environ)
                
                if m.mimetype('html'):
                    return render('/some/template.html')
                elif m.mimetype('atom'):
                    return render('/some/xml_template.xml')
                elif m.mimetype('csv'):
                    # write the data to a csv file
                    return csvfile
                else:
                    abort(404)

            # Code in a non-Pylons controller.
            m = MIMETypes(environ)
            response_type = m.mimetype('html')
            # ``response_type`` is a MIME type or ``False``.
        iÿÿÿÿNt	   PATH_INFOi    R   i   t   .t   HTTP_ACCEPTt   ACCEPT(   t   webobR    R   R
   R   t
   guess_typet   Nonet   Falset   lent   splitt   Truet   acceptparset
   MIMEAcceptR   (   R   R   R   t   patht   guess_from_urlt   possible_from_accept_headert   has_extensiont	   last_part(    (    se   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/mimehelper.pyR   5   s2    /(
   t   __name__t
   __module__t   __doc__R   R   t   classmethodR	   R   R   R   (    (    (    se   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/mimehelper.pyR       s   				(   R%   R   t   objectR    (    (    (    se   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/mimehelper.pys   <module>   s   