comparison project1/project1.py @ 0:032bc65ebafc

added core components
author George Fazekas <gyorgy.fazekas@eecs.qmul.ac.uk>
date Wed, 06 Mar 2013 15:45:48 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:032bc65ebafc
1 """
2 Tutorial - Multiple objects
3
4 This tutorial shows you how to create a site structure through multiple
5 possibly nested request handler objects.
6 """
7
8 import cherrypy, os
9 from cherrypy.lib.static import serve_file
10
11 PATH = os.path.abspath(os.path.dirname(__file__))
12 class Root(object): pass
13 cherrypy.tree.mount(Root(), '/mp3/beat.mp3', config={
14 '/': {
15 'tools.staticdir.on': True,
16 'tools.staticdir.dir': PATH,
17 'tools.staticdir.index': 'mp3/beat.mp3',
18 },
19 })
20
21
22
23 class HomePage:
24
25
26 def index(self):
27 return '''
28 <p>Hi, this is the home page! Check out the other
29 fun stuff on this site:</p>
30
31 <ul>
32 <li><a href="/joke/">A silly joke</a></li>
33 <li><a href="/links/">Useful links</a></li>
34 <li><a href="/audio/">Audio</a></li>
35 </ul>'''
36 index.exposed = True
37
38
39 class MP3Page:
40 def index(self):
41 filepath = '/Users/tmc/Documents/LiveMesh/cherry/beat.mp3'
42 return serve_file(filepath, "application/x-download", "attachment")
43 index.exposed = True
44
45
46
47 class AudioPage:
48 def index(self):
49 #mp3file = 'http://www.voanews.com/MediaAssets2/learningenglish/2011_08/se-exp-death-valley-fixed-03aug11.mp3'
50 #mp3file = "/Users/tmc/Documents/LiveMesh/cherry/mp3/mp3_1.html"
51 #return open(mp3file)
52 mp3file = 'http://localhost:8080/mp3/beat.mp3'
53 return '''
54 <embed src="%s" width="300"/>''' %mp3file
55 index.exposed = True
56
57 class JokePage:
58 def index(self):
59 return '''
60 <p>"In Python, how do you create a string of random
61 characters?" -- "Read a Perl file!"</p>
62 <p>[<a href="../">Return</a>]</p>'''
63 index.exposed = True
64
65
66 class LinksPage:
67 def __init__(self):
68 # Request handler objects can create their own nested request
69 # handler objects. Simply create them inside their __init__
70 # methods!
71 self.extra = ExtraLinksPage()
72
73 def index(self):
74 # Note the way we link to the extra links page (and back).
75 # As you can see, this object doesn't really care about its
76 # absolute position in the site tree, since we use relative
77 # links exclusively.
78 return '''
79 <p>Here are some useful links:</p>
80
81 <ul>
82 <li><a href="http://www.cherrypy.org">The CherryPy Homepage</a></li>
83 <li><a href="http://www.python.org">The Python Homepage</a></li>
84 </ul>
85
86 <p>You can check out some extra useful
87 links <a href="./extra/">here</a>.</p>
88
89 <p>[<a href="../">Return</a>]</p>
90 '''
91 index.exposed = True
92
93
94 class ExtraLinksPage:
95 def index(self):
96 # Note the relative link back to the Links page!
97 return '''
98 <p>Here are some extra useful links:</p>
99
100 <ul>
101 <li><a href="http://del.icio.us">del.icio.us</a></li>
102 <li><a href="http://www.mornography.de">Hendrik's weblog</a></li>
103 </ul>
104
105 <p>[<a href="../">Return to links page</a>]</p>'''
106 index.exposed = True
107
108
109 # Of course we can also mount request handler objects right here!
110 root = HomePage()
111 root.joke = JokePage()
112 root.links = LinksPage()
113 root.audio = AudioPage()
114 root.mp3 = MP3Page()
115
116 # Remember, we don't need to mount ExtraLinksPage here, because
117 # LinksPage does that itself on initialization. In fact, there is
118 # no reason why you shouldn't let your root object take care of
119 # creating all contained request handler objects.
120
121
122 import os.path
123 tutconf = os.path.join(os.path.dirname(__file__), '/Users/tmc/Documents/LiveMesh/cherry/CherryPy-3.2.0/py2/cherrypy/tutorial/tutorial.conf')
124
125 if __name__ == '__main__':
126 # CherryPy always starts with app.root when trying to map request URIs
127 # to objects, so we need to mount a request handler root. A request
128 # to '/' will be mapped to HelloWorld().index().
129 cherrypy.quickstart(root, config=tutconf)
130 else:
131 # This branch is for the test suite; you can ignore it.
132 cherrypy.tree.mount(root, config=tutconf)
133