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, get_dimensions 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 quotes = '"' + "'" 40 self.xorigin = float((attrs.get("xorigin") or '0').strip(quotes)) 41 self.yorigin = float((attrs.get("yorigin") or '0').strip(quotes)) 42 width = (attrs.get("width") or '').strip(quotes) or None 43 height = (attrs.get("height") or '').strip(quotes) or None 44 self.width = width and int(width) 45 self.height = height and int(height) 46 47 def format(self, fmt): 48 49 "Format a chart using the given formatter 'fmt'." 50 51 request = self.request 52 page = request.page 53 _ = request.getText 54 55 cache_key = cache.key(request, itemname=page.page_name, content="%s,%s,%s,%s,%s" % ( 56 self.width, self.height, self.xorigin, self.yorigin, self.raw)) 57 58 if not cache.exists(request, cache_key): 59 width, height, chart = get_chart(self.data, self.width, self.height, self.xorigin, self.yorigin, 60 styles_url="%s/%s/css/svgchart.css" % (request.cfg.url_prefix_static, request.theme.name)) 61 cache.put(request, cache_key, chart, content_type="image/svg+xml") 62 else: 63 width, height = get_dimensions(self.data, self.width, self.height, self.xorigin, self.yorigin) 64 65 request.write(fmt.div(1, css_class="svgchart")) 66 request.write(fmt.transclusion(1, data=cache.url(request, cache_key), width=width, height=height)) 67 request.write(fmt.text(_("SVG chart of CSV data."))) 68 request.write(fmt.transclusion(0)) 69 request.write(fmt.div(0)) 70 71 # vim: tabstop=4 expandtab shiftwidth=4