Python bindings examples: PEP8 conpliance

* Appears to be complete compliance.
This commit is contained in:
Ben McGinnes 2018-08-18 18:46:47 +10:00
parent fc55caccfc
commit b5fbe90676
12 changed files with 49 additions and 35 deletions

View File

@ -14,14 +14,14 @@
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
"""Demonstrate the use of the Assuan protocol engine"""
from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import gpg
del absolute_import, print_function, unicode_literals
with gpg.Context(protocol=gpg.constants.protocol.ASSUAN) as c:
# Invoke the pinentry to get a confirmation.
err = c.assuan_transact(['GET_CONFIRMATION', 'Hello there'])

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python
#
# Copyright (C) 2016 g10 Code GmbH
# Copyright (C) 2016, 2018 g10 Code GmbH
#
# 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
@ -14,7 +14,6 @@
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
"""A decryption filter
This demonstrates decryption using gpg3 in three lines of code. To
@ -25,8 +24,10 @@ be used like this:
"""
from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import sys
import gpg
del absolute_import, print_function, unicode_literals
gpg.Context().decrypt(sys.stdin, sink=sys.stdout)

View File

@ -20,10 +20,11 @@
# It deletes keys for joe@example.org generated by genkey.py script
from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import gpg
del absolute_import, print_function, unicode_literals
with gpg.Context() as c:
# Note: We must not modify the key store during iteration,
# therefore, we explicitly make a list.

View File

@ -20,12 +20,13 @@
# It uses keys for joe+gpg@example.org generated by genkey.py script
from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import sys
import os
import gpg
del absolute_import, print_function, unicode_literals
user = "joe+gpg@example.org"
with gpg.Context(armor=True) as c, gpg.Data() as expkey:

View File

@ -18,10 +18,11 @@
# along with this program; if not, see <http://www.gnu.org/licenses/>.
from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import gpg
del absolute_import, print_function, unicode_literals
# This is the example from the GPGME manual.
parms = """<GnupgKeyParms format="internal">

View File

@ -15,15 +15,15 @@
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
"""Simple interactive editor to test editor scripts"""
from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import sys
import gpg
del absolute_import, print_function, unicode_literals
if len(sys.argv) != 2:
sys.exit("Usage: %s <Gpg key pattern>\n" % sys.argv[0])
@ -40,10 +40,12 @@ with gpg.Context() as c:
print("Editing key {} ({}):".format(key.uids[0].uid, key.subkeys[0].fpr))
def edit_fnc(keyword, args):
print("Status: {}, args: {} > ".format(
keyword, args), end='', flush=True)
print(
"Status: {}, args: {} > ".format(keyword, args),
end='',
flush=True)
if not 'GET' in keyword:
if 'GET' not in keyword:
# no prompt
print()
return None

View File

@ -16,18 +16,18 @@
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
"""
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 __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import sys
import gpg
del absolute_import, print_function, unicode_literals
with gpg.Context(armor=True) as c:
recipients = list()
for key in c.keylist():
@ -40,14 +40,15 @@ with gpg.Context(armor=True) as c:
while not ciphertext:
print("Encrypting to %d recipients" % len(recipients))
try:
ciphertext, _, _ = c.encrypt(b'This is my message.',
recipients=recipients)
ciphertext, _, _ = c.encrypt(
b'This is my message.', recipients=recipients)
except gpg.errors.InvalidRecipients as e:
print("Encryption failed for these keys:\n{0!s}".format(e))
# filter out the bad keys
bad_keys = {bad.fpr for bad in e.recipients}
recipients = [r for r in recipients
if not r.subkeys[0].fpr in bad_keys]
recipients = [
r for r in recipients if not r.subkeys[0].fpr in bad_keys
]
sys.stdout.buffer.write(ciphertext)

View File

@ -17,12 +17,13 @@
# along with this program; if not, see <http://www.gnu.org/licenses/>.
from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import sys
import gpg
from gpg.constants.sig import mode
del absolute_import, print_function, unicode_literals
with gpg.Context() as c:
signed, _ = c.sign(b"Test message", mode=mode.CLEAR)
sys.stdout.buffer.write(signed)

View File

@ -20,12 +20,13 @@
# It uses keys for joe+gpg@example.org generated by genkey.py script
from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import sys
import gpg
from gpg.constants.sig import mode
del absolute_import, print_function, unicode_literals
user = "joe+gpg"
with gpg.Context(pinentry_mode=gpg.constants.PINENTRY_MODE_LOOPBACK) as c:

View File

@ -18,11 +18,12 @@
# along with this program; if not, see <http://www.gnu.org/licenses/>.
from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import sys
import gpg
del absolute_import, print_function, unicode_literals
with gpg.Context(armor=True) as c:
recipients = []
print("Enter name of your recipient(s), end with a blank line.")
@ -40,8 +41,8 @@ with gpg.Context(armor=True) as c:
if not recipients:
sys.exit("No recipients.")
print("Encrypting for {}.".format(", ".join(k.uids[0].name
for k in recipients)))
print("Encrypting for {}.".format(", ".join(
k.uids[0].name for k in recipients)))
ciphertext, _, _ = c.encrypt(b"This is my message,", recipients)
sys.stdout.buffer.write(ciphertext)

View File

@ -15,15 +15,15 @@
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
"""A test applicaton for the CMS protocol."""
from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import sys
import gpg
del absolute_import, print_function, unicode_literals
if len(sys.argv) != 2:
sys.exit("fingerprint or unique key ID for gpgme_get_key()")

View File

@ -18,11 +18,13 @@
# along with this program; if not, see <http://www.gnu.org/licenses/>.
from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import sys
import gpg
del absolute_import, print_function, unicode_literals
def print_engine_infos():
print("gpgme version:", gpg.core.check_version(None))
print("engines:")
@ -31,8 +33,9 @@ def print_engine_infos():
print(engine.file_name, engine.version)
for proto in [gpg.constants.protocol.OpenPGP, gpg.constants.protocol.CMS]:
print("Have {}? {}".format(gpg.core.get_protocol_name(proto),
gpg.core.engine_check_version(proto)))
print("Have {}? {}".format(
gpg.core.get_protocol_name(proto),
gpg.core.engine_check_version(proto)))
def verifyprintdetails(filename, detached_sig_filename=None):
@ -40,9 +43,9 @@ def verifyprintdetails(filename, detached_sig_filename=None):
with gpg.Context() as c:
# Verify.
data, result = c.verify(open(filename),
open(detached_sig_filename)
if detached_sig_filename else None)
data, result = c.verify(
open(filename),
open(detached_sig_filename) if detached_sig_filename else None)
# List results for all signatures. Status equal 0 means "Ok".
for index, sign in enumerate(result.signatures):
@ -57,15 +60,15 @@ def verifyprintdetails(filename, detached_sig_filename=None):
if data:
sys.stdout.buffer.write(data)
def main():
print_engine_infos()
print()
argc = len(sys.argv)
if argc < 2 or argc > 3:
sys.exit(
"Usage: {} <filename>[ <detached_signature_filename>]".format(
sys.argv[0]))
sys.exit("Usage: {} <filename>[ <detached_signature_filename>]".format(
sys.argv[0]))
if argc == 2:
print("trying to verify file {}.".format(sys.argv[1]))
@ -74,5 +77,6 @@ def main():
print("trying to verify signature {1} for file {0}.".format(*sys.argv))
verifyprintdetails(sys.argv[1], sys.argv[2])
if __name__ == "__main__":
main()