]> code.delx.au - offlineimap/blob - offlineimap/head/offlineimap/ui/TTY.py
/offlineimap/head: changeset 360
[offlineimap] / offlineimap / head / offlineimap / ui / TTY.py
1 # TTY UI
2 # Copyright (C) 2002 John Goerzen
3 # <jgoerzen@complete.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; version 2 of the License.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18 from UIBase import UIBase
19 from getpass import getpass
20 import select, sys
21 from threading import *
22
23 class TTYUI(UIBase):
24 def __init__(s, config, verbose = 0):
25 UIBase.__init__(s, config, verbose)
26 s.iswaiting = 0
27 s.outputlock = Lock()
28
29 def isusable(s):
30 return sys.stdout.isatty() and sys.stdin.isatty()
31
32 def _msg(s, msg):
33 s.outputlock.acquire()
34 try:
35 if (currentThread().getName() == 'MainThread'):
36 print msg
37 else:
38 print "%s:\n %s" % (currentThread().getName(), msg)
39 sys.stdout.flush()
40 finally:
41 s.outputlock.release()
42
43 def getpass(s, accountname, config, errmsg = None):
44 if errmsg:
45 s._msg("%s: %s" % (accountname, errmsg))
46 s.outputlock.acquire()
47 try:
48 return getpass("%s: Enter password for %s on %s: " %
49 (accountname, config.get(accountname, "remoteuser"),
50 config.get(accountname, "remotehost")))
51 finally:
52 s.outputlock.release()
53
54 def mainException(s):
55 if isinstance(sys.exc_info()[1], KeyboardInterrupt) and \
56 s.iswaiting:
57 sys.stdout.write("Timer interrupted at user request; program terminating. \n")
58 s.terminate()
59 else:
60 UIBase.mainException(s)
61