/
zenity.py
179 lines (150 loc) · 6.21 KB
/
zenity.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import datetime
import os
import subprocess
from vsgui import utils
import ucltip
class Zenity(ucltip.CmdDispatcher):
"""
To communicate with Zenity in Python
Zenity is a program that will display GTK+ dialogs, and return
(either in the return code, or on standard output) the users input.
This allows you to present information, and ask for information from
the user, from all manner of shell scripts.
Here is the concept, more method could be found in `zenity --help`
zenity.info() maps to `zenity --info`
zenity.info(text="hello") maps to `zenity --info --text='hello'
"""
def __init__(self):
super(Zenity, self).__init__('zenity')
self.subcmd_prefix = '--'
self.conf.opt_style = 'gnu'
setattr(self, 'entry', lambda *args, **kwargs: self.__call('entry', *args, **kwargs))
setattr(self, 'error', lambda *args, **kwargs: self.__call('error', *args, **kwargs))
setattr(self, 'info', lambda *args, **kwargs: self.__call('info', *args, **kwargs))
setattr(self, 'notification', lambda *args, **kwargs: self.__call('notification', *args, **kwargs))
setattr(self, 'warning', lambda *args, **kwargs: self.__call('warning', *args, **kwargs))
def calendar(self, *args, **kwds):
"""display calendar dialog
@param text=TEXT Set the dialog text
@param selected_day=DAY Set the calendar day
@param date_format=PATTERN Set the format for the returned date
@return datetime.date
"""
(_kwds, opts) = utils.parse_kwds(kwds, ['selected_day'])
selected = opts.get('selected_day')
if selected:
_kwds['day'] = selected.day
_kwds['month'] = selected.month
_kwds['year'] = selected.year
retval = self.__call('calendar', *args, **_kwds)
if self.conf.dry_run:
return retval
if retval:
(month, day, year) = map(int, retval.split('/'))
return datetime.date(year, month, day)
def question(self, *args, **kdws):
"""display question dialog
@param text title text
@param y text of OK label
@param n text of Cancel label
@return True or False
"""
kdws['with_extend_output'] = True
retval = self.__call('question', *args, **kdws)
return not retval[0]
def text_info(self, *args, **kwds):
"""dispaly Display text information dialog
@param save save the content in current file
@param backup extenion of backup filename
@return content content user inputed
"""
(_kwds, opts) = utils.parse_kwds(kwds, ['save', 'backup'])
content = self.__call('text-info', *args, **_kwds)
if opts.get('save') and _kwds.get('filename'):
utils.savefile(_kwds.get('filename'), content, backup=opts.get('backup'))
return content
def progress(self, *args, **kwds):
"""display progress dialog
@param text=TEXT Set the dialog text
@param percentage=PERCENTAGE Set initial percentage
@param pulsate Pulsate progress bar
@param auto_close Dismiss the dialog when 100% has been reached
@param auto_kill Kill parent process if cancel button is pressed
@return update function callable function to update dialog
@example
update = zenity.progress(text="Download Script")
update("10", "starting")
time.sleep(1)
update("20", "closing")
time.sleep(1)
"""
kwds['stdin'] = subprocess.PIPE
p = self.__call('progress', as_process=True, *args, **kwds)
def update(percent, message=''):
if type(percent) == float:
percent = int(percent * 100)
try:
p.stdin.write(str(percent) + '\n')
if message:
p.stdin.write('# %s\n' % message)
except IOError:
exit()
return p.returncode
return update
def file_selection(self, *args, **kwds):
"""display file or directory selection dialog
@return list of selection path of files or directories
"""
retstr = self.__call('file-selection', *args, **kwds)
return [] if not retstr else retstr.strip().split('|')
def list(self, columns, data=[], **kwds):
"""display selection dialog
@param columns
@param data data of cells , its size must be equal to the number of columns
@return list
"""
args = []
for col in columns:
args.append("--column=%s" % col)
args += data
retstr = self.__call('list', *args, **kwds)
if kwds.get('checklist'):
return [] if not retstr else retstr.split('|')
return retstr
def scale(self, *args, **kwargs):
"""Display scale dialog
@return int scale value
"""
ret = self.__call('scale', *args, **kwargs)
if ret:
return int(ret)
else:
raise Exception
def __call(self, name, *args, **kwargs):
if name[:1] == '_':
raise AttributeError(name)
try:
method = self._subcmds.setdefault(name, ucltip.SubCmd(name, self))
except KeyError:
raise AttributeError
else:
try:
ret = method(*args, **kwargs)
return ret.replace('\n','') if type(ret) is str else ret
except ucltip.CommandExecutedError, e:
if e.errmsg:
print e.errmsg
return False
if __name__ == '__main__':
obj = Zenity()
import datetime
import time
#print obj.error(text='hi')
#print obj.question()
#print obj.text_info()
print obj.calendar(selected_day=datetime.date(2015,3,25))
#up= obj.progress()
#up(30,'hi')
#time.sleep(1)
#up(40, 'no')
print obj.list(('a','b'), [1,2,3,4], checklist=True)