]> code.delx.au - offlineimap/blob - offlineimap/ui/TTY.py
Update FSF address
[offlineimap] / 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; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 from UIBase import UIBase
20 from getpass import getpass
21 import select, sys
22 from threading import *
23
24 class TTYUI(UIBase):
25 def __init__(s, config, verbose = 0):
26 UIBase.__init__(s, config, verbose)
27 s.iswaiting = 0
28 s.outputlock = Lock()
29
30 def isusable(s):
31 return sys.stdout.isatty() and sys.stdin.isatty()
32
33 def _display(s, msg):
34 s.outputlock.acquire()
35 try:
36 if (currentThread().getName() == 'MainThread'):
37 print msg
38 else:
39 print "%s:\n %s" % (currentThread().getName(), msg)
40 sys.stdout.flush()
41 finally:
42 s.outputlock.release()
43
44 def getpass(s, accountname, config, errmsg = None):
45 if errmsg:
46 s._msg("%s: %s" % (accountname, errmsg))
47 s.outputlock.acquire()
48 try:
49 return getpass("%s: Enter password: " % accountname)
50 finally:
51 s.outputlock.release()
52
53 def mainException(s):
54 if isinstance(sys.exc_info()[1], KeyboardInterrupt) and \
55 s.iswaiting:
56 sys.stdout.write("Timer interrupted at user request; program terminating. \n")
57 s.terminate()
58 else:
59 UIBase.mainException(s)
60