forked from tschaume/gnuplot-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gp_macosx.py
146 lines (102 loc) · 4.41 KB
/
gp_macosx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# $Id$
# Copyright (C) 1998-2003 Michael Haggerty <[email protected]>
#
# This file is licensed under the GNU Lesser General Public License
# (LGPL). See LICENSE.txt for details.
"""gp_macosx -- an interface to the command line version of gnuplot
used under Mac OS X.
The only difference between this interface and gp_unix is that
default_term is 'dumb'.
This file implements a low-level interface to gnuplot. This file
should be imported through gp.py, which in turn should be imported via
'import Gnuplot' rather than using these low-level interfaces
directly.
"""
# ############ Configuration variables: ################################
class GnuplotOpts:
"""The configuration options for gnuplot on Mac OS X.
See the gp_unix.py for documentation on all of the parameters.
"""
gnuplot_command = 'gnuplot'
recognizes_persist = None # test automatically on first use
prefer_persist = 0
recognizes_binary_splot = 1
prefer_inline_data = 0
# os.mkfifo should be supported on Mac OS X. Let me know if I'm
# wrong.
support_fifo = 1
prefer_fifo_data = 1
default_term = 'dumb'
default_lpr = '| lpr'
prefer_enhanced_postscript = 1
# ############ End of configuration options ############################
from os import popen
def test_persist():
"""Determine whether gnuplot recognizes the option '-persist'.
If the configuration variable 'recognizes_persist' is set (i.e.,
to something other than 'None'), return that value. Otherwise,
try to determine whether the installed version of gnuplot
recognizes the -persist option. (If it doesn't, it should emit an
error message with '-persist' in the first line.) Then set
'recognizes_persist' accordingly for future reference.
"""
if GnuplotOpts.recognizes_persist is None:
import string
g = popen('echo | %s -persist 2>&1' % GnuplotOpts.gnuplot_command, 'r')
response = g.readlines()
g.close()
GnuplotOpts.recognizes_persist = (
(not response) or (string.find(response[0], '-persist') == -1))
return GnuplotOpts.recognizes_persist
class GnuplotProcess:
"""Unsophisticated interface to a running gnuplot program.
This represents a running gnuplot program and the means to
communicate with it at a primitive level (i.e., pass it commands
or data). When the object is destroyed, the gnuplot program exits
(unless the 'persist' option was set). The communication is
one-way; gnuplot's text output just goes to stdout with no attempt
to check it for error messages.
Members:
'gnuplot' -- the pipe to the gnuplot command.
Methods:
'__init__' -- start up the program.
'__call__' -- pass an arbitrary string to the gnuplot program,
followed by a newline.
'write' -- pass an arbitrary string to the gnuplot program.
'flush' -- cause pending output to be written immediately.
'close' -- close the connection to gnuplot.
"""
def __init__(self, persist=None):
"""Start a gnuplot process.
Create a 'GnuplotProcess' object. This starts a gnuplot
program and prepares to write commands to it.
Keyword arguments:
'persist=1' -- start gnuplot with the '-persist' option,
(which leaves the plot window on the screen even after
the gnuplot program ends, and creates a new plot window
each time the terminal type is set to 'x11'). This
option is not available on older versions of gnuplot.
"""
if persist is None:
persist = GnuplotOpts.prefer_persist
if persist:
if not test_persist():
raise ('-persist does not seem to be supported '
'by your version of gnuplot!')
self.gnuplot = popen('%s -persist' % GnuplotOpts.gnuplot_command,
'w')
else:
self.gnuplot = popen(GnuplotOpts.gnuplot_command, 'w')
# forward write and flush methods:
self.write = self.gnuplot.write
self.flush = self.gnuplot.flush
def close(self):
if self.gnuplot is not None:
self.gnuplot.close()
self.gnuplot = None
def __del__(self):
self.close()
def __call__(self, s):
"""Send a command string to gnuplot, followed by newline."""
self.write(s + '\n')
self.flush()