-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcliwrapper.py
More file actions
executable file
·124 lines (98 loc) · 3.91 KB
/
cliwrapper.py
File metadata and controls
executable file
·124 lines (98 loc) · 3.91 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
#!/usr/bin/env python
#
# Created by Aaron Beckett June 13, 2013
# Editted by:
# Aaron Beckett
#
#
'''Module for graphically managing command line options for a given command.'''
import os, sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from Wrapper import BasicWrapper
from cligui import OptionInputWindow
class CLIWrapper(BasicWrapper):
def __init__(self,command='',args=[]):
# command - what command the user wants to run
# args - a list of tuples of the form: (option name, option representation)
# where option representation is something like '-d:' if option takes arg
# and something like '-s' if option takes no arg
# options - a dict where the key is the option name which points to a list
# of the form [option name, option representation, option value]
self.base_command = command.strip()
self.args = [(name.strip(),rep.strip()) for name,rep in args]
self.options = []
for arg in self.args:
name = arg[0]
flag = arg[1].split(':')[0]
if flag[-2:] == '|*':
flag = flag[:-2]
value = ''
self.options.append([name,flag,value])
self.build_command()
def preprocess(self):
self.get_options()
def get_options(self):
'''Use an OptionInputGui to get all necessary options.'''
app = QApplication(sys.argv)
app.connect(app, SIGNAL("lastWindowClosed()"),
app, SLOT("quit()"))
self.win = self.create_window()
def run():
if self.isvalid(self.win.result):
self.base_command, self.args, self.options = self.win.result
self.run_command(self.base_command, self.options)
else:
print 'bad options'
app.connect(self.win, SIGNAL("readyToRun()"), run)
save = lambda (cm,arg,op)=self.win.result: self.save_options(cm,arg,op)
app.connect(self.win, SIGNAL("readyToSave()"), save)
self.win.show()
app.exec_()
sys.exit(0)
def create_window(self):
return OptionInputWindow(self.base_command, self.args, self.options)
def run_command(self,cmd,opts):
'''Run the command built from the OptionInputGui results.'''
self.build_command(cmd,opts)
print self.command
BasicWrapper.run_command(self)
def build_command(self,cmd=None,opts=None):
if not cmd: cmd = self.base_command
if not opts: opts = self.options
self.command = ''
self.command += cmd
for name,arg,value in opts:
if value:
self.command += ' '+arg
if not isinstance(value,bool):
self.command += ' '+value
def isvalid(self,result):
return True
#****** Manage Options ******
def save_options(self,cmd,args,opts):
print 'SAVE'
print cmd
print args
print opts
def open_options(self,previous_opts_file):
pass
def compare_options(self,previous_opts_file):
pass
def main():
'''Open a blank CLIWrapper for editting and automatically run the command it generates.'''
os.chdir('../chamview')
wrapper = CLIWrapper(command='jibber-jabber',
args=[('String','-l:'),
('No option, just value',':'),
('File','-o:_file_:Text File,*.txt'),
('Directory','-d:_dir_|*'),
('Boolean','-s|*'),
('Int','-i:_int_'),
('Int min/max','-I:_int_:0:19'),
('Float','-f:_float_'),
('Float min/max','-F:_float_:2.5:4.5'),
('Menu','-m:_menu_:One:Two:Three')])
wrapper.wrap()
if __name__ == '__main__':
sys.exit(main())