Sat, 20 Jan 2018 20:34:32 +0000
Fixed retry scheduling
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 | ||
11
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
11 | traynames={False: 'B.', True: 'B!'} |
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
12 | |
10 | 13 | def make_title(status): |
11
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
14 | active=False |
10 | 15 | if status['type']=='scheduled': |
16 | # Operation scheduled | |
17 | when=status['when'] | |
18 | now=time.time() | |
19 | if when<now: | |
20 | whenstr='overdue' | |
21 | else: | |
22 | diff=datetime.timedelta(seconds=when-now) | |
23 | if diff.days>0: | |
24 | whenday=datetime.date.fromtimestamp(when) | |
25 | whenstr='on %s' % whenday.isoformat() | |
26 | else: | |
27 | twhen=time.localtime(when) | |
28 | if twhen.tm_sec>30: | |
29 | # Round up minute display to avoid user confusion | |
30 | twhen=time.localtime(when+30) | |
31 | whenstr='at %02d:%02d' % (twhen.tm_hour, twhen.tm_min) | |
32 | detail='' | |
33 | if 'detail' in status and status['detail']: | |
34 | detail=status['detail']+' ' | |
11
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
35 | title="%s (%s%s %s)" % (status['name'], detail, status['operation'], whenstr) |
10 | 36 | elif status['type']=='current': |
37 | # Operation running | |
11
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
38 | title="%s (running: %s)" % (status['name'], status['operation']) |
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
39 | active=True |
10 | 40 | else: # status['type']=='nothing': |
41 | # 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
|
42 | title=status['name'] |
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
43 | |
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
44 | return title, active |
10 | 45 | |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
46 | |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
47 | 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
|
48 | def __init__(self, name, backups): |
10 | 49 | self.lock=Lock() |
50 | self.backups=backups | |
51 | self.statuses=[None]*len(backups) | |
52 | ||
53 | with self.lock: | |
54 | # Initialise reporting callbacks and local status copy | |
55 | # (since rumps doesn't seem to be able to update menu items | |
56 | # without rebuilding the whole menu, and we don't want to lock | |
57 | # when calling Backup.status(), especially as we will be called | |
58 | # from Backup reporting its status, we keep a copy to allow | |
59 | # rebuilding the entire menu | |
60 | for index in range(len(backups)): | |
61 | b=backups[index] | |
62 | # Python closures suck dog's balls; hence the _index=index hack | |
63 | # See also http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/ | |
64 | cb=(lambda obj, status, _index=index: | |
65 | self.__status_callback(obj, _index, status)) | |
66 | b.set_status_update_callback(cb) | |
67 | self.statuses[index]=b.status() | |
68 | ||
11
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
69 | menu, active=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
|
70 | |
11
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
71 | super().__init__(traynames[active], menu=menu, quit_button=None) |
10 | 72 | |
73 | def __rebuild_menu(self): | |
74 | menu=[] | |
11
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
75 | active=False |
10 | 76 | for index in range(len(self.backups)): |
77 | b=self.backups[index] | |
11
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
78 | title, this_active=make_title(self.statuses[index]) |
10 | 79 | logging.info('TITLE: %s' % title) |
80 | # Python closures suck dog's balls... | |
81 | # first and the last program I write in Python until somebody | |
82 | # fixes this brain damage | |
83 | cbm=lambda sender, _b=b: self.__menu_select_backup(sender, _b) | |
84 | item=rumps.MenuItem(title, callback=cbm) | |
85 | menu.append(item) | |
11
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
86 | active=active or this_active |
10 | 87 | |
88 | menu_quit=rumps.MenuItem("Quit...", callback=self.my_quit) | |
89 | menu.append(menu_quit) | |
90 | ||
11
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
91 | return menu, active |
10 | 92 | |
93 | ||
94 | 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
|
95 | 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
|
96 | |
10 | 97 | def __menu_select_backup(self, sender, b): |
98 | #sender.state=not sender.state | |
99 | logging.debug("Manually backup '%s'", b.name) | |
100 | 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
|
101 | |
10 | 102 | def __status_callback(self, obj, index, status): |
103 | logging.debug('Status callbackup %s' % str(status)) | |
104 | with self.lock: | |
105 | self.statuses[index]=status | |
106 | logging.debug('Rebuilding menu') | |
107 | self.menu.clear() | |
11
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
108 | menu, active=self.__rebuild_menu() |
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
109 | print(active) |
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
110 | self.menu.update(menu) |
0bff53095f28
New tray title: B. or B! depending on activity
Tuomo Valkonen <tuomov@iki.fi>
parents:
10
diff
changeset
|
111 | self.title=traynames[active] |
10 | 112 | |
113 | ||
114 |