Created
November 20, 2017 14:01
-
-
Save 2efPer/f4ee8a5e35c754837e0629c72389fb36 to your computer and use it in GitHub Desktop.
Python Reflection
This file contains hidden or 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
| # Plugin1: | |
| class A(object): | |
| def __init__(self): | |
| pass | |
| def process(self): | |
| print('Plugin1') |
This file contains hidden or 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
| # TestPlugin: | |
| import os | |
| def getPlugin(): | |
| path = os.path.split(os.path.realpath(__file__))[0] | |
| plugins = os.listdir(path + '/plugin/') | |
| fil = lambda str: (True, False)[str[-3:] == 'pyc' or str.find('__init__.py') != -1] | |
| return filter(fil, plugins) | |
| if __name__ == "__main__": | |
| for plugin in getPlugin(): | |
| # Get Plugin File Name | |
| print(plugin) | |
| pluginName = os.path.splitext(plugin)[0] | |
| # Load Plugin | |
| plugin = __import__("plugin." + pluginName, fromlist=[pluginName]) | |
| myplugin = getattr(plugin, "A") | |
| # Get Plugin Object | |
| p = myplugin() | |
| # # Invoke Plugin Method | |
| p.process() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment