Package gluon :: Module globals
[hide private]
[frames] | no frames]

Source Code for Module gluon.globals

 1  """ 
 2  This file is part of web2py Web Framework (Copyrighted, 2007) 
 3  Developed by Massimo Di Pierro <mdipierro@cs.depaul.edu> 
 4  License: GPL v2 
 5  """ 
 6   
 7  from storage import Storage 
 8  from compileapp import run_view_in 
 9  from streamer import streamer 
10  from xmlrpc import handler 
11  from contenttype import contenttype 
12  from html import xmlescape 
13  import sys, cPickle, cStringIO, thread, time, shelve, os, stat 
14   
15  __all__=['Request','Response','Session'] 
16   
17 -class Request(Storage):
18 """ 19 defines the request object and the default values of its members 20 """
21 - def __init__(self):
22 self.env=Storage() 23 self.cookies=Storage() 24 self.get_vars=Storage() 25 self.post_vars=Storage() 26 self.vars=Storage() 27 self.application=None 28 self.function=None 29 self.args=[]
30 pass
31
32 -class Response(Storage):
33 """ 34 defines the response object and the default values of its members 35 response.write(....) can be used to write in the output html 36 """
37 - def __init__(self):
38 self.status=200 39 self.headers=Storage() 40 self.body=cStringIO.StringIO() 41 self.session_id=None 42 self.cookies=Storage() 43 self.keywords='' # used by the default view layout 44 self.description='' # used by the default view layout 45 self.flash=None # used by the default view layout 46 self.menu=None # used by the default view layout 47 self._vars=None 48 self._view_environment=None
49 - def write(self,data,escape=True):
50 if not escape: self.body.write(str(data)) 51 else: self.body.write(xmlescape(data))
52 - def render(self,*a,**b):
53 if len(a)>1 or (len(a)==1 and not hasattr(a[0],'items')): 54 raise SyntaxError 55 if len(a): self._vars=a[0] 56 else: self._vars={} 57 for key,value in b.items(): self._vars[key]=value 58 for key,value in self._vars.items(): self._view_environment[key]=value 59 run_view_in(self._view_environment) 60 self.body=self.body.getvalue() 61 return self.body
62 - def stream(self,stream,chunk_size=10**6):
63 """ 64 if a controller function 65 > return response.stream(file,100) 66 the file content will be streamed at 100 bytes at the time 67 """ 68 if hasattr(stream,'name'): filename=stream.name 69 else: filename=None 70 keys=[item.lower() for item in self.headers.keys()] 71 if filename and not 'content-type' in keys: 72 self.headers['Content-Type']=contenttype(filename) 73 if filename and not 'content-length' in keys: 74 self.headers['Content-Length']=os.stat(filename)[stat.ST_SIZE] 75 self.body=streamer(stream,chunk_size) 76 return self.body
77 - def xmlrpc(self,request,methods):
78 """ 79 assuming: 80 > def add(a,b): return a+b 81 if a controller function "func" 82 > return response.xmlrpc(request,[add]) 83 the controller will be able to handle xmlrpc requests for 84 the add function. Example: 85 > import xmlrpclib 86 > connection=xmlrpclib.ServerProxy('http://hostname/app/contr/func') 87 > print connection.add(3,4) 88 """ 89 return handler(request,self,methods)
90
91 -class Session(Storage):
92 """ 93 defines the session object and the default values of its members (None) 94 """ 95 pass 96