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
17 # ############ Configuration variables: ################################
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
28 Gnuplot.GnuplotOpts.gnuplot_command = '/bin/mygnuplot'
32 gnuplot_command
= 'gnuplot'
33 recognizes_persist
= 1
35 recognizes_binary_splot
= 1
36 prefer_inline_data
= 0
41 prefer_enhanced_postscript
= 1
43 # ############ End of configuration options ############################
47 from java
.lang
import Thread
48 from java
.lang
import Runtime
52 """Determine whether gnuplot recognizes the option '-persist'.
56 return GnuplotOpts
.recognizes_persist
59 class OutputProcessor(Thread
):
60 """In a separate thread, read from one InputStream and output to a file.
64 def __init__(self
, name
, input, output
):
68 Thread
.__init
__(self
, name
)
73 self
.output
.write(chr(self
.input.read()))
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.
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.
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.
119 persist
= GnuplotOpts
.prefer_persist
120 command
= [GnuplotOpts
.gnuplot_command
]
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
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()
150 # ### Does this close the gnuplot process under Java?
151 if self
.gnuplot
is not None:
159 self
.gnuplot
.write(s
)
164 def __call__(self
, s
):
165 """Send a command string to gnuplot, followed by newline."""