📜 ⬆️ ⬇️

Python Plugins

Today I will tell how to build a plugin architecture in python on include'ah.




Our application will receive commands and distribute them to plugins in the hope that some plugin will even process it.
')

Tricky plan





main.py


This contains only the code of the “external” part of the example, and I hope it is not necessary to comment on it.

Copy Source | Copy HTML import plugin plugin.LoadPlugins() s = '' while (s != 'exit' ): print '>' , s = raw_input () a = s.split( ' ' ) for p in plugin.Plugins: p.OnCommand(a[ 0 ], a[ 1 :])
  1. Copy Source | Copy HTML import plugin plugin.LoadPlugins() s = '' while (s != 'exit' ): print '>' , s = raw_input () a = s.split( ' ' ) for p in plugin.Plugins: p.OnCommand(a[ 0 ], a[ 1 :])
  2. Copy Source | Copy HTML import plugin plugin.LoadPlugins() s = '' while (s != 'exit' ): print '>' , s = raw_input () a = s.split( ' ' ) for p in plugin.Plugins: p.OnCommand(a[ 0 ], a[ 1 :])
  3. Copy Source | Copy HTML import plugin plugin.LoadPlugins() s = '' while (s != 'exit' ): print '>' , s = raw_input () a = s.split( ' ' ) for p in plugin.Plugins: p.OnCommand(a[ 0 ], a[ 1 :])
  4. Copy Source | Copy HTML import plugin plugin.LoadPlugins() s = '' while (s != 'exit' ): print '>' , s = raw_input () a = s.split( ' ' ) for p in plugin.Plugins: p.OnCommand(a[ 0 ], a[ 1 :])
  5. Copy Source | Copy HTML import plugin plugin.LoadPlugins() s = '' while (s != 'exit' ): print '>' , s = raw_input () a = s.split( ' ' ) for p in plugin.Plugins: p.OnCommand(a[ 0 ], a[ 1 :])
  6. Copy Source | Copy HTML import plugin plugin.LoadPlugins() s = '' while (s != 'exit' ): print '>' , s = raw_input () a = s.split( ' ' ) for p in plugin.Plugins: p.OnCommand(a[ 0 ], a[ 1 :])
  7. Copy Source | Copy HTML import plugin plugin.LoadPlugins() s = '' while (s != 'exit' ): print '>' , s = raw_input () a = s.split( ' ' ) for p in plugin.Plugins: p.OnCommand(a[ 0 ], a[ 1 :])
  8. Copy Source | Copy HTML import plugin plugin.LoadPlugins() s = '' while (s != 'exit' ): print '>' , s = raw_input () a = s.split( ' ' ) for p in plugin.Plugins: p.OnCommand(a[ 0 ], a[ 1 :])
  9. Copy Source | Copy HTML import plugin plugin.LoadPlugins() s = '' while (s != 'exit' ): print '>' , s = raw_input () a = s.split( ' ' ) for p in plugin.Plugins: p.OnCommand(a[ 0 ], a[ 1 :])


All plugin related code is in plugin.py.

plugin.py


Copy Source | Copy HTML
  1. import os
  2. import sys
  3. # Instances of downloaded plugins
  4. Plugins = []
  5. # Basic class of the plugin
  6. class Plugin (object):
  7. Name = 'undefined'
  8. # Feedback methods
  9. def OnLoad (self):
  10. pass
  11. def OnCommand (self, cmd, args):
  12. pass
  13. def LoadPlugins ():
  14. ss = os .listdir ( 'plugins' ) # Get the list of plugins in / plugins
  15. sys .path.insert ( 0 , 'plugins' ) # Add the plugins folder to $ PATH so that __import__ can load them
  16. for s in ss:
  17. print 'Found plugin' , s
  18. __import__ ( os .path.splitext (s) [ 0 ], None, None, [ '' ]) # Import the source of the plugin
  19. for plugin in Plugin .__ subclasses __ (): # since the Plugin is derived from object, we use __subclasses__ to find all the plugins produced from this class
  20. p = plugin () # Create an instance
  21. Plugins.append (p)
  22. p. OnLoad () # Call the event of loading this plugin
  23. return


plugins / foo.py - plugin example


Copy Source | Copy HTML
  1. from plugin import plugin
  2. class HelloPlugin (Plugin): # produce our plugin from the parent class
  3. Name = 'HelloPlugin v 1.0 Extreme Edition'
  4. # replace the necessary methods
  5. def OnLoad (self):
  6. print 'HelloPlugin 1.0 Extreme VIP Edition Loaded!'
  7. def OnCommand (self, cmd, args):
  8. if (cmd == 'hello' and len (args)> 0 ):
  9. print 'It \' s' , args [ 0 ], '! \ nJeez, man, nice to meet you!'
  10. return true
  11. else :
  12. return false


Now you can run an example:
$ python main.py
Found plugin foo.py
HelloPlugin 1.0 Extreme VIP Edition Loaded!
> hello% username%
It's% username%!
Jeez, man, nice to meet you!
>


plugins / shell.py - the plugin is more complicated


Let's make a plugin that will broadcast commands to the shell and return the output:

Copy Source | Copy HTML
  1. from plugin import plugin
  2. import commands
  3. class ShellPlugin (Plugin):
  4. Name = 'Shell plugin'
  5. def OnLoad (self):
  6. print 'Shell plugin loaded.'
  7. def OnCommand (self, cmd, args):
  8. if (cmd == 'run' ):
  9. print commands .getoutput ( "" .join (args))
  10. return true
  11. else :
  12. return false


$ python main.py
Found plugin shell.py
Found plugin foo.py
Shell plugin loaded.
HelloPlugin 1.0 Extreme VIP Edition Loaded!
> run uname -r
2.6.31-14-generic
> exit


That's all!
Download source code

Source: https://habr.com/ru/post/79513/


All Articles