Sun, 05 Dec 2021 00:42:01 +0200
Need to time menu updates using Apple's frameworks to avoid segfaults.
However rumps.Timer doesn't work with menu updates while the menu is open, so
implement EventTrackingTimer class that uses NSEventTrackingRunLoopMode in
mainRunLoop.
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
1 | # |
89
51cc2e25af38
Added author information headers and content information to source files
Tuomo Valkonen <tuomov@iki.fi>
parents:
87
diff
changeset
|
2 | # Borgend by Tuomo Valkonen, 2018 |
51cc2e25af38
Added author information headers and content information to source files
Tuomo Valkonen <tuomov@iki.fi>
parents:
87
diff
changeset
|
3 | # |
51cc2e25af38
Added author information headers and content information to source files
Tuomo Valkonen <tuomov@iki.fi>
parents:
87
diff
changeset
|
4 | # Borgend MacOS tray icon UI |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
5 | # |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
6 | |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
7 | import rumps |
10 | 8 | import time |
9 | import datetime | |
86
2fe66644c50d
Can use logging.getLogger directly now after proper packageisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
85
diff
changeset
|
10 | import logging |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
11 | import objc |
110
a79409c72565
Oops, removed stray reset of menu entry info
Tuomo Valkonen <tuomov@iki.fi>
parents:
106
diff
changeset
|
12 | import math |
133
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
13 | from threading import Lock |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
14 | from Foundation import (NSDate, NSTimer, NSRunLoop, NSDefaultRunLoopMode) |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
15 | from AppKit import (NSEventTrackingRunLoopMode) |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
16 | |
80
a409242121d5
Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
79
diff
changeset
|
17 | from . import backup |
a409242121d5
Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
79
diff
changeset
|
18 | from . import dreamtime |
a409242121d5
Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
79
diff
changeset
|
19 | from . import branding |
99
281bab8361c8
Oops, log display wasn't working due to missing import
Tuomo Valkonen <tuomov@iki.fi>
parents:
98
diff
changeset
|
20 | from . import loggers |
106
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
21 | from .exprotect import protect_noreturn |
80
a409242121d5
Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
79
diff
changeset
|
22 | from .config import settings |
10 | 23 | |
86
2fe66644c50d
Can use logging.getLogger directly now after proper packageisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
85
diff
changeset
|
24 | logger=logging.getLogger(__name__) |
31 | 25 | |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
26 | 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
|
27 | backup.State.INACTIVE: 'B.', |
98 | 28 | backup.State.PAUSED: 'B‖', |
61
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.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
|
30 | 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
|
31 | 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
|
32 | } |
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
|
33 | |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
34 | 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
|
35 | # 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
|
36 | 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
|
37 | 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
|
38 | 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
|
39 | backup.Errors.ERRORS: 'B?' |
16
d0ffae5550ef
Added offline symbol B⦙ (no offline detection yet)
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
40 | } |
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
|
41 | |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
42 | 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
|
43 | 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
|
44 | 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
|
45 | 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
|
46 | 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
|
47 | else: |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
48 | 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
|
49 | |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
50 | 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
|
51 | 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
|
52 | |
133
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
53 | # Refresh the menu at most twice a second |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
54 | refresh_interval=0.5 |
27
a347387868be
UI refresh delay to reduce flicker
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
55 | |
32 | 56 | # Workaround to rumps brokenness; |
57 | # see https://github.com/jaredks/rumps/issues/59 | |
58 | def notification_workaround(title, subtitle, message): | |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
59 | try: |
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
60 | NSDictionary = objc.lookUpClass("NSDictionary") |
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
61 | d=NSDictionary() |
32 | 62 | |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
63 | rumps.notification(title, subtitle, message, data=d) |
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
64 | except Exception as err: |
50 | 65 | logger.exception("Failed to display notification") |
32 | 66 | |
19
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
67 | # Based on code snatched from |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
68 | # 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
|
69 | def humanbytes(B): |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
70 | '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
|
71 | B = float(B) |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
72 | KB = float(1024) |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
73 | 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
|
74 | 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
|
75 | 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
|
76 | |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
77 | if B < KB: |
20
fdfbe5d7b677
Keychain support and random fixes
Tuomo Valkonen <tuomov@iki.fi>
parents:
19
diff
changeset
|
78 | 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
|
79 | elif KB <= B < MB: |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
80 | 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
|
81 | elif MB <= B < GB: |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
82 | 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
|
83 | elif GB <= B < TB: |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
84 | 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
|
85 | elif TB <= B: |
f9ce2442f14f
Display original/deduplicatd size if no progress percentage available
Tuomo Valkonen <tuomov@iki.fi>
parents:
18
diff
changeset
|
86 | 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
|
87 | |
97 | 88 | |
89 | def progress_percentage(done, total, d): | |
106
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
90 | percentage = 100*float(done)/float(total) |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
91 | progress=': %d%%' % int(round(percentage)) |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
92 | if 'operation_no' in d: |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
93 | progress=':#%d%s' % (d['operation_no'], progress) |
97 | 94 | return progress |
95 | ||
96 | def progress_parts(done, total, d): | |
106
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
97 | progress=': %d/%d' % (int(done), int(total)) |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
98 | if 'operation_no' in d: |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
99 | progress=':#%d%s' % (d['operation_no'], progress) |
97 | 100 | return progress |
101 | ||
106
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
102 | _error_state=(backup.State.INACTIVE, backup.Errors.ERRORS) |
97 | 103 | |
10 | 104 | def make_title(status): |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
105 | def add_info(info, new): |
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
106 | if info: |
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
107 | 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
|
108 | else: |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
109 | 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
|
110 | |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
111 | info=None |
105 | 112 | this_refresh_time=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
|
113 | |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
114 | if not status.errors.ok(): |
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
115 | 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
|
116 | |
98 | 117 | if status.state==backup.State.PAUSED: |
118 | info=add_info(info, "paused") | |
119 | elif status.state==backup.State.SCHEDULED: | |
10 | 120 | # Operation scheduled |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
121 | when=status.when() |
10 | 122 | now=time.time() |
72
e0e6043779e2
Improved time display in menu
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
123 | |
110
a79409c72565
Oops, removed stray reset of menu entry info
Tuomo Valkonen <tuomov@iki.fi>
parents:
106
diff
changeset
|
124 | if when==math.inf: |
a79409c72565
Oops, removed stray reset of menu entry info
Tuomo Valkonen <tuomov@iki.fi>
parents:
106
diff
changeset
|
125 | whenstr='--' |
10 | 126 | else: |
72
e0e6043779e2
Improved time display in menu
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
127 | tnow=datetime.datetime.fromtimestamp(now) |
e0e6043779e2
Improved time display in menu
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
128 | twhen=datetime.datetime.fromtimestamp(when) |
112 | 129 | tendtoday=tnow.replace(hour=23,minute=59,second=59) |
72
e0e6043779e2
Improved time display in menu
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
130 | tendtomorrow=tendtoday+datetime.timedelta(days=1) |
e0e6043779e2
Improved time display in menu
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
131 | |
110
a79409c72565
Oops, removed stray reset of menu entry info
Tuomo Valkonen <tuomov@iki.fi>
parents:
106
diff
changeset
|
132 | if twhen<tnow: |
111
c3bc27cf5ece
Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
110
diff
changeset
|
133 | whenstr='overdue' + (' on %s' % twhen.isoformat()) |
110
a79409c72565
Oops, removed stray reset of menu entry info
Tuomo Valkonen <tuomov@iki.fi>
parents:
106
diff
changeset
|
134 | elif twhen>tendtomorrow: |
105 | 135 | # Display date if scheduled event is after tomorrow |
10 | 136 | whenday=datetime.date.fromtimestamp(when) |
72
e0e6043779e2
Improved time display in menu
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
137 | whenstr='on %s' % twhen.date().isoformat() |
105 | 138 | this_refresh_time=tendtoday+datetime.timedelta(seconds=1) |
112 | 139 | elif twhen>tendtoday and when-now>=12*60*60: # 12 hours |
105 | 140 | # Display 'tomorrow' if the scheduled event is tomorrow and |
141 | # not earlier than after 12 hours | |
72
e0e6043779e2
Improved time display in menu
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
142 | whenstr='tomorrow' |
105 | 143 | this_refresh_time=twhen-datetime.timedelta(hours=12) |
10 | 144 | else: |
105 | 145 | # Otherwise, display time |
10 | 146 | twhen=time.localtime(when) |
147 | if twhen.tm_sec>30: | |
148 | # Round up minute display to avoid user confusion | |
149 | twhen=time.localtime(when+30) | |
150 | whenstr='at %02d:%02d' % (twhen.tm_hour, twhen.tm_min) | |
38 | 151 | |
110
a79409c72565
Oops, removed stray reset of menu entry info
Tuomo Valkonen <tuomov@iki.fi>
parents:
106
diff
changeset
|
152 | this_info='' |
a79409c72565
Oops, removed stray reset of menu entry info
Tuomo Valkonen <tuomov@iki.fi>
parents:
106
diff
changeset
|
153 | if 'reason' in status.detail: |
a79409c72565
Oops, removed stray reset of menu entry info
Tuomo Valkonen <tuomov@iki.fi>
parents:
106
diff
changeset
|
154 | 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
|
155 | |
110
a79409c72565
Oops, removed stray reset of menu entry info
Tuomo Valkonen <tuomov@iki.fi>
parents:
106
diff
changeset
|
156 | when_how_sched= "%s%s %s" % (this_info, status.type, 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
|
157 | |
110
a79409c72565
Oops, removed stray reset of menu entry info
Tuomo Valkonen <tuomov@iki.fi>
parents:
106
diff
changeset
|
158 | 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
|
159 | |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
160 | elif status.state==backup.State.QUEUED: |
97 | 161 | info=add_info(info, "queued %s" % status.type) |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
162 | elif status.state==backup.State.ACTIVE: |
10 | 163 | # 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
|
164 | progress='' |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
165 | d=status.detail |
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
166 | if 'progress_current' in d and 'progress_total' in d: |
97 | 167 | progress=progress_percentage(d['progress_current'], |
168 | d['progress_total'], d) | |
169 | elif ('progress_current_secondary' in d and | |
170 | 'progress_total_secondary' in d): | |
171 | progress=progress_parts(d['progress_current_secondary'], | |
172 | d['progress_total_secondary'], d) | |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
173 | 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
|
174 | 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
|
175 | 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
|
176 | |
97 | 177 | howrunning = "running %s%s" % (status.type, 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
|
178 | |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
179 | 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
|
180 | else: |
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
181 | pass |
15 | 182 | |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
183 | if info: |
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
184 | 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
|
185 | else: |
62
b7d13b2ad67e
Turned Status and Operation into classes instead of dictionaries
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
186 | 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
|
187 | |
105 | 188 | return title, (status.state, status.errors), this_refresh_time |
10 | 189 | |
133
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
190 | class EventTrackingTimer(rumps.Timer): |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
191 | """ |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
192 | Variant of rumps.Timer changed to |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
193 | a) use *both* NSEventTrackingRunLoopMode (for when menu open) and |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
194 | NSDefaultRunLoopMode (for when menu closed), |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
195 | b) in the mainRunLoop, to allow creating timers there from other threads. |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
196 | """ |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
197 | def start(self): |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
198 | """Start the timer thread loop.""" |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
199 | if not self._status: |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
200 | self._nsdate = NSDate.date() |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
201 | self._nstimer = NSTimer.alloc().initWithFireDate_interval_target_selector_userInfo_repeats_( |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
202 | self._nsdate, self._interval, self, 'callback:', None, True) |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
203 | NSRunLoop.mainRunLoop().addTimer_forMode_(self._nstimer, NSEventTrackingRunLoopMode) |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
204 | NSRunLoop.mainRunLoop().addTimer_forMode_(self._nstimer, NSDefaultRunLoopMode) |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
205 | #rumps._TIMERS[self] = None |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
206 | self._status = True |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
207 | |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
208 | 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
|
209 | def __init__(self, backups): |
10 | 210 | self.lock=Lock() |
211 | self.backups=backups | |
133
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
212 | self.refresh_timer=EventTrackingTimer(self.__refresh_callback, refresh_interval) |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
213 | self.updated_recently=False |
10 | 214 | self.statuses=[None]*len(backups) |
215 | ||
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
216 | for index in range(len(backups)): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
217 | self.statuses[index]=backups[index].status() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
218 | |
133
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
219 | itemlist, title=self.build_menu_and_timer() |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
220 | |
133
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
221 | super().__init__(title, menu=itemlist, quit_button=None) |
10 | 222 | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
223 | for index in range(len(backups)): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
224 | # Python closures suck dog's balls; hence the _index=index hack |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
225 | # See also http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/ |
87
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
226 | cb=(lambda status, errorlog=None, _index=index: |
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
227 | self.__status_callback(_index, status, errorlog=errorlog)) |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
228 | backups[index].set_status_update_callback(cb) |
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
|
229 | |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
99
diff
changeset
|
230 | dreamtime.add_callback(self, self.__sleepwake_callback) |
10 | 231 | |
133
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
232 | def __rebuild_menu(self, update): |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
233 | if not update: |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
234 | self.itemlist=[] |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
235 | |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
236 | itemlist=self.itemlist |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
237 | |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
238 | state=(backup.State.INACTIVE, backup.Errors.OK) |
105 | 239 | refresh_time=None |
98 | 240 | all_paused=True |
241 | ||
10 | 242 | for index in range(len(self.backups)): |
243 | b=self.backups[index] | |
105 | 244 | title, this_state, this_refresh_time=make_title(self.statuses[index]) |
10 | 245 | # Python closures suck dog's balls... |
246 | # first and the last program I write in Python until somebody | |
247 | # fixes this brain damage | |
248 | cbm=lambda sender, _b=b: self.__menu_select_backup(sender, _b) | |
133
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
249 | itemstate=0 |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
250 | if not this_state[1].ok(): |
133
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
251 | itemstate=-1 |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
252 | elif this_state[0]==backup.State.SCHEDULED or this_state[0]==backup.State.QUEUED: |
133
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
253 | itemstate=1 |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
254 | |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
255 | if update: |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
256 | item=itemlist[index] |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
257 | if item.title!=title: |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
258 | item.title=title |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
259 | if item.state!=itemstate: |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
260 | item.state=itemstate |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
261 | else: |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
262 | item=rumps.MenuItem(title, callback=cbm) |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
263 | item.state=itemstate |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
264 | itemlist.append(item) |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
265 | |
61
bc6c3d74e6ea
Made combination error-state into an error-state matrix, as well as Enum
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
266 | state=combine_state(state, this_state) |
10 | 267 | |
98 | 268 | all_paused=(all_paused and this_state[0]==backup.State.PAUSED) |
269 | ||
72
e0e6043779e2
Improved time display in menu
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
270 | # Do we have to automatically update menu display? |
105 | 271 | if not refresh_time: |
272 | refresh_time=this_refresh_time | |
273 | elif this_refresh_time: | |
274 | refresh_time=min(refresh_time, this_refresh_time) | |
72
e0e6043779e2
Improved time display in menu
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
275 | |
133
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
276 | index = len(self.backups) |
98 | 277 | if all_paused: |
278 | pausename='Resume all' | |
279 | else: | |
280 | pausename="Pause all" | |
133
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
281 | if update: |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
282 | item=itemlist[index] |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
283 | if item.title!=pausename: |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
284 | item.title=pausename |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
285 | else: |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
286 | menu_pause=rumps.MenuItem(pausename, callback=lambda _: self.pause_resume_all()) |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
287 | itemlist.append(menu_pause) |
98 | 288 | |
133
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
289 | if not update: |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
290 | menu_log=rumps.MenuItem("Show log", callback=lambda _: showlog()) |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
291 | itemlist.append(menu_log) |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
292 | if not settings['no_quit_menu_entry']: |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
293 | menu_quit=rumps.MenuItem("Quit...", callback=lambda _: self.quit()) |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
294 | itemlist.append(menu_quit) |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
295 | |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
296 | return itemlist, state, refresh_time |
72
e0e6043779e2
Improved time display in menu
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
297 | |
133
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
298 | def build_menu_and_timer(self, update=False): |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
299 | logger.debug('Rebuilding menu') |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
300 | itemlist, state, refresh_time=self.__rebuild_menu(update) |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
301 | title=trayname(state) |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
302 | |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
303 | if update and self.title!=title: |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
304 | logger.debug("set title %s" % title) |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
305 | self.title=title |
105 | 306 | |
133
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
307 | if not self.updated_recently and not refresh_time: |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
308 | self.refresh_timer.stop() |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
309 | elif self.updated_recently: |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
310 | self.updated_recently=False |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
311 | if self.refresh_timer.interval>refresh_interval: |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
312 | self.refresh_timer.stop() |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
313 | self.refresh_timer.interval=refresh_interval |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
314 | self.refresh_timer.start() |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
315 | else: |
105 | 316 | # Need to time a refresh due to content display changing, |
317 | # e.g., 'tomorrow' changing to a more specific hour. | |
318 | when=time.mktime(refresh_time.timetuple()) | |
133
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
319 | delay=max(when-time.time(), refresh_interval) |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
320 | logger.debug('Timing menu refresh in %s seconds' % delay) |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
321 | self.refresh_timer.stop() |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
322 | self.refresh_timer.interval=delay |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
323 | self.refresh_timer.start() |
72
e0e6043779e2
Improved time display in menu
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
324 | |
133
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
325 | return itemlist, title |
10 | 326 | |
106
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
327 | # Callbacks -- exception-protected to get any indications of errors |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
328 | |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
329 | @protect_noreturn |
133
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
330 | def __refresh_callback(self, timer): |
27
a347387868be
UI refresh delay to reduce flicker
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
331 | with self.lock: |
133
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
332 | self.build_menu_and_timer(True) |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
333 | |
106
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
334 | @protect_noreturn |
55
407af23d16bb
Improved backup main thread loop, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
51
diff
changeset
|
335 | def __status_callback(self, index, status, errorlog=None): |
75
2a44b9649212
UI refresh fix; added debug messages
Tuomo Valkonen <tuomov@iki.fi>
parents:
72
diff
changeset
|
336 | logger.debug("Tray status callback") |
10 | 337 | with self.lock: |
338 | self.statuses[index]=status | |
133
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
339 | self.updated_recently=True |
75
2a44b9649212
UI refresh fix; added debug messages
Tuomo Valkonen <tuomov@iki.fi>
parents:
72
diff
changeset
|
340 | # Time the refresh if it has not been timed, or if the timer |
2a44b9649212
UI refresh fix; added debug messages
Tuomo Valkonen <tuomov@iki.fi>
parents:
72
diff
changeset
|
341 | # is timing for the "long-term" (refresh_timer_time set) |
133
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
342 | if self.refresh_timer.interval>refresh_interval: |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
343 | self.refresh_timer.stop() |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
344 | self.refresh_timer.interval=refresh_interval |
ec8014a2ee7a
Need to time menu updates using Apple's frameworks to avoid segfaults.
Tuomo Valkonen <tuomov@iki.fi>
parents:
114
diff
changeset
|
345 | self.refresh_timer.start() |
10 | 346 | |
114
ad9fb3dd9fec
Disable Notification Centre notifications.
Tuomo Valkonen <tuomov@iki.fi>
parents:
112
diff
changeset
|
347 | # if errorlog: |
ad9fb3dd9fec
Disable Notification Centre notifications.
Tuomo Valkonen <tuomov@iki.fi>
parents:
112
diff
changeset
|
348 | # if 'msgid' not in errorlog or not isinstance(errorlog['msgid'], str): |
ad9fb3dd9fec
Disable Notification Centre notifications.
Tuomo Valkonen <tuomov@iki.fi>
parents:
112
diff
changeset
|
349 | # msgid='UnknownError' |
ad9fb3dd9fec
Disable Notification Centre notifications.
Tuomo Valkonen <tuomov@iki.fi>
parents:
112
diff
changeset
|
350 | # else: |
ad9fb3dd9fec
Disable Notification Centre notifications.
Tuomo Valkonen <tuomov@iki.fi>
parents:
112
diff
changeset
|
351 | # msgid=errorlog['msgid'] |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
352 | |
114
ad9fb3dd9fec
Disable Notification Centre notifications.
Tuomo Valkonen <tuomov@iki.fi>
parents:
112
diff
changeset
|
353 | # logger.debug("Opening notification for error %s '%s'", |
ad9fb3dd9fec
Disable Notification Centre notifications.
Tuomo Valkonen <tuomov@iki.fi>
parents:
112
diff
changeset
|
354 | # msgid, errorlog['message']) |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
355 | |
114
ad9fb3dd9fec
Disable Notification Centre notifications.
Tuomo Valkonen <tuomov@iki.fi>
parents:
112
diff
changeset
|
356 | # notification_workaround(branding.appname_stylised, |
ad9fb3dd9fec
Disable Notification Centre notifications.
Tuomo Valkonen <tuomov@iki.fi>
parents:
112
diff
changeset
|
357 | # msgid, errorlog['message']) |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
358 | |
106
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
359 | @protect_noreturn |
98 | 360 | def pause_resume_all(self): |
361 | with self.lock: | |
362 | try: | |
363 | all_paused=True | |
364 | for b in self.backups: | |
365 | all_paused=all_paused and b.is_paused() | |
366 | if all_paused: | |
367 | logger.debug('Pausing all') | |
368 | for b in self.backups: | |
369 | b.resume() | |
370 | else: | |
371 | logger.debug('Resuming all') | |
372 | for b in self.backups: | |
373 | b.pause() | |
374 | except: | |
375 | logger.exception("Pause/resume error") | |
376 | ||
106
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
377 | @protect_noreturn |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
378 | def __sleepwake_callback(self, woke): |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
379 | if woke: |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
380 | self.refresh_ui() |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
381 | |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
382 | @protect_noreturn |
46
ecb41072a1b7
Unsuccesfull attempts at clean quit on exceptions that fall through
Tuomo Valkonen <tuomov@iki.fi>
parents:
45
diff
changeset
|
383 | def quit(self): |
28 | 384 | rumps.quit_application() |
385 | ||
106
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
386 | @protect_noreturn |
28 | 387 | def __menu_select_backup(self, sender, b): |
388 | #sender.state=not sender.state | |
31 | 389 | logger.debug("Manually backup '%s'", b.name) |
32 | 390 | try: |
49 | 391 | b.create() |
32 | 392 | except Exception as err: |
50 | 393 | logger.exception("Failure to initialise backup") |
79
b075b3db3044
Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.
Tuomo Valkonen <tuomov@iki.fi>
parents:
78
diff
changeset
|
394 | notification_workaround(branding.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
|
395 | err.__class__.__name__, str(err)) |
28 | 396 | |
42
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
397 | # |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
398 | # Log window |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
399 | # |
10 | 400 | |
42
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
401 | logwindow=[None] |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
402 | logwindow_lock=Lock() |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
403 | |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
404 | def showlog(): |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
405 | try: |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
406 | w=None |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
407 | with logwindow_lock: |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
408 | if not logwindow[0]: |
84
b613265bd679
A few things had not been updated to module reorganisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
80
diff
changeset
|
409 | lines=loggers.fifo.formatAll() |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
410 | msg="\n".join(lines[0:]) |
84
b613265bd679
A few things had not been updated to module reorganisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
80
diff
changeset
|
411 | w=rumps.Window(title=branding.appname_stylised+' log', |
42
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
412 | default_text=msg, |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
413 | ok='Close', |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
414 | dimensions=(640,320)) |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
415 | logwindow[0]=w |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
416 | if w: |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
417 | try: |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
418 | w.run() |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
419 | finally: |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
420 | with logwindow_lock: |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
421 | logwindow[0]=None |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
422 | except Exception as err: |
50 | 423 | logger.exception("Failed to display log") |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
424 | |
42
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
425 | # |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
426 | # 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
|
427 | # |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
428 | |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
429 | @rumps.notifications |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
430 | def notification_center(_): |
00accd11978b
Moved logwindow dispay out of BorgendTray, and made notification_center
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
431 | showlog() |
50 | 432 |