diff options
author | Sybren A. Stüvel <sybren@stuvel.eu> | 2016-01-27 15:43:28 +0100 |
---|---|---|
committer | Sybren A. Stüvel <sybren@stuvel.eu> | 2016-01-27 15:43:28 +0100 |
commit | 4a6a2b7b7facd5bcbf8b9e137d0047a170c5e8d4 (patch) | |
tree | f09bf80dcfea9c875cf605413f308a196ee9a139 | |
parent | 417ec0d7081ddcd94e31be2741d5b63600a3fe53 (diff) | |
download | rsa-git-4a6a2b7b7facd5bcbf8b9e137d0047a170c5e8d4.tar.gz |
Removed code duplication
-rw-r--r-- | rsa/key.py | 24 |
1 files changed, 13 insertions, 11 deletions
@@ -70,14 +70,21 @@ class AbstractKey(object): 'DER': cls._load_pkcs1_der, } - if format not in methods: + method = cls._assert_format_exists(format, methods) + return method(keyfile) + + @staticmethod + def _assert_format_exists(file_format, methods): + """Checks whether the given file format exists in 'methods'. + """ + + try: + return methods[file_format] + except KeyError: formats = ', '.join(sorted(methods.keys())) - raise ValueError('Unsupported format: %r, try one of %s' % (format, + raise ValueError('Unsupported format: %r, try one of %s' % (file_format, formats)) - method = methods[format] - return method(keyfile) - def save_pkcs1(self, format='PEM'): """Saves the public key in PKCS#1 DER or PEM format. @@ -90,12 +97,7 @@ class AbstractKey(object): 'DER': self._save_pkcs1_der, } - if format not in methods: - formats = ', '.join(sorted(methods.keys())) - raise ValueError('Unsupported format: %r, try one of %s' % (format, - formats)) - - method = methods[format] + method = self._assert_format_exists(format, methods) return method() def blind(self, message, r): |