Skip to content

Instantly share code, notes, and snippets.

@2efPer
Created November 20, 2017 14:01
Show Gist options
  • Select an option

  • Save 2efPer/f4ee8a5e35c754837e0629c72389fb36 to your computer and use it in GitHub Desktop.

Select an option

Save 2efPer/f4ee8a5e35c754837e0629c72389fb36 to your computer and use it in GitHub Desktop.
Python Reflection
# Plugin1:
class A(object):
def __init__(self):
pass
def process(self):
print('Plugin1')
# 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