| 1 |
""" |
|---|
| 2 |
Copyright (c) 2004, CherryPy Team (team@cherrypy.org) |
|---|
| 3 |
All rights reserved. |
|---|
| 4 |
|
|---|
| 5 |
Redistribution and use in source and binary forms, with or without modification, |
|---|
| 6 |
are permitted provided that the following conditions are met: |
|---|
| 7 |
|
|---|
| 8 |
* Redistributions of source code must retain the above copyright notice, |
|---|
| 9 |
this list of conditions and the following disclaimer. |
|---|
| 10 |
* Redistributions in binary form must reproduce the above copyright notice, |
|---|
| 11 |
this list of conditions and the following disclaimer in the documentation |
|---|
| 12 |
and/or other materials provided with the distribution. |
|---|
| 13 |
* Neither the name of the CherryPy Team nor the names of its contributors |
|---|
| 14 |
may be used to endorse or promote products derived from this software |
|---|
| 15 |
without specific prior written permission. |
|---|
| 16 |
|
|---|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|---|
| 18 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|---|
| 19 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|---|
| 20 |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
|---|
| 21 |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|---|
| 22 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|---|
| 23 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|---|
| 24 |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
|---|
| 25 |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 26 |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 27 |
""" |
|---|
| 28 |
|
|---|
| 29 |
""" |
|---|
| 30 |
Global module that all modules developing with CherryPy should import. |
|---|
| 31 |
""" |
|---|
| 32 |
|
|---|
| 33 |
__version__ = '2.1.0' |
|---|
| 34 |
|
|---|
| 35 |
import datetime |
|---|
| 36 |
import sys |
|---|
| 37 |
import types |
|---|
| 38 |
|
|---|
| 39 |
from _cperror import * |
|---|
| 40 |
import config |
|---|
| 41 |
import _cpserver |
|---|
| 42 |
server = _cpserver.Server() |
|---|
| 43 |
|
|---|
| 44 |
codecoverage = False |
|---|
| 45 |
|
|---|
| 46 |
try: |
|---|
| 47 |
from threading import local |
|---|
| 48 |
except ImportError: |
|---|
| 49 |
from cherrypy._cpthreadinglocal import local |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
serving = local() |
|---|
| 55 |
|
|---|
| 56 |
class _AttributeDump: |
|---|
| 57 |
pass |
|---|
| 58 |
|
|---|
| 59 |
class _ThreadLocalProxy: |
|---|
| 60 |
|
|---|
| 61 |
def __init__(self, attrname): |
|---|
| 62 |
self.__dict__["__attrname__"] = attrname |
|---|
| 63 |
|
|---|
| 64 |
def purge__(self): |
|---|
| 65 |
"""Make a new, emtpy proxied object in cherrypy.serving.""" |
|---|
| 66 |
setattr(serving, self.__attrname__, _AttributeDump()) |
|---|
| 67 |
|
|---|
| 68 |
def __getattr__(self, name): |
|---|
| 69 |
childobject = getattr(serving, self.__attrname__) |
|---|
| 70 |
return getattr(childobject, name) |
|---|
| 71 |
|
|---|
| 72 |
def __setattr__(self, name, value): |
|---|
| 73 |
childobject = getattr(serving, self.__attrname__) |
|---|
| 74 |
setattr(childobject, name, value) |
|---|
| 75 |
|
|---|
| 76 |
def __delattr__(self, name): |
|---|
| 77 |
childobject = getattr(serving, self.__attrname__) |
|---|
| 78 |
delattr(childobject, name) |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
request = _ThreadLocalProxy('request') |
|---|
| 84 |
response = _ThreadLocalProxy('response') |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
threadData = local() |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
from lib.filter import sessionfilter |
|---|
| 91 |
session = sessionfilter.SessionWrapper() |
|---|
| 92 |
_sessionDataHolder = {} |
|---|
| 93 |
_sessionLockDict = {} |
|---|
| 94 |
_sessionLastCleanUpTime = datetime.datetime.now() |
|---|
| 95 |
|
|---|
| 96 |
def expose(func=None, alias=None): |
|---|
| 97 |
"""Expose the function, optionally providing an alias or set of aliases.""" |
|---|
| 98 |
|
|---|
| 99 |
def expose_(func): |
|---|
| 100 |
func.exposed = True |
|---|
| 101 |
if alias is not None: |
|---|
| 102 |
if isinstance(alias, basestring): |
|---|
| 103 |
parentDict[alias] = func |
|---|
| 104 |
else: |
|---|
| 105 |
for a in alias: |
|---|
| 106 |
parentDict[a] = func |
|---|
| 107 |
return func |
|---|
| 108 |
|
|---|
| 109 |
parentDict = sys._getframe(1).f_locals |
|---|
| 110 |
if isinstance(func, (types.FunctionType, types.MethodType)): |
|---|
| 111 |
|
|---|
| 112 |
return expose_(func) |
|---|
| 113 |
else: |
|---|
| 114 |
|
|---|
| 115 |
if alias is None: |
|---|
| 116 |
alias = func |
|---|
| 117 |
return expose_ |
|---|
| 118 |
|
|---|
| 119 |
def log(msg, context='', severity=0): |
|---|
| 120 |
"""Syntactic sugar for writing to the (error) log.""" |
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
import _cputil |
|---|
| 124 |
logfunc = _cputil.getSpecialAttribute('_cpLogMessage') |
|---|
| 125 |
logfunc(msg, context, severity) |
|---|
| 126 |
|
|---|