ui.py

changeset 45
aa2a95dc6093
parent 42
00accd11978b
child 46
ecb41072a1b7
--- a/ui.py	Sun Jan 21 22:46:05 2018 +0000
+++ b/ui.py	Sun Jan 21 23:52:35 2018 +0000
@@ -8,32 +8,34 @@
 import logging
 import borgend
 import utils
+import backup
 from threading import Lock, Timer
 from config import settings
 import objc
 
 logger=borgend.logger.getChild(__name__)
 
-INACTIVE=0
-SCHEDULED_OK=1
-ACTIVE=2
-OFFLINE=3
-ERRORS=4
+traynames={
+    backup.INACTIVE: 'B.',
+    backup.SCHEDULED: 'B.',
+    backup.ACTIVE: 'B!',
+    backup.BUSY: 'B⦙',
+    backup.OFFLINE: 'B⦙',
+    backup.ERRORS: 'B?'
+}
 
-traynames={
-    INACTIVE: 'B.',
-    SCHEDULED_OK: 'B.',
-    ACTIVE: 'B!',
-    OFFLINE: 'B⦙',
-    ERRORS: 'B?'
+statestring={
+    backup.INACTIVE: 'inactive',
+    backup.SCHEDULED: 'scheduled',
+    backup.ACTIVE: 'active',
+    backup.BUSY: 'busy',
+    backup.OFFLINE: 'offline',
+    backup.ERRORS: 'errors'
 }
 
 # Refresh the menu at most once a second to reduce flicker
 refresh_interval=1.0
 
-def combine_state(state1, state2):
-    return max(state1, state2)
-
 # Workaround to rumps brokenness;
 # see https://github.com/jaredks/rumps/issues/59
 def notification_workaround(title, subtitle, message):
@@ -68,8 +70,7 @@
       return '{0:.2f}TB'.format(B/TB)
 
 def make_title(status):
-    state=INACTIVE
-
+    state=status['state']
     if status['type']=='scheduled':
         # Operation scheduled
         when=status['when']
@@ -88,13 +89,11 @@
                     twhen=time.localtime(when+30)
                 whenstr='at %02d:%02d' % (twhen.tm_hour, twhen.tm_min)
 
-            if status['detail']=='normal':
-                state=SCHEDULED_OK
-                detail=''
-            else:
-                if status['detail']=='retry':
-                    state=ERRORS
-                detail=status['detail']+' '
+            detail=''
+            if state>=backup.BUSY and state in statestring:
+                detail=statestring[state] + '; '
+            if status['detail']!='normal':
+                detail=detail+status['detail']+' '
         title="%s (%s%s %s)" % (status['name'], detail, status['operation'], whenstr)
     elif status['type']=='current':
         # Operation running
@@ -105,15 +104,12 @@
             progress=' %s→%s' % (humanbytes(status['original_size']),
                                  humanbytes(status['deduplicated_size']))
         title="%s (running: %s%s)" % (status['name'], status['operation'], progress)
-        state=ACTIVE
     else: # status['type']=='nothing':
         # Should be unscheduled, nothing running
-        title=status['name']
-        if status['errors']:
-            title=title + " (errors)"
-
-    if status['errors']:
-        state=ERRORS
+        detail=''
+        if state>=backup.BUSY and state in statestring:
+            detail=' (' + statestring[state] + ')'
+        title=status['name'] + detail
 
     return title, state
 
@@ -147,7 +143,7 @@
 
     def __rebuild_menu(self):
         menu=[]
-        state=INACTIVE
+        state=backup.INACTIVE
         for index in range(len(self.backups)):
             b=self.backups[index]
             title, this_state=make_title(self.statuses[index])
@@ -156,12 +152,12 @@
             # fixes this brain damage
             cbm=lambda sender, _b=b: self.__menu_select_backup(sender, _b)
             item=rumps.MenuItem(title, callback=cbm)
-            if this_state==SCHEDULED_OK:
+            if this_state==backup.SCHEDULED:
                 item.state=1
-            elif this_state>=OFFLINE:
+            elif this_state>=backup.BUSY:
                 item.state=-1
             menu.append(item)
-            state=combine_state(state, this_state)
+            state=backup.combine_state(state, this_state)
 
         menu_log=rumps.MenuItem("Show log", callback=lambda _: showlog())
         menu.append(menu_log)

mercurial