2002-07-11 06:38:14 +01:00
|
|
|
# TTY UI
|
|
|
|
# Copyright (C) 2002 John Goerzen
|
|
|
|
# <jgoerzen@complete.org>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
2003-04-16 20:23:45 +01:00
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
2002-07-11 06:38:14 +01:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
2006-08-12 05:15:55 +01:00
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2002-07-11 06:38:14 +01:00
|
|
|
|
2002-06-21 06:42:39 +01:00
|
|
|
from UIBase import UIBase
|
2002-06-21 06:29:12 +01:00
|
|
|
from getpass import getpass
|
2002-06-21 08:25:24 +01:00
|
|
|
import select, sys
|
2002-07-05 03:34:39 +01:00
|
|
|
from threading import *
|
2002-06-21 06:29:12 +01:00
|
|
|
|
2002-06-21 06:42:39 +01:00
|
|
|
class TTYUI(UIBase):
|
2002-07-25 00:15:30 +01:00
|
|
|
def __init__(s, config, verbose = 0):
|
2002-08-08 21:03:36 +01:00
|
|
|
UIBase.__init__(s, config, verbose)
|
2002-07-22 06:38:06 +01:00
|
|
|
s.iswaiting = 0
|
2002-11-05 00:15:42 +01:00
|
|
|
s.outputlock = Lock()
|
2002-07-25 00:13:09 +01:00
|
|
|
|
|
|
|
def isusable(s):
|
|
|
|
return sys.stdout.isatty() and sys.stdin.isatty()
|
2002-06-21 06:42:39 +01:00
|
|
|
|
2003-06-02 20:06:18 +01:00
|
|
|
def _display(s, msg):
|
2002-11-05 00:15:42 +01:00
|
|
|
s.outputlock.acquire()
|
|
|
|
try:
|
|
|
|
if (currentThread().getName() == 'MainThread'):
|
|
|
|
print msg
|
|
|
|
else:
|
|
|
|
print "%s:\n %s" % (currentThread().getName(), msg)
|
|
|
|
sys.stdout.flush()
|
|
|
|
finally:
|
|
|
|
s.outputlock.release()
|
2002-06-21 06:29:12 +01:00
|
|
|
|
2002-11-05 00:15:42 +01:00
|
|
|
def getpass(s, accountname, config, errmsg = None):
|
|
|
|
if errmsg:
|
|
|
|
s._msg("%s: %s" % (accountname, errmsg))
|
|
|
|
s.outputlock.acquire()
|
|
|
|
try:
|
2003-04-18 03:43:54 +01:00
|
|
|
return getpass("%s: Enter password: " % accountname)
|
2002-11-05 00:15:42 +01:00
|
|
|
finally:
|
|
|
|
s.outputlock.release()
|
2002-06-21 06:42:39 +01:00
|
|
|
|
2002-07-05 03:34:39 +01:00
|
|
|
def mainException(s):
|
|
|
|
if isinstance(sys.exc_info()[1], KeyboardInterrupt) and \
|
|
|
|
s.iswaiting:
|
2002-07-03 07:50:31 +01:00
|
|
|
sys.stdout.write("Timer interrupted at user request; program terminating. \n")
|
2002-07-05 03:34:39 +01:00
|
|
|
s.terminate()
|
|
|
|
else:
|
|
|
|
UIBase.mainException(s)
|
2002-07-03 07:50:31 +01:00
|
|
|
|