forked from has2k1/gnuplot_kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.py
More file actions
297 lines (253 loc) · 8.57 KB
/
kernel.py
File metadata and controls
297 lines (253 loc) · 8.57 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
from __future__ import print_function
import sys
import re
import os.path
import time
import random
from IPython.display import Image, SVG
from metakernel import MetaKernel, ProcessMetaKernel, pexpect, u
from metakernel.process_metakernel import TextOutput
from .replwrap import GnuplotREPLWrapper, PROMPT
from .exceptions import GnuplotError
# This is the only place that the version is
# specified
__version__ = '0.3.0'
try:
FileNotFoundError
except NameError:
# Python 2
FileNotFoundError = OSError
# name of the command i.e first token
CMD_RE = re.compile(r'^\s*(\w+)\s?')
# "set multiplot" and abbreviated variants
MULTI_RE = re.compile(r'\s*set\s+multip(?:l|lo|lot)?')
# "unset multiplot" and abbreviated variants
UNMULTI_RE = re.compile(r'\s*uns(?:e|et)?\s+multip(?:l|lo|lot)?')
PLOT_CMDS = {
'plot', 'plo', 'pl', 'p',
'splot', 'splo', 'spl', 'sp',
'replot', 'replo', 'repl', 'rep',
}
class GnuplotKernel(ProcessMetaKernel):
implementation = 'Gnuplot Kernel'
implementation_version = __version__
language = 'gnuplot'
language_version = '5.0'
banner = 'Gnuplot Kernel'
language_info = {
'mimetype': 'text/x-gnuplot',
'name': 'gnuplot',
'file_extension': '.gp',
'codemirror_mode': 'Octave',
'help_links': MetaKernel.help_links,
}
kernel_json = {
'argv': [sys.executable,
'-m', 'gnuplot_kernel',
'-f', '{connection_file}'],
'display_name': 'gnuplot',
'language': 'gnuplot',
'name': 'gnuplot',
}
inline_plotting = True
reset_code = ''
_first = True
_image_files = []
_error = False
@staticmethod
def is_plot(stmt):
"""
Return True if stmt is a plot statement
"""
m = re.match(CMD_RE, stmt)
if m:
return m.group(1) in PLOT_CMDS
return False
@staticmethod
def is_set_multiplot(stmt):
"""
Return True if stmt is a plot statement
"""
m = re.match(MULTI_RE, stmt)
return True if m else False
@staticmethod
def is_unset_multiplot(stmt):
"""
Return True if stmt is a plot statement
"""
m = re.match(UNMULTI_RE, stmt)
return True if m else False
def bad_prompt_warning(self):
"""
Print warning if the prompt is not 'gnuplot>'
"""
if not self.wrapper.prompt.startswith('gnuplot>'):
msg = ("Warning: The prompt is currently set "
"to '{}'".format(self.wrapper.prompt))
print(msg)
def do_execute_direct(self, code):
"""
Execute gnuplot code
"""
if self._first:
self._first = False
self.handle_plot_settings()
if self.inline_plotting:
code = self.add_inline_image_statements(code)
success = True
try:
result = super(GnuplotKernel,
self).do_execute_direct(code, silent=True)
except GnuplotError as e:
result = TextOutput(e.message)
success = False
if self.reset_code:
super(GnuplotKernel, self).do_execute_direct(
self.reset_code, silent=True)
if self.inline_plotting:
if success:
self.display_images()
self.delete_image_files()
self.bad_prompt_warning()
# No empty strings
return result if (result and result.output) else None
def add_inline_image_statements(self, code):
"""
Add 'set output ...' before every plotting statement
This is what powers inline plotting
"""
# Ensure that there are no stale images
self.delete_image_files()
def append_output_stmt(lines):
filename = self.get_image_filename()
if filename:
lines.append("set output '{}'".format(filename))
# We automatically create an output file before every
# recognisable plot statement is executed or before
# a multiplot block. When inside a multiplot block
# image files are not created.
lines = []
inside_multiplot = False
for stmt in code.splitlines():
if self.is_set_multiplot(stmt):
inside_multiplot = True
append_output_stmt(lines)
elif self.is_unset_multiplot(stmt):
inside_multiplot = False
if self.is_plot(stmt) and not inside_multiplot:
append_output_stmt(lines)
lines.append(stmt)
# Make gnuplot flush the output
if not lines[-1].endswith('\\'):
lines.append('unset output')
code = '\n'.join(lines)
return code
def get_image_filename(self):
"""
Create file to which gnuplot will write the plot
Returns the filename.
"""
# we could use tempfile.NamedTemporaryFile but we do not
# want to create the file, gnuplot will create it.
# Later on when we check if the file exists we know
# whodunnit.
settings = self.plot_settings
filename = '/tmp/gnuplot-inline-{}.{}.{}'.format(
time.time(),
random.randint(1, 10**12),
settings['format'])
filename = filename
self._image_files.append(filename)
return filename
def display_images(self):
"""
Display images if gnuplot wrote to them
"""
settings = self.plot_settings
if self.inline_plotting:
if settings['format'] == 'svg':
_Image = SVG
else:
_Image = Image
for filename in self._image_files:
try:
size = os.path.getsize(filename)
except FileNotFoundError:
size = 0
if not size:
msg = ("An unknown error occured. Failed to "
"read and display image file from "
"gnuplot.")
print(msg)
continue
im = _Image(filename)
self.Display(im)
def delete_image_files(self):
"""
Delete the image files
"""
# After display_images(), the real images are
# no longer required.
for filename in self._image_files:
try:
os.remove(filename)
except FileNotFoundError:
pass
self._image_files = []
def makeWrapper(self):
"""
Start gnuplot and return wrapper around the REPL
"""
if pexpect.which('gnuplot'):
program = 'gnuplot'
elif pexpect.which('gnuplot.exe'):
program = 'gnuplot.exe'
else:
raise Exception("gnuplot not found.")
# We don't want help commands getting stuck,
# use a non interactive PAGER
if pexpect.which('env') and pexpect.which('cat'):
command = 'env PAGER=cat {}'.format(program)
else:
command = program
d = dict(cmd_or_spawn=command,
prompt_regex=u('\w*> $'),
prompt_change_cmd=None)
wrapper = GnuplotREPLWrapper(**d)
# No sleeping before sending commands to gnuplot
wrapper.child.delaybeforesend = 0
return wrapper
def do_shutdown(self, restart):
"""
Exit the gnuplot process and any other underlying stuff
"""
self.wrapper.exit()
super(GnuplotKernel, self).do_shutdown(restart)
def get_kernel_help_on(self, info, level=0, none_on_fail=False):
obj = info.get('help_obj', '')
if not obj or len(obj.split()) > 1:
if none_on_fail:
return None
else:
return ''
res = self.do_execute_direct('help %s' % obj)
text = res.output.strip().rstrip(PROMPT)
self.bad_prompt_warning()
return text
def handle_plot_settings(self):
"""
Handle the current plot settings
This is used by the gnuplot line magic. The plot magic
is innadequate.
"""
settings = self.plot_settings
if ('termspec' not in settings or
not settings['termspec']):
settings['termspec'] = ('pngcairo size 385, 256'
' font "Arial,10"')
if ('format' not in settings or
not settings['format']):
settings['format'] = 'png'
self.inline_plotting = settings['backend'] == 'inline'
cmd = 'set terminal {}'.format(settings['termspec'])
self.do_execute_direct(cmd)