docker-offlineimap/offlineimap/ui/TTY.py

68 lines
2.4 KiB
Python
Raw Normal View History

# 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
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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-06-21 07:42:39 +02:00
from UIBase import UIBase
2002-06-21 07:29:12 +02:00
from getpass import getpass
import sys
from threading import Lock, currentThread
2002-06-21 07:29:12 +02:00
2002-06-21 07:42:39 +02:00
class TTYUI(UIBase):
def __init__(s, config, verbose = 0):
UIBase.__init__(s, config, verbose)
s.iswaiting = 0
s.outputlock = Lock()
s._lastThreaddisplay = None
def isusable(s):
return sys.stdout.isatty() and sys.stdin.isatty()
2002-06-21 07:42:39 +02:00
def _display(s, msg):
s.outputlock.acquire()
try:
#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().getName()
if (threadname == s._lastThreaddisplay \
or threadname == 'MainThread'):
print " %s" % msg
else:
print "%s:\n %s" % (threadname, msg)
s._lastThreaddisplay = threadname
sys.stdout.flush()
finally:
s.outputlock.release()
2002-06-21 07:29:12 +02:00
def getpass(s, accountname, config, errmsg = None):
if errmsg:
s._msg("%s: %s" % (accountname, errmsg))
s.outputlock.acquire()
try:
return getpass("%s: Enter password: " % accountname)
finally:
s.outputlock.release()
2002-06-21 07:42:39 +02:00
def mainException(s):
if isinstance(sys.exc_info()[1], KeyboardInterrupt) and \
s.iswaiting:
sys.stdout.write("Timer interrupted at user request; program terminating. \n")
s.terminate()
else:
UIBase.mainException(s)