-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodebase-mac.py
More file actions
180 lines (146 loc) · 5.75 KB
/
codebase-mac.py
File metadata and controls
180 lines (146 loc) · 5.75 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from Foundation import *
from AppKit import *
from PyObjCTools import AppHelper
import time
import sys
import json
import math
import urllib2
import base64
import BaseHTTPServer
base_url = "http://api3.codebasehq.com"
sessions_url = ""
username = ""
key = ""
project = ""
xml_template = """
<time-session>
<summary>{0}</summary>
<minutes type="integer">{1}</minutes>
</time-session>
"""
status_images = {'idle': 'clock.png'}
class Alert(object):
def __init__(self, messageText):
super(Alert, self).__init__()
self.messageText = messageText
self.informativeText = ""
self.buttons = []
self.accessory = None
self.alertstyle = NSInformationalAlertStyle
def displayAlert(self):
alert = NSAlert.alloc().init()
alert.setMessageText_(self.messageText)
alert.setInformativeText_(self.informativeText)
alert.setAlertStyle_(self.alertstyle)
if self.accessory is not None:
alert.setAccessoryView_(self.accessory)
for button in self.buttons:
alert.addButtonWithTitle_(button)
NSApp.activateIgnoringOtherApps_(True)
self.buttonPressed = alert.runModal()
def ask(message="Default Message", info_text="", buttons=["OK"]):
ap = Alert(message)
ap.informativeText = info_text
ap.buttons = buttons
input = NSTextField.alloc().initWithFrame_(NSMakeRect(0, 0, 200, 24))
input.setStringValue_("")
ap.accessory = input
ap.displayAlert()
return ap.accessory.stringValue()
def alert(message="Default Message", info_text="", buttons=["OK"]):
ap = Alert(message)
ap.informativeText = info_text
ap.buttons = buttons
ap.alertstyle = NSCriticalAlertStyle
ap.displayAlert()
return ap.buttonPressed
class Timer(NSObject):
images = {}
statusbar = None
state = 'idle'
newitem = None
stopitem = None
start_time = time.time()
task_name = ""
def applicationDidFinishLaunching_(self, notification):
statusbar = NSStatusBar.systemStatusBar()
self.statusitem = statusbar.statusItemWithLength_(NSVariableStatusItemLength)
for i in status_images.keys():
self.images[i] = NSImage.alloc().initByReferencingFile_(status_images[i])
self.statusitem.setImage_(self.images['idle'])
self.statusitem.setHighlightMode_(1)
self.statusitem.setToolTip_('New Task')
self.menu = NSMenu.alloc().init()
self.newitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('New Task...', 'task:', '')
self.menu.insertItem_atIndex_(self.newitem, 0)
menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Quit', 'terminate:', '')
self.menu.insertItem_atIndex_(menuitem, 1)
self.stopitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Finish Task', 'finish:', '')
self.statusitem.setMenu_(self.menu)
def task_(self, notification):
self.task_name = ask("New Task", "Enter a task description:", ["OK"])
if self.task_name == "":
return
self.timer = NSTimer.alloc().initWithFireDate_interval_target_selector_userInfo_repeats_(
NSDate.date(),
1.0,
self,
'display:',
None,
True
)
NSRunLoop.currentRunLoop().addTimer_forMode_(self.timer, NSDefaultRunLoopMode)
self.timer.fire()
self.start_time = time.time()
self.menu.removeItem_(self.newitem)
self.menu.insertItem_atIndex_(self.stopitem, 0)
def display_(self, notification):
seconds = time.time() - self.start_time
minutes = seconds // 60
seconds %= 60
elapsed = "%02i:%02i" % (minutes, seconds)
self.statusitem.setTitle_("{0}: {1}".format(self.task_name, elapsed))
def upload(self):
time_spent = int(math.ceil((time.time() - self.start_time) / 60))
xml = xml_template.format(self.task_name, time_spent)
req = urllib2.Request(url=sessions_url, data=xml)
base64string = base64.encodestring('%s:%s' % (username, key)).replace('\n', '')
authheader = "Basic %s" % base64string
req.add_header("Authorization", authheader)
req.add_header("Content-type", "application/xml")
req.add_header("Accept", "application/xml")
try:
urllib2.urlopen(req)
print "Uploaded."
except IOError, e:
message = "Try again later."
if hasattr(e, 'code'):
message = BaseHTTPServer.BaseHTTPRequestHandler.responses[e.code][1], ["OK"]
alert("There was an error uploading to Codebase", message)
def finish_(self, notification):
t = NSThread.alloc().initWithTarget_selector_object_(self,self.upload, None)
t.start()
self.menu.removeItem_(self.stopitem)
self.menu.insertItem_atIndex_(self.newitem, 0)
self.timer.invalidate()
self.statusitem.setTitle_("")
if __name__ == "__main__":
print sys.path[0]
try:
userdata = json.load(open("userdata.json"))
username = userdata["username"]
key = userdata["key"]
project = userdata["project"]
if username == "" or key == "" or project == "":
raise
except Exception as e:
username = ask("New Account", "Enter your Codebase API Username:", ["OK"])
key = ask("New Account", "Enter your Codebase API Key:", ["OK"])
project = ask("New Account", "Enter your project name:", ["OK"])
json.dump({"username": username, "key": key, "project": project}, open("userdata.json", "w+"))
sessions_url = base_url + "/" + project + "/time_sessions"
app = NSApplication.sharedApplication()
delegate = Timer.alloc().init()
app.setDelegate_(delegate)
AppHelper.runEventLoop()