-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
executable file
·114 lines (87 loc) · 3.41 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/python2.7
# encoding: utf-8
import os
import sys
d = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, d)
from src.watts import get_watts, get_time_remaining
import logging
# Apple stuff
from Foundation import *
from AppKit import *
from PyObjCTools import AppHelper
UPDATE_INTERVAL = 5
start_time = NSDate.date()
def hide_from_dock():
"""hide icon from dock"""
#NSApplicationActivationPolicyRegular = 0
#NSApplicationActivationPolicyAccessory = 1
NSApplicationActivationPolicyProhibited = 2
NSApp.setActivationPolicy_(NSApplicationActivationPolicyProhibited)
class DocNetStatDelegate(NSObject):
def applicationDidFinishLaunching_(self, sender):
self.statusItem = NSStatusBar.systemStatusBar().statusItemWithLength_(
NSVariableStatusItemLength)
self.statusImage = NSImage.alloc()
self.error = False
# Icons
mydir = os.path.dirname(os.path.abspath(__file__))
self.statusItem.setToolTip_('Watts')
self.statusItem.setHighlightMode_(TRUE)
self.statusItem.setEnabled_(TRUE)
# Menu
self.error = True
self.menu = NSMenu.alloc().init()
# Longterm
self.longterm_watts = []
self.longterm_status = NSMenuItem.alloc().init()
self.longterm_status.setTitle_("init W")
self.longterm_status.setToolTip_("5 min usage")
self.longterm_status.setKeyEquivalent_('l')
self.menu.addItem_(self.longterm_status)
# Sync and Quit buttons
menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
'Sync...', 'syncall:', '')
self.menu.addItem_(menuitem)
menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
'Quit', 'terminate:', '')
self.menu.addItem_(menuitem)
self.statusItem.setMenu_(self.menu)
self.timer = NSTimer.alloc().initWithFireDate_interval_target_selector_userInfo_repeats_(
start_time, float(UPDATE_INTERVAL), self, 'sync:', None, True)
# Initialize Ping, Tcp and Udp
NSRunLoop.currentRunLoop().addTimer_forMode_(self.timer,
NSDefaultRunLoopMode)
self.timer.fire()
NSLog("DockWatts started!")
def syncall_(self, notification):
self.sync_(notification)
def sync_(self, notification):
watts = get_watts()
remaining = get_time_remaining()
if watts is not None:
self.statusItem.setTitle_(u"%.1f W %s" % (watts, remaining))
self.longterm_watts.append(watts)
if len(self.longterm_watts) > 60:
self.longterm_watts = self.longterm_watts[1:]
longterm = sum(self.longterm_watts) / float(len(self.longterm_watts))
self.longterm_status.setTitle_(u"%.1f W" % longterm)
else:
self.statusItem.setTitle_(u"E W")
def applicationShouldTerminate_(self, notification):
return 1
if __name__ == "__main__":
try:
logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
app = NSApplication.sharedApplication()
app.hide_(TRUE)
delegate = DocNetStatDelegate.alloc().init()
app.setDelegate_(delegate)
hide_from_dock()
AppHelper.runEventLoop()
except KeyboardInterrupt:
delegate.terminate_()
AppHelper.stopEventLoop()
pass