File: gp_mac.py

package info (click to toggle)
python-gnuplot 1.8-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,272 kB
  • sloc: python: 2,042; makefile: 6
file content (129 lines) | stat: -rw-r--r-- 3,481 bytes parent folder | download | duplicates (5)
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
# $Id: gp_mac.py 292 2006-03-03 09:49:04Z mhagger $

# Copyright (C) 1999-2003 Michael Haggerty <mhagger@alum.mit.edu>
# Thanks to Tony Ingraldi and Noboru Yamamoto for their contributions.
#
# This file is licensed under the GNU Lesser General Public License
# (LGPL).  See LICENSE.txt for details.

"""gp_mac -- an interface to gnuplot for the Macintosh.

"""

import os, string

import Errors


# ############ Configuration variables: ################################

class GnuplotOpts:
    """The configuration options for gnuplot on the Macintosh.

    See gp.py for details about the meaning of these options.  Please
    let me know if you know better choices for these settings."""

    # The '-persist' option is not supported on the Mac:
    recognizes_persist = 0

    # Apparently the Mac can use binary data:
    recognizes_binary_splot = 1

    # Apparently the Mac can not use inline data:
    prefer_inline_data = 0

    # os.mkfifo is not supported on the Mac.
    support_fifo = 0
    prefer_fifo_data = 0

    # The default choice for the 'set term' command (to display on screen).
    # Terminal types are different in Gnuplot 3.7.1c.
    # For earlier versions, this was default_term = 'macintosh'
    default_term = 'pict'

    # I don't know how to print directly to a printer on the Mac:
    default_lpr = '| lpr'

    # Used the 'enhanced' option of postscript by default?  Set to
    # None (*not* 0!) if your version of gnuplot doesn't support
    # enhanced postscript.
    prefer_enhanced_postscript = 1

# ############ End of configuration options ############################


# The Macintosh doesn't support pipes so communication is via
# AppleEvents.

import gnuplot_Suites
import Required_Suite
import aetools


# Mac doesn't recognize persist.
def test_persist():
    return 0


class _GNUPLOT(aetools.TalkTo,
               Required_Suite.Required_Suite,
               gnuplot_Suites.gnuplot_Suite,
               gnuplot_Suites.odds_and_ends,
               gnuplot_Suites.Standard_Suite,
               gnuplot_Suites.Miscellaneous_Events):
    """Start a gnuplot program and emulate a pipe to it."""

    def __init__(self):
        aetools.TalkTo.__init__(self, '{GP}', start=1)


class GnuplotProcess:
    """Unsophisticated interface to a running gnuplot program.

    See gp_unix.GnuplotProcess for usage information.

    """

    def __init__(self, persist=0):
        """Start a gnuplot process.

        Create a 'GnuplotProcess' object.  This starts a gnuplot
        program and prepares to write commands to it.

        Keyword arguments:

          'persist' -- the '-persist' option is not supported on the
                       Macintosh so this argument must be zero.

        """

        if persist:
            raise Errors.OptionError(
                '-persist is not supported on the Macintosh!')

        self.gnuplot = _GNUPLOT()

    def close(self):
        if self.gnuplot is not None:
            self.gnuplot.quit()
            self.gnuplot = None

    def __del__(self):
        self.close()

    def write(self, s):
        """Mac gnuplot apparently requires '\r' to end statements."""

        self.gnuplot.gnuexec(string.replace(s, '\n', os.linesep))

    def flush(self):
        pass
        
    def __call__(self, s):
        """Send a command string to gnuplot, for immediate execution."""

        # Apple Script doesn't seem to need the trailing '\n'.
        self.write(s)
        self.flush()