Skip to content

Commit 4a6a2b7

Browse files
committed
Removed code duplication
1 parent 417ec0d commit 4a6a2b7

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

rsa/key.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,21 @@ def load_pkcs1(cls, keyfile, format='PEM'):
7070
'DER': cls._load_pkcs1_der,
7171
}
7272

73-
if format not in methods:
73+
method = cls._assert_format_exists(format, methods)
74+
return method(keyfile)
75+
76+
@staticmethod
77+
def _assert_format_exists(file_format, methods):
78+
"""Checks whether the given file format exists in 'methods'.
79+
"""
80+
81+
try:
82+
return methods[file_format]
83+
except KeyError:
7484
formats = ', '.join(sorted(methods.keys()))
75-
raise ValueError('Unsupported format: %r, try one of %s' % (format,
85+
raise ValueError('Unsupported format: %r, try one of %s' % (file_format,
7686
formats))
7787

78-
method = methods[format]
79-
return method(keyfile)
80-
8188
def save_pkcs1(self, format='PEM'):
8289
"""Saves the public key in PKCS#1 DER or PEM format.
8390
@@ -90,12 +97,7 @@ def save_pkcs1(self, format='PEM'):
9097
'DER': self._save_pkcs1_der,
9198
}
9299

93-
if format not in methods:
94-
formats = ', '.join(sorted(methods.keys()))
95-
raise ValueError('Unsupported format: %r, try one of %s' % (format,
96-
formats))
97-
98-
method = methods[format]
100+
method = self._assert_format_exists(format, methods)
99101
return method()
100102

101103
def blind(self, message, r):

0 commit comments

Comments
 (0)