comparison decoupage/web.py @ 10:a328cc9d2c74

* fix formatters * allow /inherit keyword to specify configuration * bump version
author k0s <k0scist@gmail.com>
date Fri, 25 Dec 2009 01:03:06 -0500
parents 6a802c87f070
children 9f91acf9874c
comparison
equal deleted inserted replaced
9:ec2d0d850b83 10:a328cc9d2c74
1 """ 1 """
2 decoupage: a view with webob 2 decoupage: a view with webob
3 """ 3 """
4 4
5 import os 5 import os
6
7 from formatters import formatters
6 8
7 from genshi.builder import Markup 9 from genshi.builder import Markup
8 from genshi.template import TemplateLoader 10 from genshi.template import TemplateLoader
9 from martini.config import ConfigMunger 11 from martini.config import ConfigMunger
10 from paste.fileapp import FileApp 12 from paste.fileapp import FileApp
97 files = [] 99 files = []
98 for i in os.listdir(directory): 100 for i in os.listdir(directory):
99 files.append({'path' : '%s/%s' % (path.rstrip('/'), i), 101 files.append({'path' : '%s/%s' % (path.rstrip('/'), i),
100 'name': i, 102 'name': i,
101 'description': conf.get(i.lower(), None)}) 103 'description': conf.get(i.lower(), None)})
102
103 # build data dictionary 104 # build data dictionary
104 data = {'path': path, 'files': files, 'request': request} 105 data = {'path': path, 'files': files, 'request': request}
105 106
106 # apply formatters 107 # apply formatters
107 # XXX this should be cached if not self.auto_reload 108 # XXX this should be cached if not self.auto_reload
117 if key in self.formatters and key not in formatters: 118 if key in self.formatters and key not in formatters:
118 formatters.append(key) 119 formatters.append(key)
119 for name in formatters: 120 for name in formatters:
120 formatter = self.formatters[name](conf.get('/%s' % name, '')) 121 formatter = self.formatters[name](conf.get('/%s' % name, ''))
121 formatter(request, data) 122 formatter(request, data)
122 123
124
123 # render the template 125 # render the template
124 template = conf.get('/template') 126 template = conf.get('/template')
125 if template is None: 127 if template is None:
126 if 'index.html' in [ f['name'] for f in files ]: 128 if 'index.html' in [ f['name'] for f in files ]:
127 template = os.path.join(directory, 'index.html') 129 template = os.path.join(directory, 'index.html')
135 res = template.generate(**data).render('html', doctype='html') 137 res = template.generate(**data).render('html', doctype='html')
136 return self.get_response(res) 138 return self.get_response(res)
137 139
138 ### internal methods 140 ### internal methods
139 141
140 def conf(self, path): 142 def conf(self, path, cascade=None):
141 """returns configuration dictionary appropriate to a path""" 143 """returns configuration dictionary appropriate to a path"""
144 if cascade is None:
145 cascase = self.cascade
142 146
143 directory = os.path.join(self.directory, path.strip('/')) 147 directory = os.path.join(self.directory, path.strip('/'))
144 if path.strip('/'): 148 if path.strip('/'):
145 path_tuple = tuple(path.strip('/').split('/')) 149 path_tuple = tuple(path.strip('/').split('/'))
146 else: 150 else:
161 165
162 # global configuration 166 # global configuration
163 if not conf and self.configuration and os.path.exists(self.configuration): 167 if not conf and self.configuration and os.path.exists(self.configuration):
164 conf = ConfigMunger(self.configuration).dict().get('/%s' % path.rstrip('/'), {}) 168 conf = ConfigMunger(self.configuration).dict().get('/%s' % path.rstrip('/'), {})
165 169
166 # cascade configuration 170 # inherit and cascade configuration
167 if self.cascade and path_tuple: 171 inherit_directory = None
168 parent_configuration = self.conf('/%s' % '/'.join(path_tuple[:-1])) 172 if '/inherit' in conf:
173 inherit_directory = conf['/inherit']
174 elif self.cascade and path_tuple:
175 inherit_directory = '/%s' % '/'.join(path_tuple[:-1])
176 if inherit_directory:
177 parent_configuration = self.conf(inherit_directory)
169 for key, value in parent_configuration.items(): 178 for key, value in parent_configuration.items():
170 if key.startswith('/') and key not in conf: 179 if key.startswith('/') and key not in conf:
171 conf[key] = value 180 conf[key] = value
172 181
173 # cache configuration 182 # cache configuration