""" MoinMoin - Processor for the Gnuplot Copyright (c) 2002 by Won-kyu Park All rights reserved, see COPYING for details. $Id$ Usage: {{{#!gnuplot plot sin(x) }}} """ import string, sys, os, re, sha from MoinMoin import config config_gnuplot_terminal='png' config_cache_dir='./pds' config_cache_url=config.url_prefix+'/pds' config_vartmp_dir='/var/tmp' config_external_gnuplot='/usr/bin/gnuplot' config_gnuplot_options=''#set blah blah def process(request, formatter, lines): text = string.join(lines,'\n') str = string.strip(text) str = '\n'+str+'\n' str = re.sub('\n\s*![^\n]+\n','\n',str) # strip dangerous shell commands str = re.sub('[ ]+',' ',str) # compress all spaces str = re.sub('^\s*','',str) # strip out first spaces str = re.sub('\n\s*','\n',str) str = re.sub('\nset\s+(t|o|si).*\n', '\n',str) str = re.sub('\n+','\n',str) imgname = sha.new(str).hexdigest() if config_gnuplot_terminal=='svg': term='svg' ext='svg' size='set size 0.5,0.5' mime='image/svg-xml' else: term='png' ext='png' size='set size 0.5,0.6' outpath = "%s/GnuPlot/attachments/%s.%s" % (config_cache_dir,imgname,ext) outurl = "%s/GnuPlot/attachments/%s.%s" % (config_cache_url,imgname,ext) if not os.path.isdir(config_cache_dir + "/GnuPlot/attachments"): os.makedirs(config_cache_dir + "/GnuPlot/attachments", 0777) if not os.path.exists(outpath) or not os.path.getsize(outpath): data = open("%s/%s.plt" % (config_vartmp_dir,imgname), "w") data.write('set term %s\n' % term) data.write('%s\n' % size) data.write('set out "%s"\n' % outpath) data.write('%s\n' % config_gnuplot_options) data.write(str) data.close() cmd = "%(gnuplot)s < %(plot)s 2>/dev/stdout" % { "gnuplot": config_external_gnuplot, "plot": config_vartmp_dir + '/' + imgname + '.plt' } log=os.popen(cmd,"r") lines = log.read() log.close() if lines: print "
"
            print lines
            print 'If there is no fatal error, just reload this page\n'
            print "
" term='err' del lines os.system("rm -f " + config_vartmp_dir + "/" + imgname + ".plt") if term == 'err': return '' if term == 'svg': request.write(formatter.embed( src="%s%s" % outurl, width='300', height='240', type=mime, alt='Gnuplot')) else: request.write(formatter.image(src="%s" % outurl, alt='Gnuplot'))