# HG changeset patch # User Jeff Hammel # Date 1290374614 28800 # Node ID 07cf168aa98ccab9512223cc4a4195066b598803 # Parent 1275124ed76732a7d82aec8c7e62116240e78ef3 make formats classes that can take arguments diff -r 1275124ed767 -r 07cf168aa98c decoupage/formats.py --- a/decoupage/formats.py Thu Nov 18 08:36:07 2010 -0800 +++ b/decoupage/formats.py Sun Nov 21 13:23:34 2010 -0800 @@ -7,22 +7,30 @@ except ImportError: import simplejson as json -# TODO: convert this to a class -def format_json(data): +class JSON(object): + """ + JSON format for index pages + just (basically) return the data + """ + + def __init__(self, app, foo): + self.app = app - # fix datetime - for f in data['files']: - if 'modified' in f: - f['modified'] = f['modified'].ctime() + def __call__(self, request, data): - return 'application/json', json.dumps(data['files']) + # fix datetime + for f in data['files']: + if 'modified' in f: + f['modified'] = f['modified'].ctime() + + return 'application/json', json.dumps(data['files']) class RSS(object): """RSS for indices""" def __init__(self, app, count=10, cascade=False): self.app = app # the decoupage - self.count = count + self.count = int(count) self.cascade = cascade def __call__(self, request, data): diff -r 1275124ed767 -r 07cf168aa98c decoupage/web.py --- a/decoupage/web.py Thu Nov 18 08:36:07 2010 -0800 +++ b/decoupage/web.py Sun Nov 21 13:23:34 2010 -0800 @@ -54,16 +54,25 @@ self.fileserver = FileApp # XXX still used?!? # pluggable formats + s = 'decoupage.format.' + _format_args = [ (i.split(s, 1)[-1], j) for i, j in app_conf.items() + if i.startswith(s) ] + format_args = {} + for i, j in _format_args: + assert i.count('.') == 1, 'Illegal string or something' + format_name, var_name = i.split('.') + format_args.setdefault(format_name, {})[var_name] = j self.formats = {} for _format in iter_entry_points('decoupage.formats'): try: - function = _format.load() + _cls = _format.load() + _instance = _cls(self, **format_args.get(_format.name, {})) except Exception, e: # record the error, but persist print >> sys.stderr, "Couldn't load format: %s" % _format print >> sys.stderr, e continue - self.formats[_format.name] = function + self.formats[_format.name] = _instance # pluggable index data formatters self.formatters = {} @@ -170,8 +179,8 @@ if 'format' in request.GET: format_name = request.GET['format'] if format_name in self.formats: - function = self.formats[format_name] - content_type, body = function(data) + _format = self.formats[format_name] + content_type, body = _format(request, data) return Response(content_type=content_type, body=body) # render the template diff -r 1275124ed767 -r 07cf168aa98c setup.py --- a/setup.py Thu Nov 18 08:36:07 2010 -0800 +++ b/setup.py Sun Nov 21 13:23:34 2010 -0800 @@ -6,7 +6,7 @@ except IOError: description = '' -version = '0.9.1' +version = '0.10.0' setup(name='decoupage', version=version, @@ -41,7 +41,7 @@ main = decoupage.factory:factory [decoupage.formats] - json = decoupage.formats:format_json + json = decoupage.formats:JSON [decoupage.formatters] all = decoupage.formatters:All