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@0 | 10 | from SVGChartSupport import get_chart |
paul@0 | 11 | |
paul@0 | 12 | Dependencies = ["pages"] |
paul@0 | 13 | |
paul@0 | 14 | # Parser support. |
paul@0 | 15 | |
paul@0 | 16 | class Parser: |
paul@0 | 17 | |
paul@0 | 18 | "Format CSV-like data as an SVG chart." |
paul@0 | 19 | |
paul@0 | 20 | extensions = [".csv"] |
paul@0 | 21 | |
paul@0 | 22 | def __init__(self, raw, request, **kw): |
paul@0 | 23 | self.raw = raw |
paul@0 | 24 | self.request = request |
paul@0 | 25 | self.data = [row.split() for row in raw.split("\n")] |
paul@0 | 26 | |
paul@0 | 27 | def format(self, fmt): |
paul@0 | 28 | |
paul@0 | 29 | "Format a chart using the given formatter 'fmt'." |
paul@0 | 30 | |
paul@0 | 31 | request = self.request |
paul@0 | 32 | page = request.page |
paul@0 | 33 | _ = request.getText |
paul@0 | 34 | |
paul@0 | 35 | # NOTE: Store and retrieve the width and height. |
paul@0 | 36 | |
paul@0 | 37 | cache_key = cache.key(request, itemname=page.page_name, content=self.raw) |
paul@0 | 38 | if not cache.exists(request, cache_key): |
paul@0 | 39 | chart = get_chart(self.data, styles_url="%s/%s/css/svgchart.css" % (request.cfg.url_prefix_static, request.theme.name)) |
paul@0 | 40 | cache.put(request, cache_key, chart, content_type="image/svg+xml") |
paul@0 | 41 | |
paul@0 | 42 | request.write(fmt.div(1, css_class="svgchart")) |
paul@0 | 43 | request.write(fmt.transclusion(1, data=cache.url(request, cache_key))) |
paul@0 | 44 | request.write(fmt.text(_("SVG chart of CSV data."))) |
paul@0 | 45 | request.write(fmt.transclusion(0)) |
paul@0 | 46 | request.write(fmt.div(0)) |
paul@0 | 47 | |
paul@0 | 48 | # vim: tabstop=4 expandtab shiftwidth=4 |