Blame lang/python/examples/inter-edit.py

Packit Service 672cf4
#!/usr/bin/env python
Packit Service 672cf4
#
Packit Service 672cf4
# Copyright (C) 2016 g10 Code GmbH
Packit Service 672cf4
# Copyright (C) 2005 Igor Belyi <belyi@users.sourceforge.net>
Packit Service 672cf4
#
Packit Service 672cf4
# This program is free software; you can redistribute it and/or modify
Packit Service 672cf4
# it under the terms of the GNU General Public License as published by
Packit Service 672cf4
# the Free Software Foundation; either version 2 of the License, or
Packit Service 672cf4
# (at your option) any later version.
Packit Service 672cf4
#
Packit Service 672cf4
# This program is distributed in the hope that it will be useful, but
Packit Service 672cf4
# WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 672cf4
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 672cf4
# General Public License for more details.
Packit Service 672cf4
#
Packit Service 672cf4
# You should have received a copy of the GNU General Public License
Packit Service 6c01f9
# along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit Service 6c01f9
Packit Service 672cf4
"""Simple interactive editor to test editor scripts"""
Packit Service 672cf4
Packit Service 672cf4
from __future__ import absolute_import, print_function, unicode_literals
Packit Service 6c01f9
del absolute_import, print_function, unicode_literals
Packit Service 672cf4
Packit Service 672cf4
import sys
Packit Service 672cf4
import gpg
Packit Service 672cf4
Packit Service 672cf4
if len(sys.argv) != 2:
Packit Service 672cf4
    sys.exit("Usage: %s <Gpg key pattern>\n" % sys.argv[0])
Packit Service 672cf4
Packit Service 672cf4
name = sys.argv[1]
Packit Service 672cf4
Packit Service 672cf4
with gpg.Context() as c:
Packit Service 672cf4
    keys = list(c.keylist(name))
Packit Service 672cf4
    if len(keys) == 0:
Packit Service 672cf4
        sys.exit("No key matching {}.".format(name))
Packit Service 672cf4
    if len(keys) > 1:
Packit Service 672cf4
        sys.exit("More than one key matching {}.".format(name))
Packit Service 672cf4
Packit Service 672cf4
    key = keys[0]
Packit Service 672cf4
    print("Editing key {} ({}):".format(key.uids[0].uid, key.subkeys[0].fpr))
Packit Service 672cf4
Packit Service 672cf4
    def edit_fnc(keyword, args):
Packit Service 6c01f9
        print("Status: {}, args: {} > ".format(
Packit Service 6c01f9
            keyword, args), end='', flush=True)
Packit Service 672cf4
Packit Service 6c01f9
        if not 'GET' in keyword:
Packit Service 672cf4
            # no prompt
Packit Service 672cf4
            print()
Packit Service 672cf4
            return None
Packit Service 672cf4
Packit Service 672cf4
        try:
Packit Service 672cf4
            return input()
Packit Service 672cf4
        except EOFError:
Packit Service 672cf4
            return "quit"
Packit Service 672cf4
Packit Service 672cf4
    c.interact(key, edit_fnc, sink=sys.stdout)