-
Notifications
You must be signed in to change notification settings - Fork 0
/
meow.py
225 lines (184 loc) · 6.54 KB
/
meow.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import PySide
from PySide import QtCore, QtGui, QtSvg
import httplib, urllib
import base64
import json
from dateutil.parser import parse
import os, sys
class Data:
appPath = os.path.dirname(sys.executable)
status = False
class Connection(QtCore.QThread):
def __init__(self, parent=None):
super(Connection, self).__init__(parent)
self._running = False
settings = json.loads(open(Data.appPath+"/setting.json").read())
self.repos = []
if settings[u"login"] is True:
self.token = settings[u"token"]
Data.status = True
self.getRepos()
def auth(self, username, password):
# encode username:password using base64
self.username = username
auth = base64.encodestring('%s:%s' % (username, password))[:-1]
# fill params, headers, connection
params = '{"scopes":["repo"]}'
headers = {"Authorization": "Basic %s" % auth}
conn = httplib.HTTPSConnection("api.github.com")
# request and get the response
conn.request("POST", "/authorizations", params, headers)
response = conn.getresponse()
print "\n#### Auth ####"
print response.status, response.reason
data = response.read()
print data
jsonResponse = json.loads(data)
self.token = jsonResponse[u"token"].encode('utf-8', 'ignore')
# close connection
conn.close()
Data.status = True
self.getRepos()
def simpleApiCall(self, method, url):
conn = httplib.HTTPSConnection("api.github.com")
print "{url}?access_token={token}".format(url=url, token=self.token)
conn.request(method, "{url}?access_token={token}".format(url=url, token=self.token))
response = conn.getresponse()
print "\n#### {method}: {url} ####".format(method=method, url=url)
# print response.status, response.reason
data = response.read()
# print data
conn.close()
return json.loads(data)
def getRepos(self):
# get my repos
jsonResponse = self.simpleApiCall("GET", "/user/repos")
# print jsonResponse
self.repos.extend([repo[u"full_name"].encode('utf-8', 'ignore') for repo in jsonResponse])
# get repos of my organizations
jsonResponse = self.simpleApiCall("GET", "/user/orgs")
orgs = ([org[u"login"].encode('utf-8', 'ignore') for org in jsonResponse])
for org in orgs:
jsonResponse = self.simpleApiCall("GET", "/orgs/%s/repos" % org)
orgRepos = ([repo[u"full_name"].encode('utf-8', 'ignore') for repo in jsonResponse])
self.repos.extend(orgRepos)
self.repos = dict([(r, None) for r in self.repos])
print "\n#### Repos ####"
print self.repos
def run(self):
self._running = True
while self._running:
self.getCommits()
self.msleep(1000*60)
def stop(self, wait=False):
self._running = False
if wait:
self.wait()
alarmMsg = QtCore.Signal(str, str)
def getCommits(self):
print "\n#### Get Commits ####"
if self.repos == []:
print "#### No repos yet! ####"
return
for r, t in self.repos.items():
jsonResponse = self.simpleApiCall("GET", "/repos/%s/commits" % r)
if jsonResponse.__class__.__name__ == 'list':
commit = jsonResponse[0][u"commit"]
msg = commit[u"message"]
author = commit[u"author"]
name = author[u"name"]
date = parse(author[u"date"])
repo = r
if t == None:
self.repos[r] = date
elif t < date:
self.repos[r] = date
growlTitle = "{name} Comitted to {repo}".format(name=name, repo=repo)
growlMsg = msg
print "#### %s ####\n" % growlTitle
# self.emit(QtCore.SIGNAL("SHOWMSG"), growlTitle, growlMsg)
self.alarmMsg.emit(growlTitle, growlMsg)
# meow.showMsg(growlTitle, growlMsg)
print "\n#### Commits Retrieved ####"
print self.repos
class Preferences(QtGui.QWidget):
def __init__(self):
super(Preferences, self).__init__()
self.initUI()
accountMsg = QtCore.Signal(str, str)
def initUI(self):
formLayout = QtGui.QFormLayout()
self.usernameEdit = QtGui.QLineEdit()
self.passwordEdit = QtGui.QLineEdit()
self.passwordEdit.setEchoMode(QtGui.QLineEdit.Password)
formLayout.addRow(self.tr("&Username: "), self.usernameEdit)
formLayout.addRow(self.tr("&Password: "), self.passwordEdit)
if Data.status is False:
self.btn = QtGui.QPushButton("&Login", self)
formLayout.addRow(self.btn)
self.btn.clicked.connect(self.login)
else:
self.btn = QtGui.QPushButton("&Logout", self)
self.usernameEdit.setReadOnly(True)
self.passwordEdit.setReadOnly(True)
formLayout.addRow(self.btn)
self.btn.clicked.connect(self.login)
self.setLayout(formLayout)
self.setWindowTitle('Meow Preferences')
def login(self):
if Data.status is False:
self.btn.setText("&Logout")
self.usernameEdit.setReadOnly(True)
self.passwordEdit.setReadOnly(True)
self.accountMsg.emit(self.usernameEdit.text(), self.passwordEdit.text())
else:
Data.status = False
self.btn.setText("&Login")
self.usernameEdit.setReadOnly(False)
self.passwordEdit.setReadOnly(False)
self.usernameEdit.setText("")
self.usernameEdit.setText("")
class Meow(QtGui.QSystemTrayIcon):
def __init__(self):
super(Meow, self).__init__()
self.c = Connection(self)
self.c.alarmMsg.connect(self.showMsg)
self.initUI()
def initUI(self):
menu = QtGui.QMenu()
menu.addAction('Preferences...', self.preferencesCB)
menu.addAction('Quit', self.quitCB)
QtCore.QObject.connect(menu, QtCore.SIGNAL('aboutToShow()'), self.aboutToShowCB)
self.setContextMenu(menu)
icon = QtGui.QIcon(Data.appPath+"/cat.png")
self.setIcon(icon)
self.p = Preferences()
self.p.accountMsg.connect(self.c.auth)
def startThread(self):
self.c.start()
def quitCB(self):
self.c.quit()
QtGui.QApplication.quit()
def preferencesCB(self):
print 'preferences'
self.p.show()
def aboutToShowCB(self):
print 'tray icon clicked'
def showMsg(self, title, msg):
self.showMessage(title, msg, QtGui.QSystemTrayIcon.Information, 1000)
app = QtGui.QApplication([])
app.setQuitOnLastWindowClosed(False)
"""
if sys.platform == "darwin":
import AppKit
# https://developer.apple.com/library/mac/#documentation/AppKit/Reference/NSRunningApplication_Class/Reference/Reference.html
NSApplicationActivationPolicyRegular = 0
NSApplicationActivationPolicyAccessory = 1
NSApplicationActivationPolicyProhibited = 2
AppKit.NSApp.setActivationPolicy_(NSApplicationActivationPolicyProhibited)
"""
meow = Meow()
meow.show()
meow.startThread()
app.exec_()
del app