Added var plotting using js.
[smonitor.git] / monitor / extra / Gnuplot / gp_unix.py
blobb47e273301b9de3817c1e5e94d6abdcda269602c
1 # $Id: gp_unix.py 292 2006-03-03 09:49:04Z mhagger $
3 # Copyright (C) 1998-2003 Michael Haggerty <mhagger@alum.mit.edu>
5 # This file is licensed under the GNU Lesser General Public License
6 # (LGPL). See LICENSE.txt for details.
8 """gp_unix -- an interface to gnuplot used for unix platforms.
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.
16 """
18 # ############ Configuration variables: ################################
20 class GnuplotOpts:
21 """The configuration options for gnuplot on generic platforms.
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::
28 import Gnuplot
29 Gnuplot.GnuplotOpts.gnuplot_command = '/bin/mygnuplot'
31 """
33 # Command to start up the gnuplot program. If your version of
34 # gnuplot is run otherwise, specify the correct command here. You
35 # could also specify a full path or append command-line options
36 # here if you wish.
37 gnuplot_command = 'gnuplot'
39 # Recent versions of gnuplot (at least for Xwindows) allow a
40 # `-persist' command-line option when starting up gnuplot. When
41 # this option is specified, graph windows remain on the screen
42 # even after you quit gnuplot (type `q' in the window to close
43 # it). This can be handy but unfortunately it is not supported by
44 # older versions of gnuplot. The following configuration variable
45 # specifies whether the user's version of gnuplot recognizes this
46 # option or not. You can set this variable to 1 (supports
47 # -persist) or 0 (doesn't support) yourself; if you leave it with
48 # the value None then the first time you create a Gnuplot object
49 # it will try to detect automatically whether your version accepts
50 # this option.
51 recognizes_persist = None # test automatically on first use
53 # What should be the default if the persist option is not
54 # specified explicitly?
55 prefer_persist = 0
57 # Recent versions of gnuplot allow you to specify a `binary'
58 # option to the splot command for grid data, which means that the
59 # data file is to be read in binary format. This option saves
60 # substantial time writing and reading the file, and can also save
61 # substantial disk space and therefore it is the default for that
62 # type of plot. But if you have an older version of gnuplot (or
63 # you prefer text format) you can disable the binary option in
64 # either of two ways: (a) set the following variable to 0; or (b)
65 # pass `binary=0' to the GridData constructor. (Note that the
66 # demo uses binary=0 to maximize portability.)
67 recognizes_binary_splot = 1
69 # Data can be passed to gnuplot through a temporary file or as
70 # inline data (i.e., the filename is set to '-' and the data is
71 # entered into the gnuplot interpreter followed by 'e'). If
72 # prefer_inline_data is true, then use the inline method as
73 # default whenever it is supported. This should be fast but will
74 # use more memory since currently the inline data is put into a
75 # big string when the PlotItem is created.
76 prefer_inline_data = 0
78 # Does Python implement the threading module and os.mkfifo on this
79 # operating system? If so, the _FIFOFileItem class will be
80 # defined in PlotItem.py.
81 support_fifo = 1
83 # Should FIFOs be used to send data to gnuplot by default?
84 prefer_fifo_data = 1
86 # After a hardcopy is produced, we have to set the terminal type
87 # back to `on screen' using gnuplot's `set terminal' command. The
88 # following is the usual setting for Xwindows. If it is wrong,
89 # change the following line to select the terminal type you prefer
90 # to use for on-screen work.
91 default_term = 'x11'
93 # Gnuplot can plot to a printer by using "set output '| ...'"
94 # where ... is the name of a program that sends its stdin to a
95 # printer, or by "set output 'printer_device', where
96 # 'printer_device is the name of a file-like interface to the
97 # printer. On my machine the appropriate program is `lpr', as set
98 # below. On your computer it may be something different (like
99 # `lp'); you can set that by changing the variable below. You can
100 # also add options to the print command if needed.
101 default_lpr = '| lpr'
103 # Enhanced postscript is an option to the postscript terminal
104 # driver that requests enhanced treatment of strings (for example,
105 # font changes, superscripts, and subscripts). Set to 1 to enable
106 # or 0 to disable. If you have a version of gnuplot earlier than
107 # 3.7, you should set this to None (*not* 0!) so that the option
108 # is not used at all.
109 prefer_enhanced_postscript = 1
111 # ############ End of configuration options ############################
113 from os import popen
116 def test_persist():
117 """Determine whether gnuplot recognizes the option '-persist'.
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.
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
138 class GnuplotProcess:
139 """Unsophisticated interface to a running gnuplot program.
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.
148 Members:
150 'gnuplot' -- the pipe to the gnuplot command.
152 Methods:
154 '__init__' -- start up the program.
156 '__call__' -- pass an arbitrary string to the gnuplot program,
157 followed by a newline.
159 'write' -- pass an arbitrary string to the gnuplot program.
161 'flush' -- cause pending output to be written immediately.
163 'close' -- close the connection to gnuplot.
167 def __init__(self, persist=None):
168 """Start a gnuplot process.
170 Create a 'GnuplotProcess' object. This starts a gnuplot
171 program and prepares to write commands to it.
173 Keyword arguments:
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.
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')
194 # forward write and flush methods:
195 self.write = self.gnuplot.write
196 self.flush = self.gnuplot.flush
198 def close(self):
199 if self.gnuplot is not None:
200 self.gnuplot.close()
201 self.gnuplot = None
203 def __del__(self):
204 self.close()
206 def __call__(self, s):
207 """Send a command string to gnuplot, followed by newline."""
209 self.write(s + '\n')
210 self.flush()