annotate martini/web.py @ 8:81aed4352851

make martini work with an ordered dictionary
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 24 Nov 2010 11:05:40 -0800
parents 3c3522ce6e3a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
1 """
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
2 a view with webob + genshi for editing and viewing .ini files
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
3 """
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
4 import os
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
5 import re
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
6
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
7 from genshi.template import TemplateLoader
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
8 from martini.config import ConfigMunger
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
9 from martini.utils import getbool
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
10 from martini.utils import getlist
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
11 from paste.httpexceptions import HTTPExceptionHandler
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
12 from paste.urlparser import StaticURLParser
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
13 from pkg_resources import resource_filename
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
14 from webob import Request, Response, exc
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
15
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
16 class MartiniWebView(object):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
17
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
18 ### class level variables
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
19 defaults = { 'auto_reload': 'True',
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
20 'files': None,
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
21 'directories': None }
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
22
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
23 def __init__(self, **kw):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
24
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
25 # set instance parameters from kw and defaults
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
26 for key in self.defaults:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
27 setattr(self, key, kw.get(key, self.defaults[key]))
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
28 self.auto_reload = getbool(self.auto_reload)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
29
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
30 # files
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
31 self.files = getlist(self.files)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
32 if self.files:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
33 assert [ f for f in self.files if os.path.isabs(f) ]
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
34 self.files = dict([(os.path.basename(f), f)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
35 for f in self.files])
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
36 else:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
37 self.files = {}
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
38
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
39 # directories
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
40 self.directories = getlist(self.directories)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
41 if self.directories:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
42 assert [ d for d in self.directories if os.path.isabs(d) ]
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
43 if len(self.directories) > 1 or self.files:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
44 # namespace directories
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
45 self.directories = dict([(os.path.basename(d), d)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
46 for d in self.directories])
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
47 else:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
48 # don't namespace a single directory
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
49 self.directories = { '': self.directories[0] }
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
50 else:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
51 self.directories = {}
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
52
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
53 # have to have something to serve!
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
54 assert self.files or self.directories
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
55
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
56 # methods to respond to
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
57 self.response_functions = { 'GET': self.get,
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
58 'POST': self.post,
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
59 }
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
60
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
61 # template loader
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
62 templates_dir = resource_filename(__name__, 'templates')
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
63 self.loader = TemplateLoader(templates_dir,
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
64 auto_reload=self.auto_reload)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
65
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
66 # fileserver
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
67 self.fileserver = StaticURLParser(resource_filename(__name__, 'static'))
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
68
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
69 def file(self, path):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
70 path = path.strip('/')
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
71 if not path:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
72 return None
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
73 f = self.files.get(path, None)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
74 if f is None:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
75 if path in self.directories:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
76 return self.directories[path]
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
77 if '/' in path:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
78 path = path.split('/', 1)[-1]
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
79 for d in self.directories.values():
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
80 filename = os.path.join(d, path)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
81 if os.path.exists(filename):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
82 return filename
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
83 else:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
84 return f
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
85
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
86 ### methods dealing with HTTP
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
87 def __call__(self, environ, start_response):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
88 request = Request(environ)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
89
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
90 if request.path_info.strip('/') and os.path.exists(os.path.join(resource_filename(__name__, 'static'), request.path_info.strip('/'))):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
91 return self.fileserver(environ, start_response)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
92
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
93 if request.path_info.endswith('/'):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
94 if request.path_info.strip('/'):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
95 raise exc.HTTPFound(location=request.path_info.rstrip('/'))
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
96 else:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
97 if request.path_info != '/':
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
98 raise exc.HTTPFound(location='/')
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
99 res = self.make_response(request)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
100 return res(environ, start_response)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
101
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
102 def make_response(self, request):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
103 return self.response_functions.get(request.method, self.error)(request)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
104
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
105 def get_response(self, text, content_type='text/html'):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
106 """make an HTTP response from text"""
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
107 res = Response(content_type=content_type, body=text)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
108 return res
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
109
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
110 def get(self, request):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
111 """
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
112 return response to a GET requst
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
113 """
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
114
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
115 # index of all resources
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
116 if not request.path_info.strip('/'):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
117 template = self.loader.load('index.html')
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
118 items = self.files.keys() + self.directories.keys()
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
119 res = template.generate(request=request, items=items).render('html', doctype='html')
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
120 return self.get_response(res)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
121
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
122 # get the file
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
123 f = self.file(request.path_info)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
124 if not f:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
125 raise exc.HTTPNotFound()
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
126
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
127 # index page of a directory
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
128 if os.path.isdir(f):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
129 template = self.loader.load('index.html')
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
130 items = os.listdir(f)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
131 res = template.generate(request=request, items=items).render('html', doctype='html')
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
132 return self.get_response(res)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
133
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
134 # get configuration
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
135 conf = ConfigMunger(f)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
136
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
137 # render the template
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
138 template = self.loader.load('table.html')
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
139 res = template.generate(request=request, sections=conf.dict()).render('html', doctype='html')
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
140 # generate the response
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
141 return self.get_response(res)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
142
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
143 def post(self, request):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
144 """
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
145 return response to a POST request
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
146 """
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
147
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
148 if not request.path_info.strip('/'):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
149 raise exc.HTTPMethodNotAllowed()
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
150
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
151 # get the file
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
152 f = self.file(request.path_info)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
153 if not f:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
154 raise exc.HTTPNotFound()
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
155 if os.path.isdir(f):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
156 raise exc.HTTPMethodNotAllowed()
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
157
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
158 regex = '\[([^\]]+)\](.*)'
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
159
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
160 conf = ConfigMunger(f)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
161
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
162 delete = request.POST.getall('delete')
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
163 if delete:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
164 del request.POST['delete']
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
165
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
166 for key, value in request.POST.items():
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
167 value = ' '.join(value.strip().split())
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
168 match = re.match(regex, key)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
169 section, option = match.groups()
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
170 if option:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
171 conf.set(section, option, value)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
172 else:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
173 if value:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
174 conf.move_section(section, value)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
175 else:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
176 conf.add_section(section)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
177
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
178 for d in delete:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
179 match = re.match(regex, d)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
180 section, option = match.groups()
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
181 if conf.has_section(section):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
182 if option:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
183 conf.remove_option(section, option)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
184 else:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
185 conf.remove_section(section)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
186
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
187 output = file(f, 'w')
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
188 conf.write(output)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
189
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
190 return exc.HTTPSeeOther(location=request.path_info)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
191
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
192 def error(self, request):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
193 """deal with non-supported methods"""
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
194 return exc.HTTPMethodNotAllowed("Only %r operations are allowed" % self.response_functions.keys())
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
195
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
196
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
197 ### paste factory
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
198
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
199 def factory(global_conf, **app_conf):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
200 """create a webob view and wrap it in middleware"""
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
201 keystr = 'martini.'
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
202 args = dict([(key.split(keystr, 1)[-1], value)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
203 for key, value in app_conf.items()
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
204 if key.startswith(keystr) ])
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
205 app = MartiniWebView(**args)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
206 return HTTPExceptionHandler(app)