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