2002-07-11 07:38:14 +02: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 21:23:45 +02:00
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
2002-07-11 07:38:14 +02: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 06:15:55 +02:00
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2002-07-11 07:38:14 +02:00
|
|
|
|
2002-06-21 07:42:39 +02:00
|
|
|
from UIBase import UIBase
|
2002-06-21 07:29:12 +02:00
|
|
|
from getpass import getpass
|
2002-06-21 09:25:24 +02:00
|
|
|
import select, sys
|
2002-07-05 04:34:39 +02:00
|
|
|
from threading import *
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2002-06-21 07:42:39 +02:00
|
|
|
class TTYUI(UIBase):
|
2002-07-25 01:15:30 +02:00
|
|
|
def __init__(s, config, verbose = 0):
|
2002-08-08 22:03:36 +02:00
|
|
|
UIBase.__init__(s, config, verbose)
|
2002-07-22 07:38:06 +02:00
|
|
|
s.iswaiting = 0
|
2002-11-05 00:15:42 +01:00
|
|
|
s.outputlock = Lock()
|
2010-12-05 15:35:02 +01:00
|
|
|
s._lastThreaddisplay = None
|
2002-07-25 01:13:09 +02:00
|
|
|
|
|
|
|
def isusable(s):
|
|
|
|
return sys.stdout.isatty() and sys.stdin.isatty()
|
2002-06-21 07:42:39 +02:00
|
|
|
|
2003-06-02 21:06:18 +02:00
|
|
|
def _display(s, msg):
|
2002-11-05 00:15:42 +01:00
|
|
|
s.outputlock.acquire()
|
|
|
|
try:
|
2010-12-05 15:35:02 +01:00
|
|
|
#if the next output comes from a different thread than our last one
|
|
|
|
#add the info.
|
|
|
|
#Most look like 'account sync foo' or 'Folder sync foo'.
|
|
|
|
threadname = currentThread().name
|
|
|
|
if (threadname == s._lastThreaddisplay):
|
|
|
|
print " %s" % msg
|
2002-11-05 00:15:42 +01:00
|
|
|
else:
|
2010-12-05 15:35:02 +01:00
|
|
|
print "%s:\n %s" % (threadname, msg)
|
|
|
|
s._lastThreaddisplay = threadname
|
|
|
|
|
2002-11-05 00:15:42 +01:00
|
|
|
sys.stdout.flush()
|
|
|
|
finally:
|
|
|
|
s.outputlock.release()
|
2002-06-21 07:29:12 +02: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 04:43:54 +02:00
|
|
|
return getpass("%s: Enter password: " % accountname)
|
2002-11-05 00:15:42 +01:00
|
|
|
finally:
|
|
|
|
s.outputlock.release()
|
2002-06-21 07:42:39 +02:00
|
|
|
|
2002-07-05 04:34:39 +02:00
|
|
|
def mainException(s):
|
|
|
|
if isinstance(sys.exc_info()[1], KeyboardInterrupt) and \
|
|
|
|
s.iswaiting:
|
2002-07-03 08:50:31 +02:00
|
|
|
sys.stdout.write("Timer interrupted at user request; program terminating. \n")
|
2002-07-05 04:34:39 +02:00
|
|
|
s.terminate()
|
|
|
|
else:
|
|
|
|
UIBase.mainException(s)
|
2002-07-03 08:50:31 +02:00
|
|
|
|