Python Data Source plugin

Python Data Source plugin settings

Note: No python installation is required.
This plugin uses IronPython that is an implementation of the Python programming language targeting the .NET Framework and Mono. IronPython can use the .NET Framework and Python libraries, and other .NET languages can use Python code just as easily.

  • Description - short description that will be shown as plugin's name.
  • Notes - to write down any notes about the plugin.
  • Editor - opens python scripts editor.

Note: By default python class PyClass and method dowork is evaluated. It can be changed in plugin's Settings.

It is possible to:

  • Blank - clear current Python script
  • Open - open custom Python script
  • Save - save current Python script
  • Check - check whether script's code is valid or not

Python Data Source plugin structure

Python Data Source plugin has 2 types of fields that can be added via Field tab:

  • Import fields consist of several types of fields:
    • String
    • Int
    • Float
    • Password - dropdown list with password added via Password manager. For more info go to ...
  • Export fields consist of the following types of fields:
    • Table - equals to python list data structure
    • Structure - equals to python dictionary data structure

To add any field enter its name and click Add or press Enter:

Tip: You can change field's Data Type of the added field. Simply click on the field and pick needed type from dropdown list next to Data Type.

Every of all import fields has Default Value parameter that can be edited if you click on the field.
The "D" flag in the upper left corner of every field shows that default value is used.

You can additionally mark Convert to Upper Case checkbox on String field to convert it to upper case.
Flag "U" will appear next to "D" flag.

Both Table and Structure fields can contain only String, Int and Float type of fields.
Each of them can be marked as key field with checking Key checkbox on field's settings.
"K" flag will appear in the upper left corner of the field.


Python Data Source plugin script structure

Python Data Source plugin script gets data in JSON format based on import fields structure and must return data in json format according to export fields structure.

Under Advanced section you can find:

  • Class Name field
  • Method Name field - this is method of the Class that is executed with 2 parameters - jsonData and path

jsonData - contains json string made from import fields

path - optional parameter that can be NULL, contains path to template in case of template made of this plugin. More in Creating Templates.

#Pre-requisites:
import json

#jsonData - json string
#path     - template path (may be empty)

def dowork(self, jsonData, path): #dowork- method that is specified in Python Data Source plugin settings
    string_variable = json.loads(jsonData)

Returning json

Here is an example of returning Table "table_field" with String field "string_field"

Note: You can find same examples in plugin by default.

#Pre-requisites:
import json

#jsonData - json string
#path     - template path (may be empty)

def dowork(self, jsonData, path): #dowork- method that is specified in Python Data Source plugin settings
    string_variable = json.loads(jsonData)
    #return
    #type: i - Information, e - error, string, int, float
    return_structure = {
        'type' : 'string',
        'string_field': 'Test value'
    }
    return_table=[]
    return_table.append(return_structure)
    export_structure = {
        'table_field': return_table
    }
    return_json = json.dumps(export_structure)
    return return_json


Use Smart Fields Creator checkbox

It is possible to add several types of variables that will trigger dynamic structure building.

  • String - e.g. Table name in SQL Database to pass it to the python script and then retrieve the structure.
  • Password - e.g. Gmail user & password
  • Directory Path
  • File Name - e.g. CSV file to parse it into plugins structure

 

When Use Smart Fields Creator checkbox is enabled go to Editor  and you will find that there is one more Python script available on the Structure tab

This is the script that builds plugin's structure according to given variables

How variables are retrieved from Python code:

  • String, Directory Path and File Name:

#Pre-requisites:
import json, sys, os, clr

#jsonData - json string
#path     - template path (may be empty)

def dowork(self, jsonData, path): #dowork- method that is specified in Python Data Source plugin settings
    import_dict = json.loads(jsonData)
    variable = import_dict['VARIABLE_NAME']

  • Password:

#Pre-requisites:
import json, sys, os, clr

#Password manager:

sys.path.append(os.getcwd()) #current path
clr.AddReference('VeloX.Core.dll')
from VeloX.Core.PasswordManager import PasswordManager

def dowork(self, jsonData, path): #dowork- method that is specified in Python Data Source plugin settings
    import_dict = json.loads(jsonData)
    pass_manager = import_dict['VARIABLE_NAME']
    um = PasswordManager.GetCredentials(pass_manager) #PasswordManager object
    res = client.Connect(um.User, um.Password)


Building export and import structures

Go to Structure script

To build a structure you need executed method to return json in the following format

Accordion 1

export

tables

name

fields

type

name

key

structures

fields

import

return

{
    'export' 
   
       'tables'          #Python list of table lists
              [{
              'name'      'EXPORT_TABLE_NAME' #Table name

              'fields'    [{
                              'type'   'string'
                                         'int'
                                         'float'

                              'name'     'FIELD_NAME'

                              'key'      True
                                         False
                                         optional
                          }]

              }]

      'structures':     #Python list of structure dictionaries
              [{

            'name'        'EXPORT_STRUCTURE_NAME' #Structure name
             'fields' 

                          [{
                           'type'   'string'
                                      'int'
                                      'float'

                           'name'     'FIELD_NAME'

                           'key'      True
                                      False
                                      optional
                          }]

            }],
      'fields'        #Python list of structure fields

            [{        'type'       'string'
                                      'int'
                                      'float'

                        'name'        'FIELD_NAME'

                        'key'         True
                                      False
                                      optional
            }]
    },
    'import':  

    {
      'tables':         #Python list of table lists
      [{
          'name': 'EXPORT_TABLE_NAME' #Table name
          'fields' :

          [{
            'type'   'string'
                       'int'
                       'float'

            'name'     'FIELD_NAME'

            'dv'     'DEFAULT_VALUE'

            'key'      True
                       False
                       optional
          }]

      }],

      'structures':     #Python list of structure dictionaries
      [{    

          'name': 'EXPORT_STRUCTURE_NAME' #Structure name
          'fields' :

          [{
            'type'   'string'
                       'int'
                       'float'

            'dv'     'DEFAULT_VALUE'

            'name'     'FIELD_NAME'

            'key'      True
                       False
                       optional
          }]

      }],
      'fields':        #Python list of structure fields
      [{
        'type'       'string'
                       'int'
                       'float'

        'name'         'FIELD_NAME'

        'dv'     'DEFAULT_VALUE'

        'key'          True
                       False
                       optional
    }]
    },
    'return': return_structure
}

Here is a sample of building the following structure:

#Pre-requisites:
import json

#jsonData - json string
#path     - template path (may be empty)

def dowork(self, jsonData, path): #dowork- method that is specified in Python Data Source plugin settings
    string_variable = json.loads(jsonData)
    #return
    #type: i - Information, e - error, string, int, float
    return_structure = {
        'type' : 'i',
        'message': 'Return message'
    }
    export_tables = []
    table_fields = []
    field = {
        'type': 'string',
        'name': 'export_field'
        'key' : True
    }
    table_fields.append(field)
    table = {
        'name': 'export_table' #Table name
        'fields' : table_fields
    }
    export_tables.append(table)
    export_structure = {
        'export': {
            'tables': export_tables,
            'structures': [], #Can be skipped if empty
            'fields': [] #Can be skipped if empty
        },
        'import': { #Can be empty
            'tables': [], #Can be skipped if empty
            'structures': [], #Can be skipped if empty
            'fields': [] #Can be skipped if empty
        },
        'return': return_structure
    }
    return_json = json.dumps(export_structure)
    return return_json


Creating Template from Python Plugin

  1. Expand Advanced section on Python plugin settings.

  2. Click Create Template button

  3. You are able to define PluginName, Description, Url, Version and GroupName of the plugin's template.

  4. You can add custom note in Notes tab .

  5. Click Save to save the plugin. You will be asked to add this template in the plugins section. Click Yes if you want to use it on current device or No if you just want to export the plugin for the futher importing.