Blame lang/python/src/callbacks.py

Packit d7e8d0
# Copyright (C) 2004 Igor Belyi <belyi@users.sourceforge.net>
Packit d7e8d0
# Copyright (C) 2002 John Goerzen <jgoerzen@complete.org>
Packit d7e8d0
#
Packit d7e8d0
#    This library is free software; you can redistribute it and/or
Packit d7e8d0
#    modify it under the terms of the GNU Lesser General Public
Packit d7e8d0
#    License as published by the Free Software Foundation; either
Packit d7e8d0
#    version 2.1 of the License, or (at your option) any later version.
Packit d7e8d0
#
Packit d7e8d0
#    This library is distributed in the hope that it will be useful,
Packit d7e8d0
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit d7e8d0
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit d7e8d0
#    Lesser General Public License for more details.
Packit d7e8d0
#
Packit d7e8d0
#    You should have received a copy of the GNU Lesser General Public
Packit d7e8d0
#    License along with this library; if not, write to the Free Software
Packit d7e8d0
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
Packit d7e8d0
Packit d7e8d0
from __future__ import absolute_import, print_function, unicode_literals
Packit d7e8d0
del absolute_import, print_function, unicode_literals
Packit d7e8d0
Packit d7e8d0
from getpass import getpass
Packit d7e8d0
Packit d7e8d0
def passphrase_stdin(hint, desc, prev_bad, hook=None):
Packit d7e8d0
    """This is a sample callback that will read a passphrase from
Packit d7e8d0
    the terminal.  The hook here, if present, will be used to describe
Packit d7e8d0
    why the passphrase is needed."""
Packit d7e8d0
    why = ''
Packit d7e8d0
    if hook != None:
Packit d7e8d0
        why = ' ' + hook
Packit d7e8d0
    if prev_bad:
Packit d7e8d0
        why += ' (again)'
Packit d7e8d0
    print("Please supply %s' password%s:" % (hint, why))
Packit d7e8d0
    return getpass()
Packit d7e8d0
Packit d7e8d0
def progress_stdout(what, type, current, total, hook=None):
Packit d7e8d0
    print("PROGRESS UPDATE: what = %s, type = %d, current = %d, total = %d" %\
Packit d7e8d0
          (what, type, current, total))
Packit d7e8d0
Packit d7e8d0
def readcb_fh(count, hook):
Packit d7e8d0
    """A callback for data.  hook should be a Python file-like object."""
Packit d7e8d0
    if count:
Packit d7e8d0
        # Should return '' on EOF
Packit d7e8d0
        return hook.read(count)
Packit d7e8d0
    else:
Packit d7e8d0
        # Wants to rewind.
Packit d7e8d0
        if not hasattr(hook, 'seek'):
Packit d7e8d0
            return None
Packit d7e8d0
        hook.seek(0, 0)
Packit d7e8d0
        return None