Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Check if redshift module can be imported
import imp
import c4d
class ImportTester(type):
_HaveBeenCalled = False
_CanImport = False
@classmethod
def _CheckImport(cls, clsName):
# If we already test, we return the cached value
if cls._HaveBeenCalled:
return cls._CanImport
# Check if module is already pressent
try:
imp.find_module(clsName)
cls._CanImport = True
return True
# If module is not already loaded we try to laod it
except ImportError:
try:
__import__(clsName)
cls._CanImport = True
return True
except ImportError:
cls._CanImport = False
return False
def __call__(cls, *args, **kwargs):
if not cls._HaveBeenCalled:
cls._CheckImport("redshift")
if cls._CanImport:
return super(ImportTester, cls).__call__(*args, **kwargs)
else:
return False
class Test(object):
__metaclass__ = ImportTester
def __init__(self):
print 'a'
def main():
print Test()
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment