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

Source Code for Module PyFoam.ThirdParty.Gnuplot.gp_macosx

  1  # $Id: gp_macosx.py 291 2006-03-03 08:58:48Z mhagger $ 
  2   
  3  # Copyright (C) 1998-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_macosx -- an interface to the command line version of gnuplot  
  9  used under Mac OS X. 
 10   
 11  The only difference between this interface and gp_unix is that 
 12  default_term is 'aqua'. 
 13   
 14  This file implements a low-level interface to gnuplot.  This file 
 15  should be imported through gp.py, which in turn should be imported via 
 16  'import Gnuplot' rather than using these low-level interfaces 
 17  directly. 
 18   
 19  """ 
 20   
 21  # ############ Configuration variables: ################################ 
 22   
23 -class GnuplotOpts:
24 """The configuration options for gnuplot on Mac OS X. 25 26 See the gp_unix.py for documentation on all of the parameters. 27 28 """ 29 30 gnuplot_command = 'gnuplot' 31 recognizes_persist = None # test automatically on first use 32 prefer_persist = 0 33 recognizes_binary_splot = 1 34 prefer_inline_data = 0 35 36 # os.mkfifo should be supported on Mac OS X. Let me know if I'm 37 # wrong. 38 support_fifo = 1 39 prefer_fifo_data = 1 40 41 default_term = 'aqua' 42 default_lpr = '| lpr' 43 prefer_enhanced_postscript = 1
44 45 # ############ End of configuration options ############################ 46 47 from os import popen 48 49
50 -def test_persist():
51 """Determine whether gnuplot recognizes the option '-persist'. 52 53 If the configuration variable 'recognizes_persist' is set (i.e., 54 to something other than 'None'), return that value. Otherwise, 55 try to determine whether the installed version of gnuplot 56 recognizes the -persist option. (If it doesn't, it should emit an 57 error message with '-persist' in the first line.) Then set 58 'recognizes_persist' accordingly for future reference. 59 60 """ 61 62 if GnuplotOpts.recognizes_persist is None: 63 import string 64 g = popen('echo | %s -persist 2>&1' % GnuplotOpts.gnuplot_command, 'r') 65 response = g.readlines() 66 g.close() 67 GnuplotOpts.recognizes_persist = ( 68 (not response) or (string.find(response[0], '-persist') == -1)) 69 return GnuplotOpts.recognizes_persist
70 71
72 -class GnuplotProcess:
73 """Unsophisticated interface to a running gnuplot program. 74 75 This represents a running gnuplot program and the means to 76 communicate with it at a primitive level (i.e., pass it commands 77 or data). When the object is destroyed, the gnuplot program exits 78 (unless the 'persist' option was set). The communication is 79 one-way; gnuplot's text output just goes to stdout with no attempt 80 to check it for error messages. 81 82 Members: 83 84 'gnuplot' -- the pipe to the gnuplot command. 85 86 Methods: 87 88 '__init__' -- start up the program. 89 90 '__call__' -- pass an arbitrary string to the gnuplot program, 91 followed by a newline. 92 93 'write' -- pass an arbitrary string to the gnuplot program. 94 95 'flush' -- cause pending output to be written immediately. 96 97 'close' -- close the connection to gnuplot. 98 99 """ 100
101 - def __init__(self, persist=None):
102 """Start a gnuplot process. 103 104 Create a 'GnuplotProcess' object. This starts a gnuplot 105 program and prepares to write commands to it. 106 107 Keyword arguments: 108 109 'persist=1' -- start gnuplot with the '-persist' option, 110 (which leaves the plot window on the screen even after 111 the gnuplot program ends, and creates a new plot window 112 each time the terminal type is set to 'x11'). This 113 option is not available on older versions of gnuplot. 114 115 """ 116 117 if persist is None: 118 persist = GnuplotOpts.prefer_persist 119 if persist: 120 if not test_persist(): 121 raise ('-persist does not seem to be supported ' 122 'by your version of gnuplot!') 123 self.gnuplot = popen('%s -persist' % GnuplotOpts.gnuplot_command, 124 'w') 125 else: 126 self.gnuplot = popen(GnuplotOpts.gnuplot_command, 'w') 127 128 # forward write and flush methods: 129 self.write = self.gnuplot.write 130 self.flush = self.gnuplot.flush
131
132 - def close(self):
133 if self.gnuplot is not None: 134 self.gnuplot.close() 135 self.gnuplot = None
136
137 - def __del__(self):
138 self.close()
139
140 - def __call__(self, s):
141 """Send a command string to gnuplot, followed by newline.""" 142 143 self.write(s + '\n') 144 self.flush()
145