comparison decoupage/web.py @ 59:07cf168aa98c

make formats classes that can take arguments
author Jeff Hammel <jhammel@mozilla.com>
date Sun, 21 Nov 2010 13:23:34 -0800
parents 0cc1af24602b
children f5ca54558292
comparison
equal deleted inserted replaced
58:1275124ed767 59:07cf168aa98c
52 52
53 # static file server 53 # static file server
54 self.fileserver = FileApp # XXX still used?!? 54 self.fileserver = FileApp # XXX still used?!?
55 55
56 # pluggable formats 56 # pluggable formats
57 s = 'decoupage.format.'
58 _format_args = [ (i.split(s, 1)[-1], j) for i, j in app_conf.items()
59 if i.startswith(s) ]
60 format_args = {}
61 for i, j in _format_args:
62 assert i.count('.') == 1, 'Illegal string or something'
63 format_name, var_name = i.split('.')
64 format_args.setdefault(format_name, {})[var_name] = j
57 self.formats = {} 65 self.formats = {}
58 for _format in iter_entry_points('decoupage.formats'): 66 for _format in iter_entry_points('decoupage.formats'):
59 try: 67 try:
60 function = _format.load() 68 _cls = _format.load()
69 _instance = _cls(self, **format_args.get(_format.name, {}))
61 except Exception, e: 70 except Exception, e:
62 # record the error, but persist 71 # record the error, but persist
63 print >> sys.stderr, "Couldn't load format: %s" % _format 72 print >> sys.stderr, "Couldn't load format: %s" % _format
64 print >> sys.stderr, e 73 print >> sys.stderr, e
65 continue 74 continue
66 self.formats[_format.name] = function 75 self.formats[_format.name] = _instance
67 76
68 # pluggable index data formatters 77 # pluggable index data formatters
69 self.formatters = {} 78 self.formatters = {}
70 for formatter in iter_entry_points('decoupage.formatters'): 79 for formatter in iter_entry_points('decoupage.formatters'):
71 try: 80 try:
168 # decoupage.formats should return a 2-tuple: 177 # decoupage.formats should return a 2-tuple:
169 # (content_type, body) 178 # (content_type, body)
170 if 'format' in request.GET: 179 if 'format' in request.GET:
171 format_name = request.GET['format'] 180 format_name = request.GET['format']
172 if format_name in self.formats: 181 if format_name in self.formats:
173 function = self.formats[format_name] 182 _format = self.formats[format_name]
174 content_type, body = function(data) 183 content_type, body = _format(request, data)
175 return Response(content_type=content_type, body=body) 184 return Response(content_type=content_type, body=body)
176 185
177 # render the template 186 # render the template
178 template = conf.get('/template') 187 template = conf.get('/template')
179 local_index = False 188 local_index = False