Attachment 'gnuplot.py'

Download

   1 """
   2 MoinMoin - Processor for the Gnuplot
   3 
   4 Copyright (c) 2002 by Won-kyu Park <wkpark@kldp.org>
   5 All rights reserved, see COPYING for details.
   6 
   7 $Id$
   8 
   9 Usage:
  10 {{{#!gnuplot
  11 plot sin(x)
  12 }}}
  13 
  14 
  15 changed at 2005-07-01 for MoinMoin 1.3 parser 
  16 by Hyung-Yong Kim <yong27@biohackers.net>
  17 
  18 """
  19 
  20 import string, sys, os, re, sha
  21 from MoinMoin.action import AttachFile
  22 
  23 config_gnuplot_terminal='png'
  24 config_cache_dir='/home/moin/bhn'       #wikidata directory
  25 config_vartmp_dir='/home/moin/bhn/tmp'  #temporary directory
  26 config_external_gnuplot='/usr/bin/gnuplot'
  27 config_gnuplot_options=''               #set blah blah
  28 config_external_mv = "/bin/mv"
  29 
  30 class Parser:
  31     """ Format gnuplot data plotting 
  32     """
  33     extensions = ['.gnuplot']
  34 
  35     def __init__(self, raw, request, **kw):
  36         """ Store the source text.
  37         """
  38         self.raw = raw
  39         self.request = request
  40         self.form = request.form
  41         self._ = request.getText
  42 
  43         # parse ex1tra arguments for excludes
  44         self.exclude = []
  45 
  46     def format(self, formatter):
  47         if not config_cache_dir:
  48             return
  49         lines = self.raw.split('\n')
  50         if lines[0].strip() == "#!gnuplot":
  51             del lines[0]
  52 
  53         text = '\n'.join(lines).strip()
  54 
  55         str = '\n'+text+'\n'
  56         str = re.sub('\n\s*![^\n]+\n','\n',str) # strip dangerous shell commands
  57         str = re.sub('[ ]+',' ',str) # compress all spaces
  58         str = re.sub('^\s*','',str) # strip out first spaces
  59         str = re.sub('\n\s*','\n',str)
  60         str = re.sub('\nset\s+(t|o|si).*\n', '\n',str)
  61         str = re.sub('\n+','\n',str)
  62 
  63         imgname = sha.new(str).hexdigest()
  64         imgname = imgname + "_gnuplot"
  65 
  66         attdir = config_cache_dir + "/gnuplot/attachments"
  67         outpath = "%s/%s.png" % (attdir, imgname)
  68         if not os.path.isdir(attdir):
  69             os.makedirs(attdir, 0775)
  70 
  71         pagename=formatter.page.page_name
  72 
  73         url=AttachFile.getAttachUrl(pagename,imgname+'.png',self.request)
  74         attach_dir=AttachFile.getAttachDir(self.request,pagename,create=1)
  75 
  76         term='png'
  77         ext='png'
  78         size='set size 0.5,0.6'
  79 
  80         if not os.path.isfile(attach_dir+'/'+imgname+'.png'):
  81             if not os.path.exists(outpath):
  82                 data = open("%s/%s.plt" % (config_vartmp_dir,imgname), "w")
  83                 data.write('set term %s\n' % term)
  84                 data.write('%s\n' % size)
  85                 data.write('set out "%s"\n' % outpath)
  86                 data.write('%s\n' % config_gnuplot_options)
  87                 data.write(str)
  88                 data.close()
  89                 cmd = "%(gnuplot)s < %(plot)s 2>/dev/stdout" % {
  90                    "gnuplot": config_external_gnuplot,
  91                    "plot": config_vartmp_dir + '/' + imgname + '.plt'
  92                 }
  93                 log=os.popen(cmd,"r")
  94                 lines = log.read()
  95                 log.close()
  96                 if lines:
  97                     print "<pre class='errconsole'>"
  98                     print lines
  99                     print '<font color=red>If there is no fatal error, just reload this page</font>\n'
 100                     print "</pre>"
 101                     term='err'
 102                 del lines
 103                 os.system("rm -f " + config_vartmp_dir + "/" + imgname + ".plt")
 104 
 105                 old_img_name=imgname+'.png'
 106                 new_img_name=imgname+'.png'
 107                 new_url=AttachFile.getAttachUrl(pagename,new_img_name,self.request)
 108 
 109                 cmd='%(config_external_mv)s "%(old_img_name)s" "%(new_img_name)s" ' %{
 110                         "config_external_mv": config_external_mv,
 111                         "old_img_name":outpath,
 112                         "new_img_name":attach_dir+'/'+new_img_name
 113                 }
 114                 os.system(cmd)
 115 
 116                 self.request.write(formatter.image(src="%s" % url, alt=str, align='absmiddle'))
 117 
 118         else:
 119                 self.request.write(formatter.image(src="%s" % url, alt=str, align='absmiddle'))

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-11-25 19:36:58, 3.9 KB) [[attachment:gnuplot.py]]
 All files | Selected Files: delete move to page copy to page

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

web biohackers.net