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