Attachment 'GoogleChart.py'

Download

   1 # -*- coding: UTF-8 -*-
   2 
   3 """
   4     MoinMoin - GoogleChart
   5     Based on text_x_mathtran.py approach of MoinMoin:ReimarBauer
   6 
   7     This parser creates a GoogleChart by using Google Chart Api e.g.
   8     url='http://chart.apis.google.com/chart?'+p
   9     p = urllib.urlencode(dict(chld='H', cht='qr', chs='250x250', chl=raw))
  10 
  11     For a detailed QR chart option list please read:
  12     http://code.google.com/intl/de-DE/apis/chart/docs/gallery/qr_codes.html
  13     Do not use the "cht" "chl" and  parameter, it specifies the chart typ!
  14     usage example:
  15     
  16     {{{
  17     #!GoogleChart
  18     #cht=qr
  19     #chld=L
  20     #chs=125x125
  21     #chl=hello world
  22     this is me
  23     }}}
  24     Second  Version 12.03.2011 - Small Style Changes    
  25     Initial Version 11.03.2011
  26     @copyright: 2011 by MoinMoin: Ralph Zacharias
  27     @license: GNU GPL, see COPYING for details.
  28 """
  29 import sys
  30 import urllib
  31 from MoinMoin.action import cache
  32 
  33 Dependencies = ["page"]
  34 
  35 class Parser:
  36     """ GoogleChart parser """
  37     def __init__(self, raw, request, **kw):
  38         self.pagename      = request.page.page_name
  39         self.request       = request
  40         self.formatter     = request.formatter
  41         self.raw           = raw
  42         self.text =u'http://chart.apis.google.com/chart?'
  43         self.init_settings = False
  44 
  45         try:
  46             k, d = None, {}
  47             for l in self.raw.splitlines():
  48                 if l.startswith('#'):
  49                     k, v = l[1:].split('=', 1)
  50                     d[k] = v.encode('UTF-8')
  51                 elif k:
  52                     d[k] += '\n'
  53                     d[k] += l.encode('UTF-8')              
  54             self.text += urllib.urlencode(d)
  55             self.init_settings = True
  56         except ValueError, err:
  57             request.write(self.formatter.text('GoogleChart: Argument error'))
  58 
  59         if 'debug' in kw['format_args']:
  60             request.write(self.formatter.text(self.text))
  61 
  62             
  63     def render(self, formatter):
  64         """ renders formular  """
  65         # checks if initializing of all attributes in __init__ was done
  66         if not self.init_settings:
  67             return
  68 
  69         key = cache.key(self.request, itemname=self.pagename, content=self.text)
  70 
  71         if not cache.exists(self.request, key):
  72             image = urllib.urlopen(self.text)
  73             cache.put(self.request, key, image.read(), content_type="image/png")
  74 
  75         return formatter.image(src=cache.url(self.request, key), alt=self.raw)
  76 
  77 
  78     def format(self, formatter):
  79         """ parser output """
  80         # checks if initializing of all attributes in __init__ was done
  81         if self.init_settings:
  82             self.request.write(self.formatter.div(1, css_class="GoogleChart"))
  83             self.request.write(self.render(formatter))
  84             self.request.write(self.formatter.div(0))
  85  

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2011-03-12 18:14:23, 2.7 KB) [[attachment:GoogleChart.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.