File | Date | Author | Commit |
---|---|---|---|
mg_conveyor | 2015-01-30 |
![]() |
[297476] Add own exception class |
test | 2015-01-30 |
![]() |
[297476] Add own exception class |
.gitignore | 2015-02-03 |
![]() |
[30c066] Update README |
.project | 2015-01-19 |
![]() |
[06da53] Initial commit |
.pydevproject | 2015-01-19 |
![]() |
[06da53] Initial commit |
MANIFEST.in | 2015-01-29 |
![]() |
[139e5d] Remove rst in MANIFEST file |
README.rst | 2015-02-03 |
![]() |
[256cfc] Update version and readme |
setup.cfg | 2015-01-29 |
![]() |
[4be4f6] Include deployment files. |
setup.py | 2015-02-03 |
![]() |
[256cfc] Update version and readme |
Sometime in the development or implementation we encounter things to be processed by different specialization. For example, we receive a CSV file that is intended from importing to a table. We found out that the CSV file is not formatted to what the table can consume. Thus, we need to do two thing:
As we can see above, these then can be created by two different individuals one that is specialized in manipulating CSV file and knowledge in generating SQL scripts. Addition, these two thing must be run in sequence and not on the other way around.
This is the idea around the conveyor that the work is done in sequence and can use some tools.
The installation is as easy as downloading the conveyor-<version>-py3-none-any.whl package and run the following command in the download folder in the command line:
Command 1. Using pip module for installation
If you have the wheel package
python -m pip install mg_conveyor-<version>-py3-none-any.whl
Or simply
python -m pip install mg-conveyor
Conveyor is not intended be used as a standalone command but a least a plugin must be made available to it to process.
As its name suggests it is executed based on sequence. This plugin needs the following methods to be implemented:
def sequence(self) def process(self, identity, context, *args, **kwargs)
The sequence method must return an integer that will dictate sequence on when it must be executed.
Note
Every plugin must return a different sequence number.
The process method is the entrypoint for each plugin and this is where our plugin logic must reside. This method has two important parameters namely identity and context. Currently the identity is the name of the plugin (i.e. defined in the yapsy configuration file that is particularly the Name attribute) and the context contains some information about the plugin. Also this method must return one of the predefined status.
ISequencialPlugin Return Status
Status | Description |
---|---|
STATUS_SUCCESS | Finished successfully. |
STATUS_FAILED | Error encountered also abort the execution of conveyor |
STATUS_ABORT | Abort the execution of conveyor. |
STATUS_RETRY | The plugin is requesting an interactive retry. |
STATUS_CONFIRM_RETRY | The user responded Y to the interactive retry. |
STATUS_CONFIRM_ABORT | The user responded N to the interactive retry. |
STATUS_SKIPPED | Skipped plugin |
STATUS_STARTED | Plugin is currently started. |
Example 1. Simple Plugin Implementation
import mg_conveyor as conveyor class PluginOne(conveyor.ISequencialPlugin): def sequence(self): return 100 def process(self, identity, context, *args, **kwargs): print("Hello Worlds.") return conveyor.STATUS_SUCCESS
The context parameter contains some information about the plugin like its status (i.e. CONTEXT_KEY_STATUS) and its instance (i.e. CONTEXT_KEY_PLUGIN_OBJECT). This parameter is just a simple dictionary. By default each plugin has its own context inside the context with the key as its identity (i.e. context[identity]). Thus, to get the plugin object of the current plugin we should access it like the following snippet inside the process method.
pluginObj = context[identity][CONTEXT_KEY_PLUGIN_OBJECT]
Pre-defined Context Keys per identity
Status | Description |
---|---|
CONTEXT_KEY_STATUS | Contains the numerical equivalent of the ISequencialPlugin Return Status. |
CONTEXT_KEY_PLUGIN_OBJECT | The instance of the plugin associated with the identity. |
The context has another purpose that it can hold custom plugin information. To add a new entry to the context it is better to add it under the identity key. This can be done by the snippet below (i.e. inside the process method):
plugin_context = context[identity] plugin_context['item1']='This is item 1' plugin_context['item2']='This is item 2'
More over this context is shared by all the plugins loaded by the conveyor. Therefore, we can use the context to pass information between plugins. For example the result of the first plugin can be stored in the context and that item will be processed by the following plugin.
Once the plugin is ready we must create a configuration file. Since we are using yapsy the sample configuration file is as follows:
[Core] Name = Hello World Module = helloworld [Documentation] Author = Ronaldo Webb Version = 0.1 Website = http://www.ronella.xyz Description = Test only
Note
The Name attribute in this configuration file is the one being used as the identity parameter in the process method.
See yapsy-plugin for more information about the content of the configuration file.
This plugin is the one that can be shared by all ISequencialPlugin implementations. Thus, we can think of the IUtilityPlugin implementation as a tool that the ISequencialPlugin (i.e. activing as a worker) can use to do its job properly. This tool is always available in the context and only has one method to be implemented.
def use(self, *args, **kwargs)
The use method must contain the implmentation on what the ISequencialPlugin implementaion demands it to do according to what it's purpose as a tool. It can return any type therefore the a proper documentation of the return type is necessary.
The IUtilityPlugin has built-in properties as we can see in the following table:
Property | Description |
---|---|
identity | Contains the identity of the plugin (i.e. the Name attribute in yapsy configuration file.) |
context | The context shared by all the plugins. |
localContext | This is equals to context[identity]. |
To acquire an instance of the IUtilityPlugin we must know its identity (i.e. Name attribute in yapsy configuraiton file.). Once we have the instance we can call it's use method.
Example 2: Acquiring IUtilityPlugin
If have Utility as the identity then in the process method of the ISequencialPlugin implementation we can retrieve its instance like in the snippet below:
def process(self, identity, context, *args, **kwargs): utilObj = context['Utility'][conveyor.CONTEXT_KEY_PLUGIN_OBJECT].use()
Note
We can pass any number of arguments to the use method.
Once the plugin is working fine and the configuration file were created, we can now create an implmenetation to start the conveyor. The first thing that we must do is to instantiate the Conveyor class then load the plugins and finally start it. See the sample snippet below:
Example 3: Starting the Conveyor
conv = conveyor.Conveyor() conv.loadPlugins(["plugins"]) conv.start()
Note
In the loadPlugins method we must pass in an array of locations of the plugins.
import mg_conveyor as conveyor class PluginOne(conveyor.ISequencialPlugin): def sequence(self): return 100 def process(self, identity, context, *args, **kwargs): print("Hello World.") return conveyor.STATUS_SUCCESS
[Core] Name = Hello World Module = helloworld_plugin [Documentation] Author = Ronaldo Webb Version = 0.1 Website = http://www.ronella.xyz Description = Test only
import mg_conveyor as conveyor if __name__ == '__main__': conv=conveyor.Conveyor() conv.loadPlugins(["plugins"]) conv.start()
import mg_conveyor as conveyor class Plugin(conveyor.ISequencialPlugin): def sequence(self): return 100 def process(self, identity, context, *args, **kwargs): plugin_context = context[identity] plugin_context['Message'] = "From plugin 1" return conveyor.STATUS_SUCCESS
[Core] Name = Plugin1 Module = plugin1 [Documentation] Author = Ronaldo Webb Version = 0.1 Website = http://www.ronella.xyz Description = Test only
import mg_conveyor as conveyor class Plugin(conveyor.ISequencialPlugin): def sequence(self): return 200 def process(self, identity, context, *args, **kwargs): #Plugin1 is the Name found in plugin1.yapsy-plugin plugin1_context = context['Plugin1'] print("I'm in plugin 2...") print("Message from plugin 1: " + plugin1_context['Message']) return conveyor.STATUS_SUCCESS
[Core] Name = Plugin2 Module = plugin2 [Documentation] Author = Ronaldo Webb Version = 0.1 Website = http://www.ronella.xyz Description = Test only
import mg_conveyor as conveyor if __name__ == '__main__': conv=conveyor.Conveyor() conv.loadPlugins(["plugins"]) conv.start()
import mg_conveyor as conveyor class PluginOne(conveyor.ISequencialPlugin): def sequence(self): return 100 def process(self, identity, context, *args, **kwargs): print("Argument 1: " + args[0]) print("Arguemnt 2: " + args[1]) print("Parameter 1: " + kwargs['param1']) print("Parameter 2: " + kwargs['param2']) return conveyor.STATUS_SUCCESS
[Core] Name = Parameter Passing Module = parameter [Documentation] Author = Ronaldo Webb Version = 0.1 Website = http://www.ronella.xyz Description = Test only
import mg_conveyor as conveyor if __name__ == '__main__': conv=conveyor.Conveyor() conv.loadPlugins(["plugins"]) conv.start("arg0" , "arg1" , param1="This is parameter 1" , param2="This is parameter 2")
import mg_conveyor as conveyor class Utility(conveyor.IUtilityPlugin): def __init__(self): super().__init__() def use(self, *args, **kwargs): self.localContext['Message'] = "I'm in Utility plugin" return "This is a test."
[Core] Name = Utility Module = utility [Documentation] Author = Ronaldo Webb Version = 0.1 Website = http://www.ronella.xyz Description = Test only
import mg_conveyor as conveyor class PluginOne(conveyor.ISequencialPlugin): def sequence(self): return 100 def process(self, identity, context, *args, **kwargs): utilCtx = context['Utility'] utilOutput = utilCtx[conveyor.CONTEXT_KEY_PLUGIN_OBJECT].use() if 'util_message' in kwargs: kwargs['util_message'](utilCtx['Message']) if 'util_output' in kwargs: kwargs['util_output'](utilOutput) return conveyor.STATUS_SUCCESS
[Core] Name = Sequence Module = sequence [Documentation] Author = Ronaldo Webb Version = 0.1 Website = http://www.ronella.xyz Description = Test only
import mg_conveyor as conveyor if __name__ == '__main__': conv=conveyor.Conveyor() conv.loadPlugins(["plugins"]) conv.start()