Scripting ADAM with IronPython - Part 2

A previous blogpost introduced the exciting idea of using the ADAM API from within a Python script by hosting the script engine in an ADAM command line utility. Today we take this idea one step further by discussing some improvements that will allow you to run more interesting Python scripts.

Python Power

Let's dive right in by showing an example of the kind of Python script that we would like to run in practice. Here you have a basic directory walker that creates classifications based on the file system hierarchy and adds a record to the database for any file it encounters:

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import sys
import os.path 
import Adam.Core

directories = []
for arg in sys.argv:
 directories.append(arg)

helper = Adam.Core.Classifications.ClassificationHelper(sys.adamApp)

while len(directories)>0:
    directory = directories.pop()
    if os.path.isdir(directory):
        path = directory[2:].replace('\\', '/')
        print 'Creating classification ' + path
        classpath = Adam.Core.Classifications.ClassificationPath(path)
        helper.CreateClassificationUsingPath(classpath)
        for name in os.listdir(directory):
            fullpath = os.path.join(directory,name)
            if os.path.isfile(fullpath):
                print 'Adding record ' + fullpath
                record = Adam.Core.Records.Record(sys.adamApp)
                record.AddNew()
                record.Classifications.Add(classpath)
                record.Files.AddFile(fullpath)
                record.Save()
            elif os.path.isdir(fullpath):
                directories.append(fullpath)

While this is still a somewhat trivial bit of code, it may already give you an idea of the potential benefits of using scripts like this for rapid prototyping or testing purposes. However, in order to run the above script, we need to make some adaptations to the original command line utility.

Improved Hosting Facilities

Note that the script imports the os.path module (apart from the sys module which is always available). Most real-life scripts will use additional Python modules, and for that to work we need to add a reference to the IronPython.Modules assembly in our Adam.Scripting.CommandLine project.

We also need to add the IronPython library to the search path of the hosted script engine:

C#
1
2
3
4
5
6
// Initialize the IronPython engine.
ScriptEngine engine = Python.CreateEngine();

ICollection<string> paths = engine.GetSearchPaths();
paths.Add(@"C:\Program Files\IronPython 2.6\Lib");
engine.SetSearchPaths(paths);

Another improvement is the ability to pass on arguments from the command line to the script. For instance, our example script allows to specify multiple root directories. When run through the command line utility, this is what we want it to look like:

Adam.Scripting.CommandLine RunScript -script="AddFiles.py C:\Dir1 C:\Dir2"

In Python, the sys.argv variable typically contains the command line arguments (including the name of the script being run). The following code passes the arguments from the command line of the C# application to the environment of the hosted script engine:

C#
1
2
3
string[] script = arguments.GetVariableValue("script").Split(' ');

engine.GetSysModule().SetVariable("argv", script);

Similarly, we can add additional variables (like the adamApp object) to the scope of the sys module in order to shield them off from regular Python variables.

Bottomline

These simple improvements to our scripting command line application should allow you to cross the obstacles that you may encounter when writing more interesting and practically useful Python scripts using the ADAM API.

Sample Code

The article contains sample code project(s).
You must be logged in to view or download sample code.
Sign in now

Comments

Leave a comment
You must be logged in to post comments.
Sign in now
 
 
Technical
Business
rss feed