From f1e55cd6902be34c2feacc20443e4d716387c838 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Tue, 11 Jul 2017 16:39:05 -0700 Subject: crypto: Make strings more readable This is a just a small refactor that makes things a little more readable, and provides slightly more verbose messages in for signing and encrypting failures. Mainly this uses str.format instead of inline concatenation, but it also rewraps and removes '+' operators from a string already wrapped in parens --- alot/crypto.py | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) (limited to 'alot/crypto.py') diff --git a/alot/crypto.py b/alot/crypto.py index b0849ae2..2d67c115 100644 --- a/alot/crypto.py +++ b/alot/crypto.py @@ -107,11 +107,10 @@ def get_key(keyid, validate=False, encrypt=False, sign=False, if valid_key: # we have already found one valid key and now we find # another? We really received an ambiguous keyid - raise GPGProblem(("More than one key found matching " + - "this filter. Please be more " + - "specific (use a key ID like " + - "4AC8EE1D)."), - code=GPGCode.AMBIGUOUS_NAME) + raise GPGProblem( + "More than one key found matching this filter. Please " + "be more specific (use a key ID like 4AC8EE1D).", + code=GPGCode.AMBIGUOUS_NAME) valid_key = k if not valid_key: @@ -119,18 +118,16 @@ def get_key(keyid, validate=False, encrypt=False, sign=False, # given action (we don't have private key, they are expired # etc) raise GPGProblem( - "Can not find usable key for \'" + - keyid + - "\'.", + 'Can not find usable key for "{}".'.format(keyid), code=GPGCode.NOT_FOUND) return valid_key elif e.code == gpgme.ERR_INV_VALUE or e.code == gpgme.ERR_EOF: - raise GPGProblem("Can not find key for \'" + keyid + "\'.", + raise GPGProblem('Can not find key for "{}".'.format(keyid), code=GPGCode.NOT_FOUND) else: raise e if signed_only and not check_uid_validity(key, keyid): - raise GPGProblem("Can not find a trusworthy key for '" + keyid + "'.", + raise GPGProblem('Can not find a trusworthy key for "{}".'.format(keyid), code=GPGCode.NOT_FOUND) return key @@ -261,20 +258,22 @@ def validate_key(key, sign=False, encrypt=False): """ if key.revoked: - raise GPGProblem("The key \"" + key.uids[0].uid + "\" is revoked.", + raise GPGProblem('The key "{}" is revoked.'.format(key.uids[0].uid), code=GPGCode.KEY_REVOKED) elif key.expired: - raise GPGProblem("The key \"" + key.uids[0].uid + "\" is expired.", + raise GPGProblem('The key "{}" is expired.'.format(key.uids[0].uid), code=GPGCode.KEY_EXPIRED) elif key.invalid: - raise GPGProblem("The key \"" + key.uids[0].uid + "\" is invalid.", + raise GPGProblem('The key "{}" is invalid.'.format(key.uids[0].uid), code=GPGCode.KEY_INVALID) if encrypt and not key.can_encrypt: - raise GPGProblem("The key \"" + key.uids[0].uid + "\" can not " + - "encrypt.", code=GPGCode.KEY_CANNOT_ENCRYPT) + raise GPGProblem( + 'The key "{}" cannot be used to encrypt'.format(key.uids[0].uid), + code=GPGCode.KEY_CANNOT_ENCRYPT) if sign and not key.can_sign: - raise GPGProblem("The key \"" + key.uids[0].uid + "\" can not sign.", - code=GPGCode.KEY_CANNOT_SIGN) + raise GPGProblem( + 'The key "{}" cannot be used to sign'.format(key.uids[0].uid), + code=GPGCode.KEY_CANNOT_SIGN) def check_uid_validity(key, email): -- cgit v1.2.3