Package: mg-conveyor
IntroductionSometime 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:Transform or create a CSV file to meet the table requirement based on the CSV file at hand.Wrap the contents of the transformed and newly created CSV to create the SQL statements that can be readily run against a database.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.InstallationThe 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 installationIf you have the wheel packagepython -m pip install mg_conveyor-<version>-py3-none-any.whlOr simplypython -m pip install mg-conveyorUsing the conveyorConveyoris not intended be used as a standalone command but a least a plugin must be made available to it to process.The ISequencialPlugin InterfaceAs its name suggests it is executed based on sequence. This plugin needs the following methods to be implemented:Methods to be implementeddef sequence(self)
def process(self, identity, context, *args, **kwargs)Thesequence methodmust return an integer that will dictate sequence on when it must be executed.NoteEvery pluginmust return a different sequence number.Theprocess methodis 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 theNameattribute) and the context contains some information about the plugin. Also this method must return one of the predefined status.ISequencialPlugin Return StatusStatusDescriptionSTATUS_SUCCESSFinished successfully.STATUS_FAILEDError encountered also abort the execution of conveyorSTATUS_ABORTAbort the execution of conveyor.STATUS_RETRYThe plugin is requesting an interactive retry.STATUS_CONFIRM_RETRYThe user responded Y to the interactive retry.STATUS_CONFIRM_ABORTThe user responded N to the interactive retry.STATUS_SKIPPEDSkipped pluginSTATUS_STARTEDPlugin is currently started.Example 1. Simple Plugin Implementationimport 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_SUCCESSUsing the context ParameterThe 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 identityStatusDescriptionCONTEXT_KEY_STATUSContains the numerical equivalent of theISequencialPlugin Return Status.CONTEXT_KEY_PLUGIN_OBJECTThe 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.Yapsy Configuration FileOnce 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 onlyNoteTheName attributein this configuration file is the one being used as theidentity parameterin the process method.Seeyapsy-pluginfor more information about the content of the configuration file.The IUtilityPlugin InterfaceThis plugin is the one that can be shared by all ISequencialPlugin implementations. Thus, we can think of the IUtilityPlugin implementation as atoolthat 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.Method to be implementeddef use(self, *args, **kwargs)Theuse methodmust 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.Built-in PropertiesThe IUtilityPlugin has built-in properties as we can see in the following table:PropertyDescriptionidentityContains the identity of the plugin (i.e. the Name attribute in yapsy configuration file.)contextThe context shared by all the plugins.localContextThis is equals to context[identity].Acquiring IUtilityPlugin from contextTo 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 IUtilityPluginIf haveUtilityas 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()NoteWe can pass any number of arguments to the use method.Starting The ConveyorOnce 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 Conveyorconv = conveyor.Conveyor()
conv.loadPlugins(["plugins"])
conv.start()NoteIn theloadPlugins methodwe must pass in anarray of locationsof the plugins.AppendixComplete Hello World Implementationplugins/helloworld_plugin.pyimport 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_SUCCESSplugins/helloworld.yapsy-plugin[Core]
Name = Hello World
Module = helloworld_plugin

[Documentation]
Author = Ronaldo Webb
Version = 0.1
Website = http://www.ronella.xyz
Description = Test onlyhelloworld.pyimport mg_conveyor as conveyor

if __name__ == '__main__':

    conv=conveyor.Conveyor()
    conv.loadPlugins(["plugins"])
    conv.start()Sample Context Usageplugins/plugin1.pyimport 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_SUCCESSplugins/plugin1.yapsy-plugin[Core]
Name = Plugin1
Module = plugin1

[Documentation]
Author = Ronaldo Webb
Version = 0.1
Website = http://www.ronella.xyz
Description = Test onlyplugins/plugin2.pyimport 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_SUCCESSplugins/plugin2.yapsy-plugin[Core]
Name = Plugin2
Module = plugin2

[Documentation]
Author = Ronaldo Webb
Version = 0.1
Website = http://www.ronella.xyz
Description = Test onlycontext_usage.pyimport mg_conveyor as conveyor

if __name__ == '__main__':

    conv=conveyor.Conveyor()
    conv.loadPlugins(["plugins"])
    conv.start()Passing Parameter to the Process Methodplugins/parameter.pyimport 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_SUCCESSplugins/parameter.yapsy-plugin[Core]
Name = Parameter Passing
Module = parameter

[Documentation]
Author = Ronaldo Webb
Version = 0.1
Website = http://www.ronella.xyz
Description = Test onlyprocess_parameter.pyimport 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")Basic Sample for using IUtilityPluginutility.pyimport 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."utility.yapsy-plugin[Core]
Name = Utility
Module = utility

[Documentation]
Author = Ronaldo Webb
Version = 0.1
Website = http://www.ronella.xyz
Description = Test onlysequence.pyimport 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_SUCCESSsequence.yapsy-plugin[Core]
Name = Sequence
Module = sequence

[Documentation]
Author = Ronaldo Webb
Version = 0.1
Website = http://www.ronella.xyz
Description = Test onlyutility_usage.pyimport mg_conveyor as conveyor

if __name__ == '__main__':

    conv=conveyor.Conveyor()
    conv.loadPlugins(["plugins"])
    conv.start()