1
# $Id: gp_unix.py,v 2.3 2001/01/07 21:35:13 mhagger Exp $
3
# Copyright (C) 1998,1999 Michael Haggerty <mhagger@alum.mit.edu>
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version. This program is distributed in
9
# the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
11
# PARTICULAR PURPOSE. See the GNU General Public License for more
12
# details; it is available at <http://www.fsf.org/copyleft/gpl.html>,
13
# or by writing to the Free Software Foundation, Inc., 59 Temple Place
14
# - Suite 330, Boston, MA 02111-1307, USA.
16
"""gp_unix -- an interface to gnuplot used for unix platforms.
18
This file implements a low-level interface to a gnuplot program for a
19
unix platform (actually it is used for any non-Windows, non-Mac
20
system). This file should be imported through gp.py, which in turn
21
should be imported via 'import Gnuplot' rather than these low-level
26
__cvs_version__ = '$Revision: 2.3 $'
29
# ############ Configuration variables: ################################
32
"""The configuration options for gnuplot on generic platforms.
34
Store the options in a class to make them easy to import and
35
modify en masse. If you want to modify the options from the
36
command line or within a running program, do something like the
40
Gnuplot.GnuplotOpts.gnuplot_command = '/bin/mygnuplot'
44
# Command to start up the gnuplot program. If your version of
45
# gnuplot is run otherwise, specify the correct command here. You
46
# could also specify a full path or append command-line options
48
gnuplot_command = 'gnuplot'
50
# Recent versions of gnuplot (at least for Xwindows) allow a
51
# `-persist' command-line option when starting up gnuplot. When
52
# this option is specified, graph windows remain on the screen
53
# even after you quit gnuplot (type `q' in the window to close
54
# it). This can be handy but unfortunately it is not supported by
55
# older versions of gnuplot. The following configuration variable
56
# specifies whether the user's version of gnuplot recognizes this
57
# option or not. You can set this variable to 1 (supports
58
# -persist) or 0 (doesn't support) yourself; if you leave it with
59
# the value None then the first time you create a Gnuplot object
60
# it will try to detect automatically whether your version accepts
62
recognizes_persist = None # test automatically on first use
64
# What should be the default if the persist option is not
65
# specified explicitly?
68
# Recent versions of gnuplot allow you to specify a `binary'
69
# option to the splot command for grid data, which means that the
70
# data file is to be read in binary format. This option saves
71
# substantial time writing and reading the file, and can also save
72
# substantial disk space and therefore it is the default for that
73
# type of plot. But if you have an older version of gnuplot (or
74
# you prefer text format) you can disable the binary option in
75
# either of two ways: (a) set the following variable to 0; or (b)
76
# pass `binary=0' to the GridData constructor. (Note that the
77
# demo uses binary=0 to maximize portability.)
78
recognizes_binary_splot = 1
80
# Data can be passed to gnuplot through a temporary file or as
81
# inline data (i.e., the filename is set to '-' and the data is
82
# entered into the gnuplot interpreter followed by 'e'). If
83
# prefer_inline_data is true, then use the inline method as
84
# default whenever it is supported. This should be fast but will
85
# use more memory since currently the inline data is put into a
86
# big string when the PlotItem is created.
87
prefer_inline_data = 0
89
# After a hardcopy is produced, we have to set the terminal type
90
# back to `on screen' using gnuplot's `set terminal' command. The
91
# following is the usual setting for Xwindows. If it is wrong,
92
# change the following line to select the terminal type you prefer
93
# to use for on-screen work.
96
# Gnuplot can plot to a printer by using "set output '| ...'"
97
# where ... is the name of a program that sends its stdin to a
98
# printer, or by "set output 'printer_device', where
99
# 'printer_device is the name of a file-like interface to the
100
# printer. On my machine the appropriate program is `lpr', as set
101
# below. On your computer it may be something different (like
102
# `lp'); you can set that by changing the variable below. You can
103
# also add options to the print command if needed.
104
default_lpr = '| lpr'
106
# Enhanced postscript is an option to the postscript terminal
107
# driver that requests enhanced treatment of strings (for example,
108
# font changes, superscripts, and subscripts). Set to 1 to enable
109
# or 0 to disable. If you have a version of gnuplot earlier than
110
# 3.7, you should set this to None (*not* 0!) so that the option
111
# is not used at all.
112
prefer_enhanced_postscript = 1
114
# ############ End of configuration options ############################
120
"""Determine whether gnuplot recognizes the option '-persist'.
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.
131
if GnuplotOpts.recognizes_persist is None:
133
g = popen('echo | %s -persist 2>&1' % GnuplotOpts.gnuplot_command, 'r')
134
response = g.readlines()
136
GnuplotOpts.recognizes_persist = (
137
(not response) or (string.find(response[0], '-persist') == -1))
138
return GnuplotOpts.recognizes_persist
141
class GnuplotProcess:
142
"""Unsophisticated interface to a running gnuplot program.
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.
153
'gnuplot' -- the pipe to the gnuplot command.
157
'__init__' -- start up the program.
159
'__call__' -- pass an arbitrary string to the gnuplot program,
160
followed by a newline.
162
'write' -- pass an arbitrary string to the gnuplot program.
164
'flush' -- cause pending output to be written immediately.
168
def __init__(self, persist=None):
169
"""Start a gnuplot process.
171
Create a 'GnuplotProcess' object. This starts a gnuplot
172
program and prepares to write commands to it.
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.
185
persist = GnuplotOpts.prefer_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,
193
self.gnuplot = popen(GnuplotOpts.gnuplot_command, 'w')
194
# forward write and flush methods:
195
self.write = self.gnuplot.write
196
self.flush = self.gnuplot.flush
198
def __call__(self, s):
199
"""Send a command string to gnuplot, followed by newline."""