Blame lang/python/tests/t-edit.py

Packit Service 672cf4
#!/usr/bin/env python
Packit Service 672cf4
Packit Service 672cf4
# Copyright (C) 2005 Igor Belyi <belyi@users.sourceforge.net>
Packit Service 672cf4
# Copyright (C) 2016 g10 Code GmbH
Packit Service 672cf4
#
Packit Service 672cf4
# This file is part of GPGME.
Packit Service 672cf4
#
Packit Service 672cf4
# GPGME is free software; you can redistribute it and/or modify it
Packit Service 672cf4
# 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
# GPGME is distributed in the hope that it will be useful, but WITHOUT
Packit Service 672cf4
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit Service 672cf4
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
Packit Service 672cf4
# Public License for more details.
Packit Service 672cf4
#
Packit Service 672cf4
# You should have received a copy of the GNU Lesser General Public
Packit Service 6c01f9
# License along with this program; if not, see <http://www.gnu.org/licenses/>.
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 os
Packit Service 672cf4
import gpg
Packit Service 672cf4
import support
Packit Service 6c01f9
_ = support # to appease pyflakes.
Packit Service 672cf4
Packit Service 672cf4
class KeyEditor(object):
Packit Service 672cf4
    def __init__(self):
Packit Service 672cf4
        self.steps = ["fpr", "expire", "1", "primary", "quit"]
Packit Service 672cf4
        self.step = 0
Packit Service 672cf4
        self.done = False
Packit Service 672cf4
        self.verbose = int(os.environ.get('verbose', 0)) > 1
Packit Service 672cf4
Packit Service 672cf4
    def edit_fnc(self, status, args, out=None):
Packit Service 672cf4
        if args == "keyedit.prompt":
Packit Service 672cf4
            result = self.steps[self.step]
Packit Service 672cf4
            self.step += 1
Packit Service 672cf4
        elif args == "keyedit.save.okay":
Packit Service 672cf4
            result = "Y"
Packit Service 672cf4
            self.done = self.step == len(self.steps)
Packit Service 672cf4
        elif args == "keygen.valid":
Packit Service 672cf4
            result = "0"
Packit Service 672cf4
        else:
Packit Service 672cf4
            result = None
Packit Service 672cf4
Packit Service 672cf4
        if self.verbose:
Packit Service 6c01f9
            sys.stderr.write("Code: {}, args: {!r}, Returning: {!r}\n"
Packit Service 6c01f9
                             .format(status, args, result))
Packit Service 672cf4
Packit Service 672cf4
        return result
Packit Service 672cf4
Packit Service 672cf4
c = gpg.Context()
Packit Service 672cf4
c.set_pinentry_mode(gpg.constants.PINENTRY_MODE_LOOPBACK)
Packit Service 672cf4
c.set_passphrase_cb(lambda *args: "abc")
Packit Service 672cf4
c.set_armor(True)
Packit Service 672cf4
Packit Service 672cf4
# The deprecated interface.
Packit Service 672cf4
editor = KeyEditor()
Packit Service 6c01f9
c.interact(c.get_key("A0FF4590BB6122EDEF6E3C542D727CC768697734", False),
Packit Service 6c01f9
           editor.edit_fnc)
Packit Service 672cf4
assert editor.done
Packit Service 672cf4
Packit Service 672cf4
# The deprecated interface.
Packit Service 672cf4
sink = gpg.Data()
Packit Service 672cf4
editor = KeyEditor()
Packit Service 6c01f9
c.op_edit(c.get_key("A0FF4590BB6122EDEF6E3C542D727CC768697734", False),
Packit Service 6c01f9
          editor.edit_fnc, sink, sink)
Packit Service 672cf4
assert editor.done