1 # -*- coding: iso-8859-1 -*- 2 """ 3 MoinMoin - SVGChart 4 5 @copyright: 2011 by Paul Boddie <paul@boddie.org.uk> 6 @license: GNU GPL (v2 or later), see COPYING.txt for details. 7 """ 8 9 from MoinMoin.action import cache 10 from MoinMoin import wikiutil 11 from SVGChartSupport import get_chart 12 13 Dependencies = ["pages"] 14 15 # Parser support. 16 17 class Parser: 18 19 "Format CSV-like data as an SVG chart." 20 21 extensions = [".csv"] 22 23 def __init__(self, raw, request, **kw): 24 25 """ 26 Initialise the parser with the given 'raw' data, 'request' and any 27 keyword arguments that may have been supplied. 28 """ 29 30 self.raw = raw 31 self.request = request 32 self.data = [row.split() for row in raw.split("\n")] 33 34 attrs, msg = wikiutil.parseAttributes(request, kw.get("format_args", "")) 35 36 # The attributes returned from the formatting arguments are encoded like 37 # strings. 38 39 self.xorigin = float((attrs.get("xorigin") or '"0"')[1:-1]) 40 self.yorigin = float((attrs.get("yorigin") or '"0"')[1:-1]) 41 42 def format(self, fmt): 43 44 "Format a chart using the given formatter 'fmt'." 45 46 request = self.request 47 page = request.page 48 _ = request.getText 49 50 # NOTE: Store and retrieve the width and height. 51 52 cache_key = cache.key(request, itemname=page.page_name, content="%s,%s,%s" % (self.xorigin, self.yorigin, self.raw)) 53 if not cache.exists(request, cache_key): 54 chart = get_chart(self.data, xorigin=self.xorigin, yorigin=self.yorigin, 55 styles_url="%s/%s/css/svgchart.css" % (request.cfg.url_prefix_static, request.theme.name)) 56 cache.put(request, cache_key, chart, content_type="image/svg+xml") 57 58 request.write(fmt.div(1, css_class="svgchart")) 59 request.write(fmt.transclusion(1, data=cache.url(request, cache_key))) 60 request.write(fmt.text(_("SVG chart of CSV data."))) 61 request.write(fmt.transclusion(0)) 62 request.write(fmt.div(0)) 63 64 # vim: tabstop=4 expandtab shiftwidth=4