-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturtle.py
More file actions
executable file
·139 lines (120 loc) · 3.88 KB
/
turtle.py
File metadata and controls
executable file
·139 lines (120 loc) · 3.88 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
#!/usr/bin/env python
import subprocess
import sys
import urwid as ur
import yaml
from ui.widgets import *
I_TITLEBAR = 0
I_BODY = 1
I_PREVIEW = 2
I_BUTTONS = 3
I_OUTPUT = 4
def command_form_from_file(filename):
f = open(filename)
raw = yaml.safe_load(f)
f.close()
command = raw['command']
params = raw['params']
form = CommandForm(raw['title'], command, params)
return form
class CommandForm:
def __init__(self, title, command, params=[], env={}, user=''):
self.title = title
self.command = command
self.params = params
self.env = env
self.user = user
def __len__(self):
return len(self.params)
def __str__(self):
return self.command + ' ' + ' '.join(['%s %s' % (v['flag'], v['value']) for v in self.params if v.has_key('value')])
def set_values(self, pile):
i = 0
while i < len(self):
v = pile[i].get_value()
if v != None:
self.params[i]['value'] = v
elif self.params[i].has_key('value'):
del(self.params[i]['value'])
i += 1
class FormListBox(ur.ListBox):
def __init__(self, form, separator=': '):
self.form = form
self.separator = separator
self.widget_factory = WidgetFactory(separator)
body = ur.SimpleFocusListWalker([
ur.AttrWrap(ur.Text(form.title), 'command'),
ur.Pile(self._get_widgets()),
ur.AttrWrap(ur.Text(''), 'command'),
ur.Columns([
ur.AttrWrap(ur.Button('Execute', self.execute), 'execute'),
ur.AttrWrap(ur.Button('Reset', self.reset), 'reset'),
ur.AttrWrap(ur.Button('Exit', self.exit), 'exit')
]),
ur.AttrWrap(ur.Text(''), 'output')
])
super(FormListBox, self).__init__(body)
self.update()
def _get_widgets(self):
return [self.widget_factory.get_widget(param) for param in self.form.params]
def exit(self, *args):
self.update()
raise ur.ExitMainLoop()
def execute(self, button):
self.update()
proc = subprocess.Popen(str(self.form), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# TODO: Update output periodically to make more useful for long-running processes
output = proc.communicate()
self.body[I_OUTPUT].set_text(output[1] + output[0])
def reset(self, button):
[self.body[I_BODY][i].reset() for i in range(0, len(self.form))]
self.body[I_OUTPUT].set_text('')
self.update()
def update(self):
self.form.set_values(self.body[I_BODY])
self.body[I_PREVIEW].set_text(str(self.form))
def keypress(self, size, key):
key = super(FormListBox, self).keypress(size, key)
if key == 'ctrl n' or key == 'down':
self.focus_position = self.move(1)
elif key == 'ctrl p' or key == 'up':
self.focus_position = self.move(-1)
elif key == 'ctrl d':
self.exit()
else:
return key
def move(self, delta):
self.update()
if self.focus_position == I_BODY:
return self._move_helper(self.focus_position, delta, len(self.form), I_BODY, I_BUTTONS)
elif self.focus_position == I_BUTTONS:
return self._move_helper(self.focus_position, delta, 3, I_BODY, I_BUTTONS)
else:
return self.focus_position
def _move_helper(self, major_index, delta, max_minor_index, prev_major_index, next_major_index):
minor_index = self.body[major_index].focus_position
if minor_index + delta < 0:
self.body[major_index].focus_position = 0
return prev_major_index
elif minor_index + delta >= max_minor_index:
self.body[major_index].focus_position = max_minor_index - 1
return next_major_index
else:
self.body[major_index].focus_position = minor_index + delta
return major_index
def __main__():
if len(sys.argv) != 2:
raise ValueError("Exactly one command configuration file must be specified")
filename = sys.argv[1]
form = command_form_from_file(filename)
palette = [
('command', 'black,bold', 'yellow'),
('output', 'black,bold', 'dark green'),
('execute', 'white,bold', 'dark red'),
('exit', 'white,bold', 'black'),
('reset', 'white,bold', 'dark blue')
]
ur.MainLoop(FormListBox(form), palette).run()
print form
if __name__ == '__main__':
__main__()