2
2
# Copyright (C) 2013 Marwan Alsabbagh
3
3
# license: BSD, see LICENSE for more details.
4
4
5
- __version__ = '0.2.dev'
6
-
5
+ from __future__ import print_function
7
6
import sys
8
7
import shlex
9
- from utile import save_args , flatten
8
+ import itertools
10
9
from subprocess import Popen , PIPE
11
10
from collections import namedtuple
12
11
12
+ __version__ = '0.2.dev'
13
+ PY3 = sys .version_info [0 ] == 3
14
+ string_types = str if PY3 else basestring
13
15
Response = namedtuple ('Response' , 'returncode value' )
14
16
15
17
18
+ def flatten (data ):
19
+ return list (itertools .chain .from_iterable (data ))
20
+
21
+
16
22
class Whiptail (object ):
17
- @save_args
18
- def __init__ (self , title = '' , backtitle = '' , height = 10 , width = 50 , auto_exit = True ):
19
- pass
23
+ def __init__ (self , title = '' , backtitle = '' , height = 10 , width = 50 ,
24
+ auto_exit = True ):
25
+ self .title = title
26
+ self .backtitle = backtitle
27
+ self .height = height
28
+ self .width = width
29
+ self .auto_exit = auto_exit
20
30
21
31
def run (self , control , msg , extra = (), exit_on = (1 , 255 )):
22
- cmd = ['whiptail' , '--title' , self .title , '--backtitle' , self .backtitle ,
23
- '--' + control , msg , str (self .height ), str (self .width )] + list (extra )
32
+ cmd = [
33
+ 'whiptail' , '--title' , self .title , '--backtitle' , self .backtitle ,
34
+ '--' + control , msg , str (self .height ), str (self .width )
35
+ ]
36
+ cmd += list (extra )
24
37
p = Popen (cmd , stderr = PIPE )
25
38
out , err = p .communicate ()
26
39
if self .auto_exit and p .returncode in exit_on :
27
- print 'User cancelled operation.'
40
+ print ( 'User cancelled operation.' )
28
41
sys .exit (p .returncode )
29
42
return Response (p .returncode , err )
30
43
@@ -47,15 +60,15 @@ def calc_height(self, msg):
47
60
return [str (self .height - height_offset )]
48
61
49
62
def menu (self , msg = '' , items = (), prefix = ' - ' ):
50
- if isinstance (items [0 ], basestring ):
63
+ if isinstance (items [0 ], string_types ):
51
64
items = [(i , '' ) for i in items ]
52
65
else :
53
66
items = [(k , prefix + v ) for k , v in items ]
54
67
extra = self .calc_height (msg ) + flatten (items )
55
68
return self .run ('menu' , msg , extra ).value
56
69
57
70
def showlist (self , control , msg , items , prefix ):
58
- if isinstance (items [0 ], basestring ):
71
+ if isinstance (items [0 ], string_types ):
59
72
items = [(i , '' , 'OFF' ) for i in items ]
60
73
else :
61
74
items = [(k , prefix + v , s ) for k , v , s in items ]
0 commit comments