
{Jc           @   s  d  Z  d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k l Z d d k	 l
 Z
 d d k Z d d k l Z l Z d d k l Z l Z l Z l Z d d k i Z d d k l Z l Z l Z l Z d d k l Z d d	 k l Z l Z d d
 k  l! Z! e i" d  Z# e i$ d d  oS e i$ d d  d j o6 d d k& Z& e i' h e& i( e& i$ d   d 6 qn e) d  Z* d e+ f d     YZ, d d e i- i. d d d  Z/ d e f d     YZ0 e e0 e  e0 i1 d  Z2 d   Z3 d   Z4 d d d d e) d d d d  Z5 d   Z6 d   Z7 d   Z8 d e9 f d      YZ: d! e: f d"     YZ; e; Z< d# e9 f d$     YZ= d% e9 f d&     YZ> d d'  Z? d(   Z@ d) d d*  ZA d+   ZB d d,  ZC d d- d. d/ d0 d1 d2 d3 d4 d! d5 d6 g ZD d S(7   s/   Classes and methods for TurboGears controllers.iN(   t   izip(   t   isclass(   t   requestt   response(   t   abstractt   always_overridest   Methodt   NoApplicableMethods(   t   viewt   databaset   errorhandlingt   config(   t   weak_signature_decorator(   t   error_handlert   exception_handler(   t   Invalids   turbogears.controllerss   session_filter.ons   session_filter.storage_typet
   PostgreSQLs   sessions.postgres.dsns   session_filter.get_dbc         C   s  t  |  t  owd d k l } t i   } t t | t t i d    } h  }	 t	 i
 d g   }
 t	 i
 d t  o |
 i d d  n xR |
 D]J } t i |  } t |  o |   } n | |	 d | i d  d <q W|  i |	  x |  i   D] } t | d	  o q ne y | i   } Wn t t f j
 o g  } n Xy | i   } Wn t t f j
 o g  } n X| i |  x4 | D], } t | d
 | i  } | | i |  qWq W| i d d    | |  d <x= t |  D]/ } | | i d d    | | |  d | <qWt   } | o | |  d <n h | d 6} t i |  d | d | d | d | d | }  | d } n | o | t i  d <n t i  i
 d d  } | i! d  o- t  |  t"  o |  i# t i$    }  qn |  S(   s   Produce final output form from data returned from a controller method.

    See the expose() arguments for more info since they are the same.

    i(   t   js_locations   tg.include_widgetss   tg.mochikit_alli    s   turbogears.mochikits   tg_%st   .t   retrieve_resourcest   locationt   keyc         S   s   t  |  d  d  S(   t   orderi    (   t   getattr(   t   obj(    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyt   <lambda>S   s    t   tg_cssc         S   s   t  |  d  d  S(   R   i    (   R   (   R   (    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyR   V   s    s   tg_js_%st   tg_flashs   Content-Typet   templatet   formatt   mappingt   headerst   fragments
   text/plains   text/N(%   t
   isinstancet   dictt   turbogears.widgetsR   t   tg_utilt   setlikeR    t   itert   NoneR   t   gett   Falset   insertt
   load_classR   t   splitt   updatet
   itervaluest   hasattrt   retrieve_csst   AttributeErrort	   TypeErrort   retrieve_javascriptt   add_allR   t   headt   addt   sortt
   _get_flashR   t   renderR   R   t
   startswitht   unicodet   encodet   get_template_encoding_default(   t   outputR   R   t   content_typeR   R    R   t   csst   jst   include_widgetst   include_widgets_lstt   namet   widgett   valuet   css_resourcest   js_resourcest   scriptR   R   R   (    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyt   _process_output$   sj    !   
 		t   BadFormatErrorc           B   s   e  Z d  Z RS(   s   Output-format exception.(   t   __name__t
   __module__t   __doc__(    (    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyRK   o   s   c            s%         f d   } t  |  S(   s
  Validate input.

    @param form: a form instance that must be passed throught the validation
    process... you must give a the same form instance as the one that will
    be used to post data on the controller you are putting the validate
    decorator on.
    @type form: a form instance

    @param validators: individual validators to use for parameters.
    If you use a schema for validation then the schema instance must
    be the sole argument.
    If you use simple validators, then you must pass a dictionary with
    each value name to validate as a key of the dictionary and the validator
    instance (eg: tg.validators.Int() for integer) as the value.
    @type validators: dictionary or schema instance

    @param failsafe_schema: a schema for handling failsafe values.
    The default is 'none', but you can also use 'values', 'map_errors',
    or 'defaults' to map erroneous inputs to values, corresponding exceptions
    or method defaults.
    @type failsafe_schema: errorhandling.FailsafeSchema

    @param failsafe_values: replacements for erroneous inputs. You can either
    define replacements for every parameter, or a single replacement value
    for all parameters. This is only used when failsafe_schema is 'values'.
    @type failsafe_values: a dictionary or a single value

    @param state_factory: If this is None, the initial state for validation
    is set to None, otherwise this must be a callable that returns the initial
    state to be used for validation.
    @type state_factory: callable or None

    c            s_   t    o$ t  d  o  f d     n  f d           f d   } | S(   Nt   validatec            s
     |   S(    (    (   t   self(   t   form(    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyR      s    c            s     S(    (    (   RP   (   RQ   (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyR      s    c            s=  t  t d  o |  | |   S | o | d p | d  } t i |  | |  \ } } h  }  d  j	 o    } n d  } | oc | i   } y | i | i | |   Wn* t j
 o } | i	   } | t _
 n X| t _ n   o t   t  oi x   i   D]T \ } }	 y& |	 i | i | d   |  | | <Wqt j
 o }
 |
 | | <qXqWqy) | i   } | i   i | |   Wqt j
 o } | i	   } | t _
 qXn | t _ | i   t _ | t _ | o t i   | |  |  } n t i |  | |  \ } } t i | |  | |  S(   Nt   validation_statei    RP   (   R/   R   R$   t   to_kwR'   t   copyR-   RO   R   t   unpack_errorst   validation_exceptiont   validated_formR!   R"   t	   iteritemst	   to_pythonR(   t   validation_errorst   input_valuesRR   R
   t   dispatch_failsafet   from_kwt   run_with_errors(   t   funct   argst   kwRQ   t   errorst   stateRF   t   et   fieldt	   validatort   error(   t
   validatorst   state_factoryt   failsafe_schemat   failsafe_valuest	   init_form(    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyRO      sN    "  			(   t   callableR/   (   R_   RO   (   RQ   Rj   Ri   Rk   Rh   (   Rl   st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyt   entangle   s
    0(   R   (   RQ   Rh   Rj   Rk   Ri   Rn   (    (   RQ   Rj   Ri   Rk   Rh   st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyRO   s   s    $7t   Firstc           B   s   e  Z d  Z d   Z RS(   s2   Resolve ambiguousness by calling the first method.c         C   s   |  S(   N(    (   RP   t   other(    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyt   merge   s    (   RL   RM   RN   Rq   (    (    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyRo      s   t   firstc   	      C   s   | d j oE | o4 | i  d  } | d j o
 | } qN | |  } qR t } n d | g } | o | i d |  n d i |  } t i d |  t |  |  |  | S(   Nt   defaultt   :is&   kw.get("tg_format", "default") == "%s"s@   (accept == "%s" and kw.get("tg_format", "default") == "default")s    or s   Generated rule %s(   t   findt   Truet   appendt   joint   logt   debugRr   (	   t   _exposet   found_defaultt	   as_formatt   accept_formatR   t   rulefunct   colont	   rulepartst   rule(    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyt	   _add_rule   s    

	c         C   s|   t    d    } |  i o' t i d  t | d  d    n t } x# |  i D] } t | | |  } qS W| |  _ d  S(   Nc         _   s   d  S(   N(    (   R_   t   acceptt
   allow_jsonR`   Ra   (    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyR{      s    s   Adding allow_json rule: allow_json and (kw.get("tg_format", None) == "json" or accept in ("application/json", "text/javascript"))si   allow_json and (kw.get("tg_format", None) == "json" or accept in ("application/json", "text/javascript"))c      	   _   s   t  |  d  d  d d t | |  S(   t   jsons   application/jsonN(   t   _execute_funcR'   R)   (   t   _funcR   R   R`   Ra   (    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyR      s    (	   R   t   _allow_jsonRy   Rz   Rr   R)   t	   _ruleinfoR   R{   (   R_   R{   R|   t   ruleinfo(    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyt   _build_rules   s    

 Rs   c   	         s     p
    n  d j p.  d j o1   d j o$  d j p  o d   t  n  d j o t i d d   n t i d d  o t i i   n          f d   } t |  S(   sS  Exposes a method to the web.

    By putting the expose decorator on a method, you tell TurboGears that
    the method should be accessible via URL traversal. Additionally, expose
    handles the output processing (turning a dictionary into finished
    output) and is also responsible for ensuring that the request is
    wrapped in a database transaction.

    You can apply multiple expose decorators to a method, if
    you'd like to support multiple output formats. The decorator that's
    listed first in your code without as_format or accept_format is
    the default that is chosen when no format is specifically asked for.
    Any other expose calls that are missing as_format and accept_format
    will have as_format implicitly set to the whatever comes before
    the ":" in the template name (or the whole template name if there
    is no ":". For example, <code>expose("json")</code>, if it's not
    the default expose, will have as_format set to "json".

    When as_format is set, passing the same value in the tg_format
    parameter in a request will choose the options for that expose
    decorator. Similarly, accept_format will watch for matching
    Accept headers. You can also use both. expose("json", as_format="json",
    accept_format="application/json") will choose JSON output for either
    case: tg_format=json as a parameter or Accept: application/json as a
    request header.

    Passing allow_json=True to an expose decorator
    is equivalent to adding the decorator just mentioned.

    Each expose decorator has its own set of options, and each one
    can choose a different template or even template engine (you can
    use Kid for HTML output and Cheetah for plain text, for example).
    See the other expose parameters below to learn about the options
    you can pass to the template engine.

    Take a look at the
    <a href="tests/test_expose-source.html">test_expose.py</a> suite
    for more examples.

    @param template "templateengine:dotted.reference" reference along the
            Python path for the template and the template engine. For
            example, "kid:foo.bar" will have Kid render the bar template in
            the foo package.
    @keyparam format format for the template engine to output (if the
            template engine can render different formats. Kid, for example,
            can render "html", "xml" or "xhtml")
    @keyparam content_type sets the content-type http header
    @keyparam allow_json allow the function to be exposed as json
    @keyparam fragment for template engines (like Kid) that generate
            DOCTYPE declarations and the like, this is a signal to
            just generate the immediate template fragment. Use this
            if you're building up a page from multiple templates or
            going to put something onto a page with .innerHTML.
    @keyparam mapping mapping with options that are sent to the template
            engine
    @keyparam as_format designates which value of tg_format will choose
            this expose.
    @keyparam accept_format which value of an Accept: header will
            choose this expose.

    R   s   tg.content_types   tg.session.automatic_lockc            s   t  i d |   t  i d      t |  d t  pM  f d   } t |  _ g  |  _ t i d t  } | p
  d j |  _	 n
 d   } |  i i
 d t d	  d
   d  d      f d      o t |  _	 n | S(   Ns   Exposing %ss:   template: %s, format: %s, allow_json: %s, content-type: %st   exposedc      
      sk  t  i i    d t _ t i i d d  i   } t i	 |  } t
 |  d  p t |   n yc t
 t d  o" |  i |  | |  i | |  } n. t t _ t i |  i |  | |  i | |  } Wn t j
 o } | i } | ox | d om t | d t  oY | d d |  j oD t i i } | o$ | d d j o t i |   n t i  n   n X| S(	   Ni   t   Acceptt    R{   t   in_transactioni    id   i   (   R   t   baset   _choose_engineR   t   tg_template_enginenameR   R(   t   lowerR$   t   simplify_http_accept_headerR/   R   R{   R   Rv   R   R	   t   run_with_transactionR   R`   R!   t   tuplet   cherrypyR   t   statust	   HTTPErrort   NotFound(   R_   R`   Ra   R   R>   Rd   R   (   R   (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyt   exposeX  s.    		&s   tg.allow_jsonR   c         _   s   |  | |   S(    (    (   R_   R`   Ra   (    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyR   }  s    i    R}   R~   R   R   c      	      s   t  |        | |  S(    (   R   (   R   R   R   R`   Ra   (   R?   R   R   R    R   (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyR     s    (   Ry   Rz   R   R)   Rv   R   R   R   R(   R   R*   R"   (   R_   R   t   allow_json_from_config(   R~   R   R    R   R?   R}   R   R   (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyRn   S  s     	 			N(   R'   Rv   R   R(   R   t   sessiont   acquire_lockR   (	   R   R   R   R?   R    R}   R   R~   Rn   (    (   R   R   R    R   R?   R}   R~   R   st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyR     s    @
'
$6c         C   s8  t  i d t  o* t i | d d g t  i d g    n yQ t g  } | i   D]- \ }	 }
 |	 |  i j o | |	 |
 f qT qT ~  } Wn t j
 o h  } n Xt i	 |  | |  \ } } | i
 |  t  i d d  d j o t i d |  | |  n t i |  | |  } t t t d d	   i d
  o/ y t i d =Wn t t f j
 o n Xd St | t t t t i f  p! t d | d i i |  i f  t | t  o( | i d |  } | i d |  } n | o1 | i d  o! |  i |  i i d   | } n t  | | | | | |  Sd S(   s/   Call controller method and process it's output.s   tg.strict_parameterst	   tg_randomt	   tg_formats   tg.ignore_parameterss   server.environmentt   developments   Calling %s with *(%s), **(%s)R   R   t   204s   Content-TypeNsh   Method %s.%s() returned unexpected output. Output should be of type basestring, dict, list or generator.i    t   tg_templateR   (!   R   R(   R)   R$   t   remove_keysR"   t   itemst   _tg_argsR1   t
   adapt_callR-   Ry   Rz   R
   t   try_callt   strR   R   R:   R   t   KeyErrorR!   t
   basestringt   listt   typest   GeneratorTypet   AssertionErrort	   __class__RL   t   popRM   t   rfindRJ   (   R_   R   R   R?   R   R    R`   Ra   t   _[1]t   kt   vt   tg_kwR>   (    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyR     s8    Q" "!c         C   s:   t  i t  i |    }  |  t i d <d t i d d <d S(   sB   Set a message to be displayed in the browser on next page display.R   t   /t   pathN(   R$   t   quote_cookiet   to_utf8R   t   simple_cookie(   t   message(    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyt   flash  s    c             s   t  i }  t i     f d   }   i d  o9   d i }   i d  |  i d  o |   q nC |  i d  o, |  d i }   i d  p |   q n d } | o t t i	 |  d  } n | S(   sA   Retrieve the flash message (if one is set), clearing the message.c              s*   d   d <d   d d <d   d d <d  S(   NR   R   i    t   expiresR   R   (    (    (   t   response_cookie(    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyt   clearcookie  s    
R   s   utf-8N(
   R   R   R   t   has_keyRF   R   R'   R;   R$   t   unquote_cookie(   t   request_cookieR   R   (    (   R   st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyR8     s     		t
   Controllerc           B   s   e  Z d  Z RS(   s   Base class for a web application's controller.

    Currently, this provides positional parameters functionality
    via a standard default method.

    (   RL   RM   RN   (    (    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyR     s   t   RootControllerc           B   sn   e  Z d  Z e Z e i d  Z h e i d 6e i	 d 6e i
 d 6Z d d d  Z e i d  Z d   Z RS(	   s   Base class for the root of a web application.

    Your web application should have one of these. The root of
    your application is used to compute URLs used by your app.

    s   cherrypy.msgi    i   i   t	   nocontextc         C   s3   |  i  | } d i | d | f  } | |  d  S(   NR   s   : (   t
   msglogfuncRx   (   RP   t   msgt   contextt   severityRy   t   text(    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyt   _cp_log_message  s    s   turbogears.accessc         C   s>  d } yA t  i } | o' t | t  o | i d  } qF n d } Wn t j
 o d } n Xt i d t i    } h	 t  i	 i
 d  p t  i p t  i d 6d d 6| d 6| d	 6t  i d
 6t i i d d  d d 6t i	 i
 d  p d d 6t  i	 i
 d d  d 6t  i	 i
 d d  d 6} |  i i | |  d  S(   Nsl   %(host)s %(ident)s %(authuser)s [%(date)s] "%(request)s" %(status)s %(bytes)s "%(referrer)s" "%(useragent)s"s   utf-8t   -s   %d/%b/%Y:%H:%M:%S +0000s   X-Forwarded-Fort   hostt   identt   authusert   dateR   t    i   i    R   s   Content-Lengtht   bytest   refererR   t   referrers
   user-agentt	   useragent(   R   t	   user_nameR!   R;   R<   R1   t   timet   strftimet   gmtimeR   R(   t   remote_hostt   remote_addrt   requestLineR   R   R,   t	   accesslogt   info(   RP   t   tmplt   usernamet   request_datet   request_info(    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyt   _cp_log_access  s,    	

(   RL   RM   RN   Rv   t   is_app_roott   loggingt	   getLoggert   msglogR   t   warningRg   R   R   R   R   (    (    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyR     s   $t   ExposedDescriptorc           B   s   e  Z d  Z d d  Z RS(   s7   Descriptor used by RESTMethod to tell if it is exposed.c         C   s   | d j o
 | } n t i i } | i   } t | | d  } t |  o t | d t  o t St i	 d d | t i i
 f   d S(   sN   Return True if object has a method for HTTP method of current request
        R   i  s   %s not allowed on %sN(   R'   R   R   t   methodR   R   Rm   R)   Rv   R   t   browser_url(   RP   R   t   clst   cp_methodnamet
   methodnameR   (    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyt   __get__!  s     
 N(   RL   RM   RN   R'   R   (    (    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyR     s   t
   RESTMethodc           B   s)   e  Z d  Z e   Z d   Z d   Z RS(   s  Allow REST style dispatch based on different HTTP methods.

    For an elaborate usage example see turbogears.tests.test_restmethod.

    In short, instead of an exposed method, you define a sub-class of
    RESTMethod inside the controller class and inside this class you define
    exposed methods named after each HTTP method that should be supported.

    Example::

        class Controller(controllers.Controller):

            class article(copntrollers.RESTMethod):
                @expose()
                def get(self, id):
                    ...

                @expose()
                def post(self, id):
                    ...

    c         O   s1   t  i i i   } t |  |  | |   |  _ d  S(   N(   R   R   R   R   R   t   result(   RP   t   lRa   R   (    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyt   __init__G  s    c         C   s   t  |  i  S(   N(   R&   R   (   RP   (    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyt   __iter__K  s    (   RL   RM   RN   R   R   R   R   (    (    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyR   .  s   		c         K   sW  t  |  t  p d i t |    }  n |  i d  o t i d  p d i d  } t i	   oR t
   t i |  }  y | t i d i d  7} Wq t t f j
 o q Xn | |  }  n | d	 j o
 | } n@ y | i   } | i |  Wn t j
 o t d   n Xg  } x | i   D] \ } } | d	 j o q.n t  | t t f  o+ g  } | D] } | | | f qo~ }	 n | | f g }	 xd |	 D]\ \ }
 } | d	 j o qn t  | t  o | i d  } n | i |
 t |  f  qWq.W| oC t i | t  } d |  j o |  d | 7}  qS|  d | 7}  n |  S(
   s  Computes relocatable URLs.

    tgpath can be a list or a string. If the path is absolute (starts with a
    "/"), the server.webpath, SCRIPT_NAME and the approot of the application
    are prepended to the path. In order for the approot to be detected
    properly, the root object must extend controllers.RootController.

    Query parameters for the URL can be passed in as a dictionary in
    the second argument and/or as keyword parameters where keyword args
    overwrite entries in the dictionary.

    Values which are a list or a tuple are used to create multiple
    key-value pairs.

    tgpath may also already contain a (properly escaped) query string seperated
    by a question mark ('?'), in which case additional query params are
    appended.

    R   s   server.webpathR   t   SCRIPT_NAMEs/   url() expects a dictionary for query parameterst   utf8t   ?t   &N(   R!   R   Rx   R   R:   R   R(   t   rstripR$   t   request_availablet   check_app_rootR   t   app_roott   wsgi_environR1   R   R'   RT   R-   R2   RX   R   R;   R<   Rw   R   t   urllibt	   urlencodeRv   (   t   tgpatht   tgparamsRa   t   webpathR`   R   RF   R   R   t   pairsR   t   query_string(    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyt   urlO  sP    	
 + !c          C   sk   t  i }  t i } |  d  p | i d | i d   } | p& d |  d d  |  d d  f } n | S(	   s   Return name of the server this application runs on.

    Respects 'Host' and 'X-Forwarded-Host' header.

    See the docstring of the 'absolute_url' function for more information.

    s   tg.url_domains   X-Forwarded-Hostt   Hosts   %s:%ss   server.socket_hostt	   localhosts   server.socket_porti  (   R   R(   R   R   (   R(   t   hR   (    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyt   get_server_name  s    		(R   c         K   s   t  i } | d t  } t i i d  o
 d } n | d  } | p t i } n d | t   f } | d t  o! | o | d  i d  } n d	 | t |  | |  f S(
   sH  Return absolute URL (including schema and host to this server).

    Tries to account for 'Host' header and reverse proxying
    ('X-Forwarded-Host').

    The host name is determined this way:

    * If the config setting 'tg.url_domain' is set and non-null, use this value.
    * Else, if the 'base_url_filter.use_x_forwarded_host' config setting is
      True, use the value from the 'Host' or 'X-Forwarded-Host' request header.
    * Else, if config setting 'base_url_filter.on' is True and
      'base_url_filter.base_url' is non-null, use its value for the host AND
      scheme part of the URL.
    * As a last fallback, use the value of 'server.socket_host' and
      'server.socket_port' config settings (defaults to 'localhost:8080').

    The URL scheme ('http' or 'http') used is determined in the following way:

    * If 'base_url_filter.base_url' is used, use the scheme from this URL.
    * If there is a 'X-Use-SSL' request header, use 'https'.
    * Else, if the config setting 'tg.url_scheme' is set, use its value.
    * Else, use the value of 'cherrypy.request.scheme'.

    s$   base_url_filter.use_x_forwarded_hosts	   X-Use-SSLt   httpss   tg.url_schemes   %s://%ss   base_url_filter.ons   base_url_filter.base_urlR   s   %s%s(	   R   R(   R)   R   R   t   schemeR  R   R  (   R  t   paramsRa   R(   t   use_xfhR  t   base_url(    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyt   absolute_url  s    	
c          C   s%  t  t d  o d St }  t i } t |  d } g  } x t t |  d d d  D]r } | | \ } } |  o, t | t  o | | j o Pn t }  n |  o! | d j o | i	 d |  qW qW Wd i
 |  } | i d  p d | } n | i d  o | d  } n | t _ d S(   s    Sets request.app_root if needed.R   Ni   ii    R   (   R/   R   R)   t   object_trailt   lent   xrangeR!   R   Rv   R*   Rx   R:   t   endswithR   (   t
   found_roott   trailt   topt   rootlistt   iR   R   R   (    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyR     s*    	 
c         K   s   t  |  t  p d i t |    }  n |  i d  pP t i } t   | i t i  o | t	 t i  } n t
 i | |   }  n t i t d |  d | |    d S(   s   Redirect (via cherrypy.HTTPRedirect).

    Raises the exception instead of returning it, this to allow
    users to both call it as a function or to raise it as an exception.

    R   R  R  N(   R!   R   Rx   R   R:   R   t	   path_infoR   R   R  t   urlparset   urljoinR   t   HTTPRedirectR  (   t   redirect_patht   redirect_paramsRa   R   (    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyt   redirect  s    	R  R   R   R   R  R   R!  t   RootR  RO   (E   RN   R   t   reR   R  R   R   t	   itertoolsR    t   inspectR   R   R   R   t
   peak.rulesR   R   R   R   t   turbogears.utilt   utilR$   t
   turbogearsR   R	   R
   R   t   turbogears.decoratorR   t   turbogears.errorhandlingR   R   t   turbogears.validatorsR   R   Ry   R(   R'   t   psycopg2R-   t   connectR)   RJ   t	   ExceptionRK   t   FailsafeSchemat   noneRO   Ro   t   make_decoratorRr   R   R   R   R   R   R8   t   objectR   R   R"  R   R   R  R  R  R   R!  t   __all__(    (    (    st   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/controllers.pyt   <module>   sz   ""	J	\				5			/!>	&	