Created
December 6, 2017 18:07
-
-
Save gr4ph0s/e363f5cce1cc6d6a9f06cf767e6342ed to your computer and use it in GitHub Desktop.
Check if redshift module can be imported
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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