????

Your IP : 3.144.92.138


Current Path : /lib/python3.6/site-packages/__pycache__/
Upload File :
Current File : //lib/python3.6/site-packages/__pycache__/bottle.cpython-36.pyc

3

��b�R�9@s�
dZddlmZdZdZdZedkr�ddlmZedd	�Z	e	j
Zed
ddd
�eddddd�eddddd�eddddd
�edddd
�edddd
�e	j�\Z
Ze
jr�e
jjd �r�dd!lZejj�dd!lZdd!lZdd!lZdd!lZdd!lZdd!lZdd!lZdd!lZdd!lZdd!lZdd!l Z dd!l!Z!dd!l"Z"dd!l#Z#dd!l$Z$dd!l%Z%dd"l&m'Z(m&Z&m)Z)dd#l!m*Z*dd$l+m,Z,m-Z-dd%l.m/Z/ydd&l0m1Z1d'd(�Z2Wn"e3k
�r�dd)l0m2Z2YnXydd*l4m5Z6m7Z8Wnze3k
�rPydd*l9m5Z6m7Z8WnNe3k
�rJydd*l:m5Z6m7Z8Wn"e3k
�rDd+d,�Z6e6Z8YnXYnXYnXe j;Z<e<�dJkZ=e<�dKkZ>�dLe<k�o��dMknZ?d1d2�Z@ye jAjBe jCjBZDZEWn&eFk
�r�d3d4�ZDd5d4�ZEYnXe=�r�dd!lGjHZIdd!lJZKdd6lLmMZMmNZOdd7lLmPZPmQZRmSZTejUeTd8d9�ZTdd:lVmWZWe<�dNk�rLdd;lXmYZZdd<l[m\Z]ndd;l^mYZZdd=l_m]Z]dd!l`Z`dd>lambZbdd?lcmdZdeeZfeeZgd@d4�ZhdAd4�ZiejZkdBdC�Zln�dd!lIZIdd!lKZKdd6lmmMZMmNZOdd7lnmPZPmQZRmSZTdd:lomWZWddDlmkZkdd!lpZ`dd=l_m]Z]ddElqmqZbddFldmrZde>�rRdGZse$jteseu�ddHlvmZZZdIdJ�ZweeZxndd;l^mYZZegZge8ZheyezdKdLdM���dOdOdP�Z{�dPdRdS�Z|e=�r�e|ne{Z}e?�r�ddTlam~Z~GdUdV�dVe~�ZdWdX�Z��dQdZd[�Z�d\d]�Z�Gd^d_�d_e��Z�Gd`da�dae��Z�Gdbdc�dce��Z�Gddde�dee��Z�Gdfdg�dge��Z�Gdhdi�die��Z�Gdjdk�dke��Z�Gdldm�dme��Z�Gdndo�doe��Z�dpdq�Z�Gdrds�dse��Z�Gdtdu�due��Z�Gdvdw�dwe��Z�Gdxdy�dye��Z�dzd{�Z�d|d}�Z�Gd~d�de��Z�Gd�d��d�e��Z��dRd�d��Z�Gd�d��d�e��Z�Gd�d��d�e��Z�e�Z�e�Z�Gd�d��d�e�e��Z�Gd�d��d�e��Z�Gd�d��d�e��Z�Gd�d��d�e��Z�Gd�d��d�e��Z�Gd�d��d�e��Z�Gd�d��d�eZ�Z�Gd�d��d�e��Z�Gd�d��d�e��Z�Gd�d��d�eZ�Z�Gd�d��d�e��Z�Gd�d��d�e��Z�Gd�d��d�e��Z�Gd�d��d�e��Z�Gd�d��d�e��Z�Gd�d��d�e��Z��dSd�d��Z��dTd�d��Z��dVd�d��Z��dWd�d��Z��dXd�d��Z�d�d��Z�d�d��Z�d�d��Z��dYd�d��Z�d�d��Z�d�dÄZ�d�dńZ�d�dDŽZ�d�dɄZ�d�d˄Z�d�d̈́Z�d�dτZ��dZd�dфZ��d[d�dՄZ�d�dׄZ�e�d؃Z�e�dكZ�e�dڃZ�e�dۃZ�e�d܃Z�e�d݃Z�e�dރZ�e�d߃Z�e�d�Z�e�d�Z�e�d�Z�Gd�d�d�e��Z�Gd�d�d�e̓Z�Gd�d�d�e̓Z�Gd�d�d�e̓Z�Gd�d�d�e̓Z�Gd�d�d�e̓Z�Gd�d�d�e̓Z�Gd�d�d�e̓Z�Gd�d�d�e̓Z�Gd�d��d�e̓Z�Gd�d��d�e̓Z�Gd�d��d�e̓Z�Gd�d��d�e̓Z�Gd�d��d�e̓Z�Gd��d��de̓Z�G�d�d��de̓Z�G�d�d��de̓Z�G�d�d��de̓Z�G�d�d��de̓Z�G�d	�d
��d
e̓Z�G�d�d��de̓Z�e�e�e�e�e�e�e�e�e�e�e�e�e�e�e�e�e�e�e�e�d
�Z�d�d�Z�d�d�Z�e�Z�d\�d�d�Z�G�d�d��de"j�Z�G�d�d��de��Z�G�d�d��de��Z�G�d�d��de�Z�G�d�d��de�Z�G�d �d!��d!e�Z�G�d"�d#��d#e�Z�G�d$�d%��d%e�Z�G�d&�d'��d'e��Z�d(�d)�Z�ejUe�e�d*�Z�ejUe�e�d*�Z�ejUe�e�d*�Z�d+�d,�Z�ejUe�e�d*�Z�ejUe�e�d*�Z�ejUe�e�d*�Z��d-�d.gZ�iZ�dYa�dYa�eIj�Z��d/e��d0<�d1e��d2<�d3e��d4<�d5e��d6<�d7e��d8<�d9e��d:<e��d;�d<�e�j��D���Z�d=e�Ze���Ze���Ze"�j��Ze���Z�Z�e�j�e�edk�r�d>ne�d?�d@��j�Z	edk�
r�e
ee	�Z
�Z�Z�e
�j
�rveD�dAe�e �jd��e�s��e�j�eE�dB�e �jd0�e �j�jd�dC�e �j�j�dDe �jd��e
�j�p�dE�d�Z�Z�dF�ek�
r8�e�j�dG��e�j�dF�k�
r8�e�j�dFd0�\�Z�Z�e�j�dH��Ze�ed�e�e�e��e
j�e
�j�e
�j�e
j��dI�d!S(]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_statementzMarcel Hellkampz0.12.21ZMIT�__main__)�OptionParserz)usage: %prog [options] package.module:app)Zusagez	--version�
store_truezshow version number.)�action�helpz-bz--bindZADDRESSzbind socket to ADDRESS.)�metavarrz-sz--server�wsgirefzuse SERVER as backend.)�defaultrz-pz--plugin�appendzinstall additional plugin/s.z--debugzstart server in debug mode.z--reloadzauto-reload on file changes.�geventN)�date�datetime�	timedelta)�
TemporaryFile)�
format_exc�	print_exc)�	normalize)�	signaturecCs�t|�j}gddgf\}}}}xZ|j�D]N\}}|j|jkrB|}q(|j|jkrT|}q(|j|�|j|jk	r(|j|j�q(W|||t	|�p�|fS)N)
rZ
parameters�itemsZkindZVAR_POSITIONALZVAR_KEYWORDrr
�empty�tuple)�func�params�argsZvarargs�keywords�defaults�nameZparam�r�/usr/lib/python3.6/bottle.py�
getargspec1s

r )r )�dumps�loadscCstd��dS)Nz/JSON support requires Python 2.6 or simplejson.)�ImportError)�datarrr�
json_dumpsGsr%����cCstj�dS)Nr))�sys�exc_inforrrr�_eVsr,cCstjj|�S)N)r*�stdout�write)�xrrr�<lambda>]sr0cCstjj|�S)N)r*�stderrr.)r/rrrr0^s)�urljoin�SplitResult)�	urlencode�quote�unquote�latin1)�encoding)�SimpleCookie)�MutableMapping)�
ModuleType)�
new_module)�BytesIO)�ConfigParsercCstt|��S)N)�json_lds�touni)�srrrr0sscCs
t|d�S)N�__call__)�hasattr)r/rrrr0tscGs|d|d�j|d��dS)Nrr)r')�with_traceback)�arrr�_raisevsrF)�imap)�StringIO)�SafeConfigParserz?Python 2.5 support may be dropped in future versions of Bottle.)�	DictMixincCs|j�S)N)�next)�itrrrrK�srKz&def _raise(*a): raise a[0], a[1], a[2]z<py3fix>�exec�utf8cCst|t�r|j|�St|�S)N)�
isinstance�unicode�encode�bytes)rA�encrrr�tob�srT�strictcCst|t�r|j||�St|�S)N)rOrR�decoderP)rArS�errrrrr@�sr@)�
TextIOWrapperc@seZdZdd�ZdS)�NCTextIOWrappercCsdS)Nr)�selfrrr�close�szNCTextIOWrapper.closeN)�__name__�
__module__�__qualname__r[rrrrrY�srYcOs2ytj||f|�|�Wntk
r,YnXdS)N)�	functools�update_wrapper�AttributeError)�wrapper�wrappedrE�karrrr`�sr`FcCstj|tdd�dS)Nr&)�
stacklevel)�warnings�warn�DeprecationWarning)�messageZhardrrr�depr�srjcCs,t|ttttf�rt|�S|r$|gSgSdS)N)rOr�list�set�dict)r$rrr�makelist�s
rnc@s:eZdZdZddd�Zdd�Zdd	�Zd
d�Zdd
�ZdS)�DictPropertyz= Property that maps to a key in a local dict-like attribute. NFcCs||||_|_|_dS)N)�attr�key�	read_only)rZrprqrrrrr�__init__�szDictProperty.__init__cCs*tj||gd�||jp|j|_|_|S)N)�updated)r_r`rqr\�getter)rZrrrrrB�szDictProperty.__call__cCs>|dkr|S|jt||j�}}||kr6|j|�||<||S)N)rq�getattrrpru)rZ�obj�clsrqZstoragerrr�__get__�szDictProperty.__get__cCs$|jrtd��|t||j�|j<dS)NzRead-Only property.)rrrarvrprq)rZrw�valuerrr�__set__�szDictProperty.__set__cCs"|jrtd��t||j�|j=dS)NzRead-Only property.)rrrarvrprq)rZrwrrr�
__delete__�szDictProperty.__delete__)NF)	r\r]r^�__doc__rsrBryr{r|rrrrro�s
roc@s eZdZdZdd�Zdd�ZdS)�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. cCst|d�|_||_dS)Nr})rvr}r)rZrrrrrs�szcached_property.__init__cCs(|dkr|S|j|�}|j|jj<|S)N)r�__dict__r\)rZrwrxrzrrrry�szcached_property.__get__N)r\r]r^r}rsryrrrrr~�sr~c@s eZdZdZdd�Zdd�ZdS)�lazy_attributez4 A property that caches itself to the class object. cCstj||gd�||_dS)N)rt)r_r`ru)rZrrrrrs�szlazy_attribute.__init__cCs|j|�}t||j|�|S)N)ru�setattrr\)rZrwrxrzrrrry�s
zlazy_attribute.__get__N)r\r]r^r}rsryrrrrr��sr�c@seZdZdZdS)�BottleExceptionz- A base class for exceptions used by bottle. N)r\r]r^r}rrrrr��sr�c@seZdZdZdS)�
RouteErrorz9 This is a base class for all routing related exceptions N)r\r]r^r}rrrrr��sr�c@seZdZdZdS)�
RouteResetzf If raised by a plugin or request handler, the route is reset and all
        plugins are re-applied. N)r\r]r^r}rrrrr��sr�c@seZdZdS)�RouterUnknownModeErrorN)r\r]r^rrrrr�sr�c@seZdZdZdS)�RouteSyntaxErrorz@ The route parser found something not supported by this router. N)r\r]r^r}rrrrr�sr�c@seZdZdZdS)�RouteBuildErrorz The route could not be built. N)r\r]r^r}rrrrr�
sr�cCsd|kr|Stjddd�|�S)z^ Turn all capturing groups in a regular expression pattern into
        non-capturing groups. �(z(\\*)(\(\?P<[^>]+>|\((?!\?))cSs*t|jd��dr|jd�S|jd�dS)Nr)r'rz(?:)�len�group)�mrrrr0sz_re_flatten.<locals>.<lambda>)�re�sub)�prrr�_re_flattensr�c@sbeZdZdZdZdZdZddd�Zdd	�Ze	j
d
�Zdd�Zddd�Z
dd�Zdd�Zdd�Zd
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��cFcsRg�_i�_i�_i�_i�_i�_|�_�fdd�dd�dd�dd�d��_dS)Ncst|p
�j�ddfS)N)r��default_pattern)�conf)rZrrr03sz!Router.__init__.<locals>.<lambda>cSsdtdd�fS)Nz-?\d+cSstt|��S)N)�str�int)r/rrrr05sz3Router.__init__.<locals>.<lambda>.<locals>.<lambda>)r�)r�rrrr05scSsdtdd�fS)Nz-?[\d.]+cSstt|��S)N)r��float)r/rrrr06sz3Router.__init__.<locals>.<lambda>.<locals>.<lambda>)r�)r�rrrr06scSsdS)N�.+?)r�NNr)r�rrrr07s)r�r�r��path)�rules�_groups�builder�static�dyna_routes�dyna_regexes�strict_order�filters)rZrUr)rZrrs)s
zRouter.__init__cCs||j|<dS)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�)rZrrrrr�
add_filter9szRouter.add_filterz�(\\*)(?:(?::([a-zA-Z_][a-zA-Z_0-9]*)?()(?:#(.*?)#)?)|(?:<([a-zA-Z_][a-zA-Z_0-9]*)?(?::([a-zA-Z_]*)(?::((?:\\.|[^\\>]+)+)?)?)?>))c	cs�d\}}x�|jj|�D]�}||||j��7}|j�}t|d�drn||jd�t|d�d�7}|j�}q|r~|ddfV|ddkr�|dd�n
|dd�\}}}||p�d|p�dfV|j�d}}qW|t|�ks�|r�|||d�ddfVdS)	Nr�r'��r)r
)rr�)�rule_syntax�finditer�start�groupsr�r��end)	rZ�rule�offset�prefix�match�grZfiltrr�rrr�_itertokensDs*zRouter._itertokensNc
s>d}g}d}g�g}d}	x�|j|�D]�\}
}}|r�d}	|dkrD|j}|j||�\}
}}|
sz|d|
7}d|}
|d7}n|d	|
|
f7}|j|
�|r��j|
|f�|j|
|p�tf�q$|
r$|tj|
�7}|jd
|
f�q$W||j|<|r�||j|<|	�r0|j�r0|j	j
|i�|d
f|j	||j|�<d
Sytjd|�}|j
�Wn*tjk
�rrtd|t�f��YnX��r���fd
d�}n|j�r��fdd�}nd
}t|�}||||f}||f|jk�rt�r�d}tj|||ft�||j||j||f<n0|jj
|g�j|�t|j|�d|j||f<|j|�d
S)z< Add a new rule or replace the target for an existing rule. rr�TFr
z(?:%s)zanon%dr)z
(?P<%s>%s)Nz^(%s)$zCould not add Route: %s (%s)csV�|�j�}xD�D]<\}}y|||�||<Wqtk
rLtdd��YqXqW|S)Ni�zPath has wrong format.)�	groupdict�
ValueError�	HTTPError)r��url_argsrZwildcard_filter)r��re_matchrr�getargsszRouter.add.<locals>.getargscs�|�j�S)N)r�)r�)r�rrr��sz3Route <%s %s> overwrites a previously defined route)r��default_filterr�rr�r��escaper�r�r��
setdefault�build�compiler��errorr�r,�
groupindexr�r��DEBUGrfrg�RuntimeWarningr�r��_compile)rZr��method�targetr�anons�keys�patternr�Z	is_staticrq�moder��maskZ	in_filterZ
out_filterZ
re_patternr��flatpatZ
whole_rule�msgr)r�r�r�addUsf




z
Router.addc	Cs�|j|}g}|j|<|j}xrtdt|�|�D]^}||||�}dd�|D�}djdd�|D��}tj|�j}dd�|D�}|j	||f�q0WdS)Nrcss|]\}}}}|VqdS)Nr)�.0�_r�rrr�	<genexpr>�sz"Router._compile.<locals>.<genexpr>�|css|]}d|VqdS)z(^%s$)Nr)r�r�rrrr��scSsg|]\}}}}||f�qSrr)r�r�r�r�rrr�
<listcomp>�sz#Router._compile.<locals>.<listcomp>)
r�r��_MAX_GROUPS_PER_PATTERN�ranger��joinr�r�r�r)	rZr�Z	all_rulesZ
comborulesZ	maxgroupsr/Zsome�combinedr�rrrr��s
zRouter._compilecs�|jj|�}|std|��yRx t|�D]\}}|�d|<q&Wdj�fdd�|D��}�s^|S|dt��Stk
r�tdt�jd��YnXd	S)
z2 Build an URL by filling the wildcards in a rule. zNo route with that name.zanon%dr�cs&g|]\}}|r|�j|��n|�qSr)�pop)r��n�f)�queryrrr��sz Router.build.<locals>.<listcomp>�?zMissing URL argument: %rrN)	r��getr��	enumerater�r4�KeyErrorr,r)rZ�_namer�r�r��irz�urlr)r�rr��s
zRouter.buildcCs�|dj�}|dpd}d}|dkr2d|ddg}n
d|dg}x�|D]�}||jkr�||j|kr�|j||\}}||r~||�nifS||jkrBxH|j|D]:\}}	||�}
|
r�|	|
jd	\}}||r�||�nifSq�WqBWtg�}t|�}x.t|j�|D]}||j|kr�|j|�q�WxNt|j�||D]8}x0|j|D]"\}}	||�}
|
�r@|j|��q@W�q0W|�r�d
jt|��}
tdd|
d
��tddt	|���dS)zD Return a (target, url_agrs) tuple or raise HTTPError(400/404/405). �REQUEST_METHOD�	PATH_INFO�/N�HEAD�PROXY�GET�ANYr)�,i�zMethod not allowed.)�Allowi�zNot found: )
�upperr�r��	lastindexrlr�r��sortedr��repr)rZ�environ�verbr�r��methodsr�r�r�r�r�ZallowedZnocheckZallow_headerrrrr��s<


zRouter.match)F)N)r\r]r^r}r�r�r�rsr�r�r�r�r�r�r�r�r�rrrrr�s



Fr�c@s|eZdZdZddd�Zdd�Zedd��Zd	d
�Zdd�Z	e
d
d��Zdd�Zdd�Z
dd�Zdd�Zddd�Zdd�ZdS)�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	KsL||_||_||_||_|pd|_|p(g|_|p2g|_t�j|dd�|_	dS)NT)�make_namespaces)
�appr�r��callbackr�plugins�skiplist�
ConfigDict�	load_dict�config)	rZr�r�r�r�rr�r�r�rrrrs�s


zRoute.__init__cOstd�|j||�S)Nz�Some APIs changed to return Route() instances instead of callables. Make sure to use the Route.call method and not to call Route instances directly.)rj�call)rZrErdrrrrB�szRoute.__call__cCs|j�S)z� The route callback with all plugins applied. This property is
            created on demand and then cached to speed up subsequent requests.)�_make_callback)rZrrrr��sz
Route.callcCs|jjdd�dS)zk Forget any cached values. The next time :attr:`call` is accessed,
            all plugins are re-applied. r�N)rr�)rZrrr�resetszRoute.resetcCs
|jdS)z: Do all on-demand work immediately (useful for debugging).N)r�)rZrrr�prepare
sz
Route.preparec
Cs0td�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�rr�r��apply�skip)
rjrmr�r�r�rr�r�r�r�)rZrrr�_contextszRoute._contextccs�t�}xxt|jj|j�D]b}d|jkr*Pt|dd�}|rN||jks||krNq||jkst|�|jkrhq|rv|j|�|VqWdS)z) Yield all Plugins affecting this route. TrFN)rl�reversedr�r�r�rv�typer�)rZ�uniquer�rrrr�all_pluginss

zRoute.all_pluginscCs�|j}x�|j�D]v}yBt|d�rLt|dd�}|dkr8|n|j}|j||�}n||�}Wntk
rn|j�SX||jk	rt||j�qW|S)Nr��apir))	r�rrCrvr�r�r�r�r`)rZr��pluginr�contextrrrr� s


zRoute._make_callbackcCsT|j}t|trdnd|�}tr"dnd}x(t||�rNt||�rNt||�dj}q(W|S)zq Return the callback. If the callback is a decorated function, try to
            recover the original function. �__func__Zim_func�__closure__Zfunc_closurer)r�rv�py3krC�
cell_contents)rZrZclosure_attrrrr�get_undecorated_callback0szRoute.get_undecorated_callbackcCst|j��dS)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	)rZrrr�get_callback_args:szRoute.get_callback_argscCs,x&|j|jjfD]}||kr||SqW|S)zp Lookup a config field and return its value, first checking the
            route.config, then route.app.config.)r�r�Zconifg)rZrqr
r�rrr�
get_config@szRoute.get_configcCs|j�}d|j|j|fS)Nz
<%s %r %r>)r	r�r�)rZ�cbrrr�__repr__GszRoute.__repr__)NNN)N)r\r]r^r}rsrBr~r�r�r��propertyr�rr�r	r
rr
rrrrr��s


r�c@seZdZdZdDdd�Zedd�ZdEZdZe	d
d��Z
dd
�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�ZdFdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�ZdGd*d+�ZdHd,d-�ZdId/d0�ZdJd2d3�ZdKd5d6�ZdLd8d9�Zd:d;�Zd<d=�Z dMd>d?�Z!d@dA�Z"dBdC�Z#dS)N�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.
    TcCs�t�|_tj|jd�|j_|jjddt�|jjddt�||jd<||jd<t�|_	g|_
t�|_i|_
g|_|jdr�|jt��|jt��dS)Nr��autojsonZvalidate�catchall)r�r�r_�partial�trigger_hook�
_on_change�meta_set�bool�ResourceManagerZ	resources�routesr��router�
error_handlerr��install�
JSONPlugin�TemplatePlugin)rZrrrrrrs^s


zBottle.__init__r�r�before_request�
after_request�	app_resetcCstdd�|jD��S)Ncss|]}|gfVqdS)Nr)r�rrrrr�}sz Bottle._hooks.<locals>.<genexpr>)rm�_Bottle__hook_names)rZrrr�_hooks{sz
Bottle._hookscCs2||jkr|j|jd|�n|j|j|�dS)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.
        rN)�_Bottle__hook_reversedr"�insertr)rZrrrrr�add_hooks
zBottle.add_hookcCs0||jkr,||j|kr,|j|j|�dSdS)z  Remove a callback from a hook. TN)r"�remove)rZrrrrr�remove_hook�szBottle.remove_hookcs"��fdd�|j|dd�D�S)z. Trigger a hook and return a list of results. csg|]}|����qSrr)r��hook)r�kwargsrrr��sz'Bottle.trigger_hook.<locals>.<listcomp>N)r")rZZ
_Bottle__namerr)r)rr)rr�szBottle.trigger_hookcs��fdd�}|S)ze Return a decorator that attaches a callback to a hook. See
            :meth:`add_hook` for details.cs�j�|�|S)N)r%)r)rrZrr�	decorator�szBottle.hook.<locals>.decoratorr)rZrr*r)rrZrr(�szBottle.hookcs�t�t�rtdd�dd�|jd�D�}|s4td��t|����fdd�}|jd	d�|jd
d�|jd|�d
��||d<|jddj|�f|�|j	d�s�|jddj|�f|�dS)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.TcSsg|]}|r|�qSrr)r�r�rrrr��sz Bottle.mount.<locals>.<listcomp>r�zEmpty path prefix.cshzTtj��tg��d�fdd�	}�tj|�}|rF�jrFtj�j|�}|pN�j�_�Stj��XdS)NcsD|rzt|�Wdd}X|�_x|D]\}}�j||�q$W�jjS)N)rF�status�
add_header�bodyr)r+�
headerlistr+rrz)�rsrr�start_response�sz@Bottle.mount.<locals>.mountpoint_wrapper.<locals>.start_response)N)�request�
path_shift�HTTPResponser�r-�	itertools�chain)r0r-)r��
path_depth)r/r�mountpoint_wrapper�s
	
z(Bottle.mount.<locals>.mountpoint_wrapperr�r�r�Z
mountpoint)r�r�r�z/%s/<:re:.*>N)
rO�
basestringrj�splitr�r�r��router��endswith)rZr�r��optionsZsegmentsr7r)r�r6r�mount�s


zBottle.mountcCs,t|t�r|j}x|D]}|j|�qWdS)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)rOrr�	add_route)rZrr:rrr�merge�s

zBottle.mergecCsJt|d�r|j|�t|�r2t|d�r2td��|jj|�|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())rCr@�callable�	TypeErrorr�rr�)rZrrrrr�s

zBottle.installcCs�g|}}xttt|j��ddd�D]X\}}|dksX||ksX|t|�ksXt|dd�|kr$|j|�|j|=t|d�r$|j�q$W|r�|j�|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. Nr)Trr[���)	rkr�r�r�rvrrCr[r�)rZrZremovedr&r�rrr�	uninstall�s
"

zBottle.uninstallNcCsl|dkr|j}nt|t�r"|g}n|j|g}x|D]}|j�q4Wtr^x|D]}|j�qNW|jd�dS)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. Nr )rrOr�r�r�r�r)rZr:rrrrr��s


zBottle.resetcCs,x |jD]}t|d�r|j�qWd|_dS)z2 Close the application and all installed plugins. r[TN)r�rCr[Zstopped)rZrrrrr[s
zBottle.closecKst|f|�dS)z- Calls :func:`run` with the same parameters. N)�run)rZr)rrrrEsz
Bottle.runcCs|jj|�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.)rr�)rZr�rrrr�szBottle.matchcKs>tjjdd�jd�d}|jj|f|�jd�}ttd|�|�S)z, Return a string that matches a named route �SCRIPT_NAMEr�r�)r1r�r��striprr��lstripr2)rZZ	routename�kargsZ
scriptname�locationrrr�get_urlszBottle.get_urlcCs6|jj|�|jj|j|j||jd�tr2|j�dS)zS Add a route object, but do not change the :data:`Route.app`
            attribute.)rN)	rrrr�r�r�rr�r�)rZr:rrrr>szBottle.add_router�c	sJt��rd��}t|��t|���������fdd�}|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`).
        Ncsrt|t�rt|�}xZt��p"t|�D]F}x@t��D]4}|j�}t�|||f���d����}�j|�q2Wq$W|S)N)rr�r�)rOr8�loadrn�yieldroutesr�r�r>)r�r�r�r:)r�r�rr�r�rZr�rrr*=s
zBottle.route.<locals>.decorator)rArn)	rZr�r�r�rr�r�r�r*r)r�r�rr�r�rZr�rr:s

zBottle.routecKs|j||f|�S)z Equals :meth:`route`. )r:)rZr�r�r<rrrr�Isz
Bottle.get�POSTcKs|j||f|�S)z8 Equals :meth:`route` with a ``POST`` method parameter. )r:)rZr�r�r<rrr�postMszBottle.post�PUTcKs|j||f|�S)z7 Equals :meth:`route` with a ``PUT`` method parameter. )r:)rZr�r�r<rrr�putQsz
Bottle.put�DELETEcKs|j||f|�S)z: Equals :meth:`route` with a ``DELETE`` method parameter. )r:)rZr�r�r<rrr�deleteUsz
Bottle.delete��cs��fdd�}|S)z< Decorator: Register an output handler for a HTTP error codecs|�jt��<|S)N)rr�)�handler)�coderZrrrb[szBottle.error.<locals>.wrapperr)rZrVrbr)rVrZrr�YszBottle.errorcCsttt|d��S)N)�e)rT�template�ERROR_PAGE_TEMPLATE)rZ�resrrr�default_error_handler`szBottle.default_error_handlercCsHy�||d<tj|�tj�|d}|d<trdy|jd�jd�|d<Wntk
rbtdd�SXz>|jd�|j	j
|�\}}||d	<||d
<||d<|jf|�S|jd�XWn�tk
r�t
�Stk
r�|j�|j|�Stttfk
�r�Yn@tk
�rB|j�s�t�}|d
j|�tddt
�|�SXdS)Nz
bottle.appr�zbottle.raw_pathr7rNi�z#Invalid path string. Expected UTF-8rzroute.handlezbottle.routezroute.url_argsrzwsgi.errorsi�zInternal Server Error)r1�bind�responserrQrV�UnicodeErrorr�rrr�r�r3r,r�r��_handle�KeyboardInterrupt�
SystemExit�MemoryError�	Exceptionrrr.)rZr�r�r:rZ
stacktracerrrr_cs>


zBottle._handlec
Csf|sdtkrdtd<gSt|ttf�rNt|dttf�rN|ddd�j|�}t|t�rd|jtj�}t|t�r�dtkr�t	|�td<|gSt|t
�r�|jt�|jj
|j|j�|�}|j|�St|t�r�|jt�|j|j�St|d��r$dtjk�rtjd|�St|d��st|d��r$t|�Sy*t|�}t|�}x|�sJt|�}�q8WWn�tk
�rj|jd�Stk
�r�t�}YnNtttfk
�r��Yn2tk
�r�|j�s��t
dd	t�t ��}YnXt|t��r�|j|�St|t��rt!j"|g|�}nFt|t��r.d
d�}t#|t!j"|g|��}ndt$|�}|jt
d|��St|d��rbt%||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
        zContent-Lengthr�readzwsgi.file_wrapperr[�__iter__r�i�zUnhandled exceptioncSs|jtj�S)N)rQr]�charset)r/rrrr0�szBottle._cast.<locals>.<lambda>zUnsupported response type: %s)'r]rOrrkrRrPr�rQrfr�r�r�rr��status_coder[�_castr3r-rCr1r��WSGIFileWrapper�iterrK�
StopIterationr,r`rarbrcrrr4r5rGr��
_closeiterr[)rZ�out�peekZiout�firstZnew_iter�encoderr�rrrrh�sh









zBottle._castc
Cs�yN|j|j|��}tjdks(|ddkr>t|d�r:|j�g}|tjtj�|Stt	t
fk
rj�Yn~tk
r�|js��dt
|jd	d
��}tr�|dt
tt���t
t��f7}|dj|�dg}|d|tj��t|�gSXdS)z The bottle WSGI-interface. �d�e���0r�r�r[z4<h1>Critical error while processing request: %s</h1>r�r�zD<h2>Error:</h2>
<pre>
%s
</pre>
<h2>Traceback:</h2>
<pre>
%s
</pre>
zwsgi.errors�Content-Type�text/html; charset=UTF-8z500 INTERNAL SERVER ERRORN)rqrrrsrt)rurv)rhr_r]�_status_coderCr[�_status_liner.r`rarbrcr�html_escaper�r�r�r,rr.r*r+rT)rZr�r0rmrW�headersrrr�wsgi�s.

zBottle.wsgicCs|j||�S)z9 Each instance of :class:'Bottle' is a WSGI application. )r{)rZr�r0rrrrB�szBottle.__call__)TT)rrr r�)N)Nr�NNNN)Nr�)NrN)NrP)NrR)rT)N)$r\r]r^r}rsrorr!r#r~r"r%r'rr(r=r?rrDr�r[rEr�rKr>r:r�rOrQrSr�r[r_rhr{rBrrrrrUs@

0


)




%
Hrc@s�eZdZdZdZdZdqdd�Zedddd	�d
d��Zedddd	�d
d��Z	edddd	�dd��Z
edd��Zedd��Z
edddd	�dd��Zdrdd�Zedddd	�dd��Zdsdd�Zedd dd	�d!d"��Zedd#dd	�d$d%��Zedd&dd	�d'd(��Zedd)dd	�d*d+��Zedd,dd	�d-d.��Zd/d0�Zd1d2�Zedd3dd	�d4d5��Zd6d7�Zed8d9��Zed:d;��ZeZedd<dd	�d=d>��Zed?d@��ZeddAdd	�dBdC��Z edDdE��Z!edFdG��Z"edHdI��Z#dtdKdL�Z$edMdN��Z%edOdP��Z&edQdR��Z'edSdT��Z(edUdV��Z)edWdX��Z*edYdZ��Z+d[d\�Z,dud]d^�Z-d_d`�Z.dadb�Z/dcdd�Z0dedf�Z1dgdh�Z2didj�Z3dkdl�Z4dmdn�Z5dodp�Z6dS)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�NcCs |dkrin||_||jd<dS)z! Wrap a WSGI environ dictionary. Nzbottle.request)r�)rZr�rrrrsszBaseRequest.__init__z
bottle.appT)rrcCstd��dS)z+ Bottle application handling this request. z0This request is not connected to an application.N)�RuntimeError)rZrrrr�szBaseRequest.appzbottle.routecCstd��dS)z= The bottle :class:`Route` object that matches this request. z)This request is not connected to a route.N)r})rZrrrr:szBaseRequest.routezroute.url_argscCstd��dS)z' The arguments extracted from the URL. z)This request is not connected to a route.N)r})rZrrrr�szBaseRequest.url_argscCsd|jjdd�jd�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�rH)rZrrrr�szBaseRequest.pathcCs|jjdd�j�S)z6 The ``REQUEST_METHOD`` value as an uppercase string. r�r�)r�r�r�)rZrrrr�!szBaseRequest.methodzbottle.request.headerscCs
t|j�S)zf A :class:`WSGIHeaderDict` that provides case-insensitive access to
            HTTP request headers. )�WSGIHeaderDictr�)rZrrrrz&szBaseRequest.headerscCs|jj||�S)zA Return the value of a request header, or a given default value. )rzr�)rZrr
rrr�
get_header,szBaseRequest.get_headerzbottle.request.cookiescCs(t|jjdd��j�}tdd�|D��S)z� Cookies parsed into a :class:`FormsDict`. Signed cookies are NOT
            decoded. Use :meth:`get_cookie` if you expect signed cookies. ZHTTP_COOKIEr�css|]}|j|jfVqdS)N)rqrz)r��crrrr�5sz&BaseRequest.cookies.<locals>.<genexpr>)r9r�r��values�	FormsDict)rZ�cookiesrrrr�0szBaseRequest.cookiescCsB|jj|�}|r:|r:t||�}|r6|d|kr6|dS|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. rr))r�r��
cookie_decode)rZrqr
�secretrzZdecrrr�
get_cookie7s

zBaseRequest.get_cookiezbottle.request.querycCs@t�}|jd<t|jjdd��}x|D]\}}|||<q(W|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�)rZr��pairsrqrzrrrr�Bs
zBaseRequest.queryzbottle.request.formscCs4t�}x(|jj�D]\}}t|t�s|||<qW|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�rN�allitemsrO�
FileUpload)rZ�formsr�itemrrrr�Ns

zBaseRequest.formszbottle.request.paramscCsJt�}x|jj�D]\}}|||<qWx|jj�D]\}}|||<q2W|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�)rZrrqrzrrrrZszBaseRequest.paramszbottle.request.filescCs4t�}x(|jj�D]\}}t|t�r|||<qW|S)z� File uploads parsed from `multipart/form-data` encoded POST or PUT
            request body. The values are instances of :class:`FileUpload`.

        )r�rNr�rOr�)rZ�filesrr�rrrr�es

zBaseRequest.fileszbottle.request.jsoncCs@|jjdd�j�jd�d}|dkr<|j�}|s4dSt|�SdS)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��;rzapplication/jsonN)r�r��lowerr9�_get_body_string�
json_loads)rZZctype�brrr�jsonqszBaseRequest.jsonccs@td|j�}x.|r:|t||��}|s&P|V|t|�8}qWdS)Nr)�max�content_length�minr�)rZrd�bufsize�maxread�partrrr�
_iter_bodyszBaseRequest._iter_bodyccs.tdd�}td�td�td�}}}�x|d�}x<|d
d�|krn|d�}||7}|s\|�t|�|kr4|�q4W|j|�\}	}
}
ytt|	j��d�}Wntk
r�|�YnX|d	kr�P|}xV|d	k�r|s�|t||��}|d|�||d�}
}|
�s|�|
V|t|
�8}q�W|d�|kr*|�q*WdS)Ni�z*Error while parsing chunked transfer body.z
r�r�r)r'�r���)	r�rTr��	partitionr��tonatrGr�r�)rZrdr�rWZrnZsemZbs�headerr��sizer�r��buffr�rrr�
_iter_chunked�s:

zBaseRequest._iter_chunkedzbottle.request.bodycCs�|jr|jn|j}|jdj}t�dd}}}x^|||j�D]N}|j|�|t|�7}|r>||jkr>t	dd�|}}|j|j
��~d}q>W||jd<|jd�|S)Nz
wsgi.inputrFzw+b)r�T)�chunkedr�r�r�rdr=�MEMFILE_MAXr.r�r�getvalue�seek)rZZ	body_iterZ	read_funcr-Z	body_sizeZis_temp_filer��tmprrr�_body�s


zBaseRequest._bodycCsT|j}||jkrtdd��|dkr,|jd}|jj|�}t|�|jkrPtdd��|S)z~ read body until content-length or MEMFILE_MAX into a string. Raise
            HTTPError(413) on requests that are to large. i�zRequest to largerr))r�r�r�r-rdr�)rZ�clenr$rrrr��s



zBaseRequest._get_body_stringcCs|jjd�|jS)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�)rZrrrr-�szBaseRequest.bodycCsd|jjdd�j�kS)z( True if Chunked transfer encoding was. r�ZHTTP_TRANSFER_ENCODINGr�)r�r�r�)rZrrrr��szBaseRequest.chunkedzbottle.request.postc	Cst�}|jjd�sBtt|j�d��}x|D]\}}|||<q*W|Sddi}x$dD]}||jkrP|j|||<qPWt|j|dd	�}t	r�t
|d
ddd
�|d
<ntr�d|d<tj
f|�}||d<|jp�g}x@|D]8}|jdkr�|j||j<q�t|j|j|j|j�||j<q�W|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/r7r�r�r�r��CONTENT_LENGTHT)�fpr�Zkeep_blank_valuesr�rN�
)r8�newliner8z_cgi.FieldStorageN)r�r�r�)r��content_type�
startswithr�r�r�r�rmr-�py31rYr�cgiZFieldStoragerk�filenamerzrr��filerz)	rZrOr�rqrzZsafe_envrr$r�rrrrN�s2






zBaseRequest.POSTcCs
|jj�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. )�urlpartsZgeturl)rZrrrr��szBaseRequest.urlzbottle.request.urlpartscCs�|j}|jd�p|jdd�}|jd�p.|jd�}|sn|jdd�}|jd�}|rn||dkr\d	nd
krn|d|7}t|j�}t||||jd�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. ZHTTP_X_FORWARDED_PROTOzwsgi.url_scheme�httpZHTTP_X_FORWARDED_HOSTZ	HTTP_HOSTZSERVER_NAMEz	127.0.0.1ZSERVER_PORTZ80Z443�:r�r�)r�r��urlquote�fullpath�UrlSplitResult)rZ�envr��host�portr�rrrr��s

zBaseRequest.urlpartscCst|j|jjd��S)z: Request path including :attr:`script_name` (if present). r�)r2�script_namer�rH)rZrrrr�szBaseRequest.fullpathcCs|jjdd�S)zh The raw :attr:`query` part of the URL (everything in between ``?``
            and ``#``) as a string. r�r�)r�r�)rZrrr�query_stringszBaseRequest.query_stringcCs(|jjdd�jd�}|r$d|dSdS)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. rFr�r�)r�r�rG)rZr�rrrr�szBaseRequest.script_namer)cCs,|jjdd�}t||j|�\|d<|d<dS)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)
        rFr�r�N)r�r�r2r�)rZ�shiftZscriptrrrr2"szBaseRequest.path_shiftcCst|jjd�pd�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�r)rC)r�r�r�)rZrrrr�,szBaseRequest.content_lengthcCs|jjdd�j�S)zA The Content-Type header as a lowercase-string (default: empty). r�r�)r�r�r�)rZrrrr�3szBaseRequest.content_typecCs|jjdd�}|j�dkS)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). ZHTTP_X_REQUESTED_WITHr�Zxmlhttprequest)r�r�r�)rZZrequested_withrrr�is_xhr8szBaseRequest.is_xhrcCs|jS)z9 Alias for :attr:`is_xhr`. "Ajax" is not the right term. )r�)rZrrr�is_ajax@szBaseRequest.is_ajaxcCs6t|jjdd��}|r|S|jjd�}|r2|dfSdS)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. ZHTTP_AUTHORIZATIONr�ZREMOTE_USERN)�
parse_authr�r�)rZ�basicZruserrrr�authEszBaseRequest.authcCs>|jjd�}|r$dd�|jd�D�S|jjd�}|r:|gSgS)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. ZHTTP_X_FORWARDED_FORcSsg|]}|j��qSr)rG)r�Ziprrrr�Zsz,BaseRequest.remote_route.<locals>.<listcomp>r�ZREMOTE_ADDR)r�r�r9)rZ�proxyZremoterrr�remote_routeSs
zBaseRequest.remote_routecCs|j}|r|dSdS)zg The client IP as a string. Note that this information can be forged
            by malicious clients. rN)r�)rZr:rrr�remote_addr^szBaseRequest.remote_addrcCst|jj��S)zD Return a new :class:`Request` with a shallow :attr:`environ` copy. )�Requestr��copy)rZrrrr�eszBaseRequest.copycCs|jj||�S)N)r�r�)rZrzr
rrrr�iszBaseRequest.getcCs
|j|S)N)r�)rZrqrrr�__getitem__jszBaseRequest.__getitem__cCsd||<|j|=dS)Nr�)r�)rZrqrrr�__delitem__kszBaseRequest.__delitem__cCs
t|j�S)N)rjr�)rZrrrrelszBaseRequest.__iter__cCs
t|j�S)N)r�r�)rZrrr�__len__mszBaseRequest.__len__cCs
|jj�S)N)r�r�)rZrrrr�nszBaseRequest.keyscCsp|jjd�rtd��||j|<f}|dkr0d}n|d
kr>d}n|jd�rLd}x|D]}|jjd|d�qRWdS)zA Change an environ value and clear all caches that depend on it. zbottle.request.readonlyz$The environ dictionary is read-only.z
wsgi.inputr-r�r�rrOr�r�r��HTTP_rzr�zbottle.request.N)r-r�r�rrOr�)r�r)rzr�)r�r�r�r�r�)rZrqrzZtodeleterrr�__setitem__os


zBaseRequest.__setitem__cCsd|jj|j|jfS)Nz<%s: %s %s>)�	__class__r\r�r�)rZrrrr
�szBaseRequest.__repr__cCsLy&|jd|}t|d�r$|j|�S|Stk
rFtd|��YnXdS)z@ Search in self.environ for additional user defined attributes. zbottle.request.ext.%sryzAttribute %r not defined.N)r�rCryr�ra)rZr�varrrr�__getattr__�s
zBaseRequest.__getattr__cCs(|dkrtj|||�S||jd|<dS)Nr�zbottle.request.ext.%s)�object�__setattr__r�)rZrrzrrrr��szBaseRequest.__setattr__)N)N)NN)r))N)7r\r]r^r}�	__slots__r�rsror�r:r�rr�r�rzrr�r�r�r�rr�r�r�r�r�r�r-r�r�rNr�r�r�r�r�r2r�r�r�r�r�r�r�r�r�r�r�rer�r�r�r
r�r�rrrrr|�sd



#	


r|cCs4d|ksd|ksd|kr$td|��|j�jdd�S)Nr��
�z4Header names must not contain control characters: %rr��-)r��title�replace)rqrrr�_hkey�sr�cCs0t|�}d|ks d|ks d|kr,td|��|S)Nr�r�r�z4Header value must not contain control characters: %r)r�r�)rzrrr�_hval�sr�c@s.eZdZddd�Zdd�Zdd�Zd	d
�ZdS)�HeaderPropertyNr�cCs.|||_|_|||_|_d|j�|_dS)NzCurrent value of the %r header.)rr
�reader�writerr�r})rZrr�r�r
rrrrs�szHeaderProperty.__init__cCs0|dkr|S|j|j|j�}|jr,|j|�S|S)N)rrr
r�)rZrwrxrzrrrry�szHeaderProperty.__get__cCs|jr|j|�n|||j<dS)N)r�r)rZrwrzrrrr{�szHeaderProperty.__set__cCs||j=dS)N)r)rZrwrrrr|�szHeaderProperty.__delete__)NNr�)r\r]r^rsryr{r|rrrrr��s
r�c
@s4eZdZdZdZdZedC�edD�d�ZdEdd�ZdFdd�Z	dd�Z
dd�Zedd��Z
edd��Zdd�Zdd�Zeeedd�Z[[ed d!��Zd"d#�Zd$d%�Zd&d'�Zd(d)�ZdGd*d+�Zd,d-�Zd.d/�Zd0d1�Zed2d3��Zed�Zeded4�Zed5d6d7�d8d7�d9�Z edHd;d<��Z!dId=d>�Z"d?d@�Z#dAdB�Z$dS)J�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.
    ��ztext/html; charset=UTF-8�Content-Typer��Content-Encoding�Content-Language�Content-Length�
Content-Range�Content-Md5�
Last-Modified)rsi0r�NcKs|d|_i|_||_|p|j|_|rRt|t�r4|j�}x|D]\}}|j||�q:W|rxx |j�D]\}}|j||�q`WdS)N)	�_cookies�_headersr-�default_statusr+rOrmrr,)rZr-r+rz�more_headersrrzrrrrs�s
zBaseResponse.__init__cCsf|pt}t|t�st�|�}|j|_tdd�|jj�D��|_|jrbt�|_|jj	|jj
dd��|S)z Returns a copy of self. css"|]\}}||dd�fVqdS)Nr)r��k�vrrrr��sz$BaseResponse.copy.<locals>.<genexpr>r�)r�)r��
issubclass�AssertionErrorr+rmr�rr�r9rL�output)rZrxr�rrrr��szBaseResponse.copycCs
t|j�S)N)rjr-)rZrrrre�szBaseResponse.__iter__cCst|jd�r|jj�dS)Nr[)rCr-r[)rZrrrr[�szBaseResponse.closecCs|jS)z; The HTTP status line as a string (e.g. ``404 Not Found``).)rx)rZrrr�status_line�szBaseResponse.status_linecCs|jS)z/ The HTTP status code as an integer (e.g. 404).)rw)rZrrrrg�szBaseResponse.status_codecCs�t|t�r|tj|�}}n*d|kr>|j�}t|j�d�}ntd��d|koXdknsftd��||_t|pxd|�|_	dS)N� rz+String status line without a reason phrase.rqi�zStatus code out of range.z
%d Unknown)
rOr��_HTTP_STATUS_LINESr�rGr9r�rwr�rx)rZr+rVrrr�_set_status�s
zBaseResponse._set_statuscCs|jS)N)rx)rZrrr�_get_statusszBaseResponse._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. cCst�}|j|_|S)zl An instance of :class:`HeaderDict`, a case-insensitive dict-like
            view on the response headers. )�
HeaderDictr�rm)rZZhdictrrrrzszBaseResponse.headerscCst|�|jkS)N)r�r�)rZrrrr�__contains__szBaseResponse.__contains__cCs|jt|�=dS)N)r�r�)rZrrrrr�szBaseResponse.__delitem__cCs|jt|�dS)Nr)rC)r�r�)rZrrrrr�szBaseResponse.__getitem__cCst|�g|jt|�<dS)N)r�r�r�)rZrrzrrrr�szBaseResponse.__setitem__cCs|jjt|�|g�dS)z| Return the value of a previously defined header. If there is no
            header with that name, return a default value. r)rC)r�r�r�)rZrr
rrrrszBaseResponse.get_headercCst|�g|jt|�<dS)zh Create a new response header, replacing any previously defined
            headers with the same name. N)r�r�r�)rZrrzrrr�
set_header!szBaseResponse.set_headercCs |jjt|�g�jt|��dS)z= Add an additional response header, not removing duplicates. N)r�r�r�rr�)rZrrzrrrr,&szBaseResponse.add_headercCs|jS)zx Yield (header, value) tuples, skipping headers that are not
            allowed with the current response status code. )r.)rZrrr�iter_headers*szBaseResponse.iter_headerscs�g}t|jj��}d|jkr.|jd|jgf�|j|jkrX|j|j��fdd�|D�}|dd�|D�7}|jr�x(|jj�D]}|jdt	|j
��f�q|Wtr�dd�|D�}|S)z. WSGI conform list of (header, value) tuples. zContent-Typecsg|]}|d�kr|�qS)rr)r��h)�bad_headersrrr�8sz+BaseResponse.headerlist.<locals>.<listcomp>cSs"g|]\}}|D]}||f�qqSrr)r�r�vals�valrrrr�9sz
Set-CookiecSs$g|]\}}||jd�jd�f�qS)rNr7)rQrV)r�r�r�rrrr�>s)rkr�rr�default_content_typerwr�r�r�r�ZOutputStringr)rZrmrzr�r)r�rr./s
zBaseResponse.headerlist)r�ZExpirescCstjt|��S)N)rZutcfromtimestamp�
parse_date)r/rrrr0DszBaseResponse.<lambda>cCst|�S)N)�	http_date)r/rrrr0Es)r�r��UTF-8cCs,d|jkr(|jjd�djd�dj�S|S)zJ Return the charset specified in the content-type header (default: utf8). zcharset=r)r�rrC)r�r9rG)rZr
rrrrfGs
zBaseResponse.charsetcKs�|jst�|_|r&tt||f|��}nt|t�s8td��t|�dkrLtd��||j|<x�|j	�D]�\}}|dkr�t|t
�r�|j|jdd}|dkr�t|t
tf�r�|j�}nt|ttf�r�tj|�}tjd|�}||j||jd	d
�<q`WdS)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.izCookie value to long.�max_age�i�expiresz%a, %d %b %Y %H:%M:%S GMTr�r�N)r�r9r@�
cookie_encoderOr8rBr�r�rrZsecondsZdays�datedaterZ	timetupler�r��time�gmtime�strftimer�)rZrrzr�r<rqrrr�
set_cookieNs(!




zBaseResponse.set_cookiecKs$d|d<d|d<|j|df|�dS)zq Delete a cookie. Be sure to use the same `domain` and `path`
            settings as used to create the cookie. r)rrrr�NrC)r)rZrqr)rrr�
delete_cookie�szBaseResponse.delete_cookiecCs4d}x*|jD] \}}|d|j�|j�f7}qW|S)Nr�z%s: %s
)r.r�rG)rZrmrrzrrrr
�szBaseResponse.__repr__)r�)r�r�r�r�r�r�r�r�)r�NN)N)N)r)N)%r\r]r^r}r�rrlr�rsr�rer[rr�rgr�r�r+rzr�r�r�r�rr�r,r�r.r�r�r�r�rrfrr
r
rrrrr��sL




8r�csF|rtd�tj���fdd�}�fdd�}�fdd�}t|||d�S)	Nz3local_property() is deprecated and will be removed.cs(y�jStk
r"td��YnXdS)Nz Request context not initialized.)r�rar})rZ)�lsrr�fget�szlocal_property.<locals>.fgetcs
|�_dS)N)r�)rZrz)rrr�fset�szlocal_property.<locals>.fsetcs�`dS)N)r�)rZ)rrr�fdel�szlocal_property.<locals>.fdelzThread-local property)rj�	threading�localr)rrrrr)rr�local_property�src@seZdZdZejZe�ZdS)�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)	r\r]r^r}r|rsr\rr�rrrrr�src@s4eZdZdZejZe�Ze�Z	e�Z
e�Ze�ZdS)�
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)
r\r]r^r}r�rsr\rrxrwr�r�r-rrrrr�srcs&eZdZd�fdd�	Zdd�Z�ZS)r3r�Ncstt|�j|||f|�dS)N)�superr3rs)rZr-r+rzr�)r�rrrs�szHTTPResponse.__init__cCs,|j|_|j|_|j|_|j|_|j|_dS)N)rwrxr�r�r-)rZr]rrrr��s
zHTTPResponse.apply)r�NN)r\r]r^rsr��
__classcell__rr)r�rr3�sr3cs"eZdZdZd�fdd�	Z�ZS)r�i�Ncs&||_||_tt|�j||f|�dS)N)�	exception�	tracebackrr�rs)rZr+r-rrr<)r�rrrs�szHTTPError.__init__)NNNN)r\r]r^r�rsrrr)r�rr��sr�c@seZdZdS)�PluginErrorN)r\r]r^rrrrr�src@s(eZdZdZdZefdd�Zdd�ZdS)rr�r'cCs
||_dS)N)r%)rZr%rrrrs�szJSONPlugin.__init__cs |j��s�S��fdd�}|S)Ncsry�||�}Wntk
r(t�}YnXt|t�rF�|�}dt_|St|t�rnt|jt�rn�|j�|_d|_|S)Nzapplication/json)r�r,rOrmr]r�r3r-)rErd�rvZ
json_response)r�r!rrrb�s
z!JSONPlugin.apply.<locals>.wrapper)r%)rZr�r:rbr)r�r!rr��s
zJSONPlugin.applyN)r\r]r^rrr%rsr�rrrrr�src@s eZdZdZdZdZdd�ZdS)ra 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. rXr'cCs\|jjd�}t|ttf�r>t|�dkr>t|df|d�|�St|t�rTt|�|�S|SdS)NrXr'rr))r�r�rOrrkr��viewr�)rZr�r:r�rrrr�s
zTemplatePlugin.applyN)r\r]r^r}rrr�rrrrr�src@s&eZdZdd�Zddd�Zdd�ZdS)	�_ImportRedirectcCsH||_||_tjj|t|��|_|jjjt	gg|d��tj
j|�dS)z@ Create a virtual package that redirects imports (see PEP 302). )�__file__�__path__�__all__�
__loader__N)r�impmaskr*�modulesr�r<�moduler�updater�	meta_pathr)rZrr#rrrrs
sz_ImportRedirect.__init__NcCs.d|krdS|jdd�d}||jkr*dS|S)N�.r)r)�rsplitr)rZ�fullnamer�Zpacknamerrr�find_modules
z_ImportRedirect.find_modulecCsb|tjkrtj|S|jdd�d}|j|}t|�tj|}tj|<t|j||�||_|S)Nr(r))r*r$r)r#�
__import__r�r%r")rZr*�modnameZrealnamer%rrr�load_modules


z_ImportRedirect.load_module)N)r\r]r^rsr+r.rrrrrs	
rc@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zerzdd�Z
dd�Zdd�ZeZe
ZeZeZn8dd�Z
dd�Zdd�Zdd�Zdd�Zd d!�Zd"d�Zd.d%d&�Zd'd(�Zd)d*�Zd+d,�ZeZeZd#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.
    cOs"tdd�t||�j�D��|_dS)Ncss|]\}}||gfVqdS)Nr)r�r�r�rrrr�7sz%MultiDict.__init__.<locals>.<genexpr>)rmr)rZrEr�rrrrs6szMultiDict.__init__cCs
t|j�S)N)r�rm)rZrrrr�9szMultiDict.__len__cCs
t|j�S)N)rjrm)rZrrrre:szMultiDict.__iter__cCs
||jkS)N)rm)rZrqrrrr�;szMultiDict.__contains__cCs|j|=dS)N)rm)rZrqrrrr�<szMultiDict.__delitem__cCs|j|dS)Nr)rC)rm)rZrqrrrr�=szMultiDict.__getitem__cCs|j||�dS)N)r)rZrqrzrrrr�>szMultiDict.__setitem__cCs
|jj�S)N)rmr�)rZrrrr�?szMultiDict.keyscCsdd�|jj�D�S)Ncss|]}|dVqdS)r)NrCr)r�r�rrrr�Bsz#MultiDict.values.<locals>.<genexpr>)rmr�)rZrrrr�BszMultiDict.valuescCsdd�|jj�D�S)Ncss|]\}}||dfVqdS)r)NrCr)r�r�r�rrrr�Csz"MultiDict.items.<locals>.<genexpr>)rmr)rZrrrrCszMultiDict.itemscCsdd�|jj�D�S)Ncss$|]\}}|D]}||fVqqdS)Nr)r�r��vlr�rrrr�Esz%MultiDict.allitems.<locals>.<genexpr>)rmr)rZrrrr�DszMultiDict.allitemscCsdd�|jj�D�S)NcSsg|]}|d�qS)r)rCr)r�r�rrrr�Lsz$MultiDict.values.<locals>.<listcomp>)rmr�)rZrrrr�LscCsdd�|jj�D�S)NcSsg|]\}}||df�qS)r)rCr)r�r�r�rrrr�Msz#MultiDict.items.<locals>.<listcomp>)rmr)rZrrrrMscCs
|jj�S)N)rm�iterkeys)rZrrrr1NszMultiDict.iterkeyscCsdd�|jj�D�S)Ncss|]}|dVqdS)r)NrCr)r�r�rrrr�Osz'MultiDict.itervalues.<locals>.<genexpr>)rm�
itervalues)rZrrrr2OszMultiDict.itervaluescCsdd�|jj�D�S)Ncss|]\}}||dfVqdS)r)NrCr)r�r�r�rrrr�Qsz&MultiDict.iteritems.<locals>.<genexpr>)rm�	iteritems)rZrrrr3PszMultiDict.iteritemscCsdd�|jj�D�S)Ncss$|]\}}|D]}||fVqqdS)Nr)r�r�r0r�rrrr�Ssz)MultiDict.iterallitems.<locals>.<genexpr>)rmr3)rZrrr�iterallitemsRszMultiDict.iterallitemscCsdd�|jj�D�S)NcSs"g|]\}}|D]}||f�qqSrr)r�r�r0r�rrrr�Usz&MultiDict.allitems.<locals>.<listcomp>)rmr3)rZrrrr�TsNr)cCs8y|j||}|r||�S|Stk
r2YnX|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.
        )rmrc)rZrqr
�indexr�r�rrrr�Ws
z
MultiDict.getcCs|jj|g�j|�dS)z5 Add a new value to the list of values for this key. N)rmr�r)rZrqrzrrrrhszMultiDict.appendcCs|g|j|<dS)z1 Replace the list of values with a single value. N)rm)rZrqrzrrrr�lszMultiDict.replacecCs|jj|�pgS)z5 Return a (possibly empty) list of values for a key. )rmr�)rZrqrrr�getallpszMultiDict.getallrC)NrCN)r\r]r^r}rsr�rer�r�r�r�r�rr�rr�r1r2r3r4r�rr�r6ZgetoneZgetlistrrrrr/0s<
r/csLeZdZdZdZdZd
dd�Zddd�Zdd	d
�Ze	�f�fdd�	Z
�ZS)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. rNTNcCsHt|t�r&|jr&|jd�j|p"|j�St|t�r@|j|p<|j�S|SdS)Nr7)rOrP�recode_unicoderQrV�input_encodingrR)rZrAr8rrr�_fix�s

zFormsDict._fixcCsRt�}|p|j}|_d|_x0|j�D]$\}}|j|j||�|j||��q&W|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�r8r7r�rr9)rZr8r�rSrqrzrrrrV�s zFormsDict.decodecCs.y|j|||�Sttfk
r(|SXdS)z7 Return the value as a unicode string, or the default. N)r9r^r�)rZrr
r8rrr�
getunicode�szFormsDict.getunicodecs2|jd�r$|jd�r$tt|�j|�S|j||d�S)N�__)r
)r�r;rr�r�r:)rZrr
)r�rrr��szFormsDict.__getattr__)N)N)NN)r\r]r^r}r8r7r9rVr:rPr�rrr)r�rr�ys


r�c@sbeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zddd�Zdd�Z
dS)r�zz A case-insensitive version of :class:`MultiDict` that defaults to
        replace the old value instead of appending it. cOsi|_|s|r|j||�dS)N)rmr&)rZrErdrrrrs�szHeaderDict.__init__cCst|�|jkS)N)r�rm)rZrqrrrr��szHeaderDict.__contains__cCs|jt|�=dS)N)rmr�)rZrqrrrr��szHeaderDict.__delitem__cCs|jt|�dS)Nr)rC)rmr�)rZrqrrrr��szHeaderDict.__getitem__cCst|�g|jt|�<dS)N)r�rmr�)rZrqrzrrrr��szHeaderDict.__setitem__cCs |jjt|�g�jt|��dS)N)rmr�r�rr�)rZrqrzrrrr�szHeaderDict.appendcCst|�g|jt|�<dS)N)r�rmr�)rZrqrzrrrr��szHeaderDict.replacecCs|jjt|��pgS)N)rmr�r�)rZrqrrrr6�szHeaderDict.getallNr)cCstj|t|�||�S)N)r/r�r�)rZrqr
r5rrrr��szHeaderDict.getcCs.x(dd�|D�D]}||jkr|j|=qWdS)Ncss|]}t|�VqdS)N)r�)r�r�rrrr��sz$HeaderDict.filter.<locals>.<genexpr>)rm)rZ�namesrrrr�filter�s
zHeaderDict.filterrC)NrC)r\r]r^r}rsr�r�r�r�rr�r6r�r=rrrrr��s
r�c@sfeZdZdZdZdd�Zdd�Zdd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�ZdS)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�cCs
||_dS)N)r�)rZr�rrrrs�szWSGIHeaderDict.__init__cCs&|jdd�j�}||jkr|Sd|S)z6 Translate header field name to CGI/WSGI environ key. r�r�r�)r�r��cgikeys)rZrqrrr�_ekey�s
zWSGIHeaderDict._ekeyNcCs|jj|j|�|�S)z: Return the header value as is (may be bytes or unicode). )r�r�r?)rZrqr
rrr�raw�szWSGIHeaderDict.rawcCst|j|j|�d�S)Nr7)r�r�r?)rZrqrrrr��szWSGIHeaderDict.__getitem__cCstd|j��dS)Nz%s is read-only.)rBr�)rZrqrzrrrr��szWSGIHeaderDict.__setitem__cCstd|j��dS)Nz%s is read-only.)rBr�)rZrqrrrr��szWSGIHeaderDict.__delitem__ccs\xV|jD]L}|dd�dkr8|dd�jdd�j�Vq||jkr|jdd�j�VqWdS)N�r�r�r�)r�r�r�r>)rZrqrrrre�s

zWSGIHeaderDict.__iter__cCsdd�|D�S)NcSsg|]}|�qSrr)r�r/rrrr��sz'WSGIHeaderDict.keys.<locals>.<listcomp>r)rZrrrr��szWSGIHeaderDict.keyscCst|j��S)N)r�r�)rZrrrr��szWSGIHeaderDict.__len__cCs|j|�|jkS)N)r?r�)rZrqrrrr��szWSGIHeaderDict.__contains__)r�r�)N)r\r]r^r}r>rsr?r@r�r�r�rer�r�r�rrrrr~�s

r~c@s�eZdZdZd'ZGdd�de�Zdd�Zdd	�Zd(dd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zd)dd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�ZdS)*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.
    �_metarc@s|eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�ZdS)zConfigDict.NamespacecCs||_||_dS)N)�_config�_prefix)rZr��	namespacerrrrs�szConfigDict.Namespace.__init__cCstd�|j|jd|S)Nz}Accessing namespaces as dicts is discouraged. Only use flat item access: cfg["names"]["pace"]["key"] -> cfg["name.space.key"]r()rjrCrD)rZrqrrrr�sz ConfigDict.Namespace.__getitem__cCs||j|jd|<dS)Nr()rCrD)rZrqrzrrrr�sz ConfigDict.Namespace.__setitem__cCs|j|jd|=dS)Nr()rCrD)rZrqrrrr�	sz ConfigDict.Namespace.__delitem__ccsB|jd}x2|jD](}|jd�\}}}||jkr|r|VqWdS)Nr()rDrC�
rpartition)rZZ	ns_prefixrq�ns�dotrrrrres

zConfigDict.Namespace.__iter__cCsdd�|D�S)NcSsg|]}|�qSrr)r�r/rrrr�sz-ConfigDict.Namespace.keys.<locals>.<listcomp>r)rZrrrr�szConfigDict.Namespace.keyscCst|j��S)N)r�r�)rZrrrr�szConfigDict.Namespace.__len__cCs|jd||jkS)Nr()rDrC)rZrqrrrr�sz!ConfigDict.Namespace.__contains__cCs
d|jS)Nz<Config.Namespace %s.*>)rD)rZrrrr
szConfigDict.Namespace.__repr__cCs
d|jS)Nz<Config.Namespace %s.*>)rD)rZrrr�__str__szConfigDict.Namespace.__str__cCs\td�||kr8|dj�r8tj|j|jd|�||<||krR|jd�rRt|��|j|�S)NzAttribute access is deprecated.rr(r;)	rj�isupperr��	NamespacerCrDr�rar�)rZrqrrrr�sz ConfigDict.Namespace.__getattr__cCsd|dkr||j|<dStd�tt|�r0td��||krX||rXt|||j�rXtd��|||<dS)NrCrDz#Attribute assignment is deprecated.zRead-only attribute.zNon-empty namespace attribute.)rCrD)rrjrCrJrarOr�)rZrqrzrrrr�"s

 z ConfigDict.Namespace.__setattr__cCsL||krH|j|�}t||j�rH|d}x |D]}|j|�r,|||=q,WdS)Nr()r�rOr�r�)rZrqr�r�rrr�__delattr__-s


z ConfigDict.Namespace.__delattr__cOstd�|j||�|S)Nz8Calling ConfDict is deprecated. Use the update() method.)rjr&)rZrErdrrrrB6szConfigDict.Namespace.__call__N)r\r]r^rsr�r�r�rer�r�r�r
rIr�r�rLrBrrrrrK�s	rKcOs0i|_dd�|_|s|r,td�|j||�dS)NcSsdS)Nr)rrzrrrr0=sz%ConfigDict.__init__.<locals>.<lambda>z-Constructor does no longer accept parameters.)rBrrjr&)rZrErdrrrrs;s

zConfigDict.__init__cCsZt�}|j|�xD|j�D]8}x2|j|�D]$\}}|dkrF|d|}|||<q*WqW|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()rMrN)r>rdZsectionsr)rZr�r�Zsectionrqrzrrr�load_configBs
zConfigDict.load_configr�Fc	Cs�||fg}x�|r�|j�\}}t|t�s6tdt|���xx|j�D]l\}}t|t�sbtdt|���|rr|d|n|}t|t�r�|j||f�|r�|j||�||<q@|||<q@WqW|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'}
        zSource is not a dict (r)zKey is not a string (%r)r()	r�rOrmrBr�rr8rrK)	rZ�sourcerEr��stackr�rqrzZfull_keyrrrr�Rs



zConfigDict.load_dictcOs`d}|r4t|dt�r4|djd�d}|dd�}x&t||�j�D]\}}||||<qDWdS)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�rr(r)N)rOr8rGrmr)rZrErdr�rqrzrrrr&jszConfigDict.updatecCs||kr|||<||S)Nr)rZrqrzrrrr�uszConfigDict.setdefaultcCsft|t�stdt|���|j|ddd��|�}||krH|||krHdS|j||�tj|||�dS)NzKey has type %r (not a string)r=cSs|S)Nr)r/rrrr0~sz(ConfigDict.__setitem__.<locals>.<lambda>)rOr8rBr��meta_getrrmr�)rZrqrzrrrr�zs
zConfigDict.__setitem__cCstj||�dS)N)rmr�)rZrqrrrr��szConfigDict.__delitem__cCsx|D]
}||=qWdS)Nr)rZrqrrr�clear�s
zConfigDict.clearNcCs|jj|i�j||�S)z- Return the value of a meta field for a key. )rBr�)rZrq�	metafieldr
rrrrR�szConfigDict.meta_getcCs*||jj|i�|<||kr&||||<dS)zq Set the meta field for a key to a new value. This triggers the
            on-change handler for existing keys. N)rBr�)rZrqrTrzrrrr�szConfigDict.meta_setcCs|jj|i�j�S)z; Return an iterable of meta field names defined for a key. )rBr�r�)rZrqrrr�	meta_list�szConfigDict.meta_listcCsPtd�||kr,|dj�r,|j||�||<||krF|jd�rFt|��|j|�S)NzAttribute access is deprecated.rr;)rjrJrKr�rar�)rZrqrrrr��szConfigDict.__getattr__cCsf||jkrtj|||�Std�tt|�r2td��||krZ||rZt|||j�rZtd��|||<dS)Nz#Attribute assignment is deprecated.zRead-only attribute.zNon-empty namespace attribute.)r�rmr�rjrCrarOrK)rZrqrzrrrr��s

 zConfigDict.__setattr__cCsL||krH|j|�}t||j�rH|d}x |D]}|j|�r,|||=q,WdS)Nr()r�rOrKr�)rZrqr�r�rrrrL�s


zConfigDict.__delattr__cOstd�|j||�|S)Nz8Calling ConfDict is deprecated. Use the update() method.)rjr&)rZrErdrrrrB�szConfigDict.__call__)rBr)r�F)N)r\r]r^r}r�rJrKrsrOr�r&r�r�r�rSrRrrUr�r�rLrBrrrrr��s$A



	r�c@s"eZdZdZdd�Zddd�ZdS)�AppStackz> A stack-like list. Calling it returns the head of the stack. cCs|dS)z) Return the current default application. r)rCr)rZrrrrB�szAppStack.__call__NcCst|t�st�}|j|�|S)z1 Add a new :class:`Bottle` instance to the stack )rOrr)rZrzrrr�push�s

z
AppStack.push)N)r\r]r^r}rBrWrrrrrV�srVc@seZdZd	dd�Zdd�ZdS)
rii�@cCs<|||_|_x(dD] }t||�rt||t||��qWdS)N�filenor[rd�	readlines�tellr�)rYr[rdrZr[r�)r��buffer_sizerCr�rv)rZr�r\rprrrrs�s

zWSGIFileWrapper.__init__ccs.|j|j}}x||�}|s dS|VqWdS)N)r\rd)rZr�rdr�rrrre�szWSGIFileWrapper.__iter__N�)r])r\r]r^rsrerrrrri�s
ric@s*eZdZdZd	dd�Zdd�Zdd�ZdS)
rlz� This only exists to be able to attach a .close method to iterators that
        do not support attribute assignment (most of itertools). NcCs||_t|�|_dS)N)�iteratorrn�close_callbacks)rZr^r[rrrrs�sz_closeiter.__init__cCs
t|j�S)N)rjr^)rZrrrre�sz_closeiter.__iter__cCsx|jD]
}|�qWdS)N)r_)rZrrrrr[�sz_closeiter.close)N)r\r]r^r}rsrer[rrrrrl�s
rlc@sDeZdZdZdedfdd�Zddd	�Zd
d�Zdd
�Zddd�ZdS)raf 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'.
    z./�allcCs"t|_||_||_g|_i|_dS)N)�open�opener�base�	cachemoder��cache)rZrcrbrdrrrrs�s
zResourceManager.__init__NFcCs�tjjtjj|p|j��}tjjtjj|tjj|���}|tj7}||jkrX|jj|�|rttjj|�rttj	|�|dkr�|jj
|�n|jj||�|jj
�tjj|�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�dirnamercr��sepr&�isdir�makedirsrr$rerS�exists)rZr�rcr5Zcreaterrr�add_path�s



zResourceManager.add_pathccsr|jdd�}x^|rl|j�}tjj|�s*qx>tj|�D]0}tjj||�}tjj|�r`|j|�q6|Vq6WqWdS)z: Iterate over all existing files in all registered paths. N)r�r�rfrj�listdirr�r)rZ�searchr�rZfullrrrre	szResourceManager.__iter__cCsn||jkstrdx@|jD]6}tjj||�}tjj|�r|jdkrH||j|<|SqW|jdkrdd|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`�foundN)r`rp)rer�r�rfr��isfilerd)rZrr�Zfpathrrr�lookup)	s



zResourceManager.lookup�rcOs4|j|�}|std|��|j|f|�d|i|��S)z= Find a resource and return a file object, or raise IOError. zResource %r not found.r�)rr�IOErrorrb)rZrr�rr)�fnamerrrra:	s
zResourceManager.open)NNF)rs)	r\r]r^r}rarsrmrerrrrrrr�s

rc@sVeZdZddd�Zed�Zededd�Zddd	�Ze	d
d��Z
ddd�Zddd�ZdS)r�NcCs*||_||_||_|rt|�nt�|_dS)z Wrapper for file uploads. N)r�r�raw_filenamer�rz)rZZfileobjrr�rzrrrrsC	szFileUpload.__init__zContent-TypezContent-Lengthr))r�r
cCs|jj||�S)z9 Return the value of a header within the mulripart part. )rzr�)rZrr
rrrrQ	szFileUpload.get_headercCs�|j}t|t�s|jdd�}td|�jdd�jd�}tjj|j	dtjj
��}tjdd|�j
�}tjdd	|�j
d
�}|dd�p�d
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.
        rN�ignoreZNFKD�ASCII�\z[^a-zA-Z0-9-_.\s]r�z[-\s]+r�z.-N�r)rvrOrPrVrrQrfr��basenamer�rir�r�rG)rZrurrrr�U	s

zFileUpload.filenamer'r�cCsH|jj|j|jj�}}}x||�}|s,P||�qW|jj|�dS)N)r�rdr.r[r�)rZr��
chunk_sizerdr.r�Zbufrrr�
_copy_fileh	szFileUpload._copy_fileFc
Cstt|t�rdtjj|�r&tjj||j�}|r@tjj|�r@td��t	|d��}|j
||�WdQRXn|j
||�dS)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)rOr8rfr�rjr�r�rlrtrar})rZZdestinationZ	overwriter|r�rrr�savep	s	
zFileUpload.save)NrC)N�)r��)Fr�)
r\r]r^rsr�r�r�r�rr~r�r}rrrrrr�A	s


r����Unknown Error.cCst||��dS)z+ Aborts execution and causes a HTTP error. N)r�)rV�textrrr�abort�	sr�cCsN|stjd�dkrdnd}tjtd�}||_d|_|jdttj	|��|�dS)	zd Aborts execution and causes a 303 or 302 redirect, depending on
        the HTTP protocol version. ZSERVER_PROTOCOLzHTTP/1.1i/i.)rxr�ZLocationN)
r1r�r]r�r3r+r-r�r2r�)r�rVrZrrr�redirect�	sr�iccsD|j|�x4|dkr>|jt||��}|s*P|t|�8}|VqWdS)zF Yield chunks from a range in a file. No chunk is bigger than maxread.rN)r�rdr�r�)r�r�rRr�r�rrr�_file_iter_range�	s

r��auto�UTF-8cCshtjj|�tj}tjjtjj||jd���}t�}|j|�sHtdd�Stjj	|�sdtjj
|�rntdd�Stj|tj�s�tdd�S|dkr�t
j|�\}}|r�||d<|r�|d	d
�dkr�|r�d|kr�|d
|7}||d<|�rtjj|dkr�|n|�}d||d<tj|�}|j|d<}tjdtj|j��}	|	|d<tjjd�}
|
�rbt|
jd�dj��}
|
d	k	�r�|
t|j�k�r�tjdtj��|d<tfddi|��Stjdk�r�dnt|d�}d|d<tjjd �}d tjk�r\tt tjd |��}|�std!d"�S|d\}
}d#|
|d$|f|d%<t!||
�|d<|�rHt"||
||
�}t|fdd&i|��St|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�zAccess denied.i�zFile does not exist.z/You do not have permission to access this file.r�zContent-EncodingNrAztext/rfz; charset=%szContent-TypeTzattachment; filename="%s"zContent-DispositionzContent-Lengthz%a, %d %b %Y %H:%M:%S GMTz
Last-ModifiedZHTTP_IF_MODIFIED_SINCEr�rZDater+i0r�r��rbrRz
Accept-RangesZ
HTTP_RANGEi�zRequested Range Not Satisfiablezbytes %d-%d/%dr)z
Content-Range��)#rfr�rgrir�rGrmr�r�rlrq�access�R_OK�	mimetypesZ
guess_typer{�stat�st_sizer	rr
�st_mtimer1r�r�rr9r�r3r�rark�parse_range_headerr�r�)r��rootZmimetypeZdownloadrfrzr8Zstatsr�Zlm�imsr-�rangesr�r�rrr�static_file�	sX





r�TcCs|rtjd�t|�adS)zS Change the debug level.
    There is only one debug level supported at the moment.r
N)rf�simplefilterrr�)r�rrr�debug�	s
r�cCsJt|ttf�r|j�}nt|ttf�r0tj|�}t|t�sFtj	d|�}|S)Nz%a, %d %b %Y %H:%M:%S GMT)
rOrrZutctimetupler�r�r	r
r8r)rzrrrr
s


rcCsVy4tjj|�}tj|dd�d�|dp,dtjStttt	fk
rPdSXdS)zD Parse rfc1123, rfc850 and asctime timestamps and return UTC epoch. N�r�	)r)
�emailZutilsZparsedate_tzr	ZmktimeZtimezonerBr��
IndexError�
OverflowError)r�Ztsrrrr	
s
(rcCsdyF|jdd�\}}|j�dkrDttjt|���jdd�\}}||fSWnttfk
r^dSXdS)z] Parse rfc2617 HTTP authentication header string (basic) and return (user,pass) tuple or NoneNr)r�r�)r9r�r@�base64�	b64decoderTr�r�)r�r�r$�user�pwdrrrr�
sr�ccs�|s|dd�dkrdSdd�|dd�jd�D�}x�|D]�\}}y~|sdtd|t|��|}}n0|sxt|�|}}nt|�tt|�d|�}}d|ko�|ko�|knr�||fVWq<tk
r�Yq<Xq<WdS)	z~ Yield (start, end) ranges parsed from a HTTP Range header. Skip
        unsatisfiable ranges. The end index is non-inclusive.Nr(zbytes=cSs g|]}d|kr|jdd��qS)r�r))r9)r�rsrrrr�
sz&parse_range_header.<locals>.<listcomp>r�rr))r9r�r�r�r�)r��maxlenr�r�r�rrrr�
s"r�cCszg}xp|jd�D]b}|sq|jdd�}t|�dkr<|jd�t|djdd��}t|djdd��}|j||f�qW|S)	N�&�=r)r'r�r�+r�)r9r�r�
urlunquoter�)ZqsrsZpairZnvrqrzrrrr�-
s
r�cCs*tdd�t||�D��o(t|�t|�kS)zs Compares two strings in a cryptographically safe way:
        Runtime is not affected by length of common prefix. css"|]\}}||krdndVqdS)rr)Nr)r�r/�yrrrr�;
sz_lscmp.<locals>.<genexpr>)�sum�zipr�)rEr�rrr�_lscmp8
sr�cCsJtjtj|d��}tjtjt|�|tjd�j	��}td�|td�|S)z> Encode and sign a pickle-able object. Return a (byte) string r))�	digestmod�!r�rC)
r��	b64encode�pickler!�hmac�newrT�hashlib�md5�digest)r$rqr��sigrrrr>
s rcCsft|�}t|�rb|jtd�d�\}}t|dd�tjtjt|�|tj	d�j
���rbtjtj
|��SdS)z? Verify and decode an encoded string. Return an object or None.r�r)N)r�)rT�cookie_is_encodedr9r�r�r�r�r�r�r�r�r�r"r�)r$rqr�r�rrrr�E
s.r�cCst|jtd��otd�|k�S)z9 Return True if the argument looks like a encoded cookie.r�r�)rr�rT)r$rrrr�O
sr�cCs,|jdd�jdd�jdd�jdd�jd	d
�S)z; Escape HTML special characters ``&<>`` and quotes ``'"``. r�z&amp;�<z&lt;�>z&gt;�"z&quot;�'z&#039;)r�)�stringrrrryT
srycCs$dt|�jdd�jdd�jdd�S)z; Escape and quote a string to be used as an HTTP attribute.z"%s"r�z&#10;r�z&#13;�	z&#9;)ryr�)r�rrr�
html_quoteZ
sr�ccs�d|jjdd�jd�}t|�}t|d�t|dp6g�}|d|t|dd|��7}|Vx*|d|d�D]}|d|7}|VqtWdS)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;rr&z/<%s>N)r\r�rHr r�r)rr��specZargc�argrrrrM`
s
 rMc	Cs |dkr||fS|jd�jd�}|jd�jd�}|rD|ddkrDg}|rX|ddkrXg}|dkr�|t|�kr�|d|�}||}||d�}nT|dkr�|t|�kr�||d�}||}|d|�}n|dkr�dnd}td|��ddj|�}ddj|�}|jd��r|�r|d7}||fS)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)
    rr�r�NrFr�z"Cannot shift. Nothing left from %s)rGr9r�r�r�r;)	r�Z	path_infor�ZpathlistZ
scriptlistZmovedrZnew_script_nameZ
new_path_inforrrr2t
s.	r2�private�
Access deniedcs���fdd�}|S)ze Callback decorator to require HTTP auth (basic).
        TODO: Add route(check_auth=...) parameter. cs tj������fdd��}|S)NcsJtjpd\}}|dks"�||�r@td��}|jdd��|S�||�S)Ni�zWWW-AuthenticatezBasic realm="%s")NN)r1r�r�r,)rErdr�ZpasswordrW)�checkr�realmr�rrrb�
s
z.auth_basic.<locals>.decorator.<locals>.wrapper)r_�wraps)rrb)r�r�r�)rrr*�
szauth_basic.<locals>.decoratorr)r�r�r�r*r)r�r�r�r�
auth_basic�
s
r�cs tjtt����fdd��}|S)zA Return a callable that relays calls to the current default app. cstt���||�S)N)rvr�)rErd)rrrrb�
sz)make_default_app_wrapper.<locals>.wrapper)r_r�rvr)rrbr)rr�make_default_app_wrapper�
sr�r:r�rOrQrSr�r=r(rrDrKc@s*eZdZdZddd�Zdd�Zdd	�Zd
S)�
ServerAdapterF�	127.0.0.1�cKs||_||_t|�|_dS)N)r<r�r�r�)rZr�r�r<rrrrs�
szServerAdapter.__init__cCsdS)Nr)rZrUrrrrE�
szServerAdapter.runcCs*djdd�|jj�D��}d|jj|fS)Nz, cSs g|]\}}d|t|�f�qS)z%s=%s)r�)r�r�r�rrrr��
sz*ServerAdapter.__repr__.<locals>.<listcomp>z%s(%s))r�r<rr�r\)rZrrrrr
�
szServerAdapter.__repr__N)r�r�)r\r]r^�quietrsrEr
rrrrr��
s
r�c@seZdZdZdd�ZdS)�	CGIServerTcs(ddlm}�fdd�}|�j|�dS)Nr)�
CGIHandlercs|jdd��||�S)Nr�r�)r�)r�r0)rUrr�
fixed_environ�
sz$CGIServer.run.<locals>.fixed_environ)Zwsgiref.handlersr�rE)rZrUr�r�r)rUrrE�
sz
CGIServer.runN)r\r]r^r�rErrrrr��
sr�c@seZdZdd�ZdS)�FlupFCGIServercCs:ddl}|jjd|j|jf�|jjj|f|j�j�dS)NrZbindAddress)	Zflup.server.fcgir<r�r�r��serverZfcgi�
WSGIServerrE)rZrU�fluprrrrE�
szFlupFCGIServer.runN)r\r]r^rErrrrr��
sr�c@seZdZdd�ZdS)�
WSGIRefServercs�ddlm�m}ddlm}ddl�G��fdd�d��}�jjd|�}�jjd|�}d�jkr�t|d	��j	kr�G�fd
d�d|�}|�j�j
|||�}|j�dS)Nr)�WSGIRequestHandlerr�)�make_servercs"eZdZdd�Z��fdd�ZdS)z'WSGIRefServer.run.<locals>.FixedHandlercSs
|jdS)Nr)Zclient_address)rZrrr�address_string�
sz6WSGIRefServer.run.<locals>.FixedHandler.address_stringcs�js�j||�SdS)N)r��log_request)r�kw)r�rZrrr��
sz3WSGIRefServer.run.<locals>.FixedHandler.log_requestN)r\r]r^r�r�r)r�rZrr�FixedHandler�
sr�Z
handler_classZserver_classr��address_familycseZdZ�jZdS)z%WSGIRefServer.run.<locals>.server_clsN)r\r]r^ZAF_INET6r�r)�socketrr�
server_cls�
sr�)Zwsgiref.simple_serverr�r�r�r�r<r�r�rvZAF_INETr��
serve_forever)rZr�r�r�r�Zhandler_clsr�Zsrvr)r�rZr�rrE�
s
zWSGIRefServer.runN)r\r]r^rErrrrr��
sr�c@seZdZdd�ZdS)�CherryPyServerc
Cs�td�ddlm}|j|jf|jd<||jd<|jjd�}|rH|jd=|jjd�}|r`|jd=|jf|j�}|rx||_|r�||_	z|j
�Wd|j�XdS)Nz�The wsgi server part of cherrypy was split into a new project called 'cheroot'. Use the 'cheroot' server adapter instead of cherrypy.r)�
wsgiserver�	bind_addr�wsgi_app�certfile�keyfile)rj�cherrypyr�r�r�r<r�ZCherryPyWSGIServerZssl_certificateZssl_private_keyr��stop)rZrUr�r�r�r�rrrrE�
s$
zCherryPyServer.runN)r\r]r^rErrrrr��
sr�c@seZdZdd�ZdS)�
CherootServerc
Cs�ddlm}ddlm}|j|jf|jd<||jd<|jjdd�}|jjdd�}|jjdd�}|jf|j�}|r�|r�|j	|||�|_
z|j�Wd|j�XdS)	Nr)r{)�builtinr�r�r�r��	chainfile)
�cherootr{Zcheroot.sslr�r�r�r<r�ZServerZBuiltinSSLAdapterZssl_adapterr�r�)rZrUr{r�r�r�r�r�rrrrEs
zCherootServer.runN)r\r]r^rErrrrr�sr�c@seZdZdd�ZdS)�WaitressServercCs"ddlm}|||j|jd�dS)Nr)�serve)r�r�)�waitressr�r�r�)rZrUr�rrrrE.szWaitressServer.runN)r\r]r^rErrrrr�-sr�c@seZdZdd�ZdS)�PasteServercCsNddlm}ddlm}|||jd�}|j|f|jt|j�d�|j	��dS)Nr)�
httpserver)�TransLogger)Zsetup_console_handler)r�r�)
�paster�Zpaste.transloggerr�r�r�r�r�r�r<)rZrUr�r�rrrrE4s
zPasteServer.runN)r\r]r^rErrrrr�3sr�c@seZdZdd�ZdS)�MeinheldServercCs,ddlm}|j|j|jf�|j|�dS)Nr)r�)�meinheldr��listenr�r�rE)rZrUr�rrrrE=szMeinheldServer.runN)r\r]r^rErrrrr�<sr�c@seZdZdZdd�ZdS)�FapwsServerzA Extremely fast webserver using libev. See http://www.fapws.org/ cs�ddlj}ddlm}m}|j}t|jdd��dkr>t|�}|j	|j
|�dtjkrn|j
rntd�td�|j|��fdd	�}|jd
|f�|j�dS)Nr)rcr�r'g�������?�BOTTLE_CHILDz3WARNING: Auto-reloading does not work with Fapws3.
z/         (Fapws3 breaks python thread support)
csd|d<�||�S)NFzwsgi.multiprocessr)r�r0)rUrrr�RszFapwsServer.run.<locals>.appr�r�)Z
fapws._evwsgiZ_evwsgiZfapwsrcr�r�r�ZSERVER_IDENTr�r�r�rfr�r��_stderrZset_base_moduleZwsgi_cbrE)rZrUZevwsgircr�r�r�r)rUrrEEs

zFapwsServer.runN)r\r]r^r}rErrrrr�Csr�c@seZdZdZdd�ZdS)�
TornadoServerz< The super hyped asynchronous server by facebook. Untested. cCsVddl}ddl}ddl}|jj|�}|jj|�}|j|j|j	d�|j
jj�j
�dS)Nr)r��address)Ztornado.wsgiZtornado.httpserverZtornado.ioloopr{Z
WSGIContainerr�Z
HTTPServerr�r�r�ZioloopZIOLoop�instancer�)rZrU�tornadoZ	containerr�rrrrE[s
zTornadoServer.runN)r\r]r^r}rErrrrr�Ysr�c@seZdZdZdZdd�ZdS)�AppEngineServerz  Adapter for Google App Engine. TcsFddlm�tjjd�}|r8t|d�r8��fdd�|_�j��dS)Nr)�utilr�maincs
�j��S)N)�run_wsgi_appr)rUr�rrr0lsz%AppEngineServer.run.<locals>.<lambda>)Zgoogle.appengine.ext.webappr�r*r$r�rCr�r�)rZrUr%r)rUr�rrEfs
zAppEngineServer.runN)r\r]r^r}r�rErrrrr�csr�c@seZdZdZdd�ZdS)�
TwistedServerz Untested. cCszddlm}m}ddlm}ddlm}|�}|j�|jdd|j	�|j
|j|||��}|j|j
||jd�|j�dS)Nr)r�r{)�
ThreadPool)�reactor�afterZshutdown)Z	interface)Ztwisted.webr�r{Ztwisted.python.threadpoolr�Ztwisted.internetr�r�ZaddSystemEventTriggerr�ZSiteZWSGIResourceZ	listenTCPr�r�rE)rZrUr�r{r�r�Zthread_pool�factoryrrrrErszTwistedServer.runN)r\r]r^r}rErrrrr�psr�c@seZdZdZdd�ZdS)�DieselServerz Untested. cCs&ddlm}|||jd�}|j�dS)Nr)�WSGIApplication)r�)Zdiesel.protocols.wsgirr�rE)rZrUrr�rrrrE�szDieselServer.runN)r\r]r^r}rErrrrr~src@seZdZdZdd�ZdS)�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.
    cs�ddlm}m}ttj�|j�s,d}t|��|jjdd�rBtd�|j	rRd|jd<|j
|jf}|j||f|j��dt
jkr�ddl}|j|j�fdd	���j�dS)
Nr)�pywsgirz9Bottle requires gevent.monkey.patch_all() (before import)Zfastz<The "fast" option has been deprecated and removed by Gevent.�logr�cs�j�S)N)r�)rAr�)r�rrr0�sz"GeventServer.run.<locals>.<lambda>)rrrrOrr}r<r�rjr�r�r�r�rfr��signal�SIGINTr�)rZrUrrr�r�rr)r�rrE�s

zGeventServer.runN)r\r]r^r}rErrrrr�src@seZdZdd�ZdS)�GeventSocketIOServercCs2ddlm}|j|jf}|j||f|j�j�dS)Nr)r�)Zsocketior�r�r�ZSocketIOServerr<r�)rZrUr�r�rrrrE�szGeventSocketIOServer.runN)r\r]r^rErrrrr	�sr	c@seZdZdZdd�ZdS)�GunicornServerz? Untested. See http://gunicorn.org/configure.html for options. csTddlm}dd|jt|j�fi��j|j�G��fdd�d|�}|�j�dS)Nr)�Applicationr\z%s:%dcs$eZdZ�fdd�Z�fdd�ZdS)z/GunicornServer.run.<locals>.GunicornApplicationcs�S)Nr)rZ�parserZoptsr)r�rr�init�sz4GunicornServer.run.<locals>.GunicornApplication.initcs�S)Nr)rZ)rUrrrL�sz4GunicornServer.run.<locals>.GunicornApplication.loadN)r\r]r^r
rLr)r�rUrr�GunicornApplication�sr)Zgunicorn.app.baserr�r�r�r&r<rE)rZrUrrr)r�rUrrE�s
zGunicornServer.runN)r\r]r^r}rErrrrr
�sr
c@seZdZdZdd�ZdS)�EventletServerz
 Untested cCsfddlm}m}y$|j||j|jf�||jd�Wn,tk
r`|j||j|jf�|�YnXdS)Nr)r{r�)Z
log_output)�eventletr{r�r�r�r�r�rB)rZrUr{r�rrrrE�szEventletServer.runN)r\r]r^r}rErrrrr�src@seZdZdZdd�ZdS)�RocketServerz Untested. cCs0ddlm}||j|jfdd|i�}|j�dS)Nr)�Rocketr{r�)�rocketrr�r�r�)rZrUrr�rrrrE�szRocketServer.runN)r\r]r^r}rErrrrr�src@seZdZdZdd�ZdS)�BjoernServerz? Fast server written in C: https://github.com/jonashaag/bjoern cCs ddlm}|||j|j�dS)Nr)rE)�bjoernrEr�r�)rZrUrErrrrE�szBjoernServer.runN)r\r]r^r}rErrrrr�src@s(eZdZdZeeeeee	gZ
dd�ZdS)�
AutoServerz Untested. cCsDx>|jD]4}y||j|jf|j�j|�Stk
r:YqXqWdS)N)�adaptersr�r�r<rEr#)rZrUZsarrrrE�s
zAutoServer.runN)r\r]r^r}r�r�r�r�r�r�rrErrrrr�sr)r�r�r	r�r�r�r�Zfapws3r�ZgaeZtwistedZdieselr�ZgunicornrrZgeventSocketIOrrr�cKs�d|kr|jdd�n|df\}}|tjkr2t|�|s@tj|S|j�rXttj||�S|jd�d}tj|||<td||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(rz%s.%s)r9r*r$r,�isalnumrv�eval)r�rEr%Zpackage_namerrrrL�s 

rLcCs@dta}z tj�}t|�}t|�r(|S|Stj|�|aXdS)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_apprWrLrAr&)r�Znr_oldr�rrrr�load_apps

r�	127.0.0.1�c	Ks�trdS|otjjd��rz�y�d}
tjddd�\}}
tj|�x�tjj|
�r�t	j
gt	j}tjj�}
d|
d<|
|
d<t
j||
d�}x&|j�dkr�tj|
d�tj|�q�W|j�d	krBtjj|
�r�tj|
�t	j|j��qBWWntk
r�YnXWdtjj|
��rtj|
�XdS�yj|dk	�r0t|�|�p:t�}t|t��rPt|�}t|��sftd
|��x|�ppgD]}|j|��qrW|tk�r�tj|�}t|t��r�t|�}t|t ��r�|f||d�|	��}t|t!��s�td|��|j"�p�||_"|j"�s,t#d
t$t%|�f�t#d|j&|j'f�t#d�|�rztjjd�}
t(|
|�}|�|j)|�WdQRX|j*dk�r�t	jd	�n
|j)|�Wnjtk
�r�YnVt+t,fk
�r��Yn<|�sĂt-|d|��s�t.�tj|�t	jd	�YnXdS)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�zbottle.z.lock)r��suffix�trueZBOTTLE_LOCKFILE)r�r&zApplication is not callable: %r)r�r�z!Unknown or unsupported server: %rz,Bottle v%s server starting up (using %s)...
zListening on http://%s:%d/
zHit Ctrl-C to quit.

�reloadr�)/rrfr�r��tempfileZmkstempr[r�rlr*�
executable�argvr��
subprocess�PopenZpoll�utimer	�sleep�unlink�exitr`�_debugrrOr8rrAr�r�server_namesrLr�r�r�r��__version__r�r�r��FileCheckerThreadrEr+rarbrvr)r�r�r�r��interval�reloaderr�r�r�rI�lockfile�fdrr�r�rZbgcheckrrrrE!s�









rEc@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)r.zw Interrupt main-thread as soon as a changed module file is detected,
        the lockfile gets deleted or gets to old. cCs$tjj|�|||_|_d|_dS)N)r�Threadrsr1r/r+)rZr1r/rrrrs�szFileCheckerThread.__init__cCstjj}dd�}t�}xZttjj��D]H}t|dd�p8d}|dd�d
krV|dd�}|r&||�r&||�||<q&Wx�|j	�s||j
�s�||j
�tj�|jd	kr�d
|_	t
j�x>t|j��D].\}}||�s�||�|kr�d|_	t
j�Pq�Wtj|j�qtWdS)NcSstj|�jS)N)rfr�r�)r�rrrr0�sz'FileCheckerThread.run.<locals>.<lambda>rr�r��.pyo�.pycr)rAr�r!���)r4r5rC)rfr�rlrmrkr*r$r�rvr+r1r	r/�thread�interrupt_mainrr()rZrl�mtimer�r%r�ZlmtimerrrrE�s(
zFileCheckerThread.runcCs|j�dS)N)r�)rZrrr�	__enter__�szFileCheckerThread.__enter__cCs&|jsd|_|j�|dk	o$t|t�S)Nr*)r+r�r�r`)rZ�exc_typeZexc_valZexc_tbrrr�__exit__�szFileCheckerThread.__exit__N)r\r]r^r}rsrEr:r<rrrrr.}s
r.c@seZdZdd�ZdS)�
TemplateErrorcCstj|d|�dS)Ni�)r�rs)rZrirrrrs�szTemplateError.__init__N)r\r]r^rsrrrrr=�sr=c@sbeZdZdZddddgZiZiZddgdfdd	�Zegfd
d��Z	edd
��Z
dd�Zdd�ZdS)�BaseTemplatez2 Base class and minimal API for template adapters �tplZhtmlZthtmlZstplNrNcKs�||_t|d�r|j�n||_t|d�r.|jnd|_dd�|D�|_||_|jj�|_|jj	|�|jr�|jr�|j
|j|j�|_|js�tdt|���|jr�|jr�td��|j
f|j�dS)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.
        rdr�NcSsg|]}tjj|��qSr)rfr�rg)r�r/rrrr��sz)BaseTemplate.__init__.<locals>.<listcomp>zTemplate %s not found.zNo template specified.)rrCrdrPr�rrr8�settingsr�r&ror=r�r�)rZrPrrrr8r@rrrrs�szBaseTemplate.__init__cCs�|std�dg}tjj|�r>tjj|�r>td�tjj|�Sx�|D]x}tjj|�tj}tjjtjj||��}|j|�s|qDtjj|�r�|Sx.|j	D]$}tjjd||f�r�d||fSq�WqDWdS)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.z%s.%sN)
rjrfr��isabsrqrgrir�r��
extensions)rxrrrZspathru�extrrrro�s 

zBaseTemplate.searchcGs.|r |jj�|_|d|j|<n
|j|SdS)zB This reads or sets the global settings stored in class.settings. rN)r@r�)rxrqrrrr�
global_config�szBaseTemplate.global_configcKst�dS)z� Run preparations (parsing, caching, ...).
        It should be possible to call this again to refresh a template or to
        update settings.
        N)�NotImplementedError)rZr<rrrr��szBaseTemplate.preparecOst�dS)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).
        N)rE)rZrr)rrr�render�szBaseTemplate.render)
r\r]r^r}rBr@rrs�classmethodrorDr�rFrrrrr>�s	r>c@seZdZdd�Zdd�ZdS)�MakoTemplatecKs�ddlm}ddlm}|jd|ji�|jdtt��|fd|j	i|��}|j
rl||j
fd|i|��|_n|f|j|j
|d�|��|_dS)	Nr)�Template)�TemplateLookupr8Zformat_exceptionsZdirectoriesrr)Zurir�rr)Z
mako.templaterIZmako.lookuprJr&r8r�rr�rrrPr?rr�)rZr<rIrJrrrrrr�
szMakoTemplate.preparecOs:x|D]}|j|�qW|jj�}|j|�|jjf|�S)N)r&rr�r?rF)rZrr)�dictarg�	_defaultsrrrrF
s



zMakoTemplate.renderN)r\r]r^r�rFrrrrrH
srHc@seZdZdd�Zdd�ZdS)�CheetahTemplatecKsdddlm}tj�|_i|j_|jjg|d<|jrJ|fd|ji|��|_n|fd|ji|��|_dS)Nr)rIZ
searchListrPr�)	ZCheetah.TemplaterIrrr�varsrPr?r�)rZr<rIrrrr�
s
zCheetahTemplate.preparecOsPx|D]}|j|�qW|jjj|j�|jjj|�t|j�}|jjj�|S)N)r&rrNrr�r?rS)rZrr)rKrmrrrrF 
s

zCheetahTemplate.renderN)r\r]r^r�rFrrrrrM
s
rMc@s,eZdZddifdd�Zdd�Zdd�ZdS)�Jinja2TemplateNcKs�ddlm}m}d|kr td��|fd||j�i|��|_|rL|jjj|�|r^|jjj|�|rp|jj	j|�|j
r�|jj|j
�|_n|jj
|j�|_dS)Nr)�Environment�FunctionLoaderr�zsThe keyword argument `prefix` has been removed. Use the full jinja2 environment name line_statement_prefix instead.�loader)Zjinja2rPrQr}rRr�r�r&�tests�globalsrPZfrom_stringr?Zget_templater�)rZr�rSrTr)rPrQrrrr�*
szJinja2Template.preparecOs:x|D]}|j|�qW|jj�}|j|�|jjf|�S)N)r&rr�r?rF)rZrr)rKrLrrrrF8
s



zJinja2Template.renderc	Cs<|j||j�}|sdSt|d��}|j�j|j�SQRXdS)Nr�)rorrrardrVr8)rZrrur�rrrrR>
s
zJinja2Template.loader)r\r]r^r�rFrRrrrrrO)
srOc@sXeZdZeddfdd�Zedd��Zedd��Zdd	d
�Zddd�Z	d
d�Z
dd�ZdS)�SimpleTemplateFNcsJi|_|j��fdd�|_��fdd�|_||_|rF|j|j|_|_dS)Ncs
t|��S)N)r@)r/)rSrrr0J
sz(SimpleTemplate.prepare.<locals>.<lambda>cs�t|���S)N)r@)r/)rS�escape_funcrrr0K
s)rer8�_str�_escape�syntax)rZrVZnoescaperYrdr)rSrVrr�G
szSimpleTemplate.preparecCst|j|jpdd�S)Nz<string>rM)r�rVr�)rZrrr�coP
szSimpleTemplate.cocCs�|j}|s*t|jd��}|j�}WdQRXyt|�d}}Wn,tk
rhtd�t|d�d}}YnXt|||jd�}|j	�}|j
|_
|S)Nr�rNz;Template encodings other than utf8 are no longer supported.r7)r8rY)rPrar�rdr@r^rj�
StplParserrY�	translater8)rZrPr�r8rrVrrrrVT
szSimpleTemplate.codecKs"|dkrtdd�||f|d<dS)NzQRebase function called without arguments. You were probably looking for {{base}}?T�_rebase)rj)rZ�_envr�r)rrrr]d
szSimpleTemplate._rebasecKsZ|dkrtdd�|j�}|j|�||jkrD|j||jd�|j|<|j|j|d|�S)NzQRebase function called without arguments. You were probably looking for {{base}}?T)rrr�_stdout)rjr�r&rer�rr�execute)rZr^r�r)r�rrr�_includej
s

zSimpleTemplate._includecCs�|jj�}|j|�|j||jtj|j|�tj|j|�d|j|j	|j
|j|jd�
�t
|j|�|j
d�r�|jd�\}}dj|�|d<|dd�=|j||f|�S|S)N)
r_Z
_printlist�include�rebaser]rWrXr�r�Zdefinedr]r�rc)rr�r&�extendr_rrar]rWrXr�r�r�rrZr�r�)rZr_r)r�ZsubtplZrargsrrrr`t
s




zSimpleTemplate.executecOs@i}g}x|D]}|j|�qW|j|�|j||�dj|�S)zA Render the template using keyword arguments as local variables. r�)r&r`r�)rZrr)r�r-rKrrrrF�
s

zSimpleTemplate.render)N)N)r\r]r^ryr�r~rZrVr]rar`rFrrrrrUE
s	


rUc@seZdZdS)�StplSyntaxErrorN)r\r]r^rrrrre�
srec@s�eZdZdZiZdZejdd�Zed7Zed7Zed7Zed7Zed	7Zed
7Zed7ZdZd
eZdeZdZ	d$dd�Z
dd�Zdd�Ze
ee�Zdd�Zdd�Zdd�Zdd�Zd%d d!�Zd"d#�ZdS)&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
<% %> % {{ }}NrNcCsTt||�||_|_|j|p |j�gg|_|_d\|_|_d\|_	|_
d|_dS)Nr)r)r)r)rr)r@rPr8�
set_syntax�default_syntax�code_buffer�text_buffer�linenor��indent�
indent_mod�paren_depth)rZrPrYr8rrrrs�
szStplParser.__init__cCs|jS)z= Tokens as a space separated string (default: <% %> % {{ }}) )�_syntax)rZrrr�
get_syntax�
szStplParser.get_syntaxcs�||_|j�|_||jkrjd}ttj|j�}tt|j�|���|j	|j
|jf}�fdd�|D�}||j|<|j|\|_|_
|_dS)Nz:block_start block_close line_start inline_start inline_endcsg|]}tj|���qSr)r�r�)r�r�)�pattern_varsrrr��
sz)StplParser.set_syntax.<locals>.<listcomp>)rnr9�_tokens�	_re_cache�mapr�r�rmr��	_re_split�_re_tok�_re_inl�re_split�re_tok�re_inl)rZrYr<ZetokensZpatternsr)rprrf�
s


zStplParser.set_syntaxcCs�|jrtd���xJ|jj|j|jd��}|�rV|j|j|j|j��}|jj|�|j|j�7_|j	d�r�|j|jd�j
d�\}}}|jj|j	d�|j	d�||�|jt||�d7_qnf|j	d��r6td�|j|jd�j
d�\}}}|jj|j	d�||�|jt||�d7_q|j
�|jt|j	d��d�qPqW|jj|j|jd��|j
�d	j|j�S)
NzParser is a one time instance.r)r�r'rAz#Escape code lines with a backslash.r�)�	multiliner�)r�r}rwrorPr�rirr�r�r�r�rj�
flush_text�	read_coderr�rh)rZr�r��linerir�rrrr\�
s2
$zStplParser.translatec	Cs�d\}}�x�|jj|j|jd��}|s\||j|jd�7}t|j�|_|j|j�|�dS||j|j|j|j��7}|j|j�7_|j	�\	}}}}}	}
}}}
|s�|j
dkr�|	s�|
r�||	p�|
7}q|r�||7}q|�r|}|�r�|j�j|jd��r�d}q|�r"|j
d7_
||7}q|�rL|j
dk�rB|j
d8_
||7}q|	�rn|	d}|_
|jd7_q|
�r�|
d}|_
q|�r�|jd8_q|�r�|�r�d}n||7}q|j|j�|�|jd7_d\}}|_
|sPqWdS)	Nr�rr)F)r�r�rCrC)r�r�r)rxrorPr�r��
write_coderGr�r�r�rmr;rqrlrkrj)rZrzZ	code_line�commentr�rWZ_comZ_poZ_pcZ_blk1Z_blk2Z_endZ_cendZ_nlrrrr|�
sV



zStplParser.read_codec	Csvdj|j�}|jdd�=|s dSgddd|j}}}x�|jj|�D]r}|||j��|j�}}|r�|j|jtt	|j
d����|jd�r�|d|7<|j|j|j
d�j���qHW|t|�k�rB||d�}|j
d�}|djd��r|ddd�|d<n$|djd
��r,|ddd�|d<|j|jtt	|���dd
j|�}|j|jd�d7_|j|�dS)Nr�rz\
z  Tr�r)z\\
r&z\\
r�z_printlist((%s,))z, rCrCrC���rCrCrCr6rC)r�rirkryr�r�r�rrsr��
splitlinesr;�process_inliner�rGr�rj�countr~)	rZr��parts�pos�nlr�r��linesrVrrrr{s.

zStplParser.flush_textcCs$|ddkrd|dd�Sd|S)Nrr�z_str(%s)r)z_escape(%s)r)rZ�chunkrrrr�)szStplParser.process_inlinecCsD|j||�\}}d|j|j}||j�|d7}|jj|�dS)Nz  r�)�fix_backward_compatibilityrkrlrHrhr)rZr}rrVrrrr~-szStplParser.write_codecCs�|j�jdd�}|rh|ddkrhtd�t|�dkr<d|fSt|�dkrXdt|�|fSd	t|�|fS|jdkr�|j�r�d
|kr�tjd|�}|r�td�|jd�}|j	j
|j�j|�|_	||_||j
d
d
�fS||fS)Nr'rrbrcz2The include and rebase keywords are functions now.r)z_printlist([base])z_=%s(%r)z_=%s(%r, %s)Zcodingz#.*coding[:=]\s*([-\w.]+)z4PEP263 encoding strings in templates are deprecated.zcoding*)rbrc)rGr9rjr�rrjr�r�r�rPrQr8rVr�)rZr}rr�r�rSrrrr�3s"
z%StplParser.fix_backward_compatibility)NrN)r�)r\r]r^r}rrrur�rvrtrgrsrorfrrYr\r|r{r�r~r�rrrrr[�
s2

/
r[cOs
|r|dnd}|jdt�}|jdt�}t|�|f}|tks@tr�|jdi�}t||�rt|t|<|r�t|jf|�nRd|ks�d|ks�d|ks�d	|kr�|f||d
�|��t|<n|f||d�|��t|<t|s�tdd
|�x|dd�D]}|j	|�q�Wt|j
|�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).
    rN�template_adapterZtemplate_lookupZtemplate_settingsr��{�%�$)rPrr)rrri�zTemplate (%s) not foundr))r�rU�
TEMPLATE_PATH�id�	TEMPLATESr�rOr�r�r&rF)rr)r?ZadapterrrZtplidr@rKrrrrXEs$
 rX)r�cs��fdd�}|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.
    cstj�����fdd��}|S)NcsL�||�}t|ttf�r6�j�}|j|�t�f|�S|dkrHt���S|S)N)rOrmrJr�r&rX)rr)�resultZtplvars)rr�tpl_namerrrbns


z(view.<locals>.decorator.<locals>.wrapper)r_r�)rrb)rr�)rrr*ms
zview.<locals>.decoratorr)r�rr*r)rr�rrcs
rz./z./views/zI'm a teapoti�zUnprocessable Entityi�zPrecondition Requiredi�zToo Many Requestsi�zRequest Header Fields Too Largei�zNetwork Authentication Requiredi�ccs"|]\}}|d||ffVqdS)z%d %sNr)r�r�r�rrrr��sr�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(rNZ	localhostr��]z[])r�r�r�r0r�r�)r&rr)r'r(r)r&r)r)r&r'r)r&r&r)rN)rNrU)F)N)r�r�)N�)r�)r�Fr�)T)r)r))r�r�)	Nr	rrr)FFNN(r}Z
__future__r�
__author__r-Z__license__r\ZoptparserZ_cmd_parserZ
add_optionZ_opt�
parse_argsZ_cmd_optionsZ	_cmd_argsr�r�Z
gevent.monkeyrZmonkeyZ	patch_allr�r�Zemail.utilsr�r_r�r4r�rfr�r%r*r"rr	rfr�rr
rrrrrrZunicodedatar�inspectrr r#Z
simplejsonr!r%r"r?r�Zdjango.utils.simplejson�version_info�pyrZpy25r�r,r-r.r1r_r�rtZhttp.clientZclientZhttplib�_threadr7Zurllib.parser2r3r�r4r5r�r6r�rZhttp.cookiesr9�collections.abcr:rJ�typesr;r<�collectionsZimpr��ior=Zconfigparserr>r�r8rPr�rArsrGrFZurlparseZurllibZCookieZcPicklerHrIr�rgrh�UserDictrKrRrr�rTr@r�rXrYr`rjrnr�ror~r�rcr�r�r�r�r�r�r�r�r�rr|r�r�r�r�rrrr�ZResponser3r�rrrrr/r�r�r~rmr�rkrVrirlrr�r�r�r�r�r�rrr�r�r�r�rr�r�ryr�rMr2r�r�r:r�rOrQrSr�r=r(rrDr�r�r�r�r�r�r�r�r�r�r�r�r�r�rrr	r
rrrrr,rLrr+rEr3r.r=r>rHrMrOrUrer[rXZ
mako_templateZcheetah_templateZjinja2_templaterZ	mako_viewZcheetah_viewZjinja2_viewr�r�r�rZ	responsesZ
HTTP_CODESrr�rYr1r]rr�rrWr%rC�optrr�versionr*Z
print_helpr�r$r$r�r\r�r��rfindr)rGr�r!rrrrr�<module>
s.

�




	Iw%c
$I.2NVL
Q	



	





Z1OH6$(

0