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,v 1.3 2003/04/21 09:44:09 mhagger Exp $ 
  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  __cvs_version__ = '$Revision: 1.3 $' 
 22   
 23   
 24  # ############ Configuration variables: ################################ 
 25   
26 -class GnuplotOpts:
27 """The configuration options for gnuplot on Mac OS X. 28 29 See the gp_unix.py for documentation on all of the parameters. 30 31 """ 32 33 gnuplot_command = 'gnuplot' 34 recognizes_persist = None # test automatically on first use 35 prefer_persist = 0 36 recognizes_binary_splot = 1 37 prefer_inline_data = 0 38 39 # os.mkfifo should be supported on Mac OS X. Let me know if I'm 40 # wrong. 41 support_fifo = 1 42 prefer_fifo_data = 1 43 44 default_term = 'aqua' 45 default_lpr = '| lpr' 46 prefer_enhanced_postscript = 1
47 48 # ############ End of configuration options ############################ 49 50 from os import popen 51 52
53 -def test_persist():
54 """Determine whether gnuplot recognizes the option '-persist'. 55 56 If the configuration variable 'recognizes_persist' is set (i.e., 57 to something other than 'None'), return that value. Otherwise, 58 try to determine whether the installed version of gnuplot 59 recognizes the -persist option. (If it doesn't, it should emit an 60 error message with '-persist' in the first line.) Then set 61 'recognizes_persist' accordingly for future reference. 62 63 """ 64 65 if GnuplotOpts.recognizes_persist is None: 66 import string 67 g = popen('echo | %s -persist 2>&1' % GnuplotOpts.gnuplot_command, 'r') 68 response = g.readlines() 69 g.close() 70 GnuplotOpts.recognizes_persist = ( 71 (not response) or (string.find(response[0], '-persist') == -1)) 72 return GnuplotOpts.recognizes_persist
73 74
75 -class GnuplotProcess:
76 """Unsophisticated interface to a running gnuplot program. 77 78 This represents a running gnuplot program and the means to 79 communicate with it at a primitive level (i.e., pass it commands 80 or data). When the object is destroyed, the gnuplot program exits 81 (unless the 'persist' option was set). The communication is 82 one-way; gnuplot's text output just goes to stdout with no attempt 83 to check it for error messages. 84 85 Members: 86 87 'gnuplot' -- the pipe to the gnuplot command. 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 if persist: 121 if not test_persist(): 122 raise ('-persist does not seem to be supported ' 123 'by your version of gnuplot!') 124 self.gnuplot = popen('%s -persist' % GnuplotOpts.gnuplot_command, 125 'w') 126 else: 127 self.gnuplot = popen(GnuplotOpts.gnuplot_command, 'w') 128 # forward write and flush methods: 129 self.write = self.gnuplot.write 130 self.flush = self.gnuplot.flush
131
132 - def __call__(self, s):
133 """Send a command string to gnuplot, followed by newline.""" 134 135 self.write(s + '\n') 136 self.flush()
137