~ubuntu-branches/ubuntu/utopic/lptools/utopic-proposed

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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/usr/bin/python
#
# Author: Rodney Dawes <rodney.dawes@canonical.com>
#
# Copyright 2009 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import with_statement

import pygtk
pygtk.require('2.0')
import gobject
import gtk
import pango

from ConfigParser import ConfigParser
import os
import re
import subprocess
import sys

from xdg.BaseDirectory import xdg_cache_home, xdg_config_home

from threading import Thread

from launchpadlib.launchpad import Launchpad, EDGE_SERVICE_ROOT
from launchpadlib.credentials import Credentials

VOTES = { "Approve" : "#00ff00",
          "Needs Fixing" : "#993300",
          "Disapprove" : "#ff0000",
          "Resubmit" : "#ff0000",
          "Pending" : "#ff6600",
          "Abstain" : "#909090",
          "Needs Information" : "#909090",
          }

class Preferences(object):

      def __init__(self):
            self.filename = os.path.join(xdg_config_home, "lptools",
                                         "lptools.conf")
            self.config = ConfigParser()
            self.config.read(self.filename)
            if not os.path.isdir(os.path.dirname(self.filename)):
                  os.makedirs(os.path.dirname(self.filename))

            if not self.config.has_section("lptools"):
                  self.config.add_section("lptools")

            if self.config.has_option("lptools", "projects"):
                  self.projects = self.config.get("lptools",
                                                  "projects").split(",")
            else:
                  self.projects = []

            if self.config.has_option("lptools", "server"):
                  self.api_server = self.config.get("lptools", "server")
            else:
                  self.api_server = EDGE_SERVICE_ROOT

            # gtk.ListStore for the dialog
            self.store = None
            self.dialog = self.__build_dialog()

      def __build_dialog(self):
            dialog = gtk.Dialog()
            dialog.set_title("Pending Reviews Preferences")
            dialog.set_destroy_with_parent(True)
            dialog.set_has_separator(False)
            dialog.set_default_size(240, 320)

            area = dialog.get_content_area()

            vbox = gtk.VBox(spacing=6)
            vbox.set_border_width(12)
            area.add(vbox)
            vbox.show()

            label = gtk.Label("<b>%s</b>" % "_Projects")
            label.set_use_underline(True)
            label.set_use_markup(True)
            label.set_alignment(0.0, 0.5)
            vbox.pack_start(label, expand=False, fill=False)
            label.show()

            hbox = gtk.HBox(spacing=12)
            vbox.pack_start(hbox, expand=True, fill=True)
            hbox.show()

            misc = gtk.Label()
            hbox.pack_start(misc, expand=False, fill=False)
            misc.show()

            scrollwin = gtk.ScrolledWindow()
            scrollwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
            hbox.pack_start(scrollwin, expand=True, fill=True)
            scrollwin.show()

            self.store = gtk.ListStore(str)

            view = gtk.TreeView(self.store)
            label.set_mnemonic_widget(view)
            view.set_headers_visible(False)
            scrollwin.add(view)
            view.show()

            cell = gtk.CellRendererText()
            cell.set_property("editable", True)
            cell.connect("editing_started", self.__edit_started)
            cell.connect("edited", self.__edit_finished)
            col = gtk.TreeViewColumn("Project", cell, markup=0)
            view.append_column(col)

            dialog.connect("close", self.__dialog_closed, 0)
            dialog.connect("response", self.__dialog_closed)

            return dialog

      def __edit_started(self, cell, editable, path):
            return

      def __edit_finished(self, sell, path, text):
            if text == "Click here to add a project...":
                  return
            treeiter = self.store.get_iter_from_string(path)
            label = "<i>%s</i>" % "Click here to add a project..."
            self.store.set(treeiter, 0, label)
            self.projects.append(text)
            self.store.append([text,])

      def __dialog_closed(self, dialog, response):
            dialog.hide()
            if len(self.projects) > 0:
                  self.config.set("lptools", "projects",
                                  ",".join(self.projects))
                  with open(self.filename, "w+b") as f:
                        self.config.write(f)

      def show_dialog(self, parent):
            if not self.dialog.get_transient_for():
                  self.dialog.set_transient_for(parent)
            self.store.clear()
            text = "<i>%s</i>" % "Click here to add a project..."
            self.store.append([text,])
            if len(self.projects) != 0:
                  for project in self.projects:
                        self.store.append([project,])
            self.dialog.run()


class Window(gtk.Window):

      def __init__(self):
            gtk.Window.__init__(self)
            self.set_title("Pending Reviews")
            self.set_default_size(320, 400)
            self.connect("destroy", lambda w : gtk.main_quit())
            self.connect("delete_event", lambda w, x: gtk.main_quit())

            vbox = gtk.VBox()
            self.add(vbox)
            vbox.show()

            toolbar = gtk.Toolbar()
            vbox.pack_start(toolbar, expand=False, fill=False)
            toolbar.show()

            button = gtk.ToolButton(gtk.STOCK_REFRESH)
            button.connect("clicked", self.__refresh)
            toolbar.insert(button, -1);
            button.show()

            button = gtk.ToolButton(gtk.STOCK_PREFERENCES)
            button.connect("clicked", self.__edit_prefs)
            toolbar.insert(button, -1)
            button.show()

            scrollwin = gtk.ScrolledWindow()
            scrollwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
            vbox.pack_start(scrollwin, expand=True, fill=True)
            scrollwin.show()

            self.store = gtk.ListStore(str, str)

            view = gtk.TreeView(self.store)
            view.connect("row-activated", self.__open_link)
            scrollwin.add(view)
            view.show()

            cell = gtk.CellRendererText()
            col = gtk.TreeViewColumn("Branch", cell, markup=0)
            view.append_column(col)

            self.cachedir = os.path.join(xdg_cache_home, "review-list")
            if not os.path.isdir(self.cachedir):
                  os.makedirs(self.cachedir)

            self.launchpad = None
            self.me = None

            self.config = Preferences()
            if len(self.config.projects) == 0:
                  text = "<i>%s</i>" % "No projects specified"
                  self.store.append((text, ""))

            self.thread = None
            self.id = 0
            Thread(target=self.__lp_login).start()

      def __lp_login(self):
            credsfile = os.path.join(self.cachedir, "credentials")

            if os.path.exists(credsfile):
                  creds = Credentials()

                  with file(credsfile) as f:
                        creds.load(f)
                  self.launchpad = Launchpad(creds, EDGE_SERVICE_ROOT)
            else:
                  self.launchpad = Launchpad.get_token_and_login(
                        'review-list',
                        EDGE_SERVICE_ROOT,
                        self.cachedir)
                  with file(credsfile, "w") as f:
                        self.launchpad.credentials.save(f)

            self.me = self.launchpad.me

            print "Allo, %s" % self.me.name
            gtk.gdk.threads_enter()
            self.__refresh(None)
            gtk.gdk.threads_leave()
            return False

      def __refresh(self, button, data=None):
            if self.id != 0:
                  gobject.source_remove(self.id)
                  self.id = 0
            self.__timeout()
            self.id = gobject.timeout_add_seconds(5 * 60, self.__timeout)
            return False

      def __edit_prefs(self, button, data=None):
            self.config.show_dialog(self)

      def __open_link(self, view, path, column, data=None):
            row = self.store.get_iter(path)
            url, = self.store.get(row, 1)
            if url == "":
                  return
            ret = subprocess.call(["xdg-open", url])
            if ret != 0:
                  dialog = gtk.MessageDialog(self, 0, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, "Failed to run 'xdg-open %s'\n" % url)
                  dialog.run()
                  dialog.destroy()

      def __load_merges(self):
            for project in self.config.projects:
                  lp_project = None
                  try:
                        lp_project = self.launchpad.projects[project]
                  except AttributeError:
                        print "Project %s has no development focus." % project
                        continue
                  except KeyError:
                        print "Project %s not found." % project
                        continue

                  for c in lp_project.getMergeProposals(status="Needs review"):
                        votes = {}
                        for key in VOTES.keys():
                              votes[key] = 0

                        for vote in c.votes:
                              if not vote.comment:
                                    continue
                              else:
                                    votes[vote.comment.vote] += 1

                        for key in votes.keys():
                              if votes[key] == 0:
                                    votes.pop(key, None)

                        vstr = ", ".join(
                              ["<span color='%s'>%s</span>: %d" \
                                     % (VOTES[key], key, votes[key]) \
                                     for key in votes.keys()]
                              )
                        if vstr == "":
                              vstr = "No Reviews"
                        status = "%s\n%s" % (c.source_branch.display_name,
                                             vstr)
                        urlp = re.compile(
                              'http[s]?://api\.(.*)launchpad\.net/[^/]+/')
                        merge_url = urlp.sub(
                              'http://launchpad.net/', c.self_link)

                        gtk.gdk.threads_enter()
                        self.store.append((status, merge_url))
                        gtk.gdk.threads_leave()
            
      def __timeout(self):
            self.store.clear()
            if self.thread and self.thread.isAlive():
                  return True

            if self.thread is None:
                  thread = Thread(target=self.__load_merges)

            thread.start()
            return True

if __name__ == "__main__":
      gobject.threads_init()
      gtk.gdk.threads_init()
      try:
            win = Window()
            win.show()
            gtk.gdk.threads_enter()
            gtk.main()
            gtk.gdk.threads_leave()
      except KeyboardInterrupt:
            gtk.main_quit()