Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

root/branches/cherrypy-2.x/cherrypy/__init__.py

Revision 1859 (checked in by dowski, 9 months ago)

Woops - missed a version# in setup.py. Also expanded to n.n.n version numbers (from n.n).

  • Property svn:eol-style set to native
Line 
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 # Legacy code may clobber this.
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 # Create a threadlocal object to hold the request and response
31 # objects. In this way, we can easily dump those objects when
32 # we stop/start a new HTTP conversation, yet still refer to
33 # them as module-level globals in a thread-safe way.
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 # Create request and response object (the same objects will be used
66 #   throughout the entire life of the webserver, but will redirect
67 #   to the "serving" object)
68 request = _ThreadLocalProxy('request')
69 response = _ThreadLocalProxy('response')
70
71 # Create thread_data object as a thread-specific all-purpose storage
72 thread_data = local()
73 threadData = thread_data # Backward compatibility
74
75 # Create variables needed for session (see lib/sessionfilter.py for more info)
76 from filters import sessionfilter
77 session = sessionfilter.SessionWrapper()
78 _session_data_holder = {} # Needed for RAM sessions only
79 _session_lock_dict = {} # Needed for RAM sessions only
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         # expose is being called directly, before the method has been bound
98         return expose_(func)
99     else:
100         # expose is being called as a decorator
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     # Load _cputil lazily to avoid circular references, and
108     # to allow profiler and coverage tools to work on it.
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)
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets