1
2
3
4
5
6
7
8 """gp_unix -- an interface to gnuplot used for unix platforms.
9
10 This file implements a low-level interface to a gnuplot program for a
11 unix platform (actually it is used for any non-Windows, non-Mac
12 system). This file should be imported through gp.py, which in turn
13 should be imported via 'import Gnuplot' rather than these low-level
14 interfaces.
15
16 """
17
18
19
21 """The configuration options for gnuplot on generic platforms.
22
23 Store the options in a class to make them easy to import and
24 modify en masse. If you want to modify the options from the
25 command line or within a running program, do something like the
26 following::
27
28 import Gnuplot
29 Gnuplot.GnuplotOpts.gnuplot_command = '/bin/mygnuplot'
30
31 """
32
33
34
35
36
37 gnuplot_command = 'gnuplot'
38
39
40
41
42
43
44
45
46
47
48
49
50
51 recognizes_persist = None
52
53
54
55 prefer_persist = 0
56
57
58
59
60
61
62
63
64
65
66
67 recognizes_binary_splot = 1
68
69
70
71
72
73
74
75
76 prefer_inline_data = 0
77
78
79
80
81 support_fifo = 1
82
83
84 prefer_fifo_data = 1
85
86
87
88
89
90
91 default_term = 'x11'
92
93
94
95
96
97
98
99
100
101 default_lpr = '| lpr'
102
103
104
105
106
107
108
109 prefer_enhanced_postscript = 1
110
111
112
113 from os import popen
114
115
117 """Determine whether gnuplot recognizes the option '-persist'.
118
119 If the configuration variable 'recognizes_persist' is set (i.e.,
120 to something other than 'None'), return that value. Otherwise,
121 try to determine whether the installed version of gnuplot
122 recognizes the -persist option. (If it doesn't, it should emit an
123 error message with '-persist' in the first line.) Then set
124 'recognizes_persist' accordingly for future reference.
125
126 """
127
128 if GnuplotOpts.recognizes_persist is None:
129 import string
130 g = popen('echo | %s -persist 2>&1' % GnuplotOpts.gnuplot_command, 'r')
131 response = g.readlines()
132 g.close()
133 GnuplotOpts.recognizes_persist = (
134 (not response) or (string.find(response[0], '-persist') == -1))
135 return GnuplotOpts.recognizes_persist
136
137
139 """Unsophisticated interface to a running gnuplot program.
140
141 This represents a running gnuplot program and the means to
142 communicate with it at a primitive level (i.e., pass it commands
143 or data). When the object is destroyed, the gnuplot program exits
144 (unless the 'persist' option was set). The communication is
145 one-way; gnuplot's text output just goes to stdout with no attempt
146 to check it for error messages.
147
148 Members:
149
150 'gnuplot' -- the pipe to the gnuplot command.
151
152 Methods:
153
154 '__init__' -- start up the program.
155
156 '__call__' -- pass an arbitrary string to the gnuplot program,
157 followed by a newline.
158
159 'write' -- pass an arbitrary string to the gnuplot program.
160
161 'flush' -- cause pending output to be written immediately.
162
163 'close' -- close the connection to gnuplot.
164
165 """
166
168 """Start a gnuplot process.
169
170 Create a 'GnuplotProcess' object. This starts a gnuplot
171 program and prepares to write commands to it.
172
173 Keyword arguments:
174
175 'persist=1' -- start gnuplot with the '-persist' option,
176 (which leaves the plot window on the screen even after
177 the gnuplot program ends, and creates a new plot window
178 each time the terminal type is set to 'x11'). This
179 option is not available on older versions of gnuplot.
180
181 """
182
183 if persist is None:
184 persist = GnuplotOpts.prefer_persist
185 if persist:
186 if not test_persist():
187 raise ('-persist does not seem to be supported '
188 'by your version of gnuplot!')
189 self.gnuplot = popen('%s -persist' % GnuplotOpts.gnuplot_command,
190 'w')
191 else:
192 self.gnuplot = popen(GnuplotOpts.gnuplot_command, 'w')
193
194
195 self.write = self.gnuplot.write
196 self.flush = self.gnuplot.flush
197
199 if self.gnuplot is not None:
200 self.gnuplot.close()
201 self.gnuplot = None
202
205
207 """Send a command string to gnuplot, followed by newline."""
208
209 self.write(s + '\n')
210 self.flush()
211