Fri, 26 Jan 2018 09:27:07 +0000
Scheduling list fix and simplifications
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
1 | # |
40
cfbeeec8cb82
Improved exception reporting etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
2 | # Borgend MacOS UI |
1
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 | |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
9 | import borgend |
45
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
10 | import backup |
27
a347387868be
UI refresh delay to reduce flicker
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
11 | from threading import Lock, Timer |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
12 | from config import settings |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
13 | import objc |
10 | 14 | |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
15 | logger=borgend.logger.getChild(__name__) |
31 | 16 | |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
17 | traynames_ok={ |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
18 | backup.State.INACTIVE: 'B.', |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
19 | backup.State.SCHEDULED: 'B.', |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
20 | backup.State.QUEUED: 'B:', |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
21 | backup.State.ACTIVE: 'B!', |
45
aa2a95dc6093
Further improvements to state reporting; indicate busyness if repository lock cannot be acquired
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
22 | } |
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
|
23 | |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
24 | traynames_errors={ |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
25 | # The first one should never be used |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
26 | backup.Errors.OK: traynames_ok[backup.State.INACTIVE], |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
27 | backup.Errors.BUSY: 'B⦙', |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
28 | backup.Errors.OFFLINE: 'B⦙', |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
29 | backup.Errors.ERRORS: 'B?' |
16
d0ffae5550ef
Added offline symbol B⦙ (no offline detection yet)
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
30 | } |
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
|
31 | |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
32 | def trayname(ste): |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
33 | state=ste[0] |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
34 | errors=ste[1] |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
35 | if not errors.ok(): |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
36 | return traynames_errors[errors] |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
37 | else: |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
38 | return traynames_ok[state] |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
39 | |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
40 | def combine_state(a, b): |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
41 | return (max(a[0], b[0]), max(a[1], b[1])) |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
42 | |
27
a347387868be
UI refresh delay to reduce flicker
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
43 | # Refresh the menu at most once a second to reduce flicker |
a347387868be
UI refresh delay to reduce flicker
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
44 | refresh_interval=1.0 |
a347387868be
UI refresh delay to reduce flicker
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
45 | |
32 | 46 | # Workaround to rumps brokenness; |
47 | # see https://github.com/jaredks/rumps/issues/59 | |
48 | def notification_workaround(title, subtitle, message): | |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
49 | try: |
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
50 | NSDictionary = objc.lookUpClass("NSDictionary") |
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
51 | d=NSDictionary() |
32 | 52 | |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
53 | rumps.notification(title, subtitle, message, data=d) |
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
54 | except Exception as err: |
50 | 55 | logger.exception("Failed to display notification") |
32 | 56 | |
19
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
57 | # Based on code snatched from |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
58 | # https://stackoverflow.com/questions/12523586/python-format-size-application-converting-b-to-kb-mb-gb-tb/37423778 |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
59 | def humanbytes(B): |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
60 | 'Return the given bytes as a human friendly KB, MB, GB, or TB string' |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
61 | B = float(B) |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
62 | KB = float(1024) |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
63 | MB = float(KB ** 2) # 1,048,576 |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
64 | GB = float(KB ** 3) # 1,073,741,824 |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
65 | TB = float(KB ** 4) # 1,099,511,627,776 |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
66 | |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
67 | if B < KB: |
20
fdfbe5d7b677
Keychain support and random fixes
Tuomo Valkonen <tuomov@iki.fi>
parents:
19
diff
changeset
|
68 | return '{0}B'.format(B) |
19
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
69 | elif KB <= B < MB: |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
70 | return '{0:.2f}KB'.format(B/KB) |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
71 | elif MB <= B < GB: |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
72 | return '{0:.2f}MB'.format(B/MB) |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
73 | elif GB <= B < TB: |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
74 | return '{0:.2f}GB'.format(B/GB) |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
75 | elif TB <= B: |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
76 | return '{0:.2f}TB'.format(B/TB) |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
77 | |
10 | 78 | def make_title(status): |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
79 | def add_info(info, new): |
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
80 | if info: |
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
81 | return "%s; %s" % (info, new) |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
82 | else: |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
83 | return new |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
84 | |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
85 | info=None |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
86 | |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
87 | if not status.errors.ok(): |
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
88 | info=add_info(info, str(status.errors)) |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
89 | |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
90 | if status.state==backup.State.SCHEDULED: |
10 | 91 | # Operation scheduled |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
92 | when=status.when() |
10 | 93 | now=time.time() |
94 | if when<now: | |
95 | whenstr='overdue' | |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
96 | info='' |
10 | 97 | else: |
98 | diff=datetime.timedelta(seconds=when-now) | |
99 | if diff.days>0: | |
100 | whenday=datetime.date.fromtimestamp(when) | |
101 | whenstr='on %s' % whenday.isoformat() | |
102 | else: | |
103 | twhen=time.localtime(when) | |
104 | if twhen.tm_sec>30: | |
105 | # Round up minute display to avoid user confusion | |
106 | twhen=time.localtime(when+30) | |
107 | whenstr='at %02d:%02d' % (twhen.tm_hour, twhen.tm_min) | |
38 | 108 | |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
109 | this_info='' |
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
110 | if 'reason' in status.detail: |
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
111 | this_info=status.detail['reason'] + ' ' |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
112 | |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
113 | when_how_sched= "%s%s %s" % (this_info, status.operation, whenstr) |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
114 | |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
115 | info=add_info(info, when_how_sched) |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
116 | |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
117 | elif status.state==backup.State.QUEUED: |
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
118 | info=add_info(info, "queued") |
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
119 | elif status.state==backup.State.ACTIVE: |
10 | 120 | # 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
|
121 | progress='' |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
122 | d=status.detail |
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
123 | if 'progress_current' in d and 'progress_total' in d: |
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
124 | progress=' %d%%' % (d['progress_current']/d['progress_total']) |
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
125 | elif 'original_size' in d and 'deduplicated_size' in d: |
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
126 | progress=' %s→%s' % (humanbytes(d['original_size']), |
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
127 | humanbytes(d['deduplicated_size'])) |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
128 | |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
129 | howrunning = "running %s%s" % (status.operation, progress) |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
130 | |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
131 | info=add_info(info, howrunning) |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
132 | else: |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
133 | pass |
15 | 134 | |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
135 | if info: |
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
136 | title=status.name + ' (' + info + ')' |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
137 | else: |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
138 | title=status.name |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
139 | |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
140 | return title, (status.state, status.errors) |
10 | 141 | |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
142 | class BorgendTray(rumps.App): |
33
91421eeb4426
Use rumps.application_support instead of xdg paths. Also separated branding into config.py
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
143 | def __init__(self, backups): |
10 | 144 | self.lock=Lock() |
145 | self.backups=backups | |
146 | self.statuses=[None]*len(backups) | |
147 | ||
148 | with self.lock: | |
149 | # Initialise reporting callbacks and local status copy | |
150 | # (since rumps doesn't seem to be able to update menu items | |
151 | # without rebuilding the whole menu, and we don't want to lock | |
152 | # when calling Backup.status(), especially as we will be called | |
153 | # from Backup reporting its status, we keep a copy to allow | |
154 | # rebuilding the entire menu | |
155 | for index in range(len(backups)): | |
156 | b=backups[index] | |
157 | # Python closures suck dog's balls; hence the _index=index hack | |
158 | # See also http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/ | |
55
407af23d16bb
Improved backup main thread loop, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
51
diff
changeset
|
159 | cb=(lambda status, errors=None, _index=index: |
407af23d16bb
Improved backup main thread loop, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
51
diff
changeset
|
160 | self.__status_callback(_index, status, errorlog=errors)) |
10 | 161 | b.set_status_update_callback(cb) |
162 | self.statuses[index]=b.status() | |
163 | ||
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
|
164 | 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
|
165 | |
27
a347387868be
UI refresh delay to reduce flicker
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
166 | self.refresh_timer=None |
a347387868be
UI refresh delay to reduce flicker
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
167 | |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
168 | super().__init__(trayname(state), menu=menu, quit_button=None) |
10 | 169 | |
170 | def __rebuild_menu(self): | |
171 | menu=[] | |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
172 | state=(backup.State.INACTIVE, backup.Errors.OK) |
10 | 173 | for index in range(len(self.backups)): |
174 | 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
|
175 | title, this_state=make_title(self.statuses[index]) |
10 | 176 | # Python closures suck dog's balls... |
177 | # first and the last program I write in Python until somebody | |
178 | # fixes this brain damage | |
179 | cbm=lambda sender, _b=b: self.__menu_select_backup(sender, _b) | |
180 | item=rumps.MenuItem(title, callback=cbm) | |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
181 | if not this_state[1].ok(): |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
182 | item.state=-1 |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
183 | elif this_state[0]==backup.State.SCHEDULED or this_state[0]==backup.State.QUEUED: |
38 | 184 | item.state=1 |
10 | 185 | menu.append(item) |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
186 | state=combine_state(state, this_state) |
10 | 187 | |
42
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
188 | menu_log=rumps.MenuItem("Show log", callback=lambda _: showlog()) |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
189 | menu.append(menu_log) |
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
190 | |
30
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
28
diff
changeset
|
191 | if not settings['no_quit_menu_entry']: |
46
ecb41072a1b7
Unsuccesfull attempts at clean quit on exceptions that fall through
Tuomo Valkonen <tuomov@iki.fi>
parents:
45
diff
changeset
|
192 | menu_quit=rumps.MenuItem("Quit...", callback=lambda _: self.quit()) |
30
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
28
diff
changeset
|
193 | menu.append(menu_quit) |
10 | 194 | |
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
|
195 | return menu, state |
10 | 196 | |
27
a347387868be
UI refresh delay to reduce flicker
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
197 | def refresh_ui(self): |
a347387868be
UI refresh delay to reduce flicker
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
198 | with self.lock: |
a347387868be
UI refresh delay to reduce flicker
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
199 | self.refresh_timer=None |
31 | 200 | logger.debug('Rebuilding menu') |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
201 | menu, state=self.__rebuild_menu() |
27
a347387868be
UI refresh delay to reduce flicker
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
202 | self.menu.clear() |
a347387868be
UI refresh delay to reduce flicker
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
203 | self.menu.update(menu) |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
204 | self.title=trayname(state) |
27
a347387868be
UI refresh delay to reduce flicker
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
205 | |
55
407af23d16bb
Improved backup main thread loop, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
51
diff
changeset
|
206 | def __status_callback(self, index, status, errorlog=None): |
50 | 207 | logger.debug('Status callback: %s' % str(status)) |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
208 | |
10 | 209 | with self.lock: |
210 | self.statuses[index]=status | |
27
a347387868be
UI refresh delay to reduce flicker
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
211 | if self.refresh_timer==None: |
a347387868be
UI refresh delay to reduce flicker
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
212 | self.refresh_timer=Timer(refresh_interval, self.refresh_ui) |
a347387868be
UI refresh delay to reduce flicker
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
213 | self.refresh_timer.start() |
10 | 214 | |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
215 | if errorlog: |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
216 | if 'msgid' not in errorlog or not isinstance(errorlog['msgid'], str): |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
217 | msgid='UnknownError' |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
218 | else: |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
219 | msgid=errorlog['msgid'] |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
220 | |
32 | 221 | logger.debug("Opening notification for error %s '%s'", |
222 | msgid, errorlog['message']) | |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
223 | |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
224 | notification_workaround(borgend.appname_stylised, |
33
91421eeb4426
Use rumps.application_support instead of xdg paths. Also separated branding into config.py
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
225 | msgid, errorlog['message']) |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
226 | |
46
ecb41072a1b7
Unsuccesfull attempts at clean quit on exceptions that fall through
Tuomo Valkonen <tuomov@iki.fi>
parents:
45
diff
changeset
|
227 | def quit(self): |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
228 | logging.shutdown() |
28 | 229 | rumps.quit_application() |
230 | ||
231 | def __menu_select_backup(self, sender, b): | |
232 | #sender.state=not sender.state | |
31 | 233 | logger.debug("Manually backup '%s'", b.name) |
32 | 234 | try: |
49 | 235 | b.create() |
32 | 236 | except Exception as err: |
50 | 237 | logger.exception("Failure to initialise backup") |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
238 | notification_workaround(borgend.appname_stylised, |
33
91421eeb4426
Use rumps.application_support instead of xdg paths. Also separated branding into config.py
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
239 | err.__class__.__name__, str(err)) |
28 | 240 | |
42
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
241 | # |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
242 | # Log window |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
243 | # |
10 | 244 | |
42
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
245 | logwindow=[None] |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
246 | logwindow_lock=Lock() |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
247 | |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
248 | def showlog(): |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
249 | try: |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
250 | w=None |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
251 | with logwindow_lock: |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
252 | if not logwindow[0]: |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
253 | lines=borgend.fifolog.formatAll() |
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
254 | msg="\n".join(lines[0:]) |
42
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
255 | w=rumps.Window(title=borgend.appname_stylised+' log', |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
256 | default_text=msg, |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
257 | ok='Close', |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
258 | dimensions=(640,320)) |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
259 | logwindow[0]=w |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
260 | if w: |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
261 | try: |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
262 | w.run() |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
263 | finally: |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
264 | with logwindow_lock: |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
265 | logwindow[0]=None |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
266 | except Exception as err: |
50 | 267 | logger.exception("Failed to display log") |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
268 | |
42
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
269 | # |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
270 | # Notification click response => show log window |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
271 | # |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
272 | |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
273 | @rumps.notifications |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
274 | def notification_center(_): |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
275 | showlog() |
50 | 276 |