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

root/branches/cherrypy-2.1/cherrypy/__init__.py

Revision 744 (checked in by rdelon, 3 years ago)

Preparing for 2.1.0 release

Line 
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 # Create a threadlocal object to hold the request and response objects.
52 # In this way, we can easily dump those objects when we stop/start a
53 # new HTTP conversation.
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 # Create request and response object (the same objects will be used
81 #   throughout the entire life of the webserver, but will redirect
82 #   to the "serving" object)
83 request = _ThreadLocalProxy('request')
84 response = _ThreadLocalProxy('response')
85
86 # Create threadData object as a thread-specific all-purpose storage
87 threadData = local()
88
89 # Create variables needed for session (see lib/sessionfilter.py for more info)
90 from lib.filter import sessionfilter
91 session = sessionfilter.SessionWrapper()
92 _sessionDataHolder = {} # Needed for RAM sessions only
93 _sessionLockDict = {} # Needed for RAM sessions only
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         # expose is being called directly, before the method has been bound
112         return expose_(func)
113     else:
114         # expose is being called as a decorator
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     # Load _cputil lazily to avoid circular references, and
122     # to allow profiler and coverage tools to work on it.
123     import _cputil
124     logfunc = _cputil.getSpecialAttribute('_cpLogMessage')
125     logfunc(msg, context, severity)
126
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets