1
2
3
4
5
6
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
22
44
45
46
47 from os import popen
48
49
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
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
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
129 self.write = self.gnuplot.write
130 self.flush = self.gnuplot.flush
131
133 if self.gnuplot is not None:
134 self.gnuplot.close()
135 self.gnuplot = None
136
139
141 """Send a command string to gnuplot, followed by newline."""
142
143 self.write(s + '\n')
144 self.flush()
145