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