ui.py

changeset 11
0bff53095f28
parent 10
76dbfb06eba0
child 14
5a988a2c2624
equal deleted inserted replaced
10:76dbfb06eba0 11:0bff53095f28
6 import time 6 import time
7 import datetime 7 import datetime
8 import logging 8 import logging
9 from threading import Lock 9 from threading import Lock
10 10
11 traynames={False: 'B.', True: 'B!'}
12
11 def make_title(status): 13 def make_title(status):
14 active=False
12 if status['type']=='scheduled': 15 if status['type']=='scheduled':
13 # Operation scheduled 16 # Operation scheduled
14 when=status['when'] 17 when=status['when']
15 now=time.time() 18 now=time.time()
16 if when<now: 19 if when<now:
27 twhen=time.localtime(when+30) 30 twhen=time.localtime(when+30)
28 whenstr='at %02d:%02d' % (twhen.tm_hour, twhen.tm_min) 31 whenstr='at %02d:%02d' % (twhen.tm_hour, twhen.tm_min)
29 detail='' 32 detail=''
30 if 'detail' in status and status['detail']: 33 if 'detail' in status and status['detail']:
31 detail=status['detail']+' ' 34 detail=status['detail']+' '
32 return "%s (%s%s %s)" % (status['name'], detail, status['operation'], whenstr) 35 title="%s (%s%s %s)" % (status['name'], detail, status['operation'], whenstr)
33 elif status['type']=='current': 36 elif status['type']=='current':
34 # Operation running 37 # Operation running
35 return "%s (running: %s)" % (status['name'], status['operation']) 38 title="%s (running: %s)" % (status['name'], status['operation'])
39 active=True
36 else: # status['type']=='nothing': 40 else: # status['type']=='nothing':
37 # Should be unscheduled, nothing running 41 # Should be unscheduled, nothing running
38 return status['name'] 42 title=status['name']
43
44 return title, active
39 45
40 46
41 class BorgendTray(rumps.App): 47 class BorgendTray(rumps.App):
42 def __init__(self, name, backups): 48 def __init__(self, name, backups):
43 self.lock=Lock() 49 self.lock=Lock()
58 cb=(lambda obj, status, _index=index: 64 cb=(lambda obj, status, _index=index:
59 self.__status_callback(obj, _index, status)) 65 self.__status_callback(obj, _index, status))
60 b.set_status_update_callback(cb) 66 b.set_status_update_callback(cb)
61 self.statuses[index]=b.status() 67 self.statuses[index]=b.status()
62 68
63 menu=self.__rebuild_menu() 69 menu, active=self.__rebuild_menu()
64 70
65 super().__init__(name, menu=menu, quit_button=None) 71 super().__init__(traynames[active], menu=menu, quit_button=None)
66 72
67 def __rebuild_menu(self): 73 def __rebuild_menu(self):
68 menu=[] 74 menu=[]
75 active=False
69 for index in range(len(self.backups)): 76 for index in range(len(self.backups)):
70 b=self.backups[index] 77 b=self.backups[index]
71 title=make_title(self.statuses[index]) 78 title, this_active=make_title(self.statuses[index])
72 logging.info('TITLE: %s' % title) 79 logging.info('TITLE: %s' % title)
73 # Python closures suck dog's balls... 80 # Python closures suck dog's balls...
74 # first and the last program I write in Python until somebody 81 # first and the last program I write in Python until somebody
75 # fixes this brain damage 82 # fixes this brain damage
76 cbm=lambda sender, _b=b: self.__menu_select_backup(sender, _b) 83 cbm=lambda sender, _b=b: self.__menu_select_backup(sender, _b)
77 item=rumps.MenuItem(title, callback=cbm) 84 item=rumps.MenuItem(title, callback=cbm)
78 menu.append(item) 85 menu.append(item)
86 active=active or this_active
79 87
80 menu_quit=rumps.MenuItem("Quit...", callback=self.my_quit) 88 menu_quit=rumps.MenuItem("Quit...", callback=self.my_quit)
81 menu.append(menu_quit) 89 menu.append(menu_quit)
82 90
83 return menu 91 return menu, active
84 92
85 93
86 def my_quit(self, _): 94 def my_quit(self, _):
87 rumps.quit_application() 95 rumps.quit_application()
88 96
95 logging.debug('Status callbackup %s' % str(status)) 103 logging.debug('Status callbackup %s' % str(status))
96 with self.lock: 104 with self.lock:
97 self.statuses[index]=status 105 self.statuses[index]=status
98 logging.debug('Rebuilding menu') 106 logging.debug('Rebuilding menu')
99 self.menu.clear() 107 self.menu.clear()
100 self.menu.update(self.__rebuild_menu()) 108 menu, active=self.__rebuild_menu()
109 print(active)
110 self.menu.update(menu)
111 self.title=traynames[active]
101 112
102 113
103 114

mercurial