The example of creating a plugin

Directory with plugins for installed application: /var/www/geomongo/plugins.

To create a plugin you have to:

  • create a directory the name of which should be written by latin lowercase letters, words should be divided by underscores;
  • create empty file __init__.py  in order for this directory could be python-package;
  • create file main.py with two functions getPluginInfo() and getPluginResources().

getPluginInfo() returns the information about the plugin;  getPluginResources() is the dictionary which contains couples ‘link’: Resource’s derived class.

Example

Let there are two classes-resource that we want to turn into a plugin.

test_resource_1.py:
 from flask.ext.restful import Resource
 class TestResource1(Resource):
   def get(self):
     return 'test_resource_1'
test_resource_2.py:
from flask.ext.restful import Resource
class TestResource2(Resource):
   def get(self):
     return 'test_resource_2'


It’s necessary to connect both classes in main.py:

sys.path.append('../../plugins/test_plugin/')
# required hack in order for resource of plugin could be visible (https://geo2tag.atlassian.net/browse/GT-1438)
from test_resource_1 import TestResource1
from test_resource_2 import TestResource2

and create mandatory functions for the plugin:

def getPluginResources():
    result = {'res1': TestResource1, 'res2':TestResource2}  
    return result

def getPluginInfo():
    info = 'This plugin was creating for present plugins feature. The function getPluginResources is for return TestResource1 and TestResource2 '
    return info

Then you have to create the directory /var/www/geomongo/plugins/test_plugin and place into it files main.py, test_resource_2.py, test_resource_1.py and empty file  __init__.py.

Next, you need to restart the web server to run the plugins up.

   sudo service apache2 restart

After that, the plugins will be available at:

http://geomongo/instance/plugin/test_plugin/res1

http://geomongo/instance/plugin/test_plugin/res2

ShareShare on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on VKEmail this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *