Python Script 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 Script plugin structure

Python Data Source plugin has Import and Export fields that can be added via Field tab

  • Import fields consist of several types of fields:
    • String
    • Int
    • Float
    • Structure - equals to python dictionary data structure.
      Can consist of String, Int, Float
    • Table - equals to python list data structure.
      Can consist of 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:
    • String
    • Int
    • Float
    • Table - equals to python list data structure.
      Can consist of String, Int, Float
    • Structure - equals to python dictionary data structure.
      Can consist of String, Int, Float

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

Also you can add fields with Smart Fields Creator.

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 up if default value is used. It is possible if you click Ignore Source or Only If Source Empty. 

Only If Source Empty is available when field is mapped.

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.

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.

Plugin's 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
# settings - json with settings
# imports, exports - json with import and export structures
# simulate
# log
# path     - template path (may be empty)

#dowork- method that is specified in Python Data Source plugin settings
def dowork(self, jsonData, settings, imports, exports, simulate, log, logref, path):
    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
# settings - json with settings
# imports, exports - json with import and export structures
# simulate
# log
# path     - template path (may be empty)

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

This code returns the following json:

{  
  "table_field" : [
    {
      'string_field' : 0
    },
    {
      'string_field' : 1
    }
  ]
}

Using 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
  • Button
  • Combo Box - dropdown list

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

How settings variables are retrieved from Python code

When using Structure script to build import and export fields:

  • String, Directory Path and File Name:

#Pre-requisites:
import json

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

def getParameters(self, jsonData, path):
    import_dict = json.loads(jsonData)
    variable = import_dict['VARIABLE_NAME']

  • Password:

#Pre-requisites:
import json

#Password manager:

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

def getParameters(self, jsonData, path):
    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)

When using Main script to use this variables when executing the script:

  • String, Directory Path and File Name:

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

#LINQ
clr.AddReference("System.Core")
import System
clr.ImportExtensions(System.Linq)

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

def dowork(self, jsonData, settings, imports, exports, simulate, log, logref, path):
    variable_value = settings.First(lambda x: x.Name == 'VARIABLE_NAME').ImportConvertation.DefaultValue

  • Password:

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

#LINQ
clr.AddReference("System.Core")
import System
clr.ImportExtensions(System.Linq)

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

def dowork(self, jsonData, settings, imports, exports, simulate, log, logref, path):
    pass_manager = settings.First(lambda x: x.Name == 'VARIABLE_NAME').ImportConvertation.DefaultValue
    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
}

There must be import and export structures that consist of fields, tables and structures lists.

{  
  "import" : {
      'fields' : [],
      'tables' : [],
      'structures' : []
  },
  "export" : {
      'fields' : [],
      'tables' : [],
      'structures' : []
  }
}

Every field in fields list has several parameters:

  • type - can be string, float or int
  • name
  • vis - is this field visible or hidden after being built
  • dv - default value. (only for import fields)
  • attr - list of field attributes, simply saying - list of flags

List of field attributes attr has:

  • name - name of the flag
  • char - Character that represents this field. Will be shown in the upper left corner of the field.
  • color - Color of the flag
  • selected - Is flag selected or nor

f_attr = {
    'name' : 'Key',
    'char': 'K',
    'color' : 'blue',
    'selected': True
}

field = {
    'type': 'string',
    'name': 'export_field',
    'vis' : True,
    'dv': 'Default value',
    'attr': [f_attr]
}

Here is a sample of building the following structure:

#Pre-requisites:
import json

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

def getParameters(self, jsonData, path):
    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 = []
    f_attrs = []
    f_attr = {
        'name' : 'Key',
        'char': 'K',
        'color' : 'blue',
        'selected': True
    }
    f_attrs.append(f_attr)
    field = {
        'type': 'string',
        'name': 'export_field',
        'vis' : True,
        'dv': 'Default value',
        'attr': f_attrs
    }
    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. New windows with settings will appear

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

Note

Is Closed checkitem will hide all source code on the plugin.
Remove All Fields will erase all existing import, export fields and settings values.

  5. You can add custom notes in Notes tab .

  6. You can attach custom libraries in the Libraries tab.

  7. 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.