Sat, 20 Jan 2018 23:07:53 +0000
Oops..
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
1 | # |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
2 | # MacOS UI |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
3 | # |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
4 | |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
5 | import rumps |
10 | 6 | import time |
7 | import datetime | |
8 | import logging | |
9 | from threading import Lock | |
10 | ||
14
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
11
diff
changeset
|
11 | INACTIVE=0 |
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
11
diff
changeset
|
12 | ACTIVE=1 |
16
d0ffae5550ef
Added offline symbol B⦙ (no offline detection yet)
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
13 | OFFLINE=2 |
d0ffae5550ef
Added offline symbol B⦙ (no offline detection yet)
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
14 | ERRORS=3 |
14
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
11
diff
changeset
|
15 | |
16
d0ffae5550ef
Added offline symbol B⦙ (no offline detection yet)
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
16 | traynames={ |
d0ffae5550ef
Added offline symbol B⦙ (no offline detection yet)
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
17 | INACTIVE: 'B.', |
d0ffae5550ef
Added offline symbol B⦙ (no offline detection yet)
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
18 | ACTIVE: 'B!', |
d0ffae5550ef
Added offline symbol B⦙ (no offline detection yet)
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
19 | OFFLINE: 'B⦙', |
d0ffae5550ef
Added offline symbol B⦙ (no offline detection yet)
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
20 | ERRORS: 'B?' |
d0ffae5550ef
Added offline symbol B⦙ (no offline detection yet)
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
21 | } |
14
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
11
diff
changeset
|
22 | |
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
11
diff
changeset
|
23 | def combine_state(state1, state2): |
16
d0ffae5550ef
Added offline symbol B⦙ (no offline detection yet)
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
24 | return max(state1, state2) |
11
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
25 | |
10 | 26 | def make_title(status): |
14
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
11
diff
changeset
|
27 | state=INACTIVE |
15 | 28 | |
10 | 29 | if status['type']=='scheduled': |
30 | # Operation scheduled | |
31 | when=status['when'] | |
32 | now=time.time() | |
33 | if when<now: | |
34 | whenstr='overdue' | |
35 | else: | |
36 | diff=datetime.timedelta(seconds=when-now) | |
37 | if diff.days>0: | |
38 | whenday=datetime.date.fromtimestamp(when) | |
39 | whenstr='on %s' % whenday.isoformat() | |
40 | else: | |
41 | twhen=time.localtime(when) | |
42 | if twhen.tm_sec>30: | |
43 | # Round up minute display to avoid user confusion | |
44 | twhen=time.localtime(when+30) | |
45 | whenstr='at %02d:%02d' % (twhen.tm_hour, twhen.tm_min) | |
46 | detail='' | |
47 | if 'detail' in status and status['detail']: | |
14
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
11
diff
changeset
|
48 | if status['detail']=='retry': |
15 | 49 | state=ERRORS |
10 | 50 | detail=status['detail']+' ' |
11
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
51 | title="%s (%s%s %s)" % (status['name'], detail, status['operation'], whenstr) |
10 | 52 | elif status['type']=='current': |
53 | # Operation running | |
14
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
11
diff
changeset
|
54 | progress='' |
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
11
diff
changeset
|
55 | if 'progress_current' in status and 'progress_total' in status: |
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
11
diff
changeset
|
56 | progress='%d%%' % (status.progress_current/status.progress_total) |
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
11
diff
changeset
|
57 | title="%s (running: %s%s)" % (status['name'], status['operation'], progress) |
18 | 58 | state=ACTIVE |
10 | 59 | else: # status['type']=='nothing': |
60 | # Should be unscheduled, nothing running | |
11
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
61 | title=status['name'] |
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
62 | |
15 | 63 | if status['errors']: |
64 | state=ERRORS | |
65 | ||
14
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
11
diff
changeset
|
66 | return title, state |
10 | 67 | |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
68 | |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
69 | class BorgendTray(rumps.App): |
9
aa121291eb0e
Rumps/Mac UI stuff is fucked and disables ^C etc.; threading doesn't help
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
70 | def __init__(self, name, backups): |
10 | 71 | self.lock=Lock() |
72 | self.backups=backups | |
73 | self.statuses=[None]*len(backups) | |
74 | ||
75 | with self.lock: | |
76 | # Initialise reporting callbacks and local status copy | |
77 | # (since rumps doesn't seem to be able to update menu items | |
78 | # without rebuilding the whole menu, and we don't want to lock | |
79 | # when calling Backup.status(), especially as we will be called | |
80 | # from Backup reporting its status, we keep a copy to allow | |
81 | # rebuilding the entire menu | |
82 | for index in range(len(backups)): | |
83 | b=backups[index] | |
84 | # Python closures suck dog's balls; hence the _index=index hack | |
85 | # See also http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/ | |
86 | cb=(lambda obj, status, _index=index: | |
87 | self.__status_callback(obj, _index, status)) | |
88 | b.set_status_update_callback(cb) | |
89 | self.statuses[index]=b.status() | |
90 | ||
14
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
11
diff
changeset
|
91 | menu, state=self.__rebuild_menu() |
9
aa121291eb0e
Rumps/Mac UI stuff is fucked and disables ^C etc.; threading doesn't help
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
92 | |
14
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
11
diff
changeset
|
93 | super().__init__(traynames[state], menu=menu, quit_button=None) |
10 | 94 | |
95 | def __rebuild_menu(self): | |
96 | menu=[] | |
14
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
11
diff
changeset
|
97 | state=INACTIVE |
10 | 98 | for index in range(len(self.backups)): |
99 | b=self.backups[index] | |
14
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
11
diff
changeset
|
100 | title, this_state=make_title(self.statuses[index]) |
10 | 101 | logging.info('TITLE: %s' % title) |
102 | # Python closures suck dog's balls... | |
103 | # first and the last program I write in Python until somebody | |
104 | # fixes this brain damage | |
105 | cbm=lambda sender, _b=b: self.__menu_select_backup(sender, _b) | |
106 | item=rumps.MenuItem(title, callback=cbm) | |
107 | menu.append(item) | |
14
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
11
diff
changeset
|
108 | state=combine_state(state, this_state) |
10 | 109 | |
110 | menu_quit=rumps.MenuItem("Quit...", callback=self.my_quit) | |
111 | menu.append(menu_quit) | |
112 | ||
14
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
11
diff
changeset
|
113 | return menu, state |
10 | 114 | |
115 | ||
116 | def my_quit(self, _): | |
9
aa121291eb0e
Rumps/Mac UI stuff is fucked and disables ^C etc.; threading doesn't help
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
117 | rumps.quit_application() |
aa121291eb0e
Rumps/Mac UI stuff is fucked and disables ^C etc.; threading doesn't help
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
118 | |
10 | 119 | def __menu_select_backup(self, sender, b): |
120 | #sender.state=not sender.state | |
121 | logging.debug("Manually backup '%s'", b.name) | |
122 | b.create(None) | |
9
aa121291eb0e
Rumps/Mac UI stuff is fucked and disables ^C etc.; threading doesn't help
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
123 | |
10 | 124 | def __status_callback(self, obj, index, status): |
125 | logging.debug('Status callbackup %s' % str(status)) | |
126 | with self.lock: | |
127 | self.statuses[index]=status | |
128 | logging.debug('Rebuilding menu') | |
129 | self.menu.clear() | |
11
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
130 | menu, active=self.__rebuild_menu() |
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
131 | self.menu.update(menu) |
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
132 | self.title=traynames[active] |
10 | 133 | |
134 | ||
135 |