| 1 |
"""Global module that all modules developing with CherryPy should import.""" |
|---|
| 2 |
|
|---|
| 3 |
__version__ = '2.3.0' |
|---|
| 4 |
|
|---|
| 5 |
import datetime |
|---|
| 6 |
import sys |
|---|
| 7 |
import types |
|---|
| 8 |
|
|---|
| 9 |
from _cperror import * |
|---|
| 10 |
import config |
|---|
| 11 |
|
|---|
| 12 |
import _cptree |
|---|
| 13 |
tree = _cptree.Tree() |
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
root = None |
|---|
| 17 |
|
|---|
| 18 |
lowercase_api = False |
|---|
| 19 |
|
|---|
| 20 |
import _cpserver |
|---|
| 21 |
server = _cpserver.Server() |
|---|
| 22 |
|
|---|
| 23 |
codecoverage = False |
|---|
| 24 |
|
|---|
| 25 |
try: |
|---|
| 26 |
from threading import local |
|---|
| 27 |
except ImportError: |
|---|
| 28 |
from cherrypy._cpthreadinglocal import local |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
serving = local() |
|---|
| 35 |
|
|---|
| 36 |
class _ThreadLocalProxy: |
|---|
| 37 |
|
|---|
| 38 |
def __init__(self, attrname): |
|---|
| 39 |
self.__dict__["__attrname__"] = attrname |
|---|
| 40 |
|
|---|
| 41 |
def __getattr__(self, name): |
|---|
| 42 |
try: |
|---|
| 43 |
childobject = getattr(serving, self.__attrname__) |
|---|
| 44 |
except AttributeError: |
|---|
| 45 |
raise AttributeError("cherrypy.%s has no properties outside of " |
|---|
| 46 |
"an HTTP request." % self.__attrname__) |
|---|
| 47 |
return getattr(childobject, name) |
|---|
| 48 |
|
|---|
| 49 |
def __setattr__(self, name, value): |
|---|
| 50 |
try: |
|---|
| 51 |
childobject = getattr(serving, self.__attrname__) |
|---|
| 52 |
except AttributeError: |
|---|
| 53 |
raise AttributeError("cherrypy.%s has no properties outside of " |
|---|
| 54 |
"an HTTP request." % self.__attrname__) |
|---|
| 55 |
setattr(childobject, name, value) |
|---|
| 56 |
|
|---|
| 57 |
def __delattr__(self, name): |
|---|
| 58 |
try: |
|---|
| 59 |
childobject = getattr(serving, self.__attrname__) |
|---|
| 60 |
except AttributeError: |
|---|
| 61 |
raise AttributeError("cherrypy.%s has no properties outside of " |
|---|
| 62 |
"an HTTP request." % self.__attrname__) |
|---|
| 63 |
delattr(childobject, name) |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
request = _ThreadLocalProxy('request') |
|---|
| 69 |
response = _ThreadLocalProxy('response') |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
thread_data = local() |
|---|
| 73 |
threadData = thread_data |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
from filters import sessionfilter |
|---|
| 77 |
session = sessionfilter.SessionWrapper() |
|---|
| 78 |
_session_data_holder = {} |
|---|
| 79 |
_session_lock_dict = {} |
|---|
| 80 |
_session_last_clean_up_time = datetime.datetime.now() |
|---|
| 81 |
|
|---|
| 82 |
def expose(func=None, alias=None): |
|---|
| 83 |
"""Expose the function, optionally providing an alias or set of aliases.""" |
|---|
| 84 |
|
|---|
| 85 |
def expose_(func): |
|---|
| 86 |
func.exposed = True |
|---|
| 87 |
if alias is not None: |
|---|
| 88 |
if isinstance(alias, basestring): |
|---|
| 89 |
parents[alias.replace(".", "_")] = func |
|---|
| 90 |
else: |
|---|
| 91 |
for a in alias: |
|---|
| 92 |
parents[a.replace(".", "_")] = func |
|---|
| 93 |
return func |
|---|
| 94 |
|
|---|
| 95 |
parents = sys._getframe(1).f_locals |
|---|
| 96 |
if isinstance(func, (types.FunctionType, types.MethodType)): |
|---|
| 97 |
|
|---|
| 98 |
return expose_(func) |
|---|
| 99 |
else: |
|---|
| 100 |
|
|---|
| 101 |
if alias is None: |
|---|
| 102 |
alias = func |
|---|
| 103 |
return expose_ |
|---|
| 104 |
|
|---|
| 105 |
def log(msg='', context='', severity=0, traceback=False): |
|---|
| 106 |
"""Syntactic sugar for writing to the (error) log.""" |
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
import _cputil |
|---|
| 110 |
logfunc = _cputil.get_special_attribute('_cp_log_message', '_cpLogMessage') |
|---|
| 111 |
|
|---|
| 112 |
if traceback: |
|---|
| 113 |
msg += _cputil.formatExc() |
|---|
| 114 |
|
|---|
| 115 |
logfunc(msg, context, severity) |
|---|