From cdf38c1f55acf03194aced47c61a1fb6a43231e2 Mon Sep 17 00:00:00 2001 From: grahamhar Date: Thu, 3 Oct 2019 20:55:16 +0100 Subject: [PATCH] Support python 3 only --- .travis.yml | 2 +- encrypteddict/__init__.py | 12 +++++++----- setup.py | 4 ++-- tests/test_decrypt.py | 6 +++--- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6327af3..f6fd717 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ sudo: false language: python python: - - "2.7" + - "3.7" addons: apt: packages: diff --git a/encrypteddict/__init__.py b/encrypteddict/__init__.py index 427dae9..5aa96b0 100644 --- a/encrypteddict/__init__.py +++ b/encrypteddict/__init__.py @@ -24,12 +24,14 @@ def __init__(self, gpg_home=None): def decrypt_gpg(self, value): try: - encrypted_bytes = io.BytesIO('{}'.format(base64.b64decode(value))) + encrypted_string = base64.b64decode(value) + encrypted_bytes = io.BytesIO(encrypted_string) encrypted_bytes.seek(0) decrypted_bytes = io.BytesIO() self.ctx.decrypt(encrypted_bytes, decrypted_bytes) - return decrypted_bytes.getvalue() + return decrypted_bytes.getvalue().decode("utf-8") except gpgme.GpgmeError as e: + print(e) raise DecryptionError(e) def decrypt_match_group(self, value): @@ -43,7 +45,7 @@ def decrypt_match_group(self, value): def decrypt_all(self, decrypt_this): if type(decrypt_this) == dict: - for key, value in decrypt_this.iteritems(): + for key, value in decrypt_this.items(): decrypt_this[key] = self.decrypt_all(value) return decrypt_this elif type(decrypt_this) == list: @@ -57,7 +59,7 @@ def decrypt_all(self, decrypt_this): def encrypt_gpg(self, value, recipients): try: keys = [ self.ctx.get_key(recipient) for recipient in recipients ] - decrypted_bytes = io.BytesIO('{}'.format(value)) + decrypted_bytes = io.BytesIO(bytes('{}'.format(value).encode())) encrypted_bytes = io.BytesIO() decrypted_bytes.seek(0) self.ctx.encrypt(keys, 1, decrypted_bytes, encrypted_bytes ) @@ -77,7 +79,7 @@ def encrypt_all(self, encrypt_this, recipients=None): if recipients: self.recipients = recipients if type(encrypt_this) == dict: - for key, value in encrypt_this.iteritems(): + for key, value in encrypt_this.items(): encrypt_this[key] = self.encrypt_all(value) return encrypt_this elif type(encrypt_this) == list: diff --git a/setup.py b/setup.py index 256be33..18de7a9 100644 --- a/setup.py +++ b/setup.py @@ -10,8 +10,8 @@ classifiers=[ 'Development Status :: 3 - Alpha', 'Intended Audience :: Developers', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.7', ], keywords='encryption gpg dict', packages=find_packages(exclude=['static', 'docs', 'tests']), diff --git a/tests/test_decrypt.py b/tests/test_decrypt.py index 65e76b2..b9a3184 100644 --- a/tests/test_decrypt.py +++ b/tests/test_decrypt.py @@ -38,7 +38,7 @@ def test_decrypt_with_no_dict_raises_exception(self): self.test_crypter.decrypt_all() def test_decrypt_with_unknown_key_raises_exception(self): - with open('tests/static/unknown_key.yaml', 'ro') as yaml_file: + with open('tests/static/unknown_key.yaml', 'r') as yaml_file: test_dict = yaml.load(yaml_file) with self.assertRaises(DecryptionError): self.test_crypter.decrypt_all(test_dict) @@ -47,7 +47,7 @@ def test_decrypt_a_value(self): self.assertEqual(self.test_crypter.decrypt_gpg('hQIMAyCeTdU57g8QAQ/9GoRLUllutEJR7avVaRilafGs+UnoDO9y/7p86H9PtRUXxsRzLgb7xHDstP7xNTXHh/eY/7eQbQGulhmJ1pTpq+F9Vwbr3R9XKXDiYvFTQq+NQPD/+9QzFYI8HjYVU2JEj1FOX/VFJ2rF9YPY7FKu6kBVUT0RFjZ7qSlQmDgzTUpBRTLGLy7Xm1u7AwYMFrERJB3GK/HbnTnzPTUdzxeztnmjMzAxcz+IjFrCosjxW9zioKJKAKO4F5g4k0/IIMTBsoeq2hZAaB1jRE+d+dL0eTOtGlxR5YggurIvCSt486jziDrM3qZM525CA1ApFf0MfCZItFBo7xknpvWxhjSOXh/QkiA8I7+A1MGMfeQOevfBZGQxKKtwDsjKYIpip+0KXATwlCR26chpx8X9pPjVizQJQLFKiz6Z1ULT9nUMKit76NMh91UDk6jUkryt0qQd70oX5XzgUMspjEKYmJk8OBLnV4ZAHTwkNkdvS2MaMUqR7OIbruJUQykMQMI6jDcEeQvyfrwTlSv8YUvj2GF9vm9wRljZsR3pSk898nK0QmCCq6yLUrBmE5VD6D5EJOCfeeTuku8tmzPA6cSRAMGK1aAnofQ0M3QjE6AMsit+kYnyrB06A+DTddzBLyL8MBZQadTcV8GvWa1AOkIbzTNC+41mfmF0AwyxjFSQOHK+pE7SQAHlVgmj5cYOJ1YSAgekv6aPxGYicCZ8XEwztBh4K+AAY08d6Bz/Fn68MmhqdCqWgmzqaOdCWkTm74jWuTB0nrk='),'thing') def test_decrypt_yaml_returns_valid_dict(self): - with open('tests/static/test_decrypt.yaml', 'ro') as yaml_file: + with open('tests/static/test_decrypt.yaml', 'r') as yaml_file: test_dict = yaml.load(yaml_file) - print self.test_crypter.decrypt_all(test_dict) + print(self.test_crypter.decrypt_all(test_dict)) self.assertEqual(self.test_crypter.decrypt_all(test_dict),{'simple_plain_number': 1, 'hash_of_hash': {'hash1': {'leaf1': 'thing1', 'leaf2': 'thing2'}, 'hash2': {'leaf1': 'thing1', 'leaf3': 'plain', 'leaf2': 'thing2'}}, 'multiline_5_lines': 'firstline\nsecondline\nthirdline\nforthline\nfithline\n', 'simple_plain_key': 'plain', 'simple_list': ['thing1', 'thing2', 'notencrypted'], 'simple_key': 'thing', 'hash_with_list': {'leaf1': ['thing1', 'thing2', 'plain']}, 'simple_hash': {'leaf1': 'thing1', 'leaf3': 'notencrypted', 'leaf2': 'thing2'}})