-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_plot
More file actions
executable file
·77 lines (66 loc) · 2.2 KB
/
run_plot
File metadata and controls
executable file
·77 lines (66 loc) · 2.2 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
#!/usr/bin/env python3
import os.path
import importlib
import atexit
import subprocess
import traceback
from datetime import datetime
from argparse import ArgumentParser
from plot import Plot
import turtle as t
class PlotWatcher():
def __init__(self, p: Plot, filename, plot_args):
self.p = p
self.filename = filename
self.plot_args = plot_args
self.module = None
self.mtime = None
self.drawing = False
def redraw_plot(self):
if self.drawing:
return False
print('reloading plot')
try:
if self.module:
importlib.reload(self.module)
else:
module_name = os.path.splitext(self.filename)[0].replace('/', '.')
self.module = importlib.import_module(module_name)
self.drawing = True
self.module.main(self.p, *self.plot_args)
except:
traceback.print_exc()
self.drawing = False
t.update()
return True
def _tick(self):
new_mtime = os.path.getmtime(os.path.abspath(self.filename))
if self.mtime is None or new_mtime > self.mtime:
if self.redraw_plot():
self.mtime = new_mtime
t._Screen._root.after(200, self._tick)
def start(self):
t.onscreenclick(lambda _x, _y: self.redraw_plot())
self._tick()
def main(args, plot_args):
p = Plot(args.plotter_enabled)
if args.plotter_enabled:
module_name = os.path.splitext(args.filename)[0].replace('/', '.')
module = importlib.import_module(module_name)
# prevent screen from sleeping
proc = subprocess.Popen(['caffeinate', '-d'])
atexit.register(proc.terminate)
start_time = datetime.now()
module.main(p, *plot_args)
print('total time elapsed:', datetime.now()-start_time)
proc.terminate()
p.done()
else:
PlotWatcher(p, args.filename, plot_args).start()
p.done()
if __name__ == "__main__":
p = ArgumentParser()
p.add_argument('filename')
p.add_argument('-p', '--plot', dest='plotter_enabled', action='store_true')
args, plot_args = p.parse_known_args()
main(args, plot_args)