-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmask.py
129 lines (104 loc) · 3.33 KB
/
mask.py
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
from snack import *
class Mask:
"""
An input mask.
"""
def __init__(self, screen, title="unnamed mask", width=30):
"""
Creates an input mask with a title.
The width is applied to the right side of the window.
"""
self._width = width
self._screen = screen
self.g = GridForm(self._screen, title, 2, 25)
self._row = 0
self._elements = {}
self._buttons = {}
def entry(self, label, name, text="", password=0):
"""
Creates an entry for text.
"""
self.g.add( Label( label ), 0, self._row, (0,0,1,1) )
self._elements[name] = Entry(self._width, text, password=password)
self.g.add( self._elements[name], 1, self._row, (0,0,0,1) )
self._row += 1
def password(self, label, name, text=""):
"""
Creates an password entry.
"""
self.entry(label, name, text, password=1)
def buttons(self, **kwargs):
"""
Creates a set of buttons given as kwargs.
IE: mask.buttons(yes="Yes", no="No")
"""
col = 0
for name in kwargs:
self._buttons[name] = Button( kwargs[name] )
self.g.add( self._buttons[name], col, self._row, (0,0,1,0) )
col = 1-col
if col == 0:
self._row += 1
def radios(self, label, name, options):
"""
Creates a radio group.
Options are given as [ (label, value, checked), ... ] where
checked equ. 0/1.
"""
self.g.add( Label( label ), 0, self._row, (0,0,1,1) )
self._elements[name] = RadioBar(self._screen, options)
self.g.add( self._elements[name], 1, self._row, (0,0,0,1), anchorLeft=1 )
self._row += 1
def list(self, label, name, options, height=None, scroll=1):
"""
Creates a single choice list.
Options are given as [ (label, value, checked), ... ] where
checked equ. 0/1.
"""
self.g.add( Label( label ), 0, self._row, (0,0,1,1) )
if height is None:
height = len(options)
self._elements[name] = Listbox(height=height, width=self._width, scroll=scroll)
for option in options:
(key, value, selected) = option
self._elements[name].append( key, value )
if selected:
self._elements[name].setCurrent(value)
self.g.add( self._elements[name], 1, self._row, (0,0,0,1), anchorLeft=1 )
self._row += 1
def checks(self, label, name, options, height=None, scroll=1):
"""
Creates a set of checkboxes.
Options are given as [ (label, value, checked), ... ] where
checked equ. 0/1.
"""
self.g.add( Label( label ), 0, self._row, (0,0,1,1) )
if height is None:
height = len(options)
self._elements[name] = CheckboxTree(height=height, scroll=scroll, width=self._width)
for option in options:
(key, value, selected) = option
self._elements[name].append( key, value, selected )
self.g.add( self._elements[name], 1, self._row, (0,0,0,1), anchorLeft=1, growx=1 )
self._row += 1
def run(self):
"""
Runs until a button is pressed.
Returns [ button, { values } ].
"""
btn = self.g.runOnce()
cmd = None
results = {}
for label in self._buttons:
if self._buttons[label] == btn:
cmd = label
if btn == 'F12':
cmd = 'F12'
for name in self._elements:
if 'value' in dir( self._elements[name] ):
results[name] = self._elements[name].value()
if 'getSelection' in dir( self._elements[name] ):
results[name] = self._elements[name].getSelection()
if 'current' in dir( self._elements[name] ):
results[name] = self._elements[name].current()
return [ cmd, results ]