comparison easyhg.py @ 158:006630304b69

* Some work on portable password prompting
author Chris Cannam
date Fri, 03 Dec 2010 12:29:24 +0000
parents
children 98fa31128e9d
comparison
equal deleted inserted replaced
157:e411bb42d934 158:006630304b69
1 # -*- coding: utf-8 -*-
2 #
3 # EasyMercurial
4 #
5 # Based on hgExplorer by Jari Korhonen
6 # Copyright (c) 2010 Jari Korhonen
7 # Copyright (c) 2010 Chris Cannam
8 # Copyright (c) 2010 Queen Mary, University of London
9 #
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License as
12 # published by the Free Software Foundation; either version 2 of the
13 # License, or (at your option) any later version. See the file
14 # COPYING included with this distribution for more information.
15
16 import sys
17 from mercurial import ui, getpass, util
18 from mercurial.i18n import _
19 from PyQt4 import QtGui
20
21 # This is a gross hack throughout
22
23 easyhg_qtapp = None
24 easyhg_userprompt = ''
25 easyhg_pwdprompt = ''
26
27 def uisetup(ui):
28 ui.__class__.write = easyhg_write
29 ui.__class__.prompt = easyhg_prompt
30 ui.__class__.getpass = easyhg_getpass
31 global easyhg_qtapp, easyhg_pwdprompt, easyhg_userprompt
32 easyhg_qtapp = QtGui.QApplication([])
33 easyhg_pwdprompt = ''
34 easyhg_userprompt = ''
35
36 def easyhg_write(self, *args, **opts):
37 global easyhg_pwdprompt
38 global easyhg_userprompt
39 for a in args:
40 (pfx, div, sfx) = a.partition(': ');
41 if pfx == 'realm' and sfx != '':
42 easyhg_userprompt = easyhg_pwdprompt = '<qt>' + sfx + '<br>';
43 elif pfx == 'user' and sfx != '':
44 easyhg_pwdprompt += _('Password for user') + ' <b>' + sfx + '</b>:';
45 sys.stdout.write(str(a))
46
47 def easyhg_prompt(self, msg, default="y"):
48 if not self.interactive():
49 self.write(msg, ' ', default, "\n")
50 return default
51 if msg == _('user:'):
52 msg = _('User:')
53 global easyhg_userprompt, easyhg_pwdprompt
54 if easyhg_userprompt != '':
55 msg = easyhg_userprompt + msg;
56 (r,ok) = QtGui.QInputDialog.getText(None, _('Question'),
57 msg, QtGui.QLineEdit.Normal)
58 if not ok:
59 raise util.Abort(_('response expected'))
60 if not r:
61 easyhg_pwdprompt += _('Password:');
62 return default
63 easyhg_pwdprompt += _('Password for user') + ' <b>' + r + '</b>:';
64 return r
65
66 def easyhg_getpass(self, prompt=None, default=None):
67 if not self.interactive():
68 return default
69 global easyhg_pwdprompt
70 if easyhg_pwdprompt != '':
71 msg = easyhg_pwdprompt
72 else:
73 msg = _('Password:');
74 (r,ok) = QtGui.QInputDialog.getText(None, _('Password'), msg,
75 QtGui.QLineEdit.Password)
76 if not ok:
77 raise util.Abort(_('response expected'))
78 if not r:
79 return default
80 return r
81