Added gnuplot backend.
[smonitor.git] / monitor / extra / Gnuplot / gp_java.py
blob811b5a0adfcea7318fe27731318dc33cea101eb1
1 # $Id: gp_java.py 291 2006-03-03 08:58:48Z mhagger $
3 # Copyright (C) 2002-2003 Michael Haggerty <mhagger@alum.mit.edu>
5 # This file is licensed under the GNU Lesser General Public License
6 # (LGPL). See LICENSE.txt for details.
8 """gp_java -- an interface to gnuplot used under Jython/Java.
10 This file implements a low-level interface to a gnuplot program run
11 via Jython/Java. This file should be imported through gp.py, which in
12 turn should be imported via 'import Gnuplot' rather than these
13 low-level interfaces.
15 """
17 # ############ Configuration variables: ################################
19 class GnuplotOpts:
20 """The configuration options for gnuplot on generic platforms.
22 Store the options in a class to make them easy to import and
23 modify en masse. If you want to modify the options from the
24 command line or within a running program, do something like the
25 following::
27 import Gnuplot
28 Gnuplot.GnuplotOpts.gnuplot_command = '/bin/mygnuplot'
30 """
32 gnuplot_command = 'gnuplot'
33 recognizes_persist = 1
34 prefer_persist = 0
35 recognizes_binary_splot = 1
36 prefer_inline_data = 0
37 support_fifo = 0
38 prefer_fifo_data = 0
39 default_term = 'x11'
40 default_lpr = '| lpr'
41 prefer_enhanced_postscript = 1
43 # ############ End of configuration options ############################
45 import sys
47 from java.lang import Thread
48 from java.lang import Runtime
51 def test_persist():
52 """Determine whether gnuplot recognizes the option '-persist'.
54 """
56 return GnuplotOpts.recognizes_persist
59 class OutputProcessor(Thread):
60 """In a separate thread, read from one InputStream and output to a file.
62 """
64 def __init__(self, name, input, output):
65 self.input = input
66 self.output = output
68 Thread.__init__(self, name)
69 self.setDaemon(1)
71 def run(self):
72 while 1:
73 self.output.write(chr(self.input.read()))
76 class GnuplotProcess:
77 """Unsophisticated interface to a running gnuplot program.
79 This represents a running gnuplot program and the means to
80 communicate with it at a primitive level (i.e., pass it commands
81 or data). When the object is destroyed, the gnuplot program exits
82 (unless the 'persist' option was set). The communication is
83 one-way; gnuplot's text output just goes to stdout with no attempt
84 to check it for error messages.
86 Members:
89 Methods:
91 '__init__' -- start up the program.
93 '__call__' -- pass an arbitrary string to the gnuplot program,
94 followed by a newline.
96 'write' -- pass an arbitrary string to the gnuplot program.
98 'flush' -- cause pending output to be written immediately.
102 def __init__(self, persist=None):
103 """Start a gnuplot process.
105 Create a 'GnuplotProcess' object. This starts a gnuplot
106 program and prepares to write commands to it.
108 Keyword arguments:
110 'persist=1' -- start gnuplot with the '-persist' option,
111 (which leaves the plot window on the screen even after
112 the gnuplot program ends, and creates a new plot window
113 each time the terminal type is set to 'x11'). This
114 option is not available on older versions of gnuplot.
118 if persist is None:
119 persist = GnuplotOpts.prefer_persist
120 command = [GnuplotOpts.gnuplot_command]
121 if persist:
122 if not test_persist():
123 raise ('-persist does not seem to be supported '
124 'by your version of gnuplot!')
125 command.append('-persist')
127 # This is a kludge: distutils wants to import everything it
128 # sees when making a distribution, and if we just call exec()
129 # normally that causes a SyntaxError in CPython because "exec"
130 # is a keyword. Therefore, we call the exec() method
131 # indirectly.
132 #self.process = Runtime.getRuntime().exec(command)
133 exec_method = getattr(Runtime.getRuntime(), 'exec')
134 self.process = exec_method(command)
136 self.outprocessor = OutputProcessor(
137 'gnuplot standard output processor',
138 self.process.getInputStream(), sys.stdout
140 self.outprocessor.start()
141 self.errprocessor = OutputProcessor(
142 'gnuplot standard error processor',
143 self.process.getErrorStream(), sys.stderr
145 self.errprocessor.start()
147 self.gnuplot = self.process.getOutputStream()
149 def close(self):
150 # ### Does this close the gnuplot process under Java?
151 if self.gnuplot is not None:
152 self.gnuplot.close()
153 self.gnuplot = None
155 def __del__(self):
156 self.close()
158 def write(self, s):
159 self.gnuplot.write(s)
161 def flush(self):
162 self.gnuplot.flush()
164 def __call__(self, s):
165 """Send a command string to gnuplot, followed by newline."""
167 self.write(s + '\n')
168 self.flush()