-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.py
81 lines (65 loc) · 2.27 KB
/
sample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
""" Sample code for Dillamond usage
Settings
root:
root of the APPLICATION, used to generate view_paths
view_paths:
set up where you want to place your mako templates.
array of paths relative to root
"""
import sys
from os.path import abspath, dirname
from dillamond import Dillamond
# Create the APPLICATION
APP = Dillamond({'root': abspath(dirname(__file__)), 'view_paths': ['views/']})
#################################################
# actions
#################################################
# use the APP.route decorator to associate uris to functions
# route(path, req = [], generate = False, **kwargs)
# path: url matcher
# req: array of functions accepting environ and returning a bool
# generate: whether or not to generate a static file for that action
# **kwargs: additional arguments you want to used within your action
@APP.route('/', generate=True)
@APP.route('/fire/?', generate=True)
def index(res):
""" Matches / and /fire
returns the Mako template index found the in view_paths
"""
return res.view('index')
@APP.get('/c1/action1')
def action1(res):
"""
return a mako template, passing viewdata into the mako template
"""
return res.view('view1', viewdata=1234)
@APP.post('/json/action/?')
def multiaction(res):
""" Matches a POST on /json/action and /json/action/
return json
"""
return res.json('{"status":"waffles!"}')
@APP.route('/json/control1/{action}/?')
def jsonaction(res, action):
""" Matches /json/.*/ and /json/.*
Anything matching this pattern will put the part
matching {action} into the kwarg action
"""
return res.json('{"status":"' + action + '"}')
@APP.error
@APP.route('{err}')
def error(res, err=''):
""" Catchall for errors
"""
return res.json('{"status":"error","error":' + err + '}')
#################################################
# wsgi APP
#################################################
# point your wsgi entry point here
# ex: the variable 'APPLICATION' is now a wsgi APP
APPLICATION = APP.wsgiapp()
# you can run this file from commandline to invoke
# the static file generator or simple wsgi server
# see dillamond.Dillamond.main() for more details
if __name__ == '__main__':
APP.main(sys.argv[1:])