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