From f4ba16b31ea282d0787a40be3f37b951584143a1 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Tue, 10 May 2016 13:19:26 +0200 Subject: python: Rename bindings. -- Signed-off-by: Justus Winter --- lang/python/examples/Examples.rst | 7 +++ lang/python/examples/delkey.py | 34 +++++++++++ lang/python/examples/encrypt-to-all.py | 68 ++++++++++++++++++++++ lang/python/examples/exportimport.py | 75 +++++++++++++++++++++++++ lang/python/examples/genkey.py | 45 +++++++++++++++ lang/python/examples/inter-edit.py | 57 +++++++++++++++++++ lang/python/examples/sign.py | 31 ++++++++++ lang/python/examples/signverify.py | 78 +++++++++++++++++++++++++ lang/python/examples/simple.py | 52 +++++++++++++++++ lang/python/examples/t-edit.py | 59 +++++++++++++++++++ lang/python/examples/testCMSgetkey.py | 49 ++++++++++++++++ lang/python/examples/verifydetails.py | 100 +++++++++++++++++++++++++++++++++ 12 files changed, 655 insertions(+) create mode 100644 lang/python/examples/Examples.rst create mode 100755 lang/python/examples/delkey.py create mode 100755 lang/python/examples/encrypt-to-all.py create mode 100755 lang/python/examples/exportimport.py create mode 100755 lang/python/examples/genkey.py create mode 100644 lang/python/examples/inter-edit.py create mode 100755 lang/python/examples/sign.py create mode 100755 lang/python/examples/signverify.py create mode 100755 lang/python/examples/simple.py create mode 100644 lang/python/examples/t-edit.py create mode 100644 lang/python/examples/testCMSgetkey.py create mode 100755 lang/python/examples/verifydetails.py (limited to 'lang/python/examples') diff --git a/lang/python/examples/Examples.rst b/lang/python/examples/Examples.rst new file mode 100644 index 00000000..18b03b27 --- /dev/null +++ b/lang/python/examples/Examples.rst @@ -0,0 +1,7 @@ +=============== +Example Scripts +=============== + +Most of the examples have been converted to work with Python 3, just as the original versions worked with Python 2. A small number produce errors on OS X, but may behave differently on other POSIX systems. The GTK based scripts (PyGtkGpgKeys.py and pygpa.py) have not been modified at all. + +When using or referring to the example scripts here, the most common change has been the byte encoded strings, so if something does not work then chances are that it will be related to the encoding or decoding of UTF-8. diff --git a/lang/python/examples/delkey.py b/lang/python/examples/delkey.py new file mode 100755 index 00000000..dfcc5ea4 --- /dev/null +++ b/lang/python/examples/delkey.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +# $Id$ +# Copyright (C) 2004,2008 Igor Belyi +# +# 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 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +# Sample of key deletion +# It deletes keys for joe@example.org generated by genkey.pl script + +from pyme import core + +core.check_version(None) + +# Note that we need to collect all keys out of the iterator return by c.op_keylist_all() +# method before starting to delete them. If you delete a key in the middle of iteration +# c.op_keylist_next() will raise INV_VALUE exception + +c = core.Context() +# 0 in keylist means to list not only public but secret keys as well. +for thekey in [x for x in c.op_keylist_all(b"joe@example.org", 0)]: + # 1 in delete means to delete not only public but secret keys as well. + c.op_delete(thekey, 1) diff --git a/lang/python/examples/encrypt-to-all.py b/lang/python/examples/encrypt-to-all.py new file mode 100755 index 00000000..8f9d2500 --- /dev/null +++ b/lang/python/examples/encrypt-to-all.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 +# $Id$ +# Copyright (C) 2008 Igor Belyi +# Copyright (C) 2002 John Goerzen +# +# 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 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +""" +This program will try to encrypt a simple message to each key on your +keyring. If your keyring has any invalid keys on it, those keys will +be skipped and it will re-try the encryption.""" + +from pyme import core +from pyme.core import Data, Context +from pyme.constants import validity + +core.check_version(None) + +plain = Data(b'This is my message.') + +c = Context() +c.set_armor(1) + +def sendto(keylist): + cipher = Data() + c.op_encrypt(keylist, 1, plain, cipher) + cipher.seek(0,0) + return cipher.read() + +names = [] +for key in c.op_keylist_all(None, 0): + try: + print(" *** Found key for %s" % key.uids[0].uid) + valid = 0 + for subkey in key.subkeys: + keyid = subkey.keyid + if keyid == None: + break + can_encrypt = subkey.can_encrypt + valid += can_encrypt + print(" Subkey %s: encryption %s" % \ + (keyid, can_encrypt and "enabled" or "disabled")) + except UnicodeEncodeError as e: + print(e) + + if valid: + names.append(key) + else: + print(" This key cannot be used for encryption; skipping.") + +passno = 0 + +print("Encrypting to %d recipients" % len(names)) +print(sendto(names)) + + diff --git a/lang/python/examples/exportimport.py b/lang/python/examples/exportimport.py new file mode 100755 index 00000000..54204fb7 --- /dev/null +++ b/lang/python/examples/exportimport.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python3 +# $Id$ +# Copyright (C) 2004,2008 Igor Belyi +# +# 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 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +# Sample of export and import of keys +# It uses keys for joe@example.org generated by genkey.pl script + +import sys +from pyme import core + +core.check_version(None) + +expkey = core.Data() +c = core.Context() +c.set_armor(1) +user = b"joe@example.org" + +print(" - Export %s's public keys - " % user) +c.op_export(user, 0, expkey) + +# print out exported data to see how it looks in armor. +expkey.seek(0,0) +expstring = expkey.read() +if expstring: + print(expstring) +else: + print("No %s's keys to export!" % user) + sys.exit(0) + + +# delete keys to ensure that they came from our imported data. +# Note that since joe's key has private part as well we can only delete +# both of them. As a side effect joe won't have private key for this +# exported public one. If it's Ok with you uncomment the next 4 lines. +#print " - Delete %s's public keys - " % user +#for thekey in [x for x in c.op_keylist_all(user, 0)]: +# if not thekey.secret: +# c.op_delete(thekey, 1) + + +# initialize import data from a string as if it was read from a file. +newkey = core.Data(expstring) + +print(" - Import exported keys - ") +c.op_import(newkey) +result = c.op_import_result() + +# show the import result +if result: + print(" - Result of the import - ") + for k in dir(result): + if not k in result.__dict__ and not k.startswith("_"): + if k == "imports": + print(k, ":") + for impkey in result.__getattr__(k): + print(" fpr=%s result=%d status=%x" % \ + (impkey.fpr, impkey.result, impkey.status)) + else: + print(k, ":", result.__getattr__(k)) +else: + print(" - No import result - ") diff --git a/lang/python/examples/genkey.py b/lang/python/examples/genkey.py new file mode 100755 index 00000000..14497eb7 --- /dev/null +++ b/lang/python/examples/genkey.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +# $Id$ +# Copyright (C) 2004 Igor Belyi +# Copyright (C) 2002 John Goerzen +# +# 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 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +from pyme import core, callbacks + +# Initialize our context. +core.check_version(None) + +c = core.Context() +c.set_armor(1) +c.set_progress_cb(callbacks.progress_stdout, None) + +# This example from the GPGME manual + +parms = b""" +Key-Type: RSA +Key-Length: 2048 +Subkey-Type: RSA +Subkey-Length: 2048 +Name-Real: Joe Tester +Name-Comment: with stupid passphrase +Name-Email: joe@example.org +Passphrase: Crypt0R0cks +Expire-Date: 2020-12-31 + +""" + +c.op_genkey(parms, None, None) +print(c.op_genkey_result().fpr) diff --git a/lang/python/examples/inter-edit.py b/lang/python/examples/inter-edit.py new file mode 100644 index 00000000..f97232b7 --- /dev/null +++ b/lang/python/examples/inter-edit.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 +# $Id$ +# Copyright (C) 2005 Igor Belyi +# +# 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 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import os, sys +from pyme import core +from pyme.core import Data, Context +from pyme.constants import status + +core.check_version(None) + +# Get names for the status codes +stat2str = {} +for name in dir(status): + if not name.startswith('__') and name != "util": + stat2str[getattr(status, name)] = name + +# Print the output received since the last prompt before giving the new prompt +def edit_fnc(stat, args, helper): + global stat_strings + try: + while True: + helper["data"].seek(helper["skip"],0) + data = helper["data"].read() + helper["skip"] += len(data) + print(data) + return input("(%s) %s > " % (stat2str[stat], args)) + except EOFError: + pass + +# Simple interactive editor to test editor scripts +if len(sys.argv) != 2: + sys.stderr.write("Usage: %s \n" % sys.argv[0]) +else: + c = Context() + out = Data() + c.op_keylist_start(sys.argv[1].encode('utf-8'), 0) + key = c.op_keylist_next() + helper = {"skip": 0, "data": out} + c.op_edit(key, edit_fnc, helper, out) + print("[-- Final output --]") + out.seek(helper["skip"],0) + print(out.read()) diff --git a/lang/python/examples/sign.py b/lang/python/examples/sign.py new file mode 100755 index 00000000..5667cc2b --- /dev/null +++ b/lang/python/examples/sign.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +# $Id$ +# Copyright (C) 2002 John Goerzen +# +# +# 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 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +from pyme import core, callbacks +from pyme.constants.sig import mode + +core.check_version(None) + +plain = core.Data(b"Test message") +sig = core.Data() +c = core.Context() +c.set_passphrase_cb(callbacks.passphrase_stdin, b'for signing') +c.op_sign(plain, sig, mode.CLEAR) +sig.seek(0,0) +print(sig.read()) diff --git a/lang/python/examples/signverify.py b/lang/python/examples/signverify.py new file mode 100755 index 00000000..f8804ca9 --- /dev/null +++ b/lang/python/examples/signverify.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python3 +# $Id$ +# Copyright (C) 2004,2008 Igor Belyi +# +# 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 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +# Sample of unattended signing/verifying of a message. +# It uses keys for joe@example.org generated by genkey.pl script + +import sys +from pyme import core, callbacks +from pyme.constants.sig import mode + +core.check_version(None) + +plain = core.Data(b"Test message") +sig = core.Data() +c = core.Context() +user = b"joe@example.org" + +c.signers_clear() +# Add joe@example.org's keys in the list of signers +for sigkey in c.op_keylist_all(user, 1): + if sigkey.can_sign: + c.signers_add(sigkey) +if not c.signers_enum(0): + print("No secret %s's keys suitable for signing!" % user) + sys.exit(0) + +# This is a map between signer e-mail and its password +passlist = { + b"": b"Crypt0R0cks" + } + +# callback will return password based on the e-mail listed in the hint. +c.set_passphrase_cb(lambda x,y,z: passlist[x[x.rindex("<"):]]) + +c.op_sign(plain, sig, mode.CLEAR) + +# Print out the signature (don't forget to rewind since signing put sig at EOF) +sig.seek(0,0) +signedtext = sig.read() +print(signedtext) + +# Create Data with signed text. +sig2 = core.Data(signedtext) +plain2 = core.Data() + +# Verify. +c.op_verify(sig2, None, plain2) +result = c.op_verify_result() + +# List results for all signatures. Status equal 0 means "Ok". +index = 0 +for sign in result.signatures: + index += 1 + print("signature", index, ":") + print(" summary: ", sign.summary) + print(" status: ", sign.status) + print(" timestamp: ", sign.timestamp) + print(" fingerprint:", sign.fpr) + print(" uid: ", c.get_key(sign.fpr, 0).uids[0].uid) + +# Print "unsigned" text. Rewind since verify put plain2 at EOF. +plain2.seek(0,0) +print("\n", plain2.read()) diff --git a/lang/python/examples/simple.py b/lang/python/examples/simple.py new file mode 100755 index 00000000..5693d40a --- /dev/null +++ b/lang/python/examples/simple.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +# $Id$ +# Copyright (C) 2005 Igor Belyi +# Copyright (C) 2002 John Goerzen +# +# 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 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import sys +from pyme import core, constants, errors +import pyme.constants.validity + +core.check_version(None) + +# Set up our input and output buffers. + +plain = core.Data(b'This is my message.') +cipher = core.Data() + +# Initialize our context. + +c = core.Context() +c.set_armor(1) + +# Set up the recipients. + +sys.stdout.write("Enter name of your recipient: ") +name = sys.stdin.readline().strip() +c.op_keylist_start(name, 0) +r = c.op_keylist_next() + +if r == None: + print("The key for user \"%s\" was not found" % name) +else: + # Do the encryption. + try: + c.op_encrypt([r], 1, plain, cipher) + cipher.seek(0,0) + print(cipher.read()) + except errors.GPGMEError as ex: + print(ex.getstring()) diff --git a/lang/python/examples/t-edit.py b/lang/python/examples/t-edit.py new file mode 100644 index 00000000..6c533429 --- /dev/null +++ b/lang/python/examples/t-edit.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 +# $Id$ +# Copyright (C) 2005 Igor Belyi +# +# 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 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import os +from pyme import core +from pyme.core import Data, Context + +core.check_version(None) + +class KeyEditor: + def __init__(self): + self.steps = ["fpr", "expire", "1", "primary", "quit"] + self.step = 0 + + def edit_fnc(self, status, args, out): + print("[-- Response --]") + out.seek(0,0) + print(out.read(), end=' ') + print("[-- Code: %d, %s --]" % (status, args)) + + if args == "keyedit.prompt": + result = self.steps[self.step] + self.step += 1 + elif args == "keyedit.save.okay": + result = "Y" + elif args == "keygen.valid": + result = "0" + else: + result = None + + return result + +if not os.getenv("GNUPGHOME"): + print("Please, set GNUPGHOME env.var. pointing to GPGME's tests/gpg dir") +else: + c = Context() + c.set_passphrase_cb(lambda x,y,z: "abc") + out = Data() + c.op_keylist_start(b"Alpha", 0) + key = c.op_keylist_next() + c.op_edit(key, KeyEditor().edit_fnc, out, out) + print("[-- Last response --]") + out.seek(0,0) + print(out.read(), end=' ') diff --git a/lang/python/examples/testCMSgetkey.py b/lang/python/examples/testCMSgetkey.py new file mode 100644 index 00000000..53fdef77 --- /dev/null +++ b/lang/python/examples/testCMSgetkey.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 +# initial 20080124 bernhard@intevation.de +# 20080124-2: removed some superflous imports +# 20080703: adapted for pyme-0.8.0 +# This script is Free Software under GNU GPL v>=2. +# +# No modification made for python3 port, Bernhard can field this one +# if it is still required. -- Ben McGinnes +# +"""A test applicaton for gpg_get_key() protocol.CMS. + +Tested on Debian Etch with + pyme 0.8.0 (manually compiled) + libgpgme11 1.1.6-0kk2 + gpgsm 2.0.9-0kk2 +""" + +import sys +from pyme import core +from pyme.constants import protocol + +def printgetkeyresults(keyfpr): + """Run gpgme_get_key().""" + + # gpgme_check_version() necessary for initialisation according to + # gogme 1.1.6 and this is not done automatically in pyme-0.7.0 + print("gpgme version:", core.check_version(None)) + c = core.Context() + c.set_protocol(protocol.CMS) + + key = c.get_key(keyfpr, False) + + print("got key: ", key.subkeys[0].fpr) + + for uid in key.uids: + print(uid.uid) + +def main(): + if len(sys.argv) < 2: + print("fingerprint or unique key ID for gpgme_get_key()") + sys.exit(1) + + printgetkeyresults(sys.argv[1]) + + +if __name__ == "__main__": + main() + + diff --git a/lang/python/examples/verifydetails.py b/lang/python/examples/verifydetails.py new file mode 100755 index 00000000..0d47ba54 --- /dev/null +++ b/lang/python/examples/verifydetails.py @@ -0,0 +1,100 @@ +#!/usr/bin/env python3 +# initial 20080123 build from the example: +# very simple - probably INCOMPLETE +# 20080703 Bernhard +# added second usage for detached signatures. +# added output of signature.summary (another bitfield) +# printing signature bitfield in hex format +# +# $Id$ +# +# Copyright (C) 2004,2008 Igor Belyi +# Copyright (c) 2008 Bernhard Reiter +# +# 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, see . + +import sys +from pyme import core, callbacks, constants +from pyme.constants.sig import mode +from pyme.constants import protocol + +def print_engine_infos(): + print("gpgme version:", core.check_version(None)) + print("engines:") + + for engine in core.get_engine_info(): + print(engine.file_name, engine.version) + + for proto in [protocol.OpenPGP, protocol.CMS]: + print(core.get_protocol_name(proto), core.engine_check_version(proto)) + + +def verifyprintdetails(sigfilename, filefilename=None): + """Verify a signature, print a lot of details.""" + c = core.Context() + + # Create Data with signed text. + sig2 = core.Data(file=sigfilename) + if filefilename: + file2 = core.Data(file=filefilename) + plain2 = None + else: + file2 = None + plain2 = core.Data() + + # Verify. + c.op_verify(sig2, file2, plain2) + result = c.op_verify_result() + + # List results for all signatures. Status equal 0 means "Ok". + index = 0 + for sign in result.signatures: + index += 1 + print("signature", index, ":") + print(" summary: %#0x" % (sign.summary)) + print(" status: %#0x" % (sign.status)) + print(" timestamp: ", sign.timestamp) + print(" fingerprint:", sign.fpr) + print(" uid: ", c.get_key(sign.fpr, 0).uids[0].uid) + + # Print "unsigned" text if inline signature + if plain2: + #Rewind since verify put plain2 at EOF. + plain2.seek(0,0) + print("\n", plain2.read()) + +def main(): + print_engine_infos() + + print() + + argc= len(sys.argv) + if argc < 2 or argc > 3: + print("need a filename for inline signature") + print("or two filename for detached signature and file to check") + sys.exit(1) + + if argc == 2: + print("trying to verify file: " + sys.argv[1].encode('utf-8')) + verifyprintdetails(sys.argv[1].encode('utf-8')) + if argc == 3: + print("trying to verify signature %s for file %s" \ + % (sys.argv[1].encode('utf-8'), sys.argv[2].encode('utf-8'))) + + verifyprintdetails(sys.argv[1].encode('utf-8'), sys.argv[2].encode('utf-8')) + +if __name__ == "__main__": + main() + + -- cgit v1.2.3 From aade53a12b9716997684b872bc2ac87229f73fb3 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Tue, 10 May 2016 13:30:30 +0200 Subject: python: Delete trailing whitespace. -- Signed-off-by: Justus Winter --- lang/python/examples/encrypt-to-all.py | 2 +- lang/python/examples/signverify.py | 2 +- lang/python/examples/t-edit.py | 2 +- lang/python/examples/testCMSgetkey.py | 4 +--- lang/python/examples/verifydetails.py | 2 -- 5 files changed, 4 insertions(+), 8 deletions(-) (limited to 'lang/python/examples') diff --git a/lang/python/examples/encrypt-to-all.py b/lang/python/examples/encrypt-to-all.py index 8f9d2500..c890df4f 100755 --- a/lang/python/examples/encrypt-to-all.py +++ b/lang/python/examples/encrypt-to-all.py @@ -54,7 +54,7 @@ for key in c.op_keylist_all(None, 0): (keyid, can_encrypt and "enabled" or "disabled")) except UnicodeEncodeError as e: print(e) - + if valid: names.append(key) else: diff --git a/lang/python/examples/signverify.py b/lang/python/examples/signverify.py index f8804ca9..7194157b 100755 --- a/lang/python/examples/signverify.py +++ b/lang/python/examples/signverify.py @@ -43,7 +43,7 @@ if not c.signers_enum(0): passlist = { b"": b"Crypt0R0cks" } - + # callback will return password based on the e-mail listed in the hint. c.set_passphrase_cb(lambda x,y,z: passlist[x[x.rindex("<"):]]) diff --git a/lang/python/examples/t-edit.py b/lang/python/examples/t-edit.py index 6c533429..5553190c 100644 --- a/lang/python/examples/t-edit.py +++ b/lang/python/examples/t-edit.py @@ -32,7 +32,7 @@ class KeyEditor: out.seek(0,0) print(out.read(), end=' ') print("[-- Code: %d, %s --]" % (status, args)) - + if args == "keyedit.prompt": result = self.steps[self.step] self.step += 1 diff --git a/lang/python/examples/testCMSgetkey.py b/lang/python/examples/testCMSgetkey.py index 53fdef77..7c953012 100644 --- a/lang/python/examples/testCMSgetkey.py +++ b/lang/python/examples/testCMSgetkey.py @@ -22,7 +22,7 @@ from pyme.constants import protocol def printgetkeyresults(keyfpr): """Run gpgme_get_key().""" - # gpgme_check_version() necessary for initialisation according to + # gpgme_check_version() necessary for initialisation according to # gogme 1.1.6 and this is not done automatically in pyme-0.7.0 print("gpgme version:", core.check_version(None)) c = core.Context() @@ -45,5 +45,3 @@ def main(): if __name__ == "__main__": main() - - diff --git a/lang/python/examples/verifydetails.py b/lang/python/examples/verifydetails.py index 0d47ba54..a8d840e7 100755 --- a/lang/python/examples/verifydetails.py +++ b/lang/python/examples/verifydetails.py @@ -96,5 +96,3 @@ def main(): if __name__ == "__main__": main() - - -- cgit v1.2.3 From 11392a80d9a85bcd8718b105e6d58038e61beaac Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Tue, 10 May 2016 14:45:44 +0200 Subject: python: PEP8 fixes. Cherry picked from 0267c151. Signed-off-by: Justus Winter --- lang/python/examples/delkey.py | 18 ++++++++++-------- lang/python/examples/encrypt-to-all.py | 17 ++++++++--------- lang/python/examples/exportimport.py | 25 +++++++++++++------------ lang/python/examples/inter-edit.py | 11 +++++++---- 4 files changed, 38 insertions(+), 33 deletions(-) (limited to 'lang/python/examples') diff --git a/lang/python/examples/delkey.py b/lang/python/examples/delkey.py index dfcc5ea4..600e0c07 100755 --- a/lang/python/examples/delkey.py +++ b/lang/python/examples/delkey.py @@ -2,10 +2,10 @@ # $Id$ # Copyright (C) 2004,2008 Igor Belyi # -# 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 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 @@ -14,7 +14,8 @@ # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +# 02111-1307 USA # Sample of key deletion # It deletes keys for joe@example.org generated by genkey.pl script @@ -23,9 +24,10 @@ from pyme import core core.check_version(None) -# Note that we need to collect all keys out of the iterator return by c.op_keylist_all() -# method before starting to delete them. If you delete a key in the middle of iteration -# c.op_keylist_next() will raise INV_VALUE exception +# Note that we need to collect all keys out of the iterator return by +# c.op_keylist_all() method before starting to delete them. If you +# delete a key in the middle of iteration c.op_keylist_next() will +# raise INV_VALUE exception c = core.Context() # 0 in keylist means to list not only public but secret keys as well. diff --git a/lang/python/examples/encrypt-to-all.py b/lang/python/examples/encrypt-to-all.py index c890df4f..672e6614 100755 --- a/lang/python/examples/encrypt-to-all.py +++ b/lang/python/examples/encrypt-to-all.py @@ -3,10 +3,10 @@ # Copyright (C) 2008 Igor Belyi # Copyright (C) 2002 John Goerzen # -# 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 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 @@ -15,7 +15,8 @@ # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +# 02111-1307 USA """ This program will try to encrypt a simple message to each key on your @@ -46,11 +47,11 @@ for key in c.op_keylist_all(None, 0): valid = 0 for subkey in key.subkeys: keyid = subkey.keyid - if keyid == None: + if keyid is None: break can_encrypt = subkey.can_encrypt valid += can_encrypt - print(" Subkey %s: encryption %s" % \ + print(" Subkey %s: encryption %s" % (keyid, can_encrypt and "enabled" or "disabled")) except UnicodeEncodeError as e: print(e) @@ -64,5 +65,3 @@ passno = 0 print("Encrypting to %d recipients" % len(names)) print(sendto(names)) - - diff --git a/lang/python/examples/exportimport.py b/lang/python/examples/exportimport.py index 54204fb7..6c7d5b8f 100755 --- a/lang/python/examples/exportimport.py +++ b/lang/python/examples/exportimport.py @@ -2,10 +2,10 @@ # $Id$ # Copyright (C) 2004,2008 Igor Belyi # -# 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 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 @@ -14,7 +14,8 @@ # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +# 02111-1307 USA # Sample of export and import of keys # It uses keys for joe@example.org generated by genkey.pl script @@ -33,7 +34,7 @@ print(" - Export %s's public keys - " % user) c.op_export(user, 0, expkey) # print out exported data to see how it looks in armor. -expkey.seek(0,0) +expkey.seek(0, 0) expstring = expkey.read() if expstring: print(expstring) @@ -46,10 +47,10 @@ else: # Note that since joe's key has private part as well we can only delete # both of them. As a side effect joe won't have private key for this # exported public one. If it's Ok with you uncomment the next 4 lines. -#print " - Delete %s's public keys - " % user -#for thekey in [x for x in c.op_keylist_all(user, 0)]: -# if not thekey.secret: -# c.op_delete(thekey, 1) +# print " - Delete %s's public keys - " % user +# for thekey in [x for x in c.op_keylist_all(user, 0)]: +# if not thekey.secret: +# c.op_delete(thekey, 1) # initialize import data from a string as if it was read from a file. @@ -63,11 +64,11 @@ result = c.op_import_result() if result: print(" - Result of the import - ") for k in dir(result): - if not k in result.__dict__ and not k.startswith("_"): + if k not in result.__dict__ and not k.startswith("_"): if k == "imports": print(k, ":") for impkey in result.__getattr__(k): - print(" fpr=%s result=%d status=%x" % \ + print(" fpr=%s result=%d status=%x" % (impkey.fpr, impkey.result, impkey.status)) else: print(k, ":", result.__getattr__(k)) diff --git a/lang/python/examples/inter-edit.py b/lang/python/examples/inter-edit.py index f97232b7..32f8edd9 100644 --- a/lang/python/examples/inter-edit.py +++ b/lang/python/examples/inter-edit.py @@ -14,9 +14,11 @@ # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +# 02111-1307 USA -import os, sys +import os +import sys from pyme import core from pyme.core import Data, Context from pyme.constants import status @@ -29,12 +31,13 @@ for name in dir(status): if not name.startswith('__') and name != "util": stat2str[getattr(status, name)] = name + # Print the output received since the last prompt before giving the new prompt def edit_fnc(stat, args, helper): global stat_strings try: while True: - helper["data"].seek(helper["skip"],0) + helper["data"].seek(helper["skip"], 0) data = helper["data"].read() helper["skip"] += len(data) print(data) @@ -53,5 +56,5 @@ else: helper = {"skip": 0, "data": out} c.op_edit(key, edit_fnc, helper, out) print("[-- Final output --]") - out.seek(helper["skip"],0) + out.seek(helper["skip"], 0) print(out.read()) -- cgit v1.2.3 From bbeee5e1a060f2d1e37a08220eb552cf4673a058 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Wed, 11 May 2016 13:51:40 +0200 Subject: python: Fix simple example. * lang/python/examples/simple.py: Flush stdout, encode name as UTF-8 before passing it to GPGME. Signed-off-by: Justus Winter --- lang/python/examples/simple.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lang/python/examples') diff --git a/lang/python/examples/simple.py b/lang/python/examples/simple.py index 5693d40a..739291ed 100755 --- a/lang/python/examples/simple.py +++ b/lang/python/examples/simple.py @@ -36,8 +36,9 @@ c.set_armor(1) # Set up the recipients. sys.stdout.write("Enter name of your recipient: ") +sys.stdout.flush() name = sys.stdin.readline().strip() -c.op_keylist_start(name, 0) +c.op_keylist_start(name.encode(), 0) r = c.op_keylist_next() if r == None: -- cgit v1.2.3 From d60deb8a127fb35c01acc729f33b014840af0e7b Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Thu, 12 May 2016 11:21:58 +0200 Subject: python: Fix type translation. * lang/python/gpgme.i: Adjust to Python3's string type being 'Unicode', not 'bytes'. Fix type checking. * lang/python/core.py (Data.write): Add docstring mentioning the expected type of parameter 'buffer'. (Data.read): Adjust read loop. Also, use a saner chunk size, and join all chunks at the end instead of adding them. * lang/python/examples/simple.py: Adjust example. Signed-off-by: Justus Winter --- lang/python/examples/simple.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lang/python/examples') diff --git a/lang/python/examples/simple.py b/lang/python/examples/simple.py index 739291ed..4ff6d285 100755 --- a/lang/python/examples/simple.py +++ b/lang/python/examples/simple.py @@ -25,7 +25,7 @@ core.check_version(None) # Set up our input and output buffers. -plain = core.Data(b'This is my message.') +plain = core.Data('This is my message.') cipher = core.Data() # Initialize our context. @@ -38,7 +38,7 @@ c.set_armor(1) sys.stdout.write("Enter name of your recipient: ") sys.stdout.flush() name = sys.stdin.readline().strip() -c.op_keylist_start(name.encode(), 0) +c.op_keylist_start(name, 0) r = c.op_keylist_next() if r == None: @@ -48,6 +48,6 @@ else: try: c.op_encrypt([r], 1, plain, cipher) cipher.seek(0,0) - print(cipher.read()) + sys.stdout.buffer.write(cipher.read()) except errors.GPGMEError as ex: print(ex.getstring()) -- cgit v1.2.3 From c89d3a71ad20ff02755539a44f254b1e59054c4a Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Thu, 12 May 2016 11:51:21 +0200 Subject: python: Make test case more robust. * lang/python/examples/t-edit.py: Check if key is found. Signed-off-by: Justus Winter --- lang/python/examples/t-edit.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lang/python/examples') diff --git a/lang/python/examples/t-edit.py b/lang/python/examples/t-edit.py index 5553190c..5e5857a2 100644 --- a/lang/python/examples/t-edit.py +++ b/lang/python/examples/t-edit.py @@ -16,6 +16,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +import sys import os from pyme import core from pyme.core import Data, Context @@ -53,6 +54,9 @@ else: out = Data() c.op_keylist_start(b"Alpha", 0) key = c.op_keylist_next() + if not key: + sys.exit("Key Alpha not found. " + + "Did you point GNUPGHOME to GPGME's tests/gpg dir?") c.op_edit(key, KeyEditor().edit_fnc, out, out) print("[-- Last response --]") out.seek(0,0) -- cgit v1.2.3 From 10328324c8fc9725cd0c885eaebfc80dc32c1ff6 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Tue, 17 May 2016 14:15:21 +0200 Subject: python: Clean up examples. * lang/python/examples/delkey.py: Clean up example. * lang/python/examples/encrypt-to-all.py: Likewise. * lang/python/examples/genkey.py: Likewise. * lang/python/examples/inter-edit.py: Likewise. * lang/python/examples/sign.py: Likewise. * lang/python/examples/signverify.py: Likewise. * lang/python/examples/simple.py: Likewise. * lang/python/examples/t-edit.py: Likewise. * lang/python/examples/verifydetails.py: Likewise. * lang/python/pyme/__init__.py: Likewise. Signed-off-by: Justus Winter --- lang/python/examples/delkey.py | 2 +- lang/python/examples/encrypt-to-all.py | 9 +++++---- lang/python/examples/genkey.py | 4 ++-- lang/python/examples/inter-edit.py | 9 ++++----- lang/python/examples/sign.py | 6 ++++-- lang/python/examples/signverify.py | 18 +++++++++--------- lang/python/examples/simple.py | 6 +++--- lang/python/examples/t-edit.py | 8 ++++---- lang/python/examples/verifydetails.py | 21 ++++++++++----------- 9 files changed, 42 insertions(+), 41 deletions(-) (limited to 'lang/python/examples') diff --git a/lang/python/examples/delkey.py b/lang/python/examples/delkey.py index 600e0c07..773b2623 100755 --- a/lang/python/examples/delkey.py +++ b/lang/python/examples/delkey.py @@ -31,6 +31,6 @@ core.check_version(None) c = core.Context() # 0 in keylist means to list not only public but secret keys as well. -for thekey in [x for x in c.op_keylist_all(b"joe@example.org", 0)]: +for thekey in [x for x in c.op_keylist_all("joe+pyme@example.org", 0)]: # 1 in delete means to delete not only public but secret keys as well. c.op_delete(thekey, 1) diff --git a/lang/python/examples/encrypt-to-all.py b/lang/python/examples/encrypt-to-all.py index 672e6614..331933e4 100755 --- a/lang/python/examples/encrypt-to-all.py +++ b/lang/python/examples/encrypt-to-all.py @@ -23,13 +23,14 @@ This program will try to encrypt a simple message to each key on your keyring. If your keyring has any invalid keys on it, those keys will be skipped and it will re-try the encryption.""" +import sys +import os from pyme import core from pyme.core import Data, Context -from pyme.constants import validity core.check_version(None) -plain = Data(b'This is my message.') +plain = Data('This is my message.') c = Context() c.set_armor(1) @@ -37,7 +38,7 @@ c.set_armor(1) def sendto(keylist): cipher = Data() c.op_encrypt(keylist, 1, plain, cipher) - cipher.seek(0,0) + cipher.seek(0, os.SEEK_SET) return cipher.read() names = [] @@ -64,4 +65,4 @@ for key in c.op_keylist_all(None, 0): passno = 0 print("Encrypting to %d recipients" % len(names)) -print(sendto(names)) +sys.stdout.buffer.write(sendto(names)) diff --git a/lang/python/examples/genkey.py b/lang/python/examples/genkey.py index 14497eb7..bc708339 100755 --- a/lang/python/examples/genkey.py +++ b/lang/python/examples/genkey.py @@ -28,14 +28,14 @@ c.set_progress_cb(callbacks.progress_stdout, None) # This example from the GPGME manual -parms = b""" +parms = """ Key-Type: RSA Key-Length: 2048 Subkey-Type: RSA Subkey-Length: 2048 Name-Real: Joe Tester Name-Comment: with stupid passphrase -Name-Email: joe@example.org +Name-Email: joe+pyme@example.org Passphrase: Crypt0R0cks Expire-Date: 2020-12-31 diff --git a/lang/python/examples/inter-edit.py b/lang/python/examples/inter-edit.py index 32f8edd9..f00928b0 100644 --- a/lang/python/examples/inter-edit.py +++ b/lang/python/examples/inter-edit.py @@ -17,7 +17,6 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA # 02111-1307 USA -import os import sys from pyme import core from pyme.core import Data, Context @@ -40,21 +39,21 @@ def edit_fnc(stat, args, helper): helper["data"].seek(helper["skip"], 0) data = helper["data"].read() helper["skip"] += len(data) - print(data) + sys.stdout.buffer.write(data) return input("(%s) %s > " % (stat2str[stat], args)) except EOFError: pass # Simple interactive editor to test editor scripts if len(sys.argv) != 2: - sys.stderr.write("Usage: %s \n" % sys.argv[0]) + sys.stderr.write("Usage: %s \n" % sys.argv[0]) else: c = Context() out = Data() - c.op_keylist_start(sys.argv[1].encode('utf-8'), 0) + c.op_keylist_start(sys.argv[1], 0) key = c.op_keylist_next() helper = {"skip": 0, "data": out} c.op_edit(key, edit_fnc, helper, out) print("[-- Final output --]") out.seek(helper["skip"], 0) - print(out.read()) + sys.stdout.buffer.write(out.read()) diff --git a/lang/python/examples/sign.py b/lang/python/examples/sign.py index 5667cc2b..ca439586 100755 --- a/lang/python/examples/sign.py +++ b/lang/python/examples/sign.py @@ -17,6 +17,8 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +import sys +import os from pyme import core, callbacks from pyme.constants.sig import mode @@ -27,5 +29,5 @@ sig = core.Data() c = core.Context() c.set_passphrase_cb(callbacks.passphrase_stdin, b'for signing') c.op_sign(plain, sig, mode.CLEAR) -sig.seek(0,0) -print(sig.read()) +sig.seek(0, os.SEEK_SET) +sys.stdout.buffer.write(sig.read()) diff --git a/lang/python/examples/signverify.py b/lang/python/examples/signverify.py index 7194157b..292deee9 100755 --- a/lang/python/examples/signverify.py +++ b/lang/python/examples/signverify.py @@ -20,7 +20,8 @@ # It uses keys for joe@example.org generated by genkey.pl script import sys -from pyme import core, callbacks +import os +from pyme import core from pyme.constants.sig import mode core.check_version(None) @@ -28,7 +29,7 @@ core.check_version(None) plain = core.Data(b"Test message") sig = core.Data() c = core.Context() -user = b"joe@example.org" +user = "joe" c.signers_clear() # Add joe@example.org's keys in the list of signers @@ -50,9 +51,9 @@ c.set_passphrase_cb(lambda x,y,z: passlist[x[x.rindex("<"):]]) c.op_sign(plain, sig, mode.CLEAR) # Print out the signature (don't forget to rewind since signing put sig at EOF) -sig.seek(0,0) +sig.seek(0, os.SEEK_SET) signedtext = sig.read() -print(signedtext) +sys.stdout.buffer.write(signedtext) # Create Data with signed text. sig2 = core.Data(signedtext) @@ -63,9 +64,7 @@ c.op_verify(sig2, None, plain2) result = c.op_verify_result() # List results for all signatures. Status equal 0 means "Ok". -index = 0 -for sign in result.signatures: - index += 1 +for index, sign in enumerate(result.signatures): print("signature", index, ":") print(" summary: ", sign.summary) print(" status: ", sign.status) @@ -74,5 +73,6 @@ for sign in result.signatures: print(" uid: ", c.get_key(sign.fpr, 0).uids[0].uid) # Print "unsigned" text. Rewind since verify put plain2 at EOF. -plain2.seek(0,0) -print("\n", plain2.read()) +plain2.seek(0, os.SEEK_SET) +print("\n") +sys.stdout.buffer.write(plain2.read()) diff --git a/lang/python/examples/simple.py b/lang/python/examples/simple.py index 4ff6d285..faa0f4cd 100755 --- a/lang/python/examples/simple.py +++ b/lang/python/examples/simple.py @@ -18,8 +18,8 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import sys -from pyme import core, constants, errors -import pyme.constants.validity +import os +from pyme import core, errors core.check_version(None) @@ -47,7 +47,7 @@ else: # Do the encryption. try: c.op_encrypt([r], 1, plain, cipher) - cipher.seek(0,0) + cipher.seek(0, os.SEEK_SET) sys.stdout.buffer.write(cipher.read()) except errors.GPGMEError as ex: print(ex.getstring()) diff --git a/lang/python/examples/t-edit.py b/lang/python/examples/t-edit.py index 5e5857a2..5c35f966 100644 --- a/lang/python/examples/t-edit.py +++ b/lang/python/examples/t-edit.py @@ -30,8 +30,8 @@ class KeyEditor: def edit_fnc(self, status, args, out): print("[-- Response --]") - out.seek(0,0) - print(out.read(), end=' ') + out.seek(0, os.SEEK_SET) + sys.stdout.buffer.write(out.read()) print("[-- Code: %d, %s --]" % (status, args)) if args == "keyedit.prompt": @@ -59,5 +59,5 @@ else: "Did you point GNUPGHOME to GPGME's tests/gpg dir?") c.op_edit(key, KeyEditor().edit_fnc, out, out) print("[-- Last response --]") - out.seek(0,0) - print(out.read(), end=' ') + out.seek(0, os.SEEK_SET) + sys.stdout.buffer.write(out.read()) diff --git a/lang/python/examples/verifydetails.py b/lang/python/examples/verifydetails.py index a8d840e7..0aa6f15b 100755 --- a/lang/python/examples/verifydetails.py +++ b/lang/python/examples/verifydetails.py @@ -25,8 +25,8 @@ # along with this program. If not, see . import sys -from pyme import core, callbacks, constants -from pyme.constants.sig import mode +import os +from pyme import core from pyme.constants import protocol def print_engine_infos(): @@ -58,9 +58,7 @@ def verifyprintdetails(sigfilename, filefilename=None): result = c.op_verify_result() # List results for all signatures. Status equal 0 means "Ok". - index = 0 - for sign in result.signatures: - index += 1 + for index, sign in enumerate(result.signatures): print("signature", index, ":") print(" summary: %#0x" % (sign.summary)) print(" status: %#0x" % (sign.status)) @@ -71,8 +69,9 @@ def verifyprintdetails(sigfilename, filefilename=None): # Print "unsigned" text if inline signature if plain2: #Rewind since verify put plain2 at EOF. - plain2.seek(0,0) - print("\n", plain2.read()) + plain2.seek(0, os.SEEK_SET) + print("\n") + sys.stdout.buffer.write(plain2.read()) def main(): print_engine_infos() @@ -86,13 +85,13 @@ def main(): sys.exit(1) if argc == 2: - print("trying to verify file: " + sys.argv[1].encode('utf-8')) - verifyprintdetails(sys.argv[1].encode('utf-8')) + print("trying to verify file: " + sys.argv[1]) + verifyprintdetails(sys.argv[1]) if argc == 3: print("trying to verify signature %s for file %s" \ - % (sys.argv[1].encode('utf-8'), sys.argv[2].encode('utf-8'))) + % (sys.argv[1], sys.argv[2])) - verifyprintdetails(sys.argv[1].encode('utf-8'), sys.argv[2].encode('utf-8')) + verifyprintdetails(sys.argv[1], sys.argv[2]) if __name__ == "__main__": main() -- cgit v1.2.3