Building A Plugin
1. Overview
We have defined what a plugin is here. Every now and then, you would feel the need to either build a plugin, extend a plugin or fix a broken plugin. To be able to do that, you need to have a very basic understanding of how a plugin works and what is the structure described in the following sections. If you want to look at the architecture of the whole thing first to get a bigger picture, please go here.
2. Building your own plugin from scratch
2.1 Building the actual plugin
- All plugins implements
PDFPlugin
interface For example take a look at the source code of GoogleDocPlugin/external.py and see line number 16
So what is a PDFPlugin
? It looks something like this.
- Define Body of Following function: 1. fetch_data: this will fetch new data from webserver and provide this data as dictionary and also combine configuration data and also provide tags if any.
- fetch_mapping: This will fetch value mapping and options mapping and combine this with data fetch dictionary and return this complete dictionary.
- build_PDF: It will take raw_data and file_name of pdf as input and return filename and pdfUrl.
- upload_PDF: This will upload pdf to server or to any other storage and return its filename.
- retrieve_PDF: This will take pdf name as input and return pdf content.
-. If we need to add more function in class which needs to be accessible from pdfbase class then they should be first define in PDFPlugin interface.
2.2 Adding the plugin to the main system
Import PDFBuilder Class
from internal import PDFBuilder, PDFPlugin
Import Plugin File
from GoogleDocPlugin.external import GoogleDocsSheetsPlugin
Add this class as plugin of PdfBuilder class and also pass config if we need any
2.3 Starting the system
Just run the following to test if everything is working fine. app.run()
Now main.py
will look like :
3. Extending a Plugin already written
A lot of times you wouldn't want to write the whole thing and would want to just extend some other plugin that is already written or maybe two and modify the parts that are not the same as the origin plugin. Here you can do this native python classes.
- Import the plugin to your plugin folder
from GoogleDocPlugin.external import GoogleDocsSheetsPlugin
- Create plugin class by extending this superclass and not extending it though the interface.
class ODKPlugin(GoogleDocsSheetsPlugin)
- All the superclass method are available here. If we need to override the superclass method then we can simply add the method with same name and argument and we can also call the superclass method using super statement. for example : File GoogleDocPlugin/external.py
3. FAQs
To be added based on incoming feedback
4. Coming Soon
Please review the following section to get information about planned updates to this module.