Package PyFoam :: Package ThirdParty :: Package Gnuplot :: Module gp_java
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.ThirdParty.Gnuplot.gp_java

  1  # $Id: gp_java.py 291 2006-03-03 08:58:48Z mhagger $ 
  2   
  3  # Copyright (C) 2002-2003 Michael Haggerty <mhagger@alum.mit.edu> 
  4  # 
  5  # This file is licensed under the GNU Lesser General Public License 
  6  # (LGPL).  See LICENSE.txt for details. 
  7   
  8  """gp_java -- an interface to gnuplot used under Jython/Java. 
  9   
 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. 
 14   
 15  """ 
 16   
 17  # ############ Configuration variables: ################################ 
 18   
19 -class GnuplotOpts:
20 """The configuration options for gnuplot on generic platforms. 21 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:: 26 27 import Gnuplot 28 Gnuplot.GnuplotOpts.gnuplot_command = '/bin/mygnuplot' 29 30 """ 31 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
42 43 # ############ End of configuration options ############################ 44 45 import sys 46 47 from java.lang import Thread 48 from java.lang import Runtime 49 50
51 -def test_persist():
52 """Determine whether gnuplot recognizes the option '-persist'. 53 54 """ 55 56 return GnuplotOpts.recognizes_persist
57 58
59 -class OutputProcessor(Thread):
60 """In a separate thread, read from one InputStream and output to a file. 61 62 """ 63
64 - def __init__(self, name, input, output):
65 self.input = input 66 self.output = output 67 68 Thread.__init__(self, name) 69 self.setDaemon(1)
70
71 - def run(self):
72 while 1: 73 self.output.write(chr(self.input.read()))
74 75
76 -class GnuplotProcess:
77 """Unsophisticated interface to a running gnuplot program. 78 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. 85 86 Members: 87 88 89 Methods: 90 91 '__init__' -- start up the program. 92 93 '__call__' -- pass an arbitrary string to the gnuplot program, 94 followed by a newline. 95 96 'write' -- pass an arbitrary string to the gnuplot program. 97 98 'flush' -- cause pending output to be written immediately. 99 100 """ 101
102 - def __init__(self, persist=None):
103 """Start a gnuplot process. 104 105 Create a 'GnuplotProcess' object. This starts a gnuplot 106 program and prepares to write commands to it. 107 108 Keyword arguments: 109 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. 115 116 """ 117 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') 126 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) 135 136 self.outprocessor = OutputProcessor( 137 'gnuplot standard output processor', 138 self.process.getInputStream(), sys.stdout 139 ) 140 self.outprocessor.start() 141 self.errprocessor = OutputProcessor( 142 'gnuplot standard error processor', 143 self.process.getErrorStream(), sys.stderr 144 ) 145 self.errprocessor.start() 146 147 self.gnuplot = self.process.getOutputStream()
148
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
154
155 - def __del__(self):
156 self.close()
157
158 - def write(self, s):
159 self.gnuplot.write(s)
160
161 - def flush(self):
162 self.gnuplot.flush()
163
164 - def __call__(self, s):
165 """Send a command string to gnuplot, followed by newline.""" 166 167 self.write(s + '\n') 168 self.flush()
169