-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathjplotter
executable file
·52 lines (46 loc) · 2.1 KB
/
jplotter
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
#!/usr/bin/env python
from __future__ import print_function
import sys, os, re, operator, functools
from functional import drap
if '-v' in sys.argv:
print("$Id: jplotter,v 1.3 2014-08-08 15:38:40 jive_cc Exp $")
sys.exit( 0 )
if '-h' in sys.argv:
print("Usage: {0} [-h] [-d] [-v] [-c <cmd>]".format(os.path.basename(sys.argv[0])))
print("""
where:
-h print this message and exit"
-v print version and exit
-c <cmd> execute <cmd> as if typed at the keyboard. if present
multiple times, commands are executed in command line
order. jplotter exits after the last command is executed.
-d turn on debug output [prints stack trace upon error]
ppgplot=/path/to/home/of{/ppgplot/__init__.py}
load ppgplot module from '/path/to/home/of' if that path exists
""")
sys.exit( 0 )
# look for an entries "ppgplot=..." and insert them into the system path
# this map() is used for its side-effect(s) so for Py3 we must drain the
# iterable. functional.drap(...) combines drain() + map()
drap(lambda mo: sys.path.insert(0, mo.group(1)) if os.path.isdir(mo.group(1)) else None,
filter(operator.truth, map(re.compile(r'^ppgplot=(.+)$').search, sys.argv[1:])))
# gobble all '-c <cmd>' commands together
def collect_cmds(acc, x):
if acc.state is not None:
# previous argument was '-c' so append this argument to the list
# that is, if it's not an option itself
if x[0]=='-' or x.startswith('ppgplot='):
raise RuntimeError("-c option expects a command, not option '{0}'".format(x))
acc.cmds.append(x)
acc.state = None
else:
# do we see '-c'?
if x == '-c':
acc.state = x
return acc
acc = functools.reduce(collect_cmds, sys.argv[1:], type('',(),{'state':None, 'cmds':list()})())
if acc.state is not None:
raise RuntimeError("-c option missing argument")
# we run interactively from the commandline
import command, jplotter
jplotter.run_plotter(command.scripted(acc.cmds) if acc.cmds else command.readkbd("jcli"), debug=('-d' in sys.argv))