author | Steve Borho <steve@borho.org> |
Wed, 02 Jan 2008 01:56:36 -0600 | |
changeset 531 | 5c5f39324c00 |
parent 527 | e0e4fa338fc4 |
child 566 | 7cba0c79391c |
permissions | -rw-r--r-- |
171 | 1 |
# |
2 |
# A simple dialog to execute random command for TortoiseHg |
|
3 |
# |
|
4 |
# Copyright (C) 2007 TK Soh <teekaysoh@gmail.com> |
|
5 |
# |
|
6 |
||
7 |
import pygtk |
|
8 |
pygtk.require("2.0") |
|
9 |
||
10 |
import gtk |
|
214
b2b08865b886
hggtk: use regular python thread to call popen in cmd dialog
TK Soh <teekaysoh@yahoo.com>
parents:
171
diff
changeset
|
11 |
import gobject |
171 | 12 |
import pango |
269
41763e7da660
hggtk/cmd: run hg in unbuffered mode to display output a.s.a.p
TK Soh <teekaysoh@yahoo.com>
parents:
214
diff
changeset
|
13 |
import os |
171 | 14 |
import threading |
214
b2b08865b886
hggtk: use regular python thread to call popen in cmd dialog
TK Soh <teekaysoh@yahoo.com>
parents:
171
diff
changeset
|
15 |
import Queue |
404
92cbcf266f60
hggtk: switch hgcmd.py to HgThread
Steve Borho <steve@borho.org>
parents:
402
diff
changeset
|
16 |
from hglib import HgThread |
455 | 17 |
from shlib import set_tortoise_icon |
171 | 18 |
|
19 |
class CmdDialog(gtk.Dialog): |
|
531
5c5f39324c00
hggtk/hgcmd: cleaner method for closing dialog
Steve Borho <steve@borho.org>
parents:
527
diff
changeset
|
20 |
def __init__(self, cmdline, width=520, height=400): |
404
92cbcf266f60
hggtk: switch hgcmd.py to HgThread
Steve Borho <steve@borho.org>
parents:
402
diff
changeset
|
21 |
title = 'hg ' + ' '.join(cmdline[1:]) |
171 | 22 |
gtk.Dialog.__init__(self, |
23 |
title=title, |
|
24 |
flags=gtk.DIALOG_MODAL, |
|
25 |
#buttons=(gtk.STOCK_OK, gtk.RESPONSE_ACCEPT) |
|
26 |
) |
|
455 | 27 |
|
28 |
set_tortoise_icon(self, 'hg.ico') |
|
214
b2b08865b886
hggtk: use regular python thread to call popen in cmd dialog
TK Soh <teekaysoh@yahoo.com>
parents:
171
diff
changeset
|
29 |
self.cmdline = cmdline |
b2b08865b886
hggtk: use regular python thread to call popen in cmd dialog
TK Soh <teekaysoh@yahoo.com>
parents:
171
diff
changeset
|
30 |
|
171 | 31 |
# construct dialog |
32 |
self.set_default_size(width, height) |
|
33 |
||
34 |
self._button_ok = gtk.Button("OK") |
|
35 |
self.action_area.pack_end(self._button_ok) |
|
36 |
||
37 |
scrolledwindow = gtk.ScrolledWindow() |
|
38 |
scrolledwindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) |
|
39 |
self.textview = gtk.TextView(buffer=None) |
|
40 |
self.textview.set_editable(False) |
|
41 |
self.textview.modify_font(pango.FontDescription("Monospace")) |
|
42 |
scrolledwindow.add(self.textview) |
|
43 |
self.textbuffer = self.textview.get_buffer() |
|
44 |
||
45 |
self.vbox.pack_start(scrolledwindow, True, True) |
|
401
9ab309fbc25c
hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents:
395
diff
changeset
|
46 |
self.connect('map_event', self._on_window_map_event) |
171 | 47 |
|
531
5c5f39324c00
hggtk/hgcmd: cleaner method for closing dialog
Steve Borho <steve@borho.org>
parents:
527
diff
changeset
|
48 |
self._button_ok.connect('clicked', self._on_ok_clicked) |
5c5f39324c00
hggtk/hgcmd: cleaner method for closing dialog
Steve Borho <steve@borho.org>
parents:
527
diff
changeset
|
49 |
self.show_all() |
171 | 50 |
|
531
5c5f39324c00
hggtk/hgcmd: cleaner method for closing dialog
Steve Borho <steve@borho.org>
parents:
527
diff
changeset
|
51 |
def _on_ok_clicked(self, button): |
5c5f39324c00
hggtk/hgcmd: cleaner method for closing dialog
Steve Borho <steve@borho.org>
parents:
527
diff
changeset
|
52 |
""" Ok button clicked handler. """ |
214
b2b08865b886
hggtk: use regular python thread to call popen in cmd dialog
TK Soh <teekaysoh@yahoo.com>
parents:
171
diff
changeset
|
53 |
self.response(gtk.RESPONSE_ACCEPT) |
171 | 54 |
|
55 |
def _on_window_map_event(self, event, param): |
|
404
92cbcf266f60
hggtk: switch hgcmd.py to HgThread
Steve Borho <steve@borho.org>
parents:
402
diff
changeset
|
56 |
self.hgthread = HgThread(self.cmdline[1:]) |
92cbcf266f60
hggtk: switch hgcmd.py to HgThread
Steve Borho <steve@borho.org>
parents:
402
diff
changeset
|
57 |
self.hgthread.start() |
92cbcf266f60
hggtk: switch hgcmd.py to HgThread
Steve Borho <steve@borho.org>
parents:
402
diff
changeset
|
58 |
self._button_ok.set_sensitive(False) |
92cbcf266f60
hggtk: switch hgcmd.py to HgThread
Steve Borho <steve@borho.org>
parents:
402
diff
changeset
|
59 |
gobject.timeout_add(10, self.process_queue) |
171 | 60 |
|
61 |
def write(self, msg, append=True): |
|
62 |
msg = unicode(msg, 'iso-8859-1') |
|
63 |
if append: |
|
64 |
enditer = self.textbuffer.get_end_iter() |
|
65 |
self.textbuffer.insert(enditer, msg) |
|
66 |
else: |
|
67 |
self.textbuffer.set_text(msg) |
|
68 |
||
214
b2b08865b886
hggtk: use regular python thread to call popen in cmd dialog
TK Soh <teekaysoh@yahoo.com>
parents:
171
diff
changeset
|
69 |
def process_queue(self): |
b2b08865b886
hggtk: use regular python thread to call popen in cmd dialog
TK Soh <teekaysoh@yahoo.com>
parents:
171
diff
changeset
|
70 |
""" |
b2b08865b886
hggtk: use regular python thread to call popen in cmd dialog
TK Soh <teekaysoh@yahoo.com>
parents:
171
diff
changeset
|
71 |
Handle all the messages currently in the queue (if any). |
b2b08865b886
hggtk: use regular python thread to call popen in cmd dialog
TK Soh <teekaysoh@yahoo.com>
parents:
171
diff
changeset
|
72 |
""" |
527
e0e4fa338fc4
hggtk/hgthread: thread-safe dialog mechanism for GtkUi
Steve Borho <steve@borho.org>
parents:
483
diff
changeset
|
73 |
self.hgthread.process_dialogs() |
404
92cbcf266f60
hggtk: switch hgcmd.py to HgThread
Steve Borho <steve@borho.org>
parents:
402
diff
changeset
|
74 |
enditer = self.textbuffer.get_end_iter() |
92cbcf266f60
hggtk: switch hgcmd.py to HgThread
Steve Borho <steve@borho.org>
parents:
402
diff
changeset
|
75 |
while self.hgthread.getqueue().qsize(): |
214
b2b08865b886
hggtk: use regular python thread to call popen in cmd dialog
TK Soh <teekaysoh@yahoo.com>
parents:
171
diff
changeset
|
76 |
try: |
404
92cbcf266f60
hggtk: switch hgcmd.py to HgThread
Steve Borho <steve@borho.org>
parents:
402
diff
changeset
|
77 |
msg = self.hgthread.getqueue().get(0) |
92cbcf266f60
hggtk: switch hgcmd.py to HgThread
Steve Borho <steve@borho.org>
parents:
402
diff
changeset
|
78 |
msg = unicode(msg, 'iso-8859-1') |
92cbcf266f60
hggtk: switch hgcmd.py to HgThread
Steve Borho <steve@borho.org>
parents:
402
diff
changeset
|
79 |
self.textbuffer.insert(enditer, msg) |
214
b2b08865b886
hggtk: use regular python thread to call popen in cmd dialog
TK Soh <teekaysoh@yahoo.com>
parents:
171
diff
changeset
|
80 |
except Queue.Empty: |
b2b08865b886
hggtk: use regular python thread to call popen in cmd dialog
TK Soh <teekaysoh@yahoo.com>
parents:
171
diff
changeset
|
81 |
pass |
404
92cbcf266f60
hggtk: switch hgcmd.py to HgThread
Steve Borho <steve@borho.org>
parents:
402
diff
changeset
|
82 |
if threading.activeCount() == 1: |
92cbcf266f60
hggtk: switch hgcmd.py to HgThread
Steve Borho <steve@borho.org>
parents:
402
diff
changeset
|
83 |
self._button_ok.set_sensitive(True) |
92cbcf266f60
hggtk: switch hgcmd.py to HgThread
Steve Borho <steve@borho.org>
parents:
402
diff
changeset
|
84 |
return False # Stop polling this function |
92cbcf266f60
hggtk: switch hgcmd.py to HgThread
Steve Borho <steve@borho.org>
parents:
402
diff
changeset
|
85 |
else: |
92cbcf266f60
hggtk: switch hgcmd.py to HgThread
Steve Borho <steve@borho.org>
parents:
402
diff
changeset
|
86 |
return True |
171 | 87 |
|
404
92cbcf266f60
hggtk: switch hgcmd.py to HgThread
Steve Borho <steve@borho.org>
parents:
402
diff
changeset
|
88 |
def run(cmdline=[], **opts): |
531
5c5f39324c00
hggtk/hgcmd: cleaner method for closing dialog
Steve Borho <steve@borho.org>
parents:
527
diff
changeset
|
89 |
dlg = CmdDialog(cmdline) |
5c5f39324c00
hggtk/hgcmd: cleaner method for closing dialog
Steve Borho <steve@borho.org>
parents:
527
diff
changeset
|
90 |
dlg.connect('response', gtk.main_quit) |
401
9ab309fbc25c
hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents:
395
diff
changeset
|
91 |
dlg.show_all() |
9ab309fbc25c
hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents:
395
diff
changeset
|
92 |
gtk.gdk.threads_init() |
9ab309fbc25c
hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents:
395
diff
changeset
|
93 |
gtk.gdk.threads_enter() |
9ab309fbc25c
hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents:
395
diff
changeset
|
94 |
gtk.main() |
9ab309fbc25c
hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents:
395
diff
changeset
|
95 |
gtk.gdk.threads_leave() |
171 | 96 |
|
97 |
if __name__ == "__main__": |
|
98 |
import sys |
|
401
9ab309fbc25c
hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents:
395
diff
changeset
|
99 |
opts = {} |
404
92cbcf266f60
hggtk: switch hgcmd.py to HgThread
Steve Borho <steve@borho.org>
parents:
402
diff
changeset
|
100 |
opts['cmdline'] = sys.argv |
401
9ab309fbc25c
hggtk: gtk threads and hgproc cleanup
Steve Borho <steve@borho.org>
parents:
395
diff
changeset
|
101 |
run(**opts) |
171 | 102 |