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

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python #!/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 # 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 # 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 # You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>. # along with this program; if not, see <http://www.gnu.org/licenses/>.
"""A decryption filter """A decryption filter
This demonstrates decryption using gpg3 in three lines of code. To 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 from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import sys import sys
import gpg import gpg
del absolute_import, print_function, unicode_literals
gpg.Context().decrypt(sys.stdin, sink=sys.stdout) 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 # It deletes keys for joe@example.org generated by genkey.py script
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import gpg import gpg
del absolute_import, print_function, unicode_literals
with gpg.Context() as c: with gpg.Context() as c:
# Note: We must not modify the key store during iteration, # Note: We must not modify the key store during iteration,
# therefore, we explicitly make a list. # 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 # It uses keys for joe+gpg@example.org generated by genkey.py script
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import sys import sys
import os import os
import gpg import gpg
del absolute_import, print_function, unicode_literals
user = "joe+gpg@example.org" user = "joe+gpg@example.org"
with gpg.Context(armor=True) as c, gpg.Data() as expkey: 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/>. # along with this program; if not, see <http://www.gnu.org/licenses/>.
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import gpg import gpg
del absolute_import, print_function, unicode_literals
# This is the example from the GPGME manual. # This is the example from the GPGME manual.
parms = """<GnupgKeyParms format="internal"> parms = """<GnupgKeyParms format="internal">

View File

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

View File

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

View File

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

View File

@ -20,12 +20,13 @@
# It uses keys for joe+gpg@example.org generated by genkey.py script # It uses keys for joe+gpg@example.org generated by genkey.py script
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import sys import sys
import gpg import gpg
from gpg.constants.sig import mode from gpg.constants.sig import mode
del absolute_import, print_function, unicode_literals
user = "joe+gpg" user = "joe+gpg"
with gpg.Context(pinentry_mode=gpg.constants.PINENTRY_MODE_LOOPBACK) as c: 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/>. # along with this program; if not, see <http://www.gnu.org/licenses/>.
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import sys import sys
import gpg import gpg
del absolute_import, print_function, unicode_literals
with gpg.Context(armor=True) as c: with gpg.Context(armor=True) as c:
recipients = [] recipients = []
print("Enter name of your recipient(s), end with a blank line.") 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: if not recipients:
sys.exit("No recipients.") sys.exit("No recipients.")
print("Encrypting for {}.".format(", ".join(k.uids[0].name print("Encrypting for {}.".format(", ".join(
for k in recipients))) k.uids[0].name for k in recipients)))
ciphertext, _, _ = c.encrypt(b"This is my message,", recipients) ciphertext, _, _ = c.encrypt(b"This is my message,", recipients)
sys.stdout.buffer.write(ciphertext) sys.stdout.buffer.write(ciphertext)

View File

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