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,v 2.3 2003/10/19 13:43:21 mhagger Exp $ 
  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  __cvs_version__ = '$Revision: 2.3 $' 
 18   
 19   
 20  # ############ Configuration variables: ################################ 
 21   
22 -class GnuplotOpts:
23 """The configuration options for gnuplot on generic platforms. 24 25 Store the options in a class to make them easy to import and 26 modify en masse. If you want to modify the options from the 27 command line or within a running program, do something like the 28 following:: 29 30 import Gnuplot 31 Gnuplot.GnuplotOpts.gnuplot_command = '/bin/mygnuplot' 32 33 """ 34 35 gnuplot_command = 'gnuplot' 36 recognizes_persist = 1 37 prefer_persist = 0 38 recognizes_binary_splot = 1 39 prefer_inline_data = 0 40 support_fifo = 0 41 prefer_fifo_data = 0 42 default_term = 'x11' 43 default_lpr = '| lpr' 44 prefer_enhanced_postscript = 1
45 46 # ############ End of configuration options ############################ 47 48 import sys 49 50 from java.lang import Thread 51 from java.lang import Runtime 52 53
54 -def test_persist():
55 """Determine whether gnuplot recognizes the option '-persist'. 56 57 """ 58 59 return GnuplotOpts.recognizes_persist
60 61
62 -class OutputProcessor(Thread):
63 """In a separate thread, read from one InputStream and output to a file. 64 65 """ 66
67 - def __init__(self, name, input, output):
68 self.input = input 69 self.output = output 70 71 Thread.__init__(self, name) 72 self.setDaemon(1)
73
74 - def run(self):
75 while 1: 76 self.output.write(chr(self.input.read()))
77 78
79 -class GnuplotProcess:
80 """Unsophisticated interface to a running gnuplot program. 81 82 This represents a running gnuplot program and the means to 83 communicate with it at a primitive level (i.e., pass it commands 84 or data). When the object is destroyed, the gnuplot program exits 85 (unless the 'persist' option was set). The communication is 86 one-way; gnuplot's text output just goes to stdout with no attempt 87 to check it for error messages. 88 89 Members: 90 91 92 Methods: 93 94 '__init__' -- start up the program. 95 96 '__call__' -- pass an arbitrary string to the gnuplot program, 97 followed by a newline. 98 99 'write' -- pass an arbitrary string to the gnuplot program. 100 101 'flush' -- cause pending output to be written immediately. 102 103 """ 104
105 - def __init__(self, persist=None):
106 """Start a gnuplot process. 107 108 Create a 'GnuplotProcess' object. This starts a gnuplot 109 program and prepares to write commands to it. 110 111 Keyword arguments: 112 113 'persist=1' -- start gnuplot with the '-persist' option, 114 (which leaves the plot window on the screen even after 115 the gnuplot program ends, and creates a new plot window 116 each time the terminal type is set to 'x11'). This 117 option is not available on older versions of gnuplot. 118 119 """ 120 121 if persist is None: 122 persist = GnuplotOpts.prefer_persist 123 command = [GnuplotOpts.gnuplot_command] 124 if persist: 125 if not test_persist(): 126 raise ('-persist does not seem to be supported ' 127 'by your version of gnuplot!') 128 command.append('-persist') 129 130 # This is a kludge: distutils wants to import everything it 131 # sees when making a distribution, and if we just call exec() 132 # normally that causes a SyntaxError in CPython because "exec" 133 # is a keyword. Therefore, we call the exec() method 134 # indirectly. 135 #self.process = Runtime.getRuntime().exec(command) 136 exec_method = getattr(Runtime.getRuntime(), 'exec') 137 self.process = exec_method(command) 138 139 self.outprocessor = OutputProcessor( 140 'gnuplot standard output processor', 141 self.process.getInputStream(), sys.stdout 142 ) 143 self.outprocessor.start() 144 self.errprocessor = OutputProcessor( 145 'gnuplot standard error processor', 146 self.process.getErrorStream(), sys.stderr 147 ) 148 self.errprocessor.start() 149 150 self.gnuplot = self.process.getOutputStream()
151
152 - def write(self, s):
153 self.gnuplot.write(s)
154
155 - def flush(self):
156 self.gnuplot.flush()
157
158 - def __call__(self, s):
159 """Send a command string to gnuplot, followed by newline.""" 160 161 self.write(s + '\n') 162 self.flush()
163