Skip to content

Instantly share code, notes, and snippets.

@heewa

heewa/crypt.py Secret

Last active December 28, 2015 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heewa/ffac2020110ad9b111b2 to your computer and use it in GitHub Desktop.
Save heewa/ffac2020110ad9b111b2 to your computer and use it in GitHub Desktop.
Nicer error msg for missing a library.
# Import third party libs
try:
from M2Crypto import RSA
from Crypto.Cipher import AES
except ImportError:
# No need for crypt in local mode, but give a nicer message
class CryptoErrorClass():
def __init__(self, necessary_lib):
self._lib = necessary_lib
def __getattr__(self, attr):
log.error('Missing %s library, needed for remote master communication.', self._lib)
raise Exception('Missing {0} library.'.format(self._lib))
RSA = CryptoErrorClass('M2Crypto')
AES = CryptoErrorClass('Crypto')
...
gen = RSA.gen_key(...)
... -> Exception: You need to install m2crypto.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment