U
    祡c%L                    @   s2  d Z ddlmZ dZdZdZedkrddlmZ edd	Z	e	j
Zed
ddd eddddd eddddd eddddd edddd edddd e	 \ZZejrejd rdd!lZej  dd!lZdd!lZdd!lZdd!lZdd!lZdd!lZdd!lZdd!lZdd!lZdd!lZdd!l Z dd!l!Z!dd!l"Z"dd!l#Z#dd!l$Z$dd!l%Z%dd"l&m'Z(m&Z&m)Z) dd#l!m*Z* dd$l+m,Z,m-Z- dd%l.m/Z/ dd&l0m1Z1 zdd'l2m3Z4m5Z6 W nz e7k
r    zdd'l8m3Z4m5Z6 W nN e7k
r   zdd'l9m3Z4m5Z6 W n" e7k
r   d(d) Z4e4Z6Y nX Y nX Y nX e j:Z;e;d*kZ<e;d+k Z=d,e;  koLd-k n  Z>d.d/ Z?ze j@jAe jBjA ZCZDW n& eEk
r   d0d1 ZCd2d1 ZDY nX e<rrdd!lFmGZH dd!lIZJdd3lKmLZLmMZN dd4lKmOZOmPZQmRZS ejTeSd5d6ZSdd7lUmVZV e;d8krdd9lWmXZY dd:lZm[Z\ ndd9l]mXZY dd;l^m\Z\ dd!l_Z_dd<l`maZa dd=lbmcZc edZeedZfd>d1 Zgd?d1 ZheiZjd@dA Zkndd!lHZHdd!lJZJdd3llmLZLmMZN dd4lmmOZOmPZQmRZS dd7lnmVZV ddBlmjZj dd!loZ_dd;l^m\Z\ ddClpmpZa ddDlcmqZc e=rdEZre$seret ddFlumYZY dGdH ZvedZwndd9l]mXZY efZfe6ZgexeydIdJdK dGdMdNZzdHdPdQZ{e<r`e{nezZ|e>rddRl`m}Z} G dSdT dTe}Z~dUdV ZdIdXdYZdZd[ ZG d\d] d]eZG d^d_ d_eZG d`da daeZG dbdc dceZG ddde deeZG dfdg dgeZG dhdi dieZG djdk dkeZG dldm dmeZdndo ZG dpdq dqeZG drds dseZG dtdu dueZG dvdw dweZdxdy Zdzd{ ZG d|d} d}eZG d~d deZdJddZG dd deZG dd deZeZeZG dd deeZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deYZG dd deZG dd deZG dd deYZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdKddZdLddZdMddZdNddZdOddZdd Zdd Zdd ZdPddZdd Zdd ZddÄ Zddń ZddǄ ZddɄ Zdd˄ Zdd̈́ ZdQddЄZdRddԄZddք Zed׃Zed؃ZedكZedڃZedۃZed܃Zed݃ZedރZed߃ZedZedZG dd deZG dd dẽZG dd dẽZG dd dẽZG dd dẽZG dd dẽZG dd dẽZG dd dẽZG dd dẽZG dd dẽZG dd dẽZG dd dẽZG dd dẽZG dd dẽZG dd dẽZG d d dẽZG dd dẽZG dd dẽZG dd dẽZG dd	 d	ẽZeeeeeeeeeeeeeeeeeeeߐd
Zdd Zdd ZeZdSddZG dd de"jZG dd deZG dd deZG dd deZG dd deZG dd deZG dd  d eZG d!d" d"eZG d#d$ d$eZd%d& ZejTeed'ZejTeed'ZejTeed'Zd(d) ZejTeed'ZejTeed'ZejTeed'Zd*d+gZi ZdWadWaeHjZd,ed-< d.ed/< d0ed1< d2ed3< d4ed5< d6ed7< ed8d9 e D Zd:e Ze Z e Ze" Ze  ZZe  eedkrd;ned< d=jZedkr.eee	  ZZ	Z
ejreCd>e  e d e	sHe
  eDd? e dΡ e jdd@ e jdAe jd  ejpdBd ZZdCekredDedCk redCdΡ\ZZedEZee	d eeeejejejejdF d!S (T  a  
Bottle is a fast and simple micro-framework for small web applications. It
offers request dispatching (Routes) with url parameter support, templates,
a built-in HTTP Server and adapters for many third party WSGI/HTTP-server and
template engines - all in a single file and with no dependencies other than the
Python Standard Library.

Homepage and documentation: http://bottlepy.org/

Copyright (c) 2016, Marcel Hellkamp.
License: MIT (see LICENSE for details)
    )with_statementzMarcel Hellkampz0.12.19MIT__main__)OptionParserz)usage: %prog [options] package.module:app)usagez	--version
store_truezshow version number.)actionhelpz-bz--bindZADDRESSzbind socket to ADDRESS.)metavarr	   z-sz--serverwsgirefzuse SERVER as backend.)defaultr	   z-pz--pluginappendzinstall additional plugin/s.z--debugzstart server in debug mode.z--reloadzauto-reload on file changes.geventN)datedatetime	timedelta)TemporaryFile)
format_exc	print_exc)
getargspec)	normalize)dumpsloadsc                 C   s   t dd S )Nz/JSON support requires Python 2.6 or simplejson.)ImportErrordata r   7/tmp/pip-unpacked-wheel-8ad_c8mj/pympler/util/bottle.py
json_dumps6   s    r   )   r   r   )      r   )r      r   )r   r    r   c                   C   s   t  d S )Nr"   )sysexc_infor   r   r   r   _eE       r%   c                 C   s   t j| S N)r#   stdoutwritexr   r   r   <lambda>L   r&   r,   c                 C   s   t j| S r'   )r#   stderrr)   r*   r   r   r   r,   M   r&   )urljoinSplitResult)	urlencodequoteunquotelatin1)encoding)SimpleCookie)r   r   r   )MutableMapping)
ModuleType)
new_module)BytesIO)ConfigParserc                 C   s   t t| S r'   )json_ldstouni)sr   r   r   r,   b   r&   c                 C   s
   t | dS )N__call__)hasattrr*   r   r   r   r,   c   r&   c                  G   s   | d | d  | d d S )Nr   r"   r    )with_traceback)ar   r   r   _raisee   r&   rB   )imap)StringIO)SafeConfigParserz?Python 2.5 support may be dropped in future versions of Bottle.)	DictMixinc                 C   s   |   S r'   )next)itr   r   r   rG   u   r&   rG   z&def _raise(*a): raise a[0], a[1], a[2]z<py3fix>executf8c                 C   s   t | tr| |S t| S r'   )
isinstanceunicodeencodebytes)r=   encr   r   r   tob~   s    rP   strictc                 C   s   t | tr| ||S t| S r'   )rK   rN   decoderL   )r=   rO   errr   r   r   r<      s    r<   )TextIOWrapperc                   @   s   e Zd Zdd ZdS )NCTextIOWrapperc                 C   s   d S r'   r   selfr   r   r   close   r&   zNCTextIOWrapper.closeN)__name__
__module____qualname__rX   r   r   r   r   rU      s   rU   c                 O   s2   zt j| |f|| W n tk
r,   Y nX d S r'   )	functoolsupdate_wrapperAttributeError)wrapperwrappedrA   kar   r   r   r]      s      r]   Fc                 C   s   t j| tdd d S )Nr   )
stacklevel)warningswarnDeprecationWarning)messagehardr   r   r   depr   s    rh   c                 C   s,   t | ttttfrt| S | r$| gS g S d S r'   )rK   tuplelistsetdictr   r   r   r   makelist   s
      rm   c                   @   s:   e Zd ZdZdddZdd Zdd	 Zd
d Zdd ZdS )DictPropertyz= Property that maps to a key in a local dict-like attribute. NFc                 C   s   |||  | _ | _| _d S r'   )attrkey	read_only)rW   ro   rp   rq   r   r   r   __init__   s    zDictProperty.__init__c                 C   s*   t j| |g d || jp|j | _| _| S N)updated)r\   r]   rp   rY   getterrW   funcr   r   r   r>      s    zDictProperty.__call__c                 C   s>   |d kr| S | j t|| j }}||kr6| |||< || S r'   )rp   getattrro   ru   )rW   objclsrp   Zstorager   r   r   __get__   s      zDictProperty.__get__c                 C   s$   | j rtd|t|| j| j< d S NzRead-Only property.rq   r^   rx   ro   rp   rW   ry   valuer   r   r   __set__   s     zDictProperty.__set__c                 C   s"   | j rtdt|| j| j= d S r|   r}   rW   ry   r   r   r   
__delete__   s     zDictProperty.__delete__)NF)	rY   rZ   r[   __doc__rr   r>   r{   r   r   r   r   r   r   rn      s   
rn   c                   @   s    e Zd ZdZdd Zdd ZdS )cached_propertyz A property that is only computed once per instance and then replaces
        itself with an ordinary attribute. Deleting the attribute resets the
        property. c                 C   s   t |d| _|| _d S )Nr   )rx   r   rw   rv   r   r   r   rr      s    zcached_property.__init__c                 C   s(   |d kr| S |  | }|j| j j< |S r'   )rw   __dict__rY   rW   ry   rz   r   r   r   r   r{      s     zcached_property.__get__NrY   rZ   r[   r   rr   r{   r   r   r   r   r      s   r   c                   @   s    e Zd ZdZdd Zdd ZdS )lazy_attributez4 A property that caches itself to the class object. c                 C   s   t j| |g d || _d S rs   )r\   r]   ru   rv   r   r   r   rr      s    zlazy_attribute.__init__c                 C   s   |  |}t|| j| |S r'   )ru   setattrrY   r   r   r   r   r{      s    
zlazy_attribute.__get__Nr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdS )BottleExceptionz- A base class for exceptions used by bottle. NrY   rZ   r[   r   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdS )
RouteErrorz9 This is a base class for all routing related exceptions Nr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdS )
RouteResetzf If raised by a plugin or request handler, the route is reset and all
        plugins are re-applied. Nr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdS )RouterUnknownModeErrorNrY   rZ   r[   r   r   r   r   r      s    r   c                   @   s   e Zd ZdZdS )RouteSyntaxErrorz@ The route parser found something not supported by this router. Nr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdS )RouteBuildErrorz The route could not be built. Nr   r   r   r   r   r      s   r   c                 C   s   d| kr| S t ddd | S )z^ Turn all capturing groups in a regular expression pattern into
        non-capturing groups. (z(\\*)(\(\?P<[^>]+>|\((?!\?))c                 S   s*   t | dd r| dS | dd S )Nr"   r    r   z(?:)lengroup)mr   r   r   r,     r&   z_re_flatten.<locals>.<lambda>)resub)pr   r   r   _re_flatten   s      r   c                   @   sb   e Zd ZdZdZdZdZdddZdd	 Ze	
d
Zdd ZdddZdd Zdd Zdd ZdS )RouteraA   A Router is an ordered collection of route->target pairs. It is used to
        efficiently match WSGI requests against a number of routes and return
        the first target that satisfies the request. The target may be anything,
        usually a string, ID or callable object. A route consists of a path-rule
        and a HTTP method.

        The path-rule is either a static path (e.g. `/contact`) or a dynamic
        path that contains wildcards (e.g. `/wiki/<page>`). The wildcard syntax
        and details on the matching order are described in docs:`routing`.
    z[^/]+r   c   Fc                    sR   g  _ i  _i  _i  _i  _i  _| _ fdddd dd dd d _d S )Nc                    s   t | p
 jd d fS r'   )r   default_patternconfrV   r   r   r,   "  s    z!Router.__init__.<locals>.<lambda>c                 S   s   dt dd fS )Nz-?\d+c                 S   s   t t| S r'   )strintr*   r   r   r   r,   $  r&   3Router.__init__.<locals>.<lambda>.<locals>.<lambda>)r   r   r   r   r   r,   $  r&   c                 S   s   dt dd fS )Nz-?[\d.]+c                 S   s   t t| S r'   )r   floatr*   r   r   r   r,   %  r&   r   )r   r   r   r   r   r,   %  r&   c                 S   s   dS )N)z.+?NNr   r   r   r   r   r,   &  r&   )r   r   r   path)rules_groupsbuilderstaticdyna_routesdyna_regexesstrict_orderfilters)rW   rQ   r   rV   r   rr     s    
zRouter.__init__c                 C   s   || j |< dS )z Add a filter. The provided function is called with the configuration
        string as parameter and must return a (regexp, to_python, to_url) tuple.
        The first element is a string, the last two are callables or None. N)r   rW   namerw   r   r   r   
add_filter(  s    zRouter.add_filterz(\\*)(?:(?::([a-zA-Z_][a-zA-Z_0-9]*)?()(?:#(.*?)#)?)|(?:<([a-zA-Z_][a-zA-Z_0-9]*)?(?::([a-zA-Z_]*)(?::((?:\\.|[^\\>]+)+)?)?)?>))c           	      c   s   d\}}| j |D ]}||||  7 }| }t|d d rl||dt|d d  7 }| }q|r||d d fV  |d d kr|dd n
|dd \}}}||pd|pd fV  | d }}q|t|ks|r|||d   d d fV  d S )	N)r    r   r          r"   r   r   )rule_syntaxfinditerstartgroupsr   r   end)	rW   ruleoffsetprefixmatchgr   Zfiltrr   r   r   r   _itertokens3  s    *zRouter._itertokensNc                    s8  d}g }d}g  g }d}	|  |D ]\}
}}|rd}	|dkrB| j}| j| |\}}}|
sx|d| 7 }d| }
|d7 }n|d	|
|f 7 }||
 |r |
|f ||
|ptf q"|
r"|t|
7 }|d
|
f q"|| j|< |r|| j|< |	r*| js*| j	
|i  |d
f| j	| | |< d
S ztd| }|jW n* tjk
rl   td|t f Y nX  r fdd}n|jrfdd}nd
}t|}||||f}||f| jkrtrd}t|||f t || j| | j||f < n0| j
|g | t| j| d | j||f< | | d
S )z< Add a new rule or replace the target for an existing rule. r   r   TFr   z(?:%s)anon%dr"   z
(?P<%s>%s)Nz^(%s)$zCould not add Route: %s (%s)c              	      sR   |   } D ]<\}}z||| ||< W q tk
rJ   tddY qX q|S )N  zPath has wrong format.)	groupdict
ValueError	HTTPError)r   url_argsr   Zwildcard_filterr   re_matchr   r   getargsn  s    zRouter.add.<locals>.getargsc                    s    |   S r'   )r   r   )r   r   r   r   w  s    z3Route <%s %s> overwrites a previously defined route)r   default_filterr   r   r   r   escaper   r   r   
setdefaultbuildcompiler   errorr   r%   
groupindexr   r   DEBUGrc   rd   RuntimeWarningr   r   _compile)rW   r   methodtargetr   anonskeyspatternr   Z	is_staticrp   moder   maskZ	in_filterZ
out_filterZ
re_patternr   flatpatZ
whole_rulemsgr   r   r   addD  sf     

 
 

z
Router.addc           	      C   s   | j | }g  }| j|< | j}tdt||D ]^}||||  }dd |D }ddd |D }t|j}dd |D }|	||f q.d S )Nr   c                 s   s   | ]\}}}}|V  qd S r'   r   ).0_r   r   r   r   	<genexpr>  s    
 z"Router._compile.<locals>.<genexpr>|c                 s   s   | ]}d | V  qdS )z(^%s$)Nr   )r   r   r   r   r   r     s     c                 S   s   g | ]\}}}}||fqS r   r   )r   r   r   r   r   r   r   
<listcomp>  s    
 z#Router._compile.<locals>.<listcomp>)
r   r   _MAX_GROUPS_PER_PATTERNranger   joinr   r   r   r   )	rW   r   Z	all_rulesZ
comborulesZ	maxgroupsr+   Zsomecombinedr   r   r   r   r     s    
zRouter._compilec                    s   | j |}|std|zPt|D ]\}}| d| < q$d fdd|D } sZ|n|d t  W S  tk
r   tdt jd  Y nX d	S )
z2 Build an URL by filling the wildcards in a rule. zNo route with that name.r   r   c                    s&   g | ]\}}|r|  |n|qS r   )pop)r   nfqueryr   r   r     s     z Router.build.<locals>.<listcomp>?zMissing URL argument: %rr   N)	r   getr   	enumerater   r0   KeyErrorr%   args)rW   _namer   r   r   ir   urlr   r   r   r     s     
 zRouter.buildc                 C   s  |d   }|d pd}d}|dkr2d|ddg}n
d|dg}|D ]}|| jkr|| j| kr| j| | \}}||r|||ni f  S || jkr@| j| D ]B\}}	||}
|
r|	|
jd	  \}}||r||ni f    S qq@tg }t|}t| j| D ] }|| j| k r||  qt| j| | D ]4}| j| D ]"\}}	||}
|
r@|| q@q2|rd
t|}tdd|dtddt	| dS )zD Return a (target, url_agrs) tuple or raise HTTPError(400/404/405). REQUEST_METHOD	PATH_INFO/NHEADPROXYGETANYr"   ,i  zMethod not allowed.)Allow  zNot found: )
upperr   r   	lastindexrk   r   r   sortedr   repr)rW   environverbr   r   methodsr   r   r   r   r   allowedZnocheckZallow_headerr   r   r   r     s<    

 zRouter.match)F)N)rY   rZ   r[   r   r   r   r   rr   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   


Fr   c                   @   s|   e Zd ZdZdddZdd Zedd Zd	d
 Zdd Z	e
dd Zdd Zdd Zdd Zdd ZdddZdd ZdS )Routez This class wraps a route callback along with route specific metadata and
        configuration and applies Plugins on demand. It is also responsible for
        turing an URL path rule into a regular expression usable by the Router.
    Nc           	      K   sL   || _ || _|| _|| _|pd | _|p(g | _|p2g | _t j|dd| _	d S )NT)make_namespaces)
appr   r   callbackr   pluginsskiplist
ConfigDict	load_dictconfig)	rW   r  r   r   r  r   r  r  r  r   r   r   rr     s    


zRoute.__init__c                 O   s   t d | j||S )NzSome APIs changed to return Route() instances instead of callables. Make sure to use the Route.call method and not to call Route instances directly.)rh   callrW   rA   ra   r   r   r   r>     s    zRoute.__call__c                 C   s   |   S )z The route callback with all plugins applied. This property is
            created on demand and then cached to speed up subsequent requests.)_make_callbackrV   r   r   r   r    s    z
Route.callc                 C   s   | j dd dS )zk Forget any cached values. The next time :attr:`call` is accessed,
            all plugins are re-applied. r  N)r   r   rV   r   r   r   reset  s    zRoute.resetc                 C   s
   | j  dS )z: Do all on-demand work immediately (useful for debugging).N)r  rV   r   r   r   prepare  s    zRoute.preparec              
   C   s0   t d t| j| j| j| j| j| j| j| j	dS )Nz=Switch to Plugin API v2 and access the Route object directly.)r   r   r  r   r  r  applyskip)
rh   rl   r   r   r  r   r  r  r  r  rV   r   r   r   _context  s       zRoute._contextc                 c   s   t  }t| jj| j D ]d}d| jkr* q~t|dd}|rN|| jks||krNq|| jkst|| jkrhq|rv|| |V  qdS )z) Yield all Plugins affecting this route. Tr   FN)rk   reversedr  r  r  rx   typer   )rW   uniquer   r   r   r   r   all_plugins  s    
    
zRoute.all_pluginsc              	   C   s   | j }|  D ]~}zBt|drJt|dd}|dkr6| n| j}|||}n||}W n  tk
rt   |   Y   S X || j k	rt|| j  q|S )Nr  apir"   )	r  r  r?   rx   r  r  r   r  r]   )rW   r  pluginr  contextr   r   r   r    s    

zRoute._make_callbackc                 C   sP   | j }t|trdnd|}tr"dnd}t||rLt||rLt||d j}q&|S )zq Return the callback. If the callback is a decorated function, try to
            recover the original function. __func__Zim_func__closure__Zfunc_closurer   )r  rx   py3kr?   cell_contents)rW   rw   Zclosure_attrr   r   r   get_undecorated_callback  s    zRoute.get_undecorated_callbackc                 C   s   t |  d S )z Return a list of argument names the callback (most likely) accepts
            as keyword arguments. If the callback is a decorated function, try
            to recover the original function before inspection. r   )r   r  rV   r   r   r   get_callback_args)  s    zRoute.get_callback_argsc                 C   s,   | j | jjfD ]}||kr||   S q|S )zp Lookup a config field and return its value, first checking the
            route.config, then route.app.config.)r  r  Zconifg)rW   rp   r   r   r   r   r   
get_config/  s     zRoute.get_configc                 C   s   |   }d| j| j|f S )Nz
<%s %r %r>)r  r   r   )rW   cbr   r   r   __repr__6  s    zRoute.__repr__)NNN)N)rY   rZ   r[   r   rr   r>   r   r  r  r  propertyr  r  r  r  r   r!  r#  r   r   r   r   r    s$       




r  c                   @   s  e Zd ZdZdCddZeddZdZdZe	d	d
 Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdDddZdd Zd d! Zd"d# Zd$d% Zd&d' ZdEd)d*ZdFd+d,ZdGd.d/ZdHd1d2ZdId4d5ZdJd7d8Zd9d: Zd;d< Z dKd=d>Z!d?d@ Z"dAdB Z#dS )LBottlea^   Each Bottle object represents a single, distinct web application and
        consists of routes, callbacks, plugins, resources and configuration.
        Instances are callable WSGI applications.

        :param catchall: If true (default), handle all exceptions. Turn off to
                         let debugging middleware handle exceptions.
    Tc                 C   s   t  | _t| jd| j_| jddt | jddt || jd< || jd< t | _	g | _
t | _i | _g | _| jd r| t  | t  d S )Nr  autojsonvalidatecatchall)r	  r  r\   partialtrigger_hook
_on_changemeta_setboolResourceManager	resourcesroutesr   routererror_handlerr  install
JSONPluginTemplatePlugin)rW   r(  r&  r   r   r   rr   M  s    


zBottle.__init__r  r(  )before_requestafter_request	app_resetr  r7  c                 C   s   t dd | jD S )Nc                 s   s   | ]}|g fV  qd S r'   r   )r   r   r   r   r   r   l  s     z Bottle._hooks.<locals>.<genexpr>)rl   _Bottle__hook_namesrV   r   r   r   _hooksj  s    zBottle._hooksc                 C   s2   || j kr| j| d| n| j| | dS )a   Attach a callback to a hook. Three hooks are currently implemented:

            before_request
                Executed once before each request. The request context is
                available, but no routing has happened yet.
            after_request
                Executed once after each request regardless of its outcome.
            app_reset
                Called whenever :meth:`Bottle.reset` is called.
        r   N)_Bottle__hook_reversedr:  insertr   r   r   r   r   add_hookn  s    
zBottle.add_hookc                 C   s0   || j kr,|| j | kr,| j | | dS dS )z  Remove a callback from a hook. TN)r:  remover   r   r   r   remove_hook~  s    zBottle.remove_hookc                    s"    fdd| j | dd D S )z. Trigger a hook and return a list of results. c                    s   g | ]}| qS r   r   )r   hookr   kwargsr   r   r     s     z'Bottle.trigger_hook.<locals>.<listcomp>N)r:  )rW   Z_Bottle__namer   rB  r   rA  r   r*    s    zBottle.trigger_hookc                    s    fdd}|S )ze Return a decorator that attaches a callback to a hook. See
            :meth:`add_hook` for details.c                    s     |  | S r'   )r=  rw   r   rW   r   r   	decorator  s    zBottle.hook.<locals>.decoratorr   )rW   r   rE  r   rD  r   r@    s    zBottle.hookc                    s   t  trtdd dd |dD }|s4tdt| fdd}|d	d |d
d |d| d ||d< | jdd| f| |	ds| jdd| f| dS )a   Mount an application (:class:`Bottle` or plain WSGI) to a specific
            URL prefix. Example::

                root_app.mount('/admin/', admin_app)

            :param prefix: path prefix or `mount-point`. If it ends in a slash,
                that slash is mandatory.
            :param app: an instance of :class:`Bottle` or a WSGI application.

            All other parameters are passed to the underlying :meth:`route` call.
        z*Parameter order of Bottle.mount() changed.Tc                 S   s   g | ]}|r|qS r   r   r   r   r   r   r   r     s      z Bottle.mount.<locals>.<listcomp>r   zEmpty path prefix.c               	      sl   zXt  tg  d fdd	} t j| }|rF jrFt j|}|pN j _ W S t   X d S )Nc                    s@   |rzt |  W 5 d }X |  _|D ]\}} || q" jjS r'   )rB   status
add_headerbodyr   )rG  
headerlistr$   r   r   rsr   r   start_response  s     z@Bottle.mount.<locals>.mountpoint_wrapper.<locals>.start_response)N)request
path_shiftHTTPResponser   rI  	itertoolschain)rM  rI  r  Z
path_depthrK  r   mountpoint_wrapper  s    
	
 z(Bottle.mount.<locals>.mountpoint_wrapperr  r   r   Z
mountpoint)r   r   r  z/%s/<:re:.*>N)
rK   
basestringrh   splitr   r   r   router   endswith)rW   r   r  optionssegmentsrT  r   rS  r   mount  s    

 
zBottle.mountc                 C   s(   t |tr|j}|D ]}| | qdS )z Merge the routes of another :class:`Bottle` application or a list of
            :class:`Route` objects into this application. The routes keep their
            'owner', meaning that the :data:`Route.app` attribute is not
            changed. N)rK   r%  r0  	add_route)rW   r0  rW  r   r   r   merge  s    
zBottle.mergec                 C   sF   t |dr||  t|s.t |ds.td| j| |   |S )z Add a plugin to the list of plugins and prepare it for being
            applied to all routes of this application. A plugin may be a simple
            decorator or an object that implements the :class:`Plugin` API.
        setupr  z.Plugins must be callable or implement .apply())r?   r^  callable	TypeErrorr  r   r  rW   r  r   r   r   r3    s    
 
zBottle.installc                 C   s   g | }}t t| jddd D ]X\}}|dksV||ksV|t|ksVt|dd|kr"|| | j|= t|dr"|  q"|r|   |S )a)   Uninstall plugins. Pass an instance to remove a specific plugin, a type
            object to remove all plugins that match that type, a string to remove
            all plugins with a matching ``name`` attribute or ``True`` to remove all
            plugins. Return the list of removed plugins. NTr   rX   )	rj   r   r  r  rx   r   r?   rX   r  )rW   r  removedr>  r   r   r   r   	uninstall  s    
 

 
 zBottle.uninstallNc                 C   sd   |dkr| j }nt|tr"|g}n| j | g}|D ]}|  q2trV|D ]}|  qH| d dS )z Reset all routes (force plugins to be re-applied) and clear all
            caches. If an ID or route object is given, only that specific route
            is affected. Nr8  )r0  rK   r  r  r   r  r*  )rW   rW  r0  r   r   r   r    s     
  
 
zBottle.resetc                 C   s(   | j D ]}t|dr|  qd| _dS )z2 Close the application and all installed plugins. rX   TN)r  r?   rX   stoppedra  r   r   r   rX     s    

 
zBottle.closec                 K   s   t | f| dS )z- Calls :func:`run` with the same parameters. Nrun)rW   rB  r   r   r   rg    s    z
Bottle.runc                 C   s   | j |S )z Search for a matching route and return a (:class:`Route` , urlargs)
            tuple. The second value is a dictionary with parameters extracted
            from the URL. Raise :exc:`HTTPError` (404/405) on a non-match.)r1  r   rW   r   r   r   r   r     s    zBottle.matchc                 K   s>   t jdddd }| jj|f|d}ttd||S )z, Return a string that matches a named route SCRIPT_NAMEr   r   )rN  r   r   stripr1  r   lstripr.   )rW   Z	routenamekargsZ
scriptnamelocationr   r   r   get_url  s    zBottle.get_urlc                 C   s6   | j | | jj|j|j||jd tr2|  dS )zS Add a route object, but do not change the :data:`Route.app`
            attribute.r   N)	r0  r   r1  r   r   r   r   r   r  rW   rW  r   r   r   r\    s     zBottle.add_router   c           	         sJ   t rd }t|t| fdd}|rF||S |S )a   A decorator to bind a function to a request URL. Example::

                @app.route('/hello/:name')
                def hello(name):
                    return 'Hello %s' % name

            The ``:name`` part is a wildcard. See :class:`Router` for syntax
            details.

            :param path: Request path or a list of paths to listen to. If no
              path is specified, it is automatically generated from the
              signature of the function.
            :param method: HTTP method (`GET`, `POST`, `PUT`, ...) or a list of
              methods to listen to. (default: `GET`)
            :param callback: An optional shortcut to avoid the decorator
              syntax. ``route(..., callback=func)`` equals ``route(...)(func)``
            :param name: The name for this route. (default: None)
            :param apply: A decorator or plugin or a list of plugins. These are
              applied to the route callback in addition to installed plugins.
            :param skip: A list of plugins, plugin classes or names. Matching
              plugins are not installed to this route. ``True`` skips all.

            Any additional keyword arguments are stored as route-specific
            configuration and passed to plugins (see :meth:`Plugin.apply`).
        Nc                    sj   t | trt| } tp t| D ]B}tD ]4}| }t||| fd }| q.q"| S )N)r   r  r  )rK   rU  loadrm   yieldroutesr   r  r\  )r  r   r   rW  r  r   r   r   r  rW   r  r   r   rE  ,  s    
  zBottle.route.<locals>.decorator)r_  rm   )	rW   r   r   r  r   r  r  r  rE  r   rs  r   rW    s     

zBottle.routec                 K   s   | j ||f|S )z Equals :meth:`route`. rW  rW   r   r   rY  r   r   r   r   8  s    z
Bottle.getPOSTc                 K   s   | j ||f|S )z8 Equals :meth:`route` with a ``POST`` method parameter. rt  ru  r   r   r   post<  s    zBottle.postPUTc                 K   s   | j ||f|S )z7 Equals :meth:`route` with a ``PUT`` method parameter. rt  ru  r   r   r   put@  s    z
Bottle.putDELETEc                 K   s   | j ||f|S )z: Equals :meth:`route` with a ``DELETE`` method parameter. rt  ru  r   r   r   deleteD  s    zBottle.delete  c                    s    fdd}|S )z< Decorator: Register an output handler for a HTTP error codec                    s   | j t < | S r'   )r2  r   handlercoderW   r   r   r_   J  s    zBottle.error.<locals>.wrapperr   )rW   r  r_   r   r  r   r   H  s    zBottle.errorc                 C   s   t tt|dS )N)e)rP   templateERROR_PAGE_TEMPLATE)rW   resr   r   r   default_error_handlerO  s    zBottle.default_error_handlerc              
   C   s`  |d  }|d< t rLz|dd|d< W n tk
rJ   tdd Y S X zp| |d< t| t  zD| d	 | j	
|\}}||d
< ||d< ||d< |jf |W W S | d X W n tk
r   t  Y S  tk
 r   |  | | Y S  tttfk
r    Y nD tk
rZ   | js2 t }|d | tddt | Y S X d S )Nr   zbottle.raw_pathr3   rJ   r   z#Invalid path string. Expected UTF-8
bottle.appr7  r6  zroute.handlebottle.routeroute.url_argswsgi.errorsr|  zInternal Server Error)r  rM   rR   UnicodeErrorr   rN  bindresponser*  r1  r   r  rP  r%   r   r  _handleKeyboardInterrupt
SystemExitMemoryError	Exceptionr(  r   r)   )rW   r   r   rW  r   Z
stacktracer   r   r   r  R  s>    


 zBottle._handlec              
   C   sd  |sdt krdt d< g S t|ttfrNt|d ttfrN|d dd |}t|trd|t j}t|trdt krt	|t d< |gS t|t
r|t  | j|j| j|}| |S t|tr|t  | |jS t|dr"dtjkrtjd |S t|dst|ds"t|S z&t|}t|}|sFt|}q4W n tk
rh   | d Y S  tk
r   t }Y nN tttfk
r    Y n2 tk
r   | js t
dd	t t  }Y nX t|tr| |S t|trt!"|g|}nFt|tr,d
d }t#|t!"|g|}ndt$| }| t
d|S t|dr`t%||j&}|S )z Try to convert the parameter into something WSGI compatible and set
        correct HTTP headers when possible.
        Support: False, str, unicode, dict, HTTPResponse, HTTPError, file-like,
        iterable of strings and iterable of unicodes
        Content-Lengthr   readzwsgi.file_wrapperrX   __iter__r   r|  zUnhandled exceptionc                 S   s   |  tjS r'   )rM   r  charsetr*   r   r   r   r,     r&   zBottle._cast.<locals>.<lambda>zUnsupported response type: %s)'r  rK   ri   rj   rN   rL   r   rM   r  r   r   r  r2  r   status_coder  _castrP  rI  r?   rN  r   WSGIFileWrapperiterrG   StopIterationr%   r  r  r  r  r(  r   rQ  rR  rC   r  
_closeiterrX   )rW   outpeekZioutfirstZnew_iterencoderr   r   r   r   r  u  sj    







 
zBottle._castc              
   C   s   zP|  | |}tjdks(|d dkr>t|dr:|  g }|tjtj |W S  tt	t
fk
rl    Y n tk
r   | js dt|dd }tr|dttt tt f 7 }|d	 | d
g}|d|t  t|g Y S X dS )z The bottle WSGI-interface. )d   e      0  r   r   rX   z4<h1>Critical error while processing request: %s</h1>r   r   zD<h2>Error:</h2>
<pre>
%s
</pre>
<h2>Traceback:</h2>
<pre>
%s
</pre>
r  )Content-Typetext/html; charset=UTF-8z500 INTERNAL SERVER ERRORN)r  r  r  _status_coder?   rX   _status_linerJ  r  r  r  r  r(  html_escaper   r   r   r%   r   r)   r#   r$   rP   )rW   r   rM  r  rS   headersr   r   r   wsgi  s4    


  zBottle.wsgic                 C   s   |  ||S )z9 Each instance of :class:'Bottle' is a WSGI application. )r  )rW   r   rM  r   r   r   r>     s    zBottle.__call__)TT)N)Nr   NNNN)Nr   )Nrv  )Nrx  )Nrz  )r|  )N)$rY   rZ   r[   r   rr   rn   r(  r9  r;  r   r:  r=  r?  r*  r@  r[  r]  r3  rd  r  rX   rg  r   rn  r\  rW  r   rw  ry  r{  r   r  r  r  r  r>   r   r   r   r   r%  D  sF   


0

    
*




#
Hr%  c                   @   s  e Zd ZdZdZdZdqddZedddd	d
d Zedddd	dd Z	edddd	dd Z
edd Zedd Zedddd	dd ZdrddZedddd	dd ZdsddZedd dd	d!d" Zedd#dd	d$d% Zedd&dd	d'd( Zedd)dd	d*d+ Zedd,dd	d-d. Zd/d0 Zd1d2 Zedd3dd	d4d5 Zd6d7 Zed8d9 Zed:d; ZeZedd<dd	d=d> Zed?d@ ZeddAdd	dBdC Z edDdE Z!edFdG Z"edHdI Z#dtdKdLZ$edMdN Z%edOdP Z&edQdR Z'edSdT Z(edUdV Z)edWdX Z*edYdZ Z+d[d\ Z,dud]d^Z-d_d` Z.dadb Z/dcdd Z0dedf Z1dgdh Z2didj Z3dkdl Z4dmdn Z5dodp Z6dS )vBaseRequestad   A wrapper for WSGI environment dictionaries that adds a lot of
        convenient access methods and properties. Most of them are read-only.

        Adding new attributes to a request actually adds them to the environ
        dictionary (as 'bottle.request.ext.<name>'). This is the recommended
        way to store and access request-specific data.
    r   i  Nc                 C   s    |dkri n|| _ | | j d< dS )z! Wrap a WSGI environ dictionary. Nzbottle.requestr   rh  r   r   r   rr     s    zBaseRequest.__init__r  T)rq   c                 C   s   t ddS )z+ Bottle application handling this request. z0This request is not connected to an application.NRuntimeErrorrV   r   r   r   r    s    zBaseRequest.appr  c                 C   s   t ddS )z= The bottle :class:`Route` object that matches this request. )This request is not connected to a route.Nr  rV   r   r   r   rW    s    zBaseRequest.router  c                 C   s   t ddS )z' The arguments extracted from the URL. r  Nr  rV   r   r   r   r     s    zBaseRequest.url_argsc                 C   s   d| j ddd S )z The value of ``PATH_INFO`` with exactly one prefixed slash (to fix
            broken clients and avoid the "empty path" edge case). r   r   r   )r   r   rk  rV   r   r   r   r     s    zBaseRequest.pathc                 C   s   | j dd S )z6 The ``REQUEST_METHOD`` value as an uppercase string. r   r   )r   r   r   rV   r   r   r   r     s    zBaseRequest.methodzbottle.request.headersc                 C   s
   t | jS )zf A :class:`WSGIHeaderDict` that provides case-insensitive access to
            HTTP request headers. )WSGIHeaderDictr   rV   r   r   r   r    s    zBaseRequest.headersc                 C   s   | j ||S )zA Return the value of a request header, or a given default value. r  r   rW   r   r   r   r   r   
get_header  s    zBaseRequest.get_headerzbottle.request.cookiesc                 C   s(   t | jdd }tdd |D S )z Cookies parsed into a :class:`FormsDict`. Signed cookies are NOT
            decoded. Use :meth:`get_cookie` if you expect signed cookies. ZHTTP_COOKIEr   c                 s   s   | ]}|j |jfV  qd S r'   )rp   r   )r   cr   r   r   r   "  s     z&BaseRequest.cookies.<locals>.<genexpr>)r5   r   r   values	FormsDict)rW   cookiesr   r   r   r    s    zBaseRequest.cookiesc                 C   sB   | j |}|r:|r:t||}|r6|d |kr6|d S |S |p@|S )a   Return the content of a cookie. To read a `Signed Cookie`, the
            `secret` must match the one used to create the cookie (see
            :meth:`BaseResponse.set_cookie`). If anything goes wrong (missing
            cookie or wrong signature), return a default value. r   r"   )r  r   cookie_decode)rW   rp   r   secretr   decr   r   r   
get_cookie$  s
    
zBaseRequest.get_cookiezbottle.request.queryc                 C   s<   t   }| jd< t| jdd}|D ]\}}|||< q&|S )a    The :attr:`query_string` parsed into a :class:`FormsDict`. These
            values are sometimes called "URL arguments" or "GET parameters", but
            not to be confused with "URL wildcards" as they are provided by the
            :class:`Router`. z
bottle.getQUERY_STRINGr   )r  r   
_parse_qslr   )rW   r   pairsrp   r   r   r   r   r   /  s
    
zBaseRequest.queryzbottle.request.formsc                 C   s0   t  }| j D ]\}}t|ts|||< q|S )a   Form values parsed from an `url-encoded` or `multipart/form-data`
            encoded POST or PUT request body. The result is returned as a
            :class:`FormsDict`. All keys and values are strings. File uploads
            are stored separately in :attr:`files`. r  rv  allitemsrK   
FileUpload)rW   formsr   itemr   r   r   r  ;  s
    

zBaseRequest.formszbottle.request.paramsc                 C   sB   t  }| j D ]\}}|||< q| j D ]\}}|||< q,|S )z A :class:`FormsDict` with the combined values of :attr:`query` and
            :attr:`forms`. File uploads are stored in :attr:`files`. )r  r   r  r  )rW   paramsrp   r   r   r   r   r  G  s    

zBaseRequest.paramszbottle.request.filesc                 C   s0   t  }| j D ]\}}t|tr|||< q|S )z File uploads parsed from `multipart/form-data` encoded POST or PUT
            request body. The values are instances of :class:`FileUpload`.

        r  )rW   filesr   r  r   r   r   r  R  s
    

zBaseRequest.fileszbottle.request.jsonc                 C   s@   | j dd dd }|dkr<|  }|s4dS t|S dS )z If the ``Content-Type`` header is ``application/json``, this
            property holds the parsed content of the request body. Only requests
            smaller than :attr:`MEMFILE_MAX` are processed to avoid memory
            exhaustion. CONTENT_TYPEr   ;r   application/jsonN)r   r   lowerrV  _get_body_string
json_loads)rW   ctypebr   r   r   json^  s    zBaseRequest.jsonc                 c   s<   t d| j}|r8|t||}|s$q8|V  |t|8 }qd S Nr   )maxcontent_lengthminr   )rW   r  bufsizemaxreadpartr   r   r   
_iter_bodyl  s     zBaseRequest._iter_bodyc                 c   s   t dd}tdtdtd  }}}|d}|dd  |krh|d}||7 }|sV|t||kr.|q.||\}	}
}
ztt|	 d}W n tk
r   |Y nX |d	krq|}|d	kr
|s|t||}|d | ||d   }}|s||V  |t|8 }q|d
|kr&|q&d S )Nr   z*Error while parsing chunked transfer body.z
r  r   r"      r   r    )	r   rP   r   	partitionr   tonatrj  r   r  )rW   r  r  rS   ZrnZsembsheaderr  sizer   r  buffr  r   r   r   _iter_chunkedt  s8    
  
 
 zBaseRequest._iter_chunkedzbottle.request.bodyc                 C   s   | j r| jn| j}| jd j}t dd  }}}||| jD ]L}|| |t|7 }|s<|| jkr<t	dd| }}||
  ~d}q<|| jd< |d |S )N
wsgi.inputr   Fzw+br   T)chunkedr  r  r   r  r9   MEMFILE_MAXr)   r   r   getvalueseek)rW   Z	body_iterZ	read_funcrI  Z	body_sizeZis_temp_filer  tmpr   r   r   _body  s    


zBaseRequest._bodyc                 C   sT   | j }|| jkrtdd|dk r,| jd }| j|}t|| jkrPtdd|S )z~ read body until content-length or MEMFILE_MAX into a string. Raise
            HTTPError(413) on requests that are to large. i  zRequest to larger   r"   )r  r  r   rI  r  r   )rW   clenr   r   r   r   r    s    

 

zBaseRequest._get_body_stringc                 C   s   | j d | j S )al   The HTTP request body as a seek-able file-like object. Depending on
            :attr:`MEMFILE_MAX`, this is either a temporary file or a
            :class:`io.BytesIO` instance. Accessing this property for the first
            time reads and replaces the ``wsgi.input`` environ variable.
            Subsequent accesses just do a `seek(0)` on the file object. r   )r  r  rV   r   r   r   rI    s    zBaseRequest.bodyc                 C   s   d| j dd kS )z( True if Chunked transfer encoding was. r  ZHTTP_TRANSFER_ENCODINGr   r   r   r  rV   r   r   r   r    s    zBaseRequest.chunkedzbottle.request.postc           	      C   s   t  }| jds>tt|  d}|D ]\}}|||< q(|S ddi}dD ]}|| jkrJ| j| ||< qJt| j|dd}t	rt
|d d	d
d|d< ntrd	|d< tjf |}|| d< |jpg }|D ]4}|jrt|j|j|j|j||j< q|j||j< q|S )z The values of :attr:`forms` and :attr:`files` combined into a single
            :class:`FormsDict`. Values are either strings (form values) or
            instances of :class:`cgi.FieldStorage` (file uploads).
        z
multipart/r3   r  r   )r   r  CONTENT_LENGTHT)fpr   keep_blank_valuesr  rJ   
)r4   newliner4   z_cgi.FieldStorage)r  content_type
startswithr  r  r  r   rl   rI  py31rU   r  cgiFieldStoragerj   filenamer  filer   r  r   )	rW   rw  r  rp   r   Zsafe_envr   r   r  r   r   r   rv    s8    

 


 zBaseRequest.POSTc                 C   s
   | j  S )z The full request URI including hostname and scheme. If your app
            lives behind a reverse proxy or load balancer and you get confusing
            results, make sure that the ``X-Forwarded-Host`` header is set
            correctly. )urlpartsgeturlrV   r   r   r   r     s    zBaseRequest.urlzbottle.request.urlpartsc                 C   s   | j }|dp|dd}|dp.|d}|sn|dd}|d}|rn||dkr\d	nd
krn|d| 7 }t| j}t||||ddS )z The :attr:`url` string as an :class:`urlparse.SplitResult` tuple.
            The tuple contains (scheme, host, path, query_string and fragment),
            but the fragment is always empty because it is not visible to the
            server. ZHTTP_X_FORWARDED_PROTOzwsgi.url_schemehttpZHTTP_X_FORWARDED_HOSTZ	HTTP_HOSTZSERVER_NAME	127.0.0.1ZSERVER_PORT80443:r  r   )r   r   urlquotefullpathUrlSplitResult)rW   envr  hostportr   r   r   r   r    s    

zBaseRequest.urlpartsc                 C   s   t | j| jdS )z: Request path including :attr:`script_name` (if present). r   )r.   script_namer   rk  rV   r   r   r   r    s    zBaseRequest.fullpathc                 C   s   | j ddS )zh The raw :attr:`query` part of the URL (everything in between ``?``
            and ``#``) as a string. r  r   r   r   rV   r   r   r   query_string   s    zBaseRequest.query_stringc                 C   s(   | j ddd}|r$d| d S dS )z The initial portion of the URL's `path` that was removed by a higher
            level (server or routing middleware) before the application was
            called. This script path is returned with leading and tailing
            slashes. ri  r   r   )r   r   rj  )rW   r  r   r   r   r    s    zBaseRequest.script_namer"   c                 C   s,   | j dd}t|| j|\| d< | d< dS )z Shift path segments from :attr:`path` to :attr:`script_name` and
            vice versa.

           :param shift: The number of path segments to shift. May be negative
                         to change the shift direction. (default: 1)
        ri  r   r   N)r   r   rO  r   )rW   shiftscriptr   r   r   rO    s    zBaseRequest.path_shiftc                 C   s   t | jdpdS )z The request body length as an integer. The client is responsible to
            set this header. Otherwise, the real length of the body is unknown
            and -1 is returned. In this case, :attr:`body` will be empty. r  rb  )r   r   r   rV   r   r   r   r    s    zBaseRequest.content_lengthc                 C   s   | j dd S )zA The Content-Type header as a lowercase-string (default: empty). r  r   r  rV   r   r   r   r     s    zBaseRequest.content_typec                 C   s   | j dd}| dkS )z True if the request was triggered by a XMLHttpRequest. This only
            works with JavaScript libraries that support the `X-Requested-With`
            header (most of the popular libraries do). ZHTTP_X_REQUESTED_WITHr   Zxmlhttprequestr  )rW   Zrequested_withr   r   r   is_xhr%  s    zBaseRequest.is_xhrc                 C   s   | j S )z9 Alias for :attr:`is_xhr`. "Ajax" is not the right term. )r  rV   r   r   r   is_ajax-  s    zBaseRequest.is_ajaxc                 C   s6   t | jdd}|r|S | jd}|r2|dfS dS )a   HTTP authentication data as a (user, password) tuple. This
            implementation currently supports basic (not digest) authentication
            only. If the authentication happened at a higher level (e.g. in the
            front web-server or a middleware), the password field is None, but
            the user field is looked up from the ``REMOTE_USER`` environ
            variable. On any errors, None is returned. ZHTTP_AUTHORIZATIONr   ZREMOTE_USERN)
parse_authr   r   )rW   basicZruserr   r   r   auth2  s      zBaseRequest.authc                 C   s>   | j d}|r$dd |dD S | j d}|r:|gS g S )a(   A list of all IPs that were involved in this request, starting with
            the client IP and followed by zero or more proxies. This does only
            work if all proxies support the ```X-Forwarded-For`` header. Note
            that this information can be forged by malicious clients. ZHTTP_X_FORWARDED_FORc                 S   s   g | ]}|  qS r   )rj  )r   ipr   r   r   r   G  s     z,BaseRequest.remote_route.<locals>.<listcomp>r   ZREMOTE_ADDR)r   r   rV  )rW   proxyremoter   r   r   remote_route@  s
     zBaseRequest.remote_routec                 C   s   | j }|r|d S dS )zg The client IP as a string. Note that this information can be forged
            by malicious clients. r   N)r  rp  r   r   r   remote_addrK  s    zBaseRequest.remote_addrc                 C   s   t | j S )zD Return a new :class:`Request` with a shallow :attr:`environ` copy. )Requestr   copyrV   r   r   r   r  R  s    zBaseRequest.copyc                 C   s   | j ||S r'   r  )rW   r   r   r   r   r   r   V  r&   zBaseRequest.getc                 C   s
   | j | S r'   r  rW   rp   r   r   r   __getitem__W  r&   zBaseRequest.__getitem__c                 C   s   d| |< | j |= d S )Nr   r  r  r   r   r   __delitem__X  s    zBaseRequest.__delitem__c                 C   s
   t | jS r'   )r  r   rV   r   r   r   r  Y  r&   zBaseRequest.__iter__c                 C   s
   t | jS r'   )r   r   rV   r   r   r   __len__Z  r&   zBaseRequest.__len__c                 C   s
   | j  S r'   )r   r   rV   r   r   r   r   [  r&   zBaseRequest.keysc                 C   sl   | j drtd|| j |< d}|dkr0d}n|dkr>d}n|drLd	}|D ]}| j d
| d qPdS )zA Change an environ value and clear all caches that depend on it. zbottle.request.readonlyz$The environ dictionary is read-only.r   r  )rI  r  r  r  rw  r  r  )r   r  HTTP_)r  r  zbottle.request.N)r   r   r   r  r   )rW   rp   r   Ztodeleter   r   r   __setitem__\  s    

zBaseRequest.__setitem__c                 C   s   d| j j| j| jf S )Nz<%s: %s %s>)	__class__rY   r   r   rV   r   r   r   r#  o  s    zBaseRequest.__repr__c                 C   sN   z(| j d|  }t|dr$|| n|W S  tk
rH   td| Y nX dS )z@ Search in self.environ for additional user defined attributes. bottle.request.ext.%sr{   zAttribute %r not defined.N)r   r?   r{   r   r^   )rW   r   varr   r   r   __getattr__r  s
    zBaseRequest.__getattr__c                 C   s(   |dkrt | ||S || jd| < d S )Nr   r  )object__setattr__r   rW   r   r   r   r   r   r  z  s     zBaseRequest.__setattr__)N)N)NN)r"   )N)7rY   rZ   r[   r   	__slots__r  rr   rn   r  rW  r   r$  r   r   r  r  r  r  r   r  r  r  r  r  r  r  r  rI  r  r   rv  r   r  r  r  r  rO  r  r  r  r	  r  r  r  r  r   r  r  r  r  r   r  r#  r  r  r   r   r   r   r    s   

















	

"















r  c                 C   s4   d| ksd| ksd| kr$t d|  |  ddS )Nr   z4Header names must not contain control characters: %rr   -)r   titlereplace)rp   r   r   r   _hkey  s    r'  c                 C   s0   t | } d| ks d| ks d| kr,td|  | S )Nr  r"  r#  z4Header value must not contain control characters: %r)r  r   r   r   r   r   _hval  s    r)  c                   @   s.   e Zd ZdddZdd Zdd Zd	d
 ZdS )HeaderPropertyNr   c                 C   s.   || | _ | _|| | _| _d|  | _d S )NzCurrent value of the %r header.)r   r   readerwriterr%  r   )rW   r   r+  r,  r   r   r   r   rr     s    zHeaderProperty.__init__c                 C   s0   |d kr| S | | j| j}| jr,| |S |S r'   )r  r   r   r+  r   r   r   r   r{     s     zHeaderProperty.__get__c                 C   s   | j r|  |n||| j< d S r'   )r,  r   r~   r   r   r   r     s    zHeaderProperty.__set__c                 C   s   || j = d S r'   ro  r   r   r   r   r     s    zHeaderProperty.__delete__)NNr   )rY   rZ   r[   rr   r{   r   r   r   r   r   r   r*    s   
r*  c                   @   s4  e Zd ZdZdZdZededdZd?d	d
Zd@ddZ	dd Z
dd Zedd Zedd Zdd Zdd ZeeeddZ[[edd Zdd Zdd Zd d! Zd"d# ZdAd$d%Zd&d' Zd(d) Zd*d+ Zed,d- Zed.Zed/ed0Zed1d2d3 d4d3 d5Z edBd7d8Z!dCd9d:Z"d;d< Z#d=d> Z$dS )DBaseResponsea   Storage class for a response body as well as headers and cookies.

        This class does support dict-like case-insensitive item-access to
        headers, but is NOT a dict. Most notably, iterating over a response
        yields parts of the body and not the headers.

        :param body: The response body as one of the supported types.
        :param status: Either an HTTP status code (e.g. 200) or a status line
                       including the reason phrase (e.g. '200 OK').
        :param headers: A dictionary or a list of name-value pairs.

        Additional keyword arguments are added to the list of headers.
        Underscores in the header name are replaced with dashes.
       r  )r  )r   Content-EncodingzContent-Languager  Content-Ranger  zContent-Md5Last-Modified)r  r  r   Nc                 K   st   d | _ i | _|| _|p| j| _|rNt|tr4| }|D ]\}}| || q8|rp| D ]\}}| || qZd S r'   )	_cookies_headersrI  default_statusrG  rK   rl   itemsrH  )rW   rI  rG  r  more_headersr   r   r   r   r   rr     s    
zBaseResponse.__init__c                 C   sf   |pt }t|t st| }| j|_tdd | j D |_| jrbt |_|j	| jj
dd |S )z Returns a copy of self. c                 s   s"   | ]\}}||d d  fV  qd S r'   r   r   kvr   r   r   r     s     z$BaseResponse.copy.<locals>.<genexpr>r   )r  )r-  
issubclassAssertionErrorrG  rl   r3  r5  r2  r5   rq  output)rW   rz   r  r   r   r   r    s    zBaseResponse.copyc                 C   s
   t | jS r'   )r  rI  rV   r   r   r   r    s    zBaseResponse.__iter__c                 C   s   t | jdr| j  d S )NrX   )r?   rI  rX   rV   r   r   r   rX     s    zBaseResponse.closec                 C   s   | j S )z; The HTTP status line as a string (e.g. ``404 Not Found``).r  rV   r   r   r   status_line  s    zBaseResponse.status_linec                 C   s   | j S )z/ The HTTP status code as an integer (e.g. 404).)r  rV   r   r   r   r    s    zBaseResponse.status_codec                 C   s   t |tr|t| }}n*d|kr>| }t| d }ntdd|  krZdksdn td|| _t|pvd| | _	d S )N r   z+String status line without a reason phrase.r  i  zStatus code out of range.z
%d Unknown)
rK   r   _HTTP_STATUS_LINESr   rj  rV  r   r  r   r  )rW   rG  r  r   r   r   _set_status  s    
 zBaseResponse._set_statusc                 C   s   | j S r'   r=  rV   r   r   r   _get_status  s    zBaseResponse._get_statusaQ   A writeable property to change the HTTP response status. It accepts
            either a numeric code (100-999) or a string with a custom reason
            phrase (e.g. "404 Brain not found"). Both :data:`status_line` and
            :data:`status_code` are updated accordingly. The return value is
            always a status string. c                 C   s   t  }| j|_|S )zl An instance of :class:`HeaderDict`, a case-insensitive dict-like
            view on the response headers. )
HeaderDictr3  rl   )rW   Zhdictr   r   r   r    s    zBaseResponse.headersc                 C   s   t || jkS r'   )r'  r3  rW   r   r   r   r   __contains__  r&   zBaseResponse.__contains__c                 C   s   | j t|= d S r'   r3  r'  rD  r   r   r   r    r&   zBaseResponse.__delitem__c                 C   s   | j t| d S Nrb  rF  rD  r   r   r   r    r&   zBaseResponse.__getitem__c                 C   s   t |g| jt|< d S r'   r)  r3  r'  r   r   r   r   r    r&   zBaseResponse.__setitem__c                 C   s   | j t||gd S )z| Return the value of a previously defined header. If there is no
            header with that name, return a default value. rb  )r3  r   r'  r  r   r   r   r  	  s    zBaseResponse.get_headerc                 C   s   t |g| jt|< dS )zh Create a new response header, replacing any previously defined
            headers with the same name. NrH  r   r   r   r   
set_header  s    zBaseResponse.set_headerc                 C   s    | j t|g t| dS )z= Add an additional response header, not removing duplicates. N)r3  r   r'  r   r)  r   r   r   r   rH    s    zBaseResponse.add_headerc                 C   s   | j S )zx Yield (header, value) tuples, skipping headers that are not
            allowed with the current response status code. )rJ  rV   r   r   r   iter_headers  s    zBaseResponse.iter_headersc                    s   g }t | j }d| jkr.|d| jgf | j| jkrX| j| j   fdd|D }|dd |D 7 }| jr| j D ]}|dt	|
 f qztrdd |D }|S )z. WSGI conform list of (header, value) tuples. r  c                    s   g | ]}|d   kr|qS r   r   )r   hbad_headersr   r   r   %  s      z+BaseResponse.headerlist.<locals>.<listcomp>c                 S   s"   g | ]\}}|D ]}||fqqS r   r   )r   r   valsvalr   r   r   r   &  s       z
Set-Cookiec                 S   s$   g | ]\}}|| d dfqS )rJ   r3   )rM   rR   r7  r   r   r   r   +  s     )rj   r3  r5  r   default_content_typer  rN  r2  r  r)  OutputStringr  )rW   r  r  r  r   rM  r   rJ    s    
zBaseResponse.headerlistr  r  )r+  ZExpiresc                 C   s   t t| S r'   )r   utcfromtimestamp
parse_dater*   r   r   r   r,   1  r&   zBaseResponse.<lambda>c                 C   s   t | S r'   )	http_dater*   r   r   r   r,   2  r&   )r+  r,  UTF-8c                 C   s,   d| j kr(| j dd dd  S |S )zJ Return the charset specified in the content-type header (default: utf8). zcharset=rb  r  r   )r  rV  rj  )rW   r   r   r   r   r  4  s    
zBaseResponse.charsetc                 K   s   | j st | _ |r&tt||f|}nt|ts8tdt|dkrLtd|| j |< |	 D ]\}}|dkrt|t
r|j|jd d  }|dkrt|ttfr| }nt|ttfrt|}td|}|| j | |d	d
< q^dS )a   Create a new cookie or replace an old one. If the `secret` parameter is
            set, create a `Signed Cookie` (described below).

            :param name: the name of the cookie.
            :param value: the value of the cookie.
            :param secret: a signature key required for signed cookies.

            Additionally, this method accepts all RFC 2109 attributes that are
            supported by :class:`cookie.Morsel`, including:

            :param max_age: maximum age in seconds. (default: None)
            :param expires: a datetime object or UNIX timestamp. (default: None)
            :param domain: the domain that is allowed to read the cookie.
              (default: current domain)
            :param path: limits the cookie to a given path (default: current path)
            :param secure: limit the cookie to HTTPS connections (default: off).
            :param httponly: prevents client-side javascript to read this cookie
              (default: off, requires Python 2.6 or newer).

            If neither `expires` nor `max_age` is set (default), the cookie will
            expire at the end of the browser session (as soon as the browser
            window is closed).

            Signed cookies may store any pickle-able object and are
            cryptographically signed to prevent manipulation. Keep in mind that
            cookies are limited to 4kb in most browsers.

            Warning: Signed cookies are not encrypted (the client can still see
            the content) and not copy-protected (the client can restore an old
            cookie). The main intention is to make pickling and unpickling
            save, not to store secret information at client side.
        z)Secret key missing for non-string Cookie.i   zCookie value to long.max_age   i  expires%a, %d %b %Y %H:%M:%S GMTr   r$  N)r2  r5   r<   cookie_encoderK   rU  r`  r   r   r5  r   secondsdaysdatedater   	timetupler   r   timegmtimestrftimer&  )rW   r   r   r  rY  rp   r   r   r   
set_cookie;  s(    !
 



zBaseResponse.set_cookiec                 K   s$   d|d< d|d< | j |df| dS )zq Delete a cookie. Be sure to use the same `domain` and `path`
            settings as used to create the cookie. rb  rW  r   rY  r   N)rc  )rW   rp   rB  r   r   r   delete_cookies  s    zBaseResponse.delete_cookiec                 C   s0   d}| j D ] \}}|d| | f 7 }q
|S )Nr   z%s: %s
)rJ  r%  rj  )rW   r  r   r   r   r   r   r#  z  s    zBaseResponse.__repr__)r   NN)N)N)rV  )N)%rY   rZ   r[   r   r4  rQ  rk   rN  rr   r  r  rX   r$  r>  r  rA  rB  rG  r  rE  r  r  r  r  rI  rH  rJ  rJ  r*  r  r   r  rY  r  rc  rd  r#  r   r   r   r   r-    sV   







8r-  c                    sF   | rt d t   fdd} fdd} fdd}t|||dS )	Nz3local_property() is deprecated and will be removed.c                    s*   z j W S  tk
r$   tdY nX d S )Nz Request context not initialized.)r  r^   r  rV   Zlsr   r   fget  s     zlocal_property.<locals>.fgetc                    s
   | _ d S r'   r  rW   r   re  r   r   fset  r&   zlocal_property.<locals>.fsetc                    s    ` d S r'   rg  rV   re  r   r   fdel  r&   zlocal_property.<locals>.fdelzThread-local property)rh   	threadinglocalr$  )r   rf  ri  rj  r   re  r   local_property  s     rm  c                   @   s   e Zd ZdZejZe ZdS )LocalRequestaT   A thread-local subclass of :class:`BaseRequest` with a different
        set of attributes for each thread. There is usually only one global
        instance of this class (:data:`request`). If accessed during a
        request/response cycle, this instance always refers to the *current*
        request (even on a multithreaded server). N)	rY   rZ   r[   r   r  rr   r  rm  r   r   r   r   r   rn    s   rn  c                   @   s4   e Zd ZdZejZe Ze Z	e Z
e Ze ZdS )LocalResponsea+   A thread-local subclass of :class:`BaseResponse` with a different
        set of attributes for each thread. There is usually only one global
        instance of this class (:data:`response`). Its attributes are used
        to build the HTTP response at the end of the request/response cycle.
    N)rY   rZ   r[   r   r-  rr   r  rm  r  r  r2  r3  rI  r   r   r   r   ro    s   ro  c                       s&   e Zd Zd fdd	Zdd Z  ZS )rP  r   Nc                    s   t t| j|||f| d S r'   )superrP  rr   )rW   rI  rG  r  r6  r  r   r   rr     s    zHTTPResponse.__init__c                 C   s,   | j |_ | j|_| j|_| j|_| j|_d S r'   )r  r  r3  r2  rI  )rW   r  r   r   r   r    s
    zHTTPResponse.apply)r   NN)rY   rZ   r[   rr   r  __classcell__r   r   rq  r   rP    s   rP  c                       s"   e Zd ZdZd fdd	Z  ZS )r   r|  Nc                    s&   || _ || _tt| j||f| d S r'   )	exception	tracebackrp  r   rr   )rW   rG  rI  rs  rt  rY  rq  r   r   rr     s    zHTTPError.__init__)NNNN)rY   rZ   r[   r4  rr   rr  r   r   rq  r   r     s   r   c                   @   s   e Zd ZdS )PluginErrorNr   r   r   r   r   ru    s    ru  c                   @   s(   e Zd ZdZdZefddZdd ZdS )r4  r  r    c                 C   s
   || _ d S r'   r   )rW   r   r   r   r   rr     s    zJSONPlugin.__init__c                    s    | j s S  fdd}|S )Nc                     sr   z | |}W n t k
r(   t }Y nX t|trF|}dt_|S t|trnt|jtrn|j|_d|_|S )Nr  )r   r%   rK   rl   r  r  rP  rI  )rA   ra   rvZjson_responser  r   r   r   r_     s    
z!JSONPlugin.apply.<locals>.wrapperrv  )rW   r  rW  r_   r   rx  r   r    s
     zJSONPlugin.applyN)rY   rZ   r[   r   r  r   rr   r  r   r   r   r   r4    s   r4  c                   @   s    e Zd ZdZdZdZdd ZdS )r5  a   This plugin applies the :func:`view` decorator to all routes with a
        `template` config parameter. If the parameter is a tuple, the second
        element must be a dict with additional options (e.g. `template_engine`)
        or default variables for the template. r  r    c                 C   s\   |j d}t|ttfr>t|dkr>t|d f|d |S t|trTt||S |S d S )Nr  r    r   r"   )r  r   rK   ri   rj   r   viewr   )rW   r  rW  r   r   r   r   r    s    
zTemplatePlugin.applyN)rY   rZ   r[   r   r   r  r  r   r   r   r   r5    s   r5  c                   @   s&   e Zd Zdd ZdddZdd ZdS )	_ImportRedirectc                 C   sH   || _ || _tj|t|| _| jjt	g g | d tj
|  dS )z@ Create a virtual package that redirects imports (see PEP 302). )__file____path____all__
__loader__N)r   impmaskr#   modulesr   r8   moduler   updater{  	meta_pathr   )rW   r   r  r   r   r   rr     s     z_ImportRedirect.__init__Nc                 C   s.   d|krd S | ddd }|| jkr*d S | S )N.r"   r   )rsplitr   )rW   fullnamer   Zpacknamer   r   r   find_module  s     
 z_ImportRedirect.find_modulec                 C   sb   |t jkrt j| S |ddd }| j| }t| t j|  }t j|< t| j|| | |_|S )Nr  r"   )r#   r  r  r  
__import__r   r  r~  )rW   r  modnameZrealnamer  r   r   r   load_module	  s    
 

z_ImportRedirect.load_module)N)rY   rZ   r[   rr   r  r  r   r   r   r   rz    s   	
rz  c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zerzdd Zdd Zdd ZeZeZeZeZn8dd Zdd Zdd Zdd Zdd Zd d! Zd"d Zd-d%d&Zd'd( Zd)d* Zd+d, ZeZeZd#S ).	MultiDictz This dict stores multiple values per key, but behaves exactly like a
        normal dict in that it returns only the newest value for any given key.
        There are special methods available to access the full list of values.
    c                 O   s"   t dd t || D | _ d S )Nc                 s   s   | ]\}}||gfV  qd S r'   r   r7  r   r   r   r   $  s     z%MultiDict.__init__.<locals>.<genexpr>rl   r5  )rW   rA   r8  r   r   r   rr   #  s    zMultiDict.__init__c                 C   s
   t | jS r'   )r   rl   rV   r   r   r   r  &  r&   zMultiDict.__len__c                 C   s
   t | jS r'   )r  rl   rV   r   r   r   r  '  r&   zMultiDict.__iter__c                 C   s
   || j kS r'   rl   r  r   r   r   rE  (  r&   zMultiDict.__contains__c                 C   s   | j |= d S r'   r  r  r   r   r   r  )  r&   zMultiDict.__delitem__c                 C   s   | j | d S rG  r  r  r   r   r   r  *  r&   zMultiDict.__getitem__c                 C   s   |  || d S r'   )r   rW   rp   r   r   r   r   r  +  r&   zMultiDict.__setitem__c                 C   s
   | j  S r'   )rl   r   rV   r   r   r   r   ,  r&   zMultiDict.keysc                 C   s   dd | j  D S )Nc                 s   s   | ]}|d  V  qdS rb  Nr   r   r9  r   r   r   r   /  s     z#MultiDict.values.<locals>.<genexpr>rl   r  rV   r   r   r   r  /  r&   zMultiDict.valuesc                 C   s   dd | j  D S )Nc                 s   s   | ]\}}||d  fV  qdS r  r   r7  r   r   r   r   0  s     z"MultiDict.items.<locals>.<genexpr>r  rV   r   r   r   r5  0  r&   zMultiDict.itemsc                 C   s   dd | j  D S )Nc                 s   s$   | ]\}}|D ]}||fV  qqd S r'   r   r   r8  Zvlr9  r   r   r   r   2  s       z%MultiDict.allitems.<locals>.<genexpr>r  rV   r   r   r   r  1  s    zMultiDict.allitemsc                 C   s   dd | j  D S )Nc                 S   s   g | ]}|d  qS rb  r   r  r   r   r   r   9  s     z$MultiDict.values.<locals>.<listcomp>r  rV   r   r   r   r  9  r&   c                 C   s   dd | j  D S )Nc                 S   s   g | ]\}}||d  fqS r  r   r7  r   r   r   r   :  s     z#MultiDict.items.<locals>.<listcomp>r  rV   r   r   r   r5  :  r&   c                 C   s
   | j  S r'   )rl   iterkeysrV   r   r   r   r  ;  r&   zMultiDict.iterkeysc                 C   s   dd | j  D S )Nc                 s   s   | ]}|d  V  qdS r  r   r  r   r   r   r   <  s     z'MultiDict.itervalues.<locals>.<genexpr>)rl   
itervaluesrV   r   r   r   r  <  r&   zMultiDict.itervaluesc                 C   s   dd | j  D S )Nc                 s   s   | ]\}}||d  fV  qdS r  r   r7  r   r   r   r   >  s     z&MultiDict.iteritems.<locals>.<genexpr>rl   	iteritemsrV   r   r   r   r  =  s    zMultiDict.iteritemsc                 C   s   dd | j  D S )Nc                 s   s$   | ]\}}|D ]}||fV  qqd S r'   r   r  r   r   r   r   @  s       z)MultiDict.iterallitems.<locals>.<genexpr>r  rV   r   r   r   iterallitems?  s    zMultiDict.iterallitemsc                 C   s   dd | j  D S )Nc                 S   s"   g | ]\}}|D ]}||fqqS r   r   r  r   r   r   r   B  s       z&MultiDict.allitems.<locals>.<listcomp>r  rV   r   r   r   r  A  s    Nrb  c                 C   s:   z | j | | }|r||n|W S  tk
r4   Y nX |S )a   Return the most recent value for a key.

            :param default: The default value to be returned if the key is not
                   present or the type conversion fails.
            :param index: An index for the list of available values.
            :param type: If defined, this callable is used to cast the value
                    into a specific type. Exception are suppressed and result in
                    the default value to be returned.
        )rl   r  )rW   rp   r   indexr  rP  r   r   r   r   D  s    
zMultiDict.getc                 C   s   | j |g | dS )z5 Add a new value to the list of values for this key. N)rl   r   r   r  r   r   r   r   U  s    zMultiDict.appendc                 C   s   |g| j |< dS )z1 Replace the list of values with a single value. Nr  r  r   r   r   r&  Y  s    zMultiDict.replacec                 C   s   | j |pg S )z5 Return a (possibly empty) list of values for a key. )rl   r   r  r   r   r   getall]  s    zMultiDict.getall)Nrb  N)rY   rZ   r[   r   rr   r  r  rE  r  r  r  r   r  r  r5  r  r  r  r  r  r   r   r&  r  Zgetonegetlistr   r   r   r   r    s<   
r  c                       sL   e Zd ZdZdZdZdddZdddZdd	d
Ze	 f fdd	Z
  ZS )r  a   This :class:`MultiDict` subclass is used to store request form data.
        Additionally to the normal dict-like item access methods (which return
        unmodified data as native strings), this container also supports
        attribute-like access to its values. Attributes are automatically de-
        or recoded to match :attr:`input_encoding` (default: 'utf8'). Missing
        attributes default to an empty string. rJ   TNc                 C   sH   t |tr&| jr&|d|p"| jS t |tr@||p<| jS |S d S Nr3   )rK   rL   recode_unicoderM   rR   input_encodingrN   )rW   r=   r4   r   r   r   _fixt  s
    
zFormsDict._fixc                 C   sN   t  }|p| j }|_d|_|  D ]$\}}|| ||| || q$|S )z Returns a copy with all keys and values de- or recoded to match
            :attr:`input_encoding`. Some libraries (e.g. WTForms) want a
            unicode dictionary. F)r  r  r  r  r   r  )rW   r4   r  rO   rp   r   r   r   r   rR   |  s    zFormsDict.decodec              	   C   s4   z|  | | |W S  ttfk
r.   | Y S X dS )z7 Return the value as a unicode string, or the default. N)r  r  r   )rW   r   r   r4   r   r   r   
getunicode  s    zFormsDict.getunicodec                    s2   | dr$|dr$tt| |S | j||dS )N__)r   )r  rX  rp  r  r  r  r  rq  r   r   r    s    zFormsDict.__getattr__)N)N)NN)rY   rZ   r[   r   r  r  r  rR   r  rL   r  rr  r   r   rq  r   r  f  s   


r  c                   @   sb   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdddZdd ZdS )rC  zz A case-insensitive version of :class:`MultiDict` that defaults to
        replace the old value instead of appending it. c                 O   s   i | _ |s|r| j|| d S r'   )rl   r  r  r   r   r   rr     s     zHeaderDict.__init__c                 C   s   t || jkS r'   )r'  rl   r  r   r   r   rE    r&   zHeaderDict.__contains__c                 C   s   | j t|= d S r'   rl   r'  r  r   r   r   r    r&   zHeaderDict.__delitem__c                 C   s   | j t| d S rG  r  r  r   r   r   r    r&   zHeaderDict.__getitem__c                 C   s   t |g| jt|< d S r'   r)  rl   r'  r  r   r   r   r    r&   zHeaderDict.__setitem__c                 C   s    | j t|g t| d S r'   )rl   r   r'  r   r)  r  r   r   r   r     r&   zHeaderDict.appendc                 C   s   t |g| jt|< d S r'   r  r  r   r   r   r&    r&   zHeaderDict.replacec                 C   s   | j t|pg S r'   )rl   r   r'  r  r   r   r   r    r&   zHeaderDict.getallNrb  c                 C   s   t | t|||S r'   )r  r   r'  )rW   rp   r   r  r   r   r   r     s    zHeaderDict.getc                 C   s*   dd |D D ]}|| j kr| j |= qd S )Nc                 s   s   | ]}t |V  qd S r'   )r'  )r   r   r   r   r   r     s     z$HeaderDict.filter.<locals>.<genexpr>r  )rW   namesr   r   r   r   filter  s    
zHeaderDict.filter)Nrb  )rY   rZ   r[   r   rr   rE  r  r  r  r   r&  r  r   r  r   r   r   r   rC    s   
rC  c                   @   sf   e Zd ZdZdZdd Zdd Zddd	Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )r  a    This dict-like class wraps a WSGI environ dict and provides convenient
        access to HTTP_* fields. Keys and values are native strings
        (2.x bytes or 3.x unicode) and keys are case-insensitive. If the WSGI
        environment contains non-native string values, these are de- or encoded
        using a lossless 'latin1' character set.

        The API will remain stable even on changes to the relevant PEPs.
        Currently PEP 333, 444 and 3333 are supported. (PEP 444 is the only one
        that uses non-native strings.)
    )r  r  c                 C   s
   || _ d S r'   r  rh  r   r   r   rr     s    zWSGIHeaderDict.__init__c                 C   s&   | dd }|| jkr|S d| S )z6 Translate header field name to CGI/WSGI environ key. r$  r   r  )r&  r   cgikeysr  r   r   r   _ekey  s    
zWSGIHeaderDict._ekeyNc                 C   s   | j | ||S )z: Return the header value as is (may be bytes or unicode). )r   r   r  )rW   rp   r   r   r   r   raw  s    zWSGIHeaderDict.rawc                 C   s   t | j| | dS r  )r  r   r  r  r   r   r   r    s    zWSGIHeaderDict.__getitem__c                 C   s   t d| j d S Nz%s is read-only.r`  r  r  r   r   r   r    s    zWSGIHeaderDict.__setitem__c                 C   s   t d| j d S r  r  r  r   r   r   r    s    zWSGIHeaderDict.__delitem__c                 c   sX   | j D ]L}|d d dkr6|dd  dd V  q|| jkr|dd V  qd S )N   r  r   r$  )r   r&  r%  r  r  r   r   r   r    s
    

zWSGIHeaderDict.__iter__c                 C   s   dd | D S )Nc                 S   s   g | ]}|qS r   r   r   r+   r   r   r   r     s     z'WSGIHeaderDict.keys.<locals>.<listcomp>r   rV   r   r   r   r     r&   zWSGIHeaderDict.keysc                 C   s   t |  S r'   r   r   rV   r   r   r   r    r&   zWSGIHeaderDict.__len__c                 C   s   |  || jkS r'   )r  r   r  r   r   r   rE    r&   zWSGIHeaderDict.__contains__)N)rY   rZ   r[   r   r  rr   r  r  r  r  r  r  r   r  rE  r   r   r   r   r    s   
r  c                   @   s   e Zd ZdZdZG dd deZdd Zdd Zd&ddZ	dd Z
dd Zdd Zdd Zdd Zd'ddZdd Zdd Zdd Zd d! Zd"d# Zd$d% ZdS )(r	  aH   A dict-like configuration storage with additional support for
        namespaces, validators, meta-data, on_change listeners and more.

        This storage is optimized for fast read access. Retrieving a key
        or using non-altering dict methods (e.g. `dict.get()`) has no overhead
        compared to a native dict.
    )_metar+  c                   @   s|   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )zConfigDict.Namespacec                 C   s   || _ || _d S r'   _config_prefix)rW   r  	namespacer   r   r   rr     s    zConfigDict.Namespace.__init__c                 C   s   t d | j| jd |  S )Nz}Accessing namespaces as dicts is discouraged. Only use flat item access: cfg["names"]["pace"]["key"] -> cfg["name.space.key"]r  )rh   r  r  r  r   r   r   r    s    z ConfigDict.Namespace.__getitem__c                 C   s   || j | jd | < d S Nr  r  r  r   r   r   r    s    z ConfigDict.Namespace.__setitem__c                 C   s   | j | jd | = d S r  r  r  r   r   r   r    s    z ConfigDict.Namespace.__delitem__c                 c   s>   | j d }| jD ](}|d\}}}|| j kr|r|V  qd S r  )r  r  
rpartition)rW   Z	ns_prefixrp   nsdotr   r   r   r   r    s
    

zConfigDict.Namespace.__iter__c                 C   s   dd | D S )Nc                 S   s   g | ]}|qS r   r   r  r   r   r   r      s     z-ConfigDict.Namespace.keys.<locals>.<listcomp>r   rV   r   r   r   r      r&   zConfigDict.Namespace.keysc                 C   s   t |  S r'   r  rV   r   r   r   r    r&   zConfigDict.Namespace.__len__c                 C   s   | j d | | jkS r  )r  r  r  r   r   r   rE    r&   z!ConfigDict.Namespace.__contains__c                 C   s
   d| j  S Nz<Config.Namespace %s.*>r  rV   r   r   r   r#    r&   zConfigDict.Namespace.__repr__c                 C   s
   d| j  S r  r  rV   r   r   r   __str__  r&   zConfigDict.Namespace.__str__c                 C   s\   t d || kr8|d  r8t| j| jd | | |< || krR|drRt|| |S )NAttribute access is deprecated.r   r  r  )	rh   isupperr	  	Namespacer  r  r  r^   r   r  r   r   r   r    s    z ConfigDict.Namespace.__getattr__c                 C   sd   |dkr|| j |< d S td tt|r0td|| krX| | rXt| | | jrXtd|| |< d S )Nr  #Attribute assignment is deprecated.Read-only attribute.Non-empty namespace attribute.)r   rh   r?   rF   r^   rK   r  r  r   r   r   r    s    

 z ConfigDict.Namespace.__setattr__c                 C   sH   || krD|  |}t|| jrD|d }| D ]}||r*| || = q*d S r  )r   rK   r  r  rW   rp   rP  r   r   r   r   __delattr__  s    

z ConfigDict.Namespace.__delattr__c                 O   s   t d | j|| | S Nz8Calling ConfDict is deprecated. Use the update() method.rh   r  r  r   r   r   r>   #  s    zConfigDict.Namespace.__call__N)rY   rZ   r[   rr   r  r  r  r  r   r  rE  r#  r  r  r  r  r>   r   r   r   r   r    s   	r  c                 O   s0   i | _ dd | _|s|r,td | j|| d S )Nc                 S   s   d S r'   r   )r   r   r   r   r   r,   *  r&   z%ConfigDict.__init__.<locals>.<lambda>z-Constructor does no longer accept parameters.)r  r+  rh   r  r  r   r   r   rr   (  s
    
zConfigDict.__init__c                 C   sR   t  }|| | D ]4}||D ]$\}}|dkrB|d | }|| |< q&q| S )a   Load values from an *.ini style config file.

            If the config file contains sections, their names are used as
            namespaces for the values within. The two special sections
            ``DEFAULT`` and ``bottle`` refer to the root namespace (no prefix).
        )DEFAULTbottler  )r:   r  sectionsr5  )rW   r  r   sectionrp   r   r   r   r   load_config/  s    
zConfigDict.load_configr   Fc           	      C   s   ||fg}|r|  \}}t|ts4tdt| | D ]l\}}t|ts^tdt| |rn|d | n|}t|tr|||f |r| | || |< q<|| |< q<q
| S )z Import values from a dictionary structure. Nesting can be used to
            represent namespaces.

            >>> ConfigDict().load_dict({'name': {'space': {'key': 'value'}}})
            {'name.space.key': 'value'}
        zSource is not a dict (r)zKey is not a string (%r)r  )	r   rK   rl   r`  r  r5  rU  r   r  )	rW   sourcer  r  stackr   rp   r   Zfull_keyr   r   r   r
  ?  s    



zConfigDict.load_dictc                 O   s\   d}|r4t |d tr4|d dd }|dd }t|| D ]\}}|| || < qBdS )z If the first parameter is a string, all keys are prefixed with this
            namespace. Apart from that it works just as the usual dict.update().
            Example: ``update('some.namespace', key='value')`` r   r   r  r"   N)rK   rU  rj  rl   r5  )rW   rA   ra   r   rp   r   r   r   r   r  W  s    zConfigDict.updatec                 C   s   || kr|| |< | | S r'   r   r  r   r   r   r   b  s    zConfigDict.setdefaultc                 C   sf   t |tstdt| | |ddd |}|| krH| | |krHd S | || t| || d S )NzKey has type %r (not a string)r  c                 S   s   | S r'   r   r*   r   r   r   r,   k  r&   z(ConfigDict.__setitem__.<locals>.<lambda>)rK   rU  r`  r  meta_getr+  rl   r  r  r   r   r   r  g  s    
zConfigDict.__setitem__c                 C   s   t | | d S r'   )rl   r  r  r   r   r   r  q  s    zConfigDict.__delitem__c                 C   s   | D ]
}| |= qd S r'   r   r  r   r   r   cleart  s    zConfigDict.clearNc                 C   s   | j |i ||S )z- Return the value of a meta field for a key. )r  r   )rW   rp   	metafieldr   r   r   r   r  x  s    zConfigDict.meta_getc                 C   s*   || j |i |< || kr&| | | |< dS )zq Set the meta field for a key to a new value. This triggers the
            on-change handler for existing keys. N)r  r   )rW   rp   r  r   r   r   r   r,  |  s    zConfigDict.meta_setc                 C   s   | j |i  S )z; Return an iterable of meta field names defined for a key. )r  r   r   r  r   r   r   	meta_list  s    zConfigDict.meta_listc                 C   sP   t d || kr,|d  r,| | || |< || krF|drFt|| |S )Nr  r   r  )rh   r  r  r  r^   r   r  r   r   r   r    s    zConfigDict.__getattr__c                 C   sf   || j krt| ||S td tt|r2td|| krZ| | rZt| | | jrZtd|| |< d S )Nr  r  r  )r!  rl   r  rh   r?   r^   rK   r  r  r   r   r   r    s    

 zConfigDict.__setattr__c                 C   sH   || krD|  |}t|| jrD|d }| D ]}||r*| || = q*d S r  )r   rK   r  r  r  r   r   r   r    s    

zConfigDict.__delattr__c                 O   s   t d | j|| | S r  r  r  r   r   r   r>     s    zConfigDict.__call__)r   F)N)rY   rZ   r[   r   r!  rF   r  rr   r  r
  r  r   r  r  r  r  r,  r  r  r  r  r>   r   r   r   r   r	    s$   A



	r	  c                   @   s"   e Zd ZdZdd ZdddZdS )AppStackz> A stack-like list. Calling it returns the head of the stack. c                 C   s   | d S )z) Return the current default application. rb  r   rV   r   r   r   r>     s    zAppStack.__call__Nc                 C   s   t |tst }| | |S )z1 Add a new :class:`Bottle` instance to the stack )rK   r%  r   rh  r   r   r   push  s    

zAppStack.push)N)rY   rZ   r[   r   r>   r  r   r   r   r   r    s   r  c                   @   s   e Zd ZdddZdd ZdS )r     c                 C   s8   || | _ | _dD ] }t||rt| |t|| qd S )N)filenorX   r  	readlinestellr  )r  buffer_sizer?   r   rx   )rW   r  r  ro   r   r   r   rr     s    
 zWSGIFileWrapper.__init__c                 c   s*   | j | j }}||}|sd S |V  qd S r'   )r  r  )rW   r  r  r  r   r   r   r    s
     zWSGIFileWrapper.__iter__N)r  )rY   rZ   r[   rr   r  r   r   r   r   r    s   
r  c                   @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
r  z This only exists to be able to attach a .close method to iterators that
        do not support attribute assignment (most of itertools). Nc                 C   s   || _ t|| _d S r'   )iteratorrm   close_callbacks)rW   r  rX   r   r   r   rr     s    z_closeiter.__init__c                 C   s
   t | jS r'   )r  r  rV   r   r   r   r    s    z_closeiter.__iter__c                 C   s   | j D ]
}|  qd S r'   )r  rv   r   r   r   rX     s    
z_closeiter.close)N)rY   rZ   r[   r   rr   r  rX   r   r   r   r   r    s   
r  c                   @   sD   e Zd ZdZdedfddZddd	Zd
d Zdd ZdddZdS )r.  af   This class manages a list of search paths and helps to find and open
        application-bound resources (files).

        :param base: default value for :meth:`add_path` calls.
        :param opener: callable used to open resources.
        :param cachemode: controls which lookups are cached. One of 'all',
                         'found' or 'none'.
    ./allc                 C   s"   t | _|| _|| _g | _i | _d S r'   )openopenerbase	cachemoder   cache)rW   r  r  r  r   r   r   rr     s
    zResourceManager.__init__NFc                 C   s   t jt j|p| j}t jt j|t j|}|t j7 }|| jkrX| j| |rrt j|srt 	| |dkr| j
| n| j|| | j  t j|S )a   Add a new path to the list of search paths. Return False if the
            path does not exist.

            :param path: The new search path. Relative paths are turned into
                an absolute and normalized form. If the path looks like a file
                (not ending in `/`), the filename is stripped off.
            :param base: Path used to absolutize relative search paths.
                Defaults to :attr:`base` which defaults to ``os.getcwd()``.
            :param index: Position within the list of search paths. Defaults
                to last index (appends to the list).

            The `base` parameter makes it easy to reference files installed
            along with a python module or package::

                res.add_path('./resources/', __file__)
        N)osr   abspathdirnamer  r   sepr>  isdirmakedirsr   r<  r  r  exists)rW   r   r  r  creater   r   r   add_path  s    



zResourceManager.add_pathc                 c   sj   | j dd }|rf| }tj |s(qt|D ]0}tj ||}tj |r\|| q2|V  q2qdS )z: Iterate over all existing files in all registered paths. N)r   r   r  r  listdirr   r   )rW   searchr   r   fullr   r   r   r  	  s      zResourceManager.__iter__c                 C   sn   || j kstrd| jD ]:}tj||}tj|r| jdkrF|| j |< |  S q| jdkrdd| j |< | j | S )z Search for a resource and return an absolute file path, or `None`.

            The :attr:`path` list is searched in order. The first match is
            returend. Symlinks are followed. The result is cached to speed up
            future lookups. )r  foundr  N)r  r   r   r  r   isfiler  )rW   r   r   Zfpathr   r   r   lookup	  s    





zResourceManager.lookuprc                 O   s4   |  |}|std| | j|f|d|i|S )z= Find a resource and return a file object, or raise IOError. zResource %r not found.r   )r  IOErrorr  )rW   r   r   r   rB  fnamer   r   r   r  '	  s    
 zResourceManager.open)NNF)r  )	rY   rZ   r[   r   r  rr   r  r  r  r   r   r   r   r.    s   	

r.  c                   @   sV   e Zd ZdddZedZededdZddd	Ze	d
d Z
dddZdddZdS )r  Nc                 C   s*   || _ || _|| _|rt|nt | _dS )z Wrapper for file uploads. N)r  r   raw_filenamerC  r  )rW   fileobjr   r  r  r   r   r   rr   0	  s    zFileUpload.__init__r  r  rb  )r+  r   c                 C   s   | j ||S )z9 Return the value of a header within the mulripart part. r  r  r   r   r   r  >	  s    zFileUpload.get_headerc                 C   s   | j }t|ts|dd}td|ddd}tj|	dtjj
}tdd| }tdd	|d
}|dd pdS )a   Name of the file on the client file system, but normalized to ensure
            file system compatibility. An empty filename is returned as 'empty'.

            Only ASCII letters, digits, dashes, underscores and dots are
            allowed in the final filename. Accents are removed, if possible.
            Whitespace is replaced by a single dash. Leading or tailing dots
            or dashes are removed. The filename is limited to 255 characters.
        rJ   ignoreZNFKDASCII\z[^a-zA-Z0-9-_.\s]r   z[-\s]+r$  z.-N   empty)r  rK   rL   rR   r   rM   r  r   basenamer&  r  r   r   rj  )rW   r  r   r   r   r  B	  s    

zFileUpload.filenamer  c                 C   sD   | j j|j| j    }}}||}|s*q4|| q| j | d S r'   )r  r  r)   r  r  )rW   r  
chunk_sizer  r)   r   bufr   r   r   
_copy_fileU	  s     
zFileUpload._copy_fileFc              	   C   sr   t |trbtj|r&tj|| j}|s>tj|r>tdt	|d}| 
|| W 5 Q R X n| 
|| dS )a   Save file to disk or copy its content to an open file(-like) object.
            If *destination* is a directory, :attr:`filename` is added to the
            path. Existing files are not overwritten by default (IOError).

            :param destination: File path, directory or file(-like) object.
            :param overwrite: If True, replace existing files. (default: False)
            :param chunk_size: Bytes to read at a time. (default: 64kb)
        zFile exists.wbN)rK   rU  r  r   r  r   r  r  r  r  r  )rW   Zdestination	overwriter  r  r   r   r   save]	  s    	
zFileUpload.save)N)N)r  )Fr  )rY   rZ   r[   rr   r*  r  r   r  r  r   r  r  r  r   r   r   r   r  .	  s   



r  r|  Unknown Error.c                 C   s   t | |dS )z+ Aborts execution and causes a HTTP error. N)r   )r  textr   r   r   abortz	  s    r  c                 C   sN   |st ddkrdnd}tjtd}||_d|_|dtt j	|  |dS )	zd Aborts execution and causes a 303 or 302 redirect, depending on
        the HTTP protocol version. ZSERVER_PROTOCOLzHTTP/1.1i/  i.  )rz   r   ZLocationN)
rN  r   r  r  rP  rG  rI  rI  r.   r   )r   r  r  r   r   r   redirect	  s    r     c                 c   s@   |  | |dkr<| t||}|s(q<|t|8 }|V  q
dS )zF Yield chunks from a range in a file. No chunk is bigger than maxread.r   N)r  r  r  r   )r  r   rN   r  r  r   r   r   _file_iter_range	  s    
 r  autorV  c                 C   sd  t j|t j }t jt j|| d} t }| |sHtddS t j	| r`t j
| sjtddS t | t jstddS |dkrt| \}}|r||d< |r|d	d
 dkr|rd|kr|d| 7 }||d< |rt j|dkr| n|}d| |d< t | }|j |d< }tdt|j}	|	|d< tjd}
|
r^t|
dd  }
|
d	k	r|
t|jkrtdt |d< tf ddi|S tjdkrdnt| d}d|d< tjd }d tjkrXtt tjd  |}|std!d"S |d \}}d#||d$ |f |d%< t!|| |d< |rDt"|||| }t|fdd&i|S t|f|S )'a   Open a file in a safe way and return :exc:`HTTPResponse` with status
        code 200, 305, 403 or 404. The ``Content-Type``, ``Content-Encoding``,
        ``Content-Length`` and ``Last-Modified`` headers are set if possible.
        Special support for ``If-Modified-Since``, ``Range`` and ``HEAD``
        requests.

        :param filename: Name or path of the file to send.
        :param root: Root path for file lookups. Should be an absolute directory
            path.
        :param mimetype: Defines the content-type header (default: guess from
            file extension)
        :param download: If True, ask the browser to open a `Save as...` dialog
            instead of opening the file with the associated program. You can
            specify a custom filename as a string. If not specified, the
            original filename is used (default: False).
        :param charset: The charset to use for files with a ``text/*``
            mime-type. (default: UTF-8)
    z/\i  zAccess denied.r   zFile does not exist.z/You do not have permission to access this file.r  r/  Nr  ztext/r  z; charset=%sr  Tzattachment; filename="%s"zContent-Dispositionr  rZ  r1  ZHTTP_IF_MODIFIED_SINCEr  r   DaterG  r  r   r   rbrN   zAccept-RangesZ
HTTP_RANGEi  zRequested Range Not Satisfiablezbytes %d-%d/%dr"   r0     )#r  r   r  r  r   rj  rl   r  r   r  r  accessR_OK	mimetypes
guess_typer  statst_sizer`  rb  ra  st_mtimerN  r   r   rT  rV  r   rP  r   r  rj   parse_range_headerr   r  )r  rootmimetypedownloadr  r  r4   statsr  ZlmimsrI  rangesr   r   r   r   r   static_file	  sX    



 

 r  Tc                 C   s   | rt d t| adS )zS Change the debug level.
    There is only one debug level supported at the moment.r   N)rc   simplefilterr-  r   r  r   r   r   debug	  s     
r  c                 C   sJ   t | ttfr|  } nt | ttfr0t| } t | tsFt	d| } | S )NrZ  )
rK   r^  r   utctimetupler   r   r`  ra  rU  rb  r(  r   r   r   rU  	  s    


rU  c                 C   sZ   z6t j| }t|dd d |d p,d tj W S  tttt	fk
rT   Y dS X dS )zD Parse rfc1123, rfc850 and asctime timestamps and return UTC epoch. N   rK  	   r   )
emailutilsparsedate_tzr`  mktimetimezoner`  r   
IndexErrorOverflowError)r  tsr   r   r   rT  	  s
    *rT  c              	   C   sh   zH|  dd\}}| dkrFttt| dd\}}||fW S W n ttfk
rb   Y dS X dS )z] Parse rfc2617 HTTP authentication header string (basic) and return (user,pass) tuple or NoneNr"   r  r  )rV  r  r<   base64	b64decoderP   r   r   )r  r   r   userpwdr   r   r   r
  	  s    r
  c              	   c   s   | r| dd dkrdS dd | dd  dD }|D ]\}}z~|s`td|t| | }}n0|stt|| }}nt|tt|d | }}d|  kr|  k r|krn n
||fV  W q8 tk
r   Y q8X q8dS )	z~ Yield (start, end) ranges parsed from a HTTP Range header. Skip
        unsatisfiable ranges. The end index is non-inclusive.Nr!   zbytes=c                 S   s    g | ]}d |kr| d dqS )r$  r"   )rV  )r   r  r   r   r   r   
  s      z&parse_range_header.<locals>.<listcomp>r   r   r"   )rV  r  r   r  r   )r  maxlenr  r   r   r   r   r   r  
  s     "r  c                 C   sv   g }|  dD ]b}|sq| dd}t|dkr:|d t|d dd}t|d dd}|||f q|S )	N&=r"   r    r   r   +r?  )rV  r   r   
urlunquoter&  )qsr  pairnvrp   r   r   r   r   r  
  s      
r  c                 C   s*   t dd t| |D  o(t| t|kS )zs Compares two strings in a cryptographically safe way:
        Runtime is not affected by length of common prefix. c                 s   s"   | ]\}}||krd ndV  qdS )r   r"   Nr   )r   r+   yr   r   r   r   (
  s     z_lscmp.<locals>.<genexpr>)sumzipr   )rA   r  r   r   r   _lscmp%
  s    r4  c                 C   sJ   t t| d}t tjt||tjd	 }td| td | S )z> Encode and sign a pickle-able object. Return a (byte) string rb  	digestmod!r   )
r%  	b64encodepickler   hmacnewrP   hashlibmd5digest)r   rp   r   sigr   r   r   r[  +
  s     r[  c              	   C   sf   t | } t| rb| t dd\}}t|dd ttjt ||tj	d
 rbtt|S dS )z? Verify and decode an encoded string. Return an object or None.r   r"   Nr5  )rP   cookie_is_encodedrV  r4  r%  r8  r:  r;  r<  r=  r>  r9  r   r&  )r   rp   r?  r   r   r   r   r  2
  s    .r  c                 C   s   t | tdotd| kS )z9 Return True if the argument looks like a encoded cookie.r7  r   )r-  r  rP   r   r   r   r   r@  <
  s    r@  c                 C   s,   |  dd dd dd dd d	d
S )z; Escape HTML special characters ``&<>`` and quotes ``'"``. r*  z&amp;<z&lt;>z&gt;"z&quot;'z&#039;)r&  stringr   r   r   r  A
  s      r  c                 C   s$   dt | dddddd S )z; Escape and quote a string to be used as an HTTP attribute.z"%s"r  z&#10;r"  z&#13;	z&#9;)r  r&  rE  r   r   r   
html_quoteG
  s      rH  c                 c   s   d| j ddd }t| }t|d t|d p6g  }|d| t|d d|  7 }|V  |d |d D ]}|d| 7 }|V  qrdS )a   Return a generator for routes that match the signature (name, args)
    of the func parameter. This may yield more than one route if the function
    takes optional keyword arguments. The output is best described by example::

        a()         -> '/a'
        b(x, y)     -> '/b/<x>/<y>'
        c(x, y=5)   -> '/c/<x>' and '/c/<x>/<y>'
        d(x=5, y=6) -> '/d' and '/d/<x>' and '/d/<x>/<y>'
    r   r  r   r   z/<%s>N)rY   r&  rk  r   r   ri   )rw   r   specZargcargr   r   r   rr  M
  s    
 rr  r"   c           	      C   s   |dkr| |fS | dd}|  dd}|rD|d dkrDg }|rX|d dkrXg }|dkr|t|kr|d| }|| }||d }nT|dk r|t| kr||d }|| }|d| }n|dk rdnd}td| dd| }dd| }|dr|r|d7 }||fS )aS   Shift path fragments from PATH_INFO to SCRIPT_NAME and vice versa.

        :return: The modified paths.
        :param script_name: The SCRIPT_NAME path.
        :param script_name: The PATH_INFO path.
        :param shift: The number of path fragments to shift. May be negative to
          change the shift direction. (default: 1)
    r   r   r   Nri  r   z"Cannot shift. Nothing left from %s)rj  rV  r   r;  r   rX  )	r  Z	path_infor  pathlistZ
scriptlistmovedr  Znew_script_nameZnew_path_infor   r   r   rO  a
  s.    	    rO  privateAccess deniedc                    s    fdd}|S )ze Callback decorator to require HTTP auth (basic).
        TODO: Add route(check_auth=...) parameter. c                    s    fdd}|S )Nc                     sH   t jpd\}}|d ks  ||s>td}|dd  |S | |S )N)NNi  zWWW-AuthenticatezBasic realm="%s")rN  r  r   rH  )rA   ra   r'  passwordrS   )checkrw   realmr   r   r   r_   
  s    
z.auth_basic.<locals>.decorator.<locals>.wrapperr   rw   r_   rP  rQ  r   rC  r   rE  
  s    zauth_basic.<locals>.decoratorr   )rP  rQ  r   rE  r   rS  r   
auth_basic
  s    	rT  c                    s    t tt  fdd}|S )zA Return a callable that relays calls to the current default app. c                     s   t t  | |S r'   )rx   r  )rA   ra   ro  r   r   r_   
  s    z)make_default_app_wrapper.<locals>.wrapper)r\   wrapsrx   r%  )r   r_   r   ro  r   make_default_app_wrapper
  s    rV  rW  r   rw  ry  r{  r   r[  r@  r3  rd  rn  c                   @   s*   e Zd ZdZdddZdd Zdd	 Zd
S )ServerAdapterFr    c                 K   s   || _ || _t|| _d S r'   )rY  r  r   r  )rW   r  r  rY  r   r   r   rr   
  s    zServerAdapter.__init__c                 C   s   d S r'   r   )rW   r~  r   r   r   rg  
  s    zServerAdapter.runc                 C   s*   d dd | j D }d| jj|f S )N, c                 S   s    g | ]\}}d |t |f qS )z%s=%s)r   r7  r   r   r   r   
  s     z*ServerAdapter.__repr__.<locals>.<listcomp>z%s(%s))r   rY  r5  r  rY   )rW   r   r   r   r   r#  
  s    zServerAdapter.__repr__N)r  rX  )rY   rZ   r[   quietrr   rg  r#  r   r   r   r   rW  
  s   
rW  c                   @   s   e Zd ZdZdd ZdS )	CGIServerTc                    s(   ddl m}  fdd}| | d S )Nr   )
CGIHandlerc                    s   |  dd  | |S )Nr   r   )r   r   rM  r}  r   r   fixed_environ
  s    z$CGIServer.run.<locals>.fixed_environ)Zwsgiref.handlersr\  rg  )rW   r~  r\  r^  r   r}  r   rg  
  s    zCGIServer.runN)rY   rZ   r[   rZ  rg  r   r   r   r   r[  
  s   r[  c                   @   s   e Zd Zdd ZdS )FlupFCGIServerc                 C   s:   dd l }| jd| j| jf |jjj|f| j  d S )Nr   ZbindAddress)	Zflup.server.fcgirY  r   r  r  serverZfcgi
WSGIServerrg  )rW   r~  flupr   r   r   rg  
  s    zFlupFCGIServer.runNrY   rZ   r[   rg  r   r   r   r   r_  
  s   r_  c                   @   s   e Zd Zdd ZdS )WSGIRefServerc                    s   ddl m m} ddl m} dd lG  fddd }jd|}jd|}djkrt|d	j	krG fd
dd|}|jj
|||}|  d S )Nr   )WSGIRequestHandlerra  )make_serverc                       s"   e Zd Zdd Z fddZdS )z'WSGIRefServer.run.<locals>.FixedHandlerc                 S   s
   | j d S r  )client_addressrV   r   r   r   address_string
  s    z6WSGIRefServer.run.<locals>.FixedHandler.address_stringc                     s   j s j| |S d S r'   )rZ  log_request)r   kwre  rW   r   r   ri  
  s    z3WSGIRefServer.run.<locals>.FixedHandler.log_requestN)rY   rZ   r[   rh  ri  r   rk  r   r   FixedHandler
  s   rl  Zhandler_classZserver_classr  address_familyc                       s   e Zd Z jZdS )z%WSGIRefServer.run.<locals>.server_clsN)rY   rZ   r[   AF_INET6rm  r   )socketr   r   
server_cls
  s   rp  )Zwsgiref.simple_serverre  ra  rf  ro  rY  r   r  rx   AF_INETr  serve_forever)rW   r  ra  rf  rl  Zhandler_clsrp  Zsrvr   )re  rW   ro  r   rg  
  s    
zWSGIRefServer.runNrc  r   r   r   r   rd  
  s   rd  c                   @   s   e Zd Zdd ZdS )CherryPyServerc                 C   s   ddl m} | j| jf| jd< || jd< | jd}|r@| jd= | jd}|rX| jd= |jf | j}|rp||_|rz||_z|
  W 5 |	  X d S )Nr   )
wsgiserverZ	bind_addrwsgi_appcertfilekeyfile)cherrypyrt  r  r  rY  r   ZCherryPyWSGIServerZssl_certificateZssl_private_keystopr   )rW   r~  rt  rv  rw  r`  r   r   r   rg  
  s"    
zCherryPyServer.runNrc  r   r   r   r   rs  
  s   rs  c                   @   s   e Zd Zdd ZdS )WaitressServerc                 C   s"   ddl m} ||| j| jd d S )Nr   )server  r  )waitressr{  r  r  )rW   r~  r{  r   r   r   rg    s    zWaitressServer.runNrc  r   r   r   r   rz    s   rz  c                   @   s   e Zd Zdd ZdS )PasteServerc                 C   sN   ddl m} ddlm} ||| j d}|j|f| jt| jd| j	 d S )Nr   )
httpserver)TransLogger)Zsetup_console_handlerr|  )
paster  Zpaste.transloggerr  rZ  r{  r  r   r  rY  )rW   r~  r  r  r   r   r   rg  	  s    zPasteServer.runNrc  r   r   r   r   r~    s   r~  c                   @   s   e Zd Zdd ZdS )MeinheldServerc                 C   s,   ddl m} || j| jf || d S Nr   r`  )meinheldr`  listenr  r  rg  )rW   r~  r`  r   r   r   rg    s    zMeinheldServer.runNrc  r   r   r   r   r    s   r  c                   @   s   e Zd ZdZdd ZdS )FapwsServerzA Extremely fast webserver using libev. See http://www.fapws.org/ c                    s   dd l m} ddlm}m} | j}t|jdd  dkr@t|}|	| j
| dtjkrn| jsntd td ||  fdd	}|d
|f |  d S )Nr   )r  r  r  g?BOTTLE_CHILDz3WARNING: Auto-reloading does not work with Fapws3.
z/         (Fapws3 breaks python thread support)
c                    s   d| d<  | |S )NFzwsgi.multiprocessr   r]  r}  r   r   r  '  s    zFapwsServer.run.<locals>.appr   )Zfapws._evwsgiZ_evwsgiZfapwsr  r  r  r   ZSERVER_IDENTr   r   r  r  r   rZ  _stderrZset_base_moduleZwsgi_cbrg  )rW   r~  Zevwsgir  r  r  r  r   r}  r   rg    s    
zFapwsServer.runNrY   rZ   r[   r   rg  r   r   r   r   r    s   r  c                   @   s   e Zd ZdZdd ZdS )TornadoServerz< The super hyped asynchronous server by facebook. Untested. c                 C   sV   dd l }dd l}dd l}|j|}|j|}|j| j| j	d |j
j   d S )Nr   )r  address)Ztornado.wsgiZtornado.httpserverZtornado.ioloopr  ZWSGIContainerr  Z
HTTPServerr  r  r  ZioloopZIOLoopinstancer   )rW   r~  tornado	containerr`  r   r   r   rg  0  s
    zTornadoServer.runNr  r   r   r   r   r  .  s   r  c                   @   s   e Zd ZdZdZdd ZdS )AppEngineServerz  Adapter for Google App Engine. Tc                    sD   ddl m tjd}|r6t|ds6 fdd|_  d S )Nr   )utilr   mainc                      s
     S r'   )run_wsgi_appr   r~  r  r   r   r,   A  r&   z%AppEngineServer.run.<locals>.<lambda>)Zgoogle.appengine.ext.webappr  r#   r  r   r?   r  r  )rW   r~  r  r   r  r   rg  ;  s
    zAppEngineServer.runN)rY   rZ   r[   r   rZ  rg  r   r   r   r   r  8  s   r  c                   @   s   e Zd ZdZdd ZdS )TwistedServer Untested. c                 C   sz   ddl m}m} ddlm} ddlm} | }|  |dd|j	 |
||||}|j| j|| jd |  d S )Nr   )r`  r  )
ThreadPool)reactoraftershutdown)Z	interface)Ztwisted.webr`  r  Ztwisted.python.threadpoolr  Ztwisted.internetr  r   ZaddSystemEventTriggerry  ZSiteZWSGIResourceZ	listenTCPr  r  rg  )rW   r~  r`  r  r  r  Zthread_poolfactoryr   r   r   rg  G  s    zTwistedServer.runNr  r   r   r   r   r  E  s   r  c                   @   s   e Zd ZdZdd ZdS )DieselServerr  c                 C   s&   ddl m} ||| jd}|  d S )Nr   )WSGIApplication)r  )Zdiesel.protocols.wsgir  r  rg  )rW   r~  r  r  r   r   r   rg  U  s    zDieselServer.runNr  r   r   r   r   r  S  s   r  c                   @   s   e Zd ZdZdd ZdS )GeventServerz Untested. Options:

        * `fast` (default: False) uses libevent's http server, but has some
          issues: No streaming, no pipelining, no SSL.
        * See gevent.wsgi.WSGIServer() documentation for more options.
    c                    s   ddl m}m} tt |js,d}t|| jdd rBtd | j	rRd | jd< | j
| jf}|j||f| j dtjkrdd l}||j fdd	    d S )
Nr   )pywsgirl  z9Bottle requires gevent.monkey.patch_all() (before import)fastz<The "fast" option has been deprecated and removed by Gevent.logr  c                    s      S r'   )ry  )r=   r   r  r   r   r,   o  r&   z"GeventServer.run.<locals>.<lambda>)r   r  rl  rK   rk  r  rY  r   rh   rZ  r  r  ra  r  r   signalSIGINTrr  )rW   r~  r  rl  r   r  r  r   r  r   rg  b  s    

zGeventServer.runNr  r   r   r   r   r  [  s   r  c                   @   s   e Zd Zdd ZdS )GeventSocketIOServerc                 C   s2   ddl m} | j| jf}|j||f| j  d S r  )Zsocketior`  r  r  ZSocketIOServerrY  rr  )rW   r~  r`  r  r   r   r   rg  t  s    zGeventSocketIOServer.runNrc  r   r   r   r   r  s  s   r  c                   @   s   e Zd ZdZdd ZdS )GunicornServerz? Untested. See http://gunicorn.org/configure.html for options. c                    sT   ddl m} dd| jt| jf i  | j G  fddd|}|   d S )Nr   )Applicationr  z%s:%dc                       s$   e Zd Z fddZfddZdS )z/GunicornServer.run.<locals>.GunicornApplicationc                    s    S r'   r   )rW   parseroptsr   )r  r   r   init  s    z4GunicornServer.run.<locals>.GunicornApplication.initc                    s    S r'   r   rV   r}  r   r   rq    s    z4GunicornServer.run.<locals>.GunicornApplication.loadN)rY   rZ   r[   r  rq  r   r  r~  r   r   GunicornApplication  s   r  )Zgunicorn.app.baser  r  r   r  r  rY  rg  )rW   r~  r  r  r   r  r   rg  |  s
    zGunicornServer.runNr  r   r   r   r   r  z  s   r  c                   @   s   e Zd ZdZdd ZdS )EventletServerz
 Untested c                 C   sf   ddl m}m} z$|j|| j| jf|| j d W n, tk
r`   ||| j| jf| Y nX d S )Nr   )r  r  )Z
log_output)eventletr  r  r`  r  r  rZ  r`  )rW   r~  r  r  r   r   r   rg    s    
zEventletServer.runNr  r   r   r   r   r    s   r  c                   @   s   e Zd ZdZdd ZdS )RocketServerr  c                 C   s0   ddl m} || j| jfdd|i}|  d S )Nr   )Rocketr  ru  )rocketr  r  r  r   )rW   r~  r  r`  r   r   r   rg    s    zRocketServer.runNr  r   r   r   r   r    s   r  c                   @   s   e Zd ZdZdd ZdS )BjoernServerz? Fast server written in C: https://github.com/jonashaag/bjoern c                 C   s    ddl m} ||| j| j d S )Nr   rf  )bjoernrg  r  r  )rW   r~  rg  r   r   r   rg    s    zBjoernServer.runNr  r   r   r   r   r    s   r  c                   @   s&   e Zd ZdZeeeeegZ	dd Z
dS )
AutoServerr  c              	   C   sF   | j D ]:}z || j| jf| j|W   S  tk
r>   Y qX qd S r'   )adaptersr  r  rY  rg  r   )rW   r~  sar   r   r   rg    s
    
 zAutoServer.runN)rY   rZ   r[   r   rz  r~  r  rs  rd  r  rg  r   r   r   r   r    s   r  )r  rb  r   r}  rx  r  Zfapws3r  ZgaeZtwistedZdieselr  Zgunicornr  r   ZgeventSocketIOr  r  r  c                 K   s   d| kr|  ddn| df\}} |tjkr2t| | s@tj| S |  rXttj| | S | dd }tj| ||< td|| f |S )a   Import a module or fetch an object from a module.

        * ``package.module`` returns `module` as a module object.
        * ``pack.mod:name`` returns the module variable `name` from `pack.mod`.
        * ``pack.mod:func()`` calls `pack.mod.func()` and returns the result.

        The last form accepts not only function calls, but any type of
        expression. Keyword arguments passed to this function are available as
        local variables. Example: ``import_string('re:compile(x)', x='[a-z]')``
    r  r"   Nr  r   %s.%s)rV  r#   r  r  isalnumrx   eval)r   r  r  package_namer   r   r   rq    s     
  
 rq  c              	   C   sD   dt  a }z$t }t| }t|r(|n|W S t| |a X dS )z Load a bottle application from a module and make sure that the import
        does not affect the current default application, but returns a separate
        application object. See :func:`load` for the target parameter. TN)NORUNdefault_appr>  r  rq  r_  )r   Znr_oldr  rw  r   r   r   load_app  s    

r  r  rX  c	              	   K   s  t rdS |rtjdszzd}
tjddd\}}
t	| tj|
rt
jgt
j }tj }d|d< |
|d< tj||d}| dkrt|
d t| q| d	kr@tj|
rt|
 t
|  q@W n tk
r   Y nX W 5 tj|
rt|
 X dS zf|dk	r(t| | p2t } t| trHt| } t| s^td
|  |pfg D ]}| | qh|tkrt|}t|trt|}t|t r|f ||d|	}t|t!std| |j"p||_"|j"s t#dt$t%|f  t#d|j&|j'f  t#d |rntjd}
t(|
|}| |)|  W 5 Q R X |j*dkrxt
d	 n
|)|  W nj tk
r   Y nV t+t,fk
r    Y n<   |s t-|d|st.  t| t
d	 Y nX dS )a   Start a server instance. This method blocks until the server terminates.

        :param app: WSGI application or target string supported by
               :func:`load_app`. (default: :func:`default_app`)
        :param server: Server adapter to use. See :data:`server_names` keys
               for valid names or pass a :class:`ServerAdapter` subclass.
               (default: `wsgiref`)
        :param host: Server address to bind to. Pass ``0.0.0.0`` to listens on
               all interfaces including the external one. (default: 127.0.0.1)
        :param port: Server port to bind to. Values below 1024 require root
               privileges. (default: 8080)
        :param reloader: Start auto-reloading server? (default: False)
        :param interval: Auto-reloader interval in seconds (default: 1)
        :param quiet: Suppress output to stdout and stderr? (default: False)
        :param options: Options passed to the server adapter.
     Nr  zbottle.z.lock)r   suffixtrueZBOTTLE_LOCKFILE)r   r   zApplication is not callable: %rr|  z!Unknown or unsupported server: %rz,Bottle v%s server starting up (using %s)...
zListening on http://%s:%d/
zHit Ctrl-C to quit.

reloadrZ  )/r  r  r   r   r   r  unlinktempfilemkstemprX   r#   
executableargvr  
subprocessPopenpollutimer`  sleepexitr  _debugr  rK   rU  r  r_  r   r3  server_namesrq  r  rW  rZ  r  __version__r   r  r  FileCheckerThreadrg  rG  r  r  rx   r   )r  r`  r  r  intervalreloaderrZ  r  r  rl  lockfilefdr   r   r   r  Zbgcheckr   r   r   rg    s     

 


 



 
rg  c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )r  zw Interrupt main-thread as soon as a changed module file is detected,
        the lockfile gets deleted or gets to old. c                 C   s$   t j|  || | _| _d | _d S r'   )rk  Threadrr   r  r  rG  )rW   r  r  r   r   r   rr   S  s    zFileCheckerThread.__init__c                 C   s   t jj}dd }t }ttj D ]H}t|ddp6d}|dd  dkrT|d d }|r$||r$||||< q$| j	s|| j
r|| j
t | j d k rd	| _	t  t| D ].\}}||r|||krd
| _	t   qqt| j qnd S )Nc                 S   s   t | jS r'   )r  r  r  r   r   r   r   r,   [  r&   z'FileCheckerThread.run.<locals>.<lambda>r{  r   )z.pyoz.pycrb  r  r   r  )r  r   r  rl   rj   r#   r  r  rx   rG  r  r`  r  threadinterrupt_mainr5  r  )rW   r  mtimer  r  r   Zlmtimer   r   r   rg  Y  s*      
zFileCheckerThread.runc                 C   s   |    d S r'   )r   rV   r   r   r   	__enter__o  s    zFileCheckerThread.__enter__c                 C   s&   | j sd| _ |   |d k	o$t|tS )Nr  )rG  r   r:  r  )rW   exc_typeexc_valexc_tbr   r   r   __exit__r  s     zFileCheckerThread.__exit__N)rY   rZ   r[   r   rr   rg  r  r  r   r   r   r   r  O  s
   r  c                   @   s   e Zd Zdd ZdS )TemplateErrorc                 C   s   t | d| d S )Nr|  )r   rr   )rW   rf   r   r   r   rr     s    zTemplateError.__init__N)rY   rZ   r[   rr   r   r   r   r   r    s   r  c                   @   sb   e Zd ZdZddddgZi Zi Zddg dfdd	Zeg fd
dZ	edd Z
dd Zdd ZdS )BaseTemplatez2 Base class and minimal API for template adapters tplhtmlZthtmlZstplNrJ   c                 K   s   || _ t|dr| n|| _t|dr.|jnd| _dd |D | _|| _| j | _| j	| | js| j r| 
| j | j| _| jstdt| | js| jstd| jf | j dS )a=   Create a new template.
        If the source parameter (str or buffer) is missing, the name argument
        is used to guess a template filename. Subclasses can assume that
        self.source and/or self.filename are set. Both are strings.
        The lookup, encoding and settings parameters are stored as instance
        variables.
        The lookup parameter stores a list containing directory paths.
        The encoding parameter should be used to decode byte strings or files.
        The settings parameter contains a dict for engine-specific settings.
        r  r  Nc                 S   s   g | ]}t j|qS r   )r  r   r  r  r   r   r   r     s     z)BaseTemplate.__init__.<locals>.<listcomp>zTemplate %s not found.zNo template specified.)r   r?   r  r  r  r  r4   settingsr  r  r  r  r   r  )rW   r  r   r  r4   r  r   r   r   rr     s    zBaseTemplate.__init__c                 C   s   |st d dg}tj|r>tj|r>t d tj|S |D ]}tj|tj }tjtj||}||szqBtj|r|  S | j	D ],}tjd||f rd||f     S qqBdS )z{ Search name in all directories specified in lookup.
        First without, then with common extensions. Return first hit. z2The template lookup path list should not be empty.r  z,Absolute template path names are deprecated.r  N)
rh   r  r   isabsr  r  r  r   r  
extensions)rz   r   r  Zspathr  extr   r   r   r    s     
  
zBaseTemplate.searchc                 G   s.   |r | j  | _ |d | j |< n
| j | S dS )zB This reads or sets the global settings stored in class.settings. r   N)r  r  )rz   rp   r   r   r   r   global_config  s    zBaseTemplate.global_configc                 K   s   t dS )z Run preparations (parsing, caching, ...).
        It should be possible to call this again to refresh a template or to
        update settings.
        NNotImplementedError)rW   rY  r   r   r   r    s    zBaseTemplate.preparec                 O   s   t dS )aF   Render the template with the specified local variables and return
        a single byte or unicode string. If it is a byte string, the encoding
        must match self.encoding. This method must be thread-safe!
        Local variables may be provided in dictionaries (args)
        or directly, as keywords (kwargs).
        Nr  )rW   r   rB  r   r   r   render  s    zBaseTemplate.render)rY   rZ   r[   r   r  r  defaultsrr   classmethodr  r  r  r  r   r   r   r   r    s   
r  c                   @   s   e Zd Zdd Zdd ZdS )MakoTemplatec                 K   s   ddl m} ddlm} |d| ji |dtt |f d| j	i|}| j
rl|| j
fd|i|| _n|f | j| j|d|| _d S )	Nr   Template)TemplateLookupr  Zformat_exceptionsdirectoriesr  )urir  r  )Zmako.templater  Zmako.lookupr  r  r4   r   r-  r   r  r  r  r   r  )rW   rY  r  r  r  r   r   r   r    s    zMakoTemplate.preparec                 O   s6   |D ]}| | q| j }| | | jjf |S r'   r  r  r  r  r  rW   r   rB  dictarg	_defaultsr   r   r   r    s
     

zMakoTemplate.renderNrY   rZ   r[   r  r  r   r   r   r   r    s   r  c                   @   s   e Zd Zdd Zdd ZdS )CheetahTemplatec                 K   sd   ddl m} t | _i | j_| jjg|d< | jrJ|f d| ji|| _n|f d| ji|| _d S )Nr   r  Z
searchListr  r  )	ZCheetah.Templater  rk  rl  r  varsr  r  r  )rW   rY  r  r   r   r   r    s    
zCheetahTemplate.preparec                 O   sL   |D ]}| | q| jj | j | jj | t| j}| jj  |S r'   )r  r  r  r  r   r  r  )rW   r   rB  r  r  r   r   r   r    s     
zCheetahTemplate.renderNr  r   r   r   r   r    s   
r  c                   @   s,   e Zd Zddi fddZdd Zdd ZdS )Jinja2TemplateNc                 K   s   ddl m}m} d|kr td|f d|| ji|| _|rL| jj| |r^| jj| |rp| jj	| | j
r| j| j
| _n| j| j| _d S )Nr   )EnvironmentFunctionLoaderr   zsThe keyword argument `prefix` has been removed. Use the full jinja2 environment name line_statement_prefix instead.loader)Zjinja2r  r  r  r  r   r   r  testsglobalsr  Zfrom_stringr  Zget_templater  )rW   r   r  r  rB  r  r  r   r   r   r    s       zJinja2Template.preparec                 O   s6   |D ]}| | q| j }| | | jjf |S r'   r  r  r   r   r   r  
  s
     

zJinja2Template.renderc              
   C   sH   |  || j}|sd S t|d}| | jW  5 Q R  S Q R X d S )Nr  )r  r  r  r  rR   r4   )rW   r   r  r   r   r   r   r    s
     zJinja2Template.loader)rY   rZ   r[   r  r  r  r   r   r   r   r    s   r  c                   @   sX   e Zd ZeddfddZedd Zedd Zdd	d
ZdddZ	dd Z
dd ZdS )SimpleTemplateFNc                    sJ   i | _ | j  fdd| _ fdd| _|| _|rF| j| j | _| _d S )Nc                    s
   t |  S r'   r<   r*   )rO   r   r   r,     r&   z(SimpleTemplate.prepare.<locals>.<lambda>c                    s   t |  S r'   r  r*   rO   escape_funcr   r   r,     r&   )r  r4   _str_escapesyntax)rW   r  Znoescaper  ra   r   r   r   r    s    zSimpleTemplate.preparec                 C   s   t | j| jpddS )Nz<string>rI   )r   r  r  rV   r   r   r   co"  s    zSimpleTemplate.coc              	   C   s   | j }|s*t| jd}| }W 5 Q R X zt|d }}W n, tk
rh   td t|dd }}Y nX t||| jd}|	 }|j
| _
|S )Nr  rJ   z;Template encodings other than utf8 are no longer supported.r3   )r4   r  )r  r  r  r  r<   r  rh   
StplParserr  	translater4   )rW   r  r   r4   r  r  r   r   r   r  &  s    zSimpleTemplate.codec                 K   s"   |d krt dd ||f|d< d S )NQRebase function called without arguments. You were probably looking for {{base}}?T_rebase)rh   )rW   _envr   rB  r   r   r   r	  6  s
    zSimpleTemplate._rebasec                 K   sZ   |d krt dd | }|| || jkrD| j|| jd| j|< | j| |d |S )Nr  Tr   r  _stdout)rh   r  r  r  r  r  execute)rW   r
  r   rB  r   r   r   r   _include<  s    

zSimpleTemplate._includec                 C   s   | j  }|| |||jt| j|t| j|d | j| j	|j
|j|jd
 t| j| |
dr|d\}}d||d< |d d = | j||f|S |S )N)
r  Z
_printlistincluderebaser	  r  r  r   r   Zdefinedr	  r   r  )r  r  r  extendr\   r)  r  r	  r  r  r   r   rE  r  r  r   r   )rW   r  rB  r   Zsubtplrargsr   r   r   r  F  s&    


    

zSimpleTemplate.executec                 O   s<   i }g }|D ]}| | q| | | || d|S )zA Render the template using keyword arguments as local variables. r   )r  r  r   )rW   r   rB  r   r(   r  r   r   r   r  V  s      
zSimpleTemplate.render)N)N)rY   rZ   r[   r  r  r   r  r  r	  r  r  r  r   r   r   r   r    s   	




r  c                   @   s   e Zd ZdS )StplSyntaxErrorNr   r   r   r   r   r  _  s    r  c                   @   s   e Zd ZdZi ZdZeddZed7 Zed7 Zed7 Zed7 Zed	7 Zed
7 Zed7 ZdZde Zde ZdZ	d$ddZ
dd Zdd ZeeeZdd Zdd Zdd Zdd Zd%d d!Zd"d# ZdS )&r  z Parser for stpl templates. z([urbURB]?(?:''(?!')|""(?!")|'{6}|"{6}|'(?:[^\\']|\\.)+?'|"(?:[^\\"]|\\.)+?"|'{3}(?:[^\\]|\\.|\n)+?'{3}|"{3}(?:[^\\]|\\.|\n)+?"{3}))z|\nr   z|(#.*)z|([\[\{\(])z|([\]\}\)])zW|^([ \t]*(?:if|for|while|with|try|def|class)\b)|^([ \t]*(?:elif|else|except|finally)\b)z?|((?:^|;)[ \t]*end[ \t]*(?=(?:%(block_close)s[ \t]*)?\r?$|;|#))z |(%(block_close)s[ \t]*(?=\r?$))z|(\r?\n)z8(?m)^[ 	]*(\\?)((%(line_start)s)|(%(block_start)s))(%%?)z6(?m)%%(inline_start)s((?:%s|[^'"
]*?)+)%%(inline_end)sz(?m)z<% %> % {{ }}NrJ   c                 C   sT   t ||| | _| _| |p | j g g  | _| _d\| _| _d\| _	| _
d| _d S )N)r"   r   )r   r   r   )r<   r  r4   
set_syntaxdefault_syntaxcode_buffertext_bufferlinenor   indent
indent_modparen_depth)rW   r  r  r4   r   r   r   rr     s    zStplParser.__init__c                 C   s   | j S )z= Tokens as a space separated string (default: <% %> % {{ }}) )_syntaxrV   r   r   r   
get_syntax  s    zStplParser.get_syntaxc                    s   || _ | | _|| jkrjd}ttj| j}tt| | | j	| j
| jf} fdd|D }|| j|< | j| \| _| _| _d S )Nz:block_start block_close line_start inline_start inline_endc                    s   g | ]}t |  qS r   )r   r   rF  Zpattern_varsr   r   r     s     z)StplParser.set_syntax.<locals>.<listcomp>)r  rV  _tokens	_re_cachemapr   r   rl   r3  	_re_split_re_tok_re_inlre_splitre_tokre_inl)rW   r  r  Zetokenspatternsr   r  r   r    s    


zStplParser.set_syntaxc                 C   s  | j rtd| j| j| j d  }|rX| j| j | j |   }| j| |  j | 7  _ |	dr| j| j d  
d\}}}| j|	d|	d | |  |  j t|| d 7  _ qnf|	dr2td | j| j d  
d\}}}| j|	d| |  |  j t|| d 7  _ q|   | jt|	dd qqXq| j| j| j d   |   d	| jS )
NzParser is a one time instance.r"   r  r    r  z#Escape code lines with a backslash.r   )	multiliner   )r   r  r%  r  r  r   r  r   r   r   r  r   rh   
flush_text	read_coder-  r   r  )rW   r   r   liner  r   r   r   r   r    s0     
$zStplParser.translatec              	   C   s  d\}}| j | j| jd  }|sX|| j| jd  7 }t| j| _| | | d S || j| j| j|   7 }|  j| 7  _|	 \	}}}}}	}
}}}|s| j
dkr|	s|
r||	p|
7 }q|r||7 }q|r|}|r| | jd rd}q|r|  j
d7  _
||7 }q|rB| j
dkr8|  j
d8  _
||7 }q|	rd|	d }| _|  jd7  _q|
rx|
d }| _q|r|  jd8  _q|r|rd}n||7 }q| | | |  jd7  _d\}}| _|sqqd S )N)r   r   r   r"   Frb  )r   r   r   )r&  r  r  r   r   
write_coderj  r   r   r   r  rX  r  r  r  r  )rW   r)  Z	code_linecommentr   r  Z_comZ_poZ_pcZ_blk1Z_blk2Z_endZ_cendZ_nlr   r   r   r+    sT    


 
zStplParser.read_codec           	   
   C   sr  d | j}| jd d = |s d S g ddd| j    }}}| j|D ]r}|||  |  }}|r|| tt	|
d |dr|d  |7  < || |d  qF|t|k r>||d  }|
d}|d d	r|d d d
 |d< n$|d dr(|d d d |d< || tt	| dd | }|  j|dd 7  _| | d S )Nr   r   z\
  Tr  rb  r"   z\\
z\\
r  z_printlist((%s,))rY  )r   r  r  r'  r   r   r   r   r!  r   
splitlinesrX  process_inliner   rj  r   r  countr-  )	rW   r   partsposnlr   r   linesr  r   r   r   r*    s.     
 
  zStplParser.flush_textc                 C   s$   |d dkrd|dd   S d| S )Nr   r7  z_str(%s)r"   z_escape(%s)r   )rW   chunkr   r   r   r2    s     zStplParser.process_inlinec                 C   sD   |  ||\}}d| j| j  }|| | d 7 }| j| d S )Nr/  r  )fix_backward_compatibilityr  r  rk  r  r   )rW   r,  r.  r  r   r   r   r-    s    zStplParser.write_codec                 C   s   |  d d}|rh|d dkrhtd t|dkr<d|fS t|dkrXdt| |fS dt| |fS | jdkr|  sd	|krtd
|}|rtd |d}| j	
| j|| _	|| _||d	dfS ||fS )Nr    r   )r  r  z2The include and rebase keywords are functions now.r"   z_printlist([base])z_=%s(%r)z_=%s(%r, %s)Zcodingz#.*coding[:=]\s*([-\w.]+)z4PEP263 encoding strings in templates are deprecated.zcoding*)rj  rV  rh   r   ri   r  r   r   r   r  rM   r4   rR   r&  )rW   r,  r.  r4  r   rO   r   r   r   r9    s"      
z%StplParser.fix_backward_compatibility)NrJ   )r   )rY   rZ   r[   r   r   r#  r&  r$  r"  r  rr   r  r  r$  r  r  r+  r*  r2  r-  r9  r   r   r   r   r  b  s2   

/
r  c                  O   s  | r| d nd}| dt}| dt}t||f}|tks@tr| di }t||rt|t|< |rt| jf | nRd|ksd|ksd|ksd	|kr|f ||d
|t|< n|f ||d|t|< t| stdd|  | dd D ]}|	| qt| 
|S )z
    Get a rendered template as a string iterator.
    You can use a name, a filename or a template string as first parameter.
    Template rendering arguments can be passed as dictionaries
    or directly (as keyword arguments).
    r   Ntemplate_adapterZtemplate_lookupZtemplate_settingsr  {%$)r  r  r  r|  zTemplate (%s) not foundr"   )r   r  TEMPLATE_PATHid	TEMPLATESr   rK   r  r  r  r  )r   rB  r  adapterr  Ztplidr  r  r   r   r   r    s$    
   r  )r:  c                    s    fdd}|S )a   Decorator: renders a template for a handler.
        The handler can control its behavior like that:

          - return a dict of template vars to fill out the template
          - return something other than a dict and the view decorator will not
            process the template, but return the handler result as is.
            This includes returning a HTTPResponse(dict) to get,
            for instance, JSON with autojson or other castfilters.
    c                    s   t   fdd}|S )Nc                     sL   | |}t |ttfr6  }|| tf|S |d krHt S |S r'   )rK   rl   rF   r  r  r  )r   rB  resultZtplvars)r  rw   tpl_namer   r   r_   @  s    


z(view.<locals>.decorator.<locals>.wrapper)r\   rU  rR  r  rC  rC  r   rE  ?  s    	zview.<locals>.decoratorr   )rC  r  rE  r   rD  r   ry  5  s    
ry  r  z./views/zI'm a teapoti  zUnprocessable Entityi  zPrecondition Requiredi  zToo Many Requestsi  zRequest Header Fields Too Largei  zNetwork Authentication Requiredi  c                 c   s"   | ]\}}|d ||f fV  qdS )z%d %sNr   r7  r   r   r   r   h  s     r   a  
%%try:
    %%from %s import DEBUG, HTTP_CODES, request, touni
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html>
        <head>
            <title>Error: {{e.status}}</title>
            <style type="text/css">
              html {background-color: #eee; font-family: sans;}
              body {background-color: #fff; border: 1px solid #ddd;
                    padding: 15px; margin: 15px;}
              pre {background-color: #eee; border: 1px solid #ddd; padding: 5px;}
            </style>
        </head>
        <body>
            <h1>Error: {{e.status}}</h1>
            <p>Sorry, the requested URL <tt>{{repr(request.url)}}</tt>
               caused an error:</p>
            <pre>{{e.body}}</pre>
            %%if DEBUG and e.exception:
              <h2>Exception:</h2>
              <pre>{{repr(e.exception)}}</pre>
            %%end
            %%if DEBUG and e.traceback:
              <h2>Traceback:</h2>
              <pre>{{e.traceback}}</pre>
            %%end
        </body>
    </html>
%%except ImportError:
    <b>ImportError:</b> Could not generate the error page. Please add bottle to
    the import path.
%%end
z
bottle.extz.extz	bottle_%sz
Bottle %s
z"
Error: No application specified.
r  r  	localhostr  ]z[])r  r  r`  r  r  r  )rJ   )rJ   rQ   )F)N)r|  r  )N)r  )r  FrV  )T)r   )r"   )rM  rN  )	Nr   r  rX  r"   FFNN(  r   
__future__r   
__author__r  __license__rY   optparser   Z_cmd_parser
add_optionZ_opt
parse_argsZ_cmd_optionsZ	_cmd_argsr`  r  Zgevent.monkeyr   ZmonkeyZ	patch_allr%  r  email.utilsr  r\   r:  rQ  r  r  r   r  r#   r  rk  r`  rc   r<  r   r   r^  r   r   rt  r   r   inspectr   unicodedatar   Z
simplejsonr   r   r   r;   r   r  Zdjango.utils.simplejsonversion_infopyr  Zpy25r  r%   r(   r)   r-   r  r  r  http.clientclienthttplib_threadr  urllib.parser.   r/   r  r0   r1   r  r2   r-  r)  http.cookiesr5   collections.abcr6   rF   typesr7   r8   collectionsimpr9  ior9   configparserr:   r   rU  rL   r  r_  r!  rC   rB   urlparseurllibCookiecPicklerD   rE   r   rd   re   UserDictrG   rN   r  r   rP   r<   r  rT   rU   r]   rh   rm   r  rn   r   r   r  r   r   r   r   r   r   r   r   r  r%  r  r'  r)  r*  r-  rm  rn  ro  r  ResponserP  r   ru  r4  r5  rz  r  r  rC  r  rl   r	  rj   r  r  r  r.  r  r  r  r  r  r  rU  rT  r
  r  r  r4  r[  r  r@  r  rH  rr  rO  rT  rV  rW  r   rw  ry  r{  r   r[  r@  r3  rd  r   rW  r[  r_  rd  rs  rz  r~  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rq  r  r  rg  r  r  r  r  r  r  r  r  r  r  r  Zmako_templateZcheetah_templateZjinja2_templatery  Z	mako_viewZcheetah_viewZjinja2_viewr>  r@  r   r  	responsesZ
HTTP_CODESr5  r@  r  rN  r  rl  r  r  r  r  r  optr   r  versionr  
print_helpr   r<  r  r   r  r  r  rfindr  rj  r   r  r  r   r   r   r   <module>   s6  
 
   

	 Iw   #    c
$I.2 NVL
Q	


	
         \1OH 6!&(

0  