Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 1.9.2, 1.9.3, and 2.0

contrib: add script to run TortoiseHg under IPython shell

Changeset fdf165b9eab8

Parent c4b33a1a86ba

by Yuya Nishihara

Changes to one file · Browse files at fdf165b9eab8 Showing diff from parent c4b33a1a86ba Diff from another changeset...

Change 1 of 1 Show Entire File contrib/​ipythg.py Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
@@ -0,0 +1,73 @@
+#!/usr/bin/env python +# ipythg.py - Run TortoiseHg Qt from IPython +# +# Copyright 2011 Yuya Nishihara <yuya@tcha.org> +# +# This software may be used and distributed according to the terms of the +# GNU General Public License version 2, incorporated herein by reference. +"""Interactive Python shell for TortoiseHg + +? -> Introduction and overview of IPython's features. +%quickref -> Quick reference. +help -> Python's own help system. +object? -> Details about 'object'. ?object also works, ?? prints more. + +hg -> Run hg command in cmdui.Dialog. Usage: hg verify +thg -> Run thg command. Usage: thg log -R mercurial + +dialogs -> List of currently-opened dialogs. +repos -> Dict of repository objects. +run -> Module to run TortoiseHg dialogs. Usage: run.log(ui) +ui -> Mercurial's ui object. +""" +import os, sys, shlex +from IPython import ipapi +from IPython.Shell import IPShellQt4 +from PyQt4 import QtCore, QtGui + +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), + '..'))) +if 'HGPATH' in os.environ and os.path.isdir(os.environ['HGPATH']): + sys.path.insert(1, os.environ['HGPATH']) + +from mercurial import ui +from tortoisehg.hgqt import cmdui, thgrepo, run + +def _runhgcommand(self, arg): + def newdlg(ui): + return cmdui.Dialog(shlex.split(arg)) + run.qtrun(newdlg, self.user_ns['ui']) + +def _runthgcommand(self, arg): + run.runcommand(self.user_ns['ui'], shlex.split(arg)) + +def _banner(): + lines = __doc__.splitlines() + lines[2:2] = [run.shortlicense.strip(), ''] + return '\n'.join(lines) + '\n' + +def _execipyshell(ui): + """Setup IPython shell and enter mainloop""" + ns = {} + for mod in (QtCore, QtGui): + ns.update((k, v) for k, v in vars(mod).iteritems() + if not k.startswith('_')) + ns.update({'dialogs': run.qtrun._dialogs, 'repos': thgrepo._repocache, + 'run': run, 'ui': ui}) + shell = IPShellQt4(argv=[], user_ns=ns) + ip = ipapi.IPApi(shell.IP) + ip.expose_magic('hg', _runhgcommand) + ip.expose_magic('thg', _runthgcommand) + shell.mainloop(banner=_banner()) + +def _hookqtrun(ui): + """Hook qtrun() at dlgfunc() to hijack exec_()""" + run.qtrun._mainapp.exec_ = lambda: _execipyshell(ui) + +def main(): + os.environ['THGDEBUG'] = '1' + os.environ['THG_GUI_SPAWN'] = '1' + run.qtrun(_hookqtrun, ui.ui()) + +if __name__ == '__main__': + main()