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