Python User's Guide for STAF Version 3

Last Updated: September 28, 2015


Table of Contents

1.0 Introduction

2.0 Supported Platforms and Python Versions

2.1 Windows (Intel 32-bit)
2.3 Windows (AMD64/Opteron)
2.4 Linux (Intel 32-bit, aka i386 or x86-32)
2.5 Linux (AMD64/Opteron)
2.6 Linux (PPC64-32)
2.7 Linux (PPC64-64)
2.9 Solaris (Sparc 32-bit)
2.10 Solaris (Sparc 64-bit)
2.11 Solaris (AMD64/Opteron x64, aka solaris-x64)
2.12 Solaris (AMD64/Opteron x64, aka solaris-x64-64)
2.13 Solaris (x86 32-bit)
2.14 FreeBSD 7.4+ (i386)
2.15 Mac OS X 10.10+ (Universal binary with support for i386 and x86_64)

3.0 Installation

4.0 Modules PySTAF, PySTAFv3

4.1 Primary STAF APIs 4.1.1 Class STAFHandle
4.1.2 Class STAFResult
4.1.3 Class STAFException
4.1.4 Function wrapData

4.2 Marshalling APIs 4.2.1 Class STAFMapClassDefinition
4.2.2 Class STAFMarshallingContext
4.2.3 Function isMarshalledData
4.2.4 Function marshall
4.2.5 Function unmarshall
4.2.6 Function formatObject

4.3 Private Data Manipulation APIs 4.3.1 Function addPrivacyDelimiters
4.3.2 Function escapePrivacyDelimiters
4.3.3 Function removePrivacyDelimiters
4.3.4 Function maskPrivateData

4.4 Constants
5.0 Module PySTAFMon 5.1 Class STAFMonitor 6.0 Module PySTAFLog 6.1 Class STAFLog 7.0 Python Program Examples 7.1 Example 1


1.0 Introduction

This document describes STAF's V3 support for the Python language.  It includes information on the core STAF Python APIs as well as the wrappers provided for the Monitor and Log services.

STAF Python support must be installed in order to submit requests to STAF via a Python program.

The version of Python used to build the STAF Python libraries is usually the only Python version that will work with those STAF Python libraries.

STAF currently provides STAF Python support on the following platforms:

Section 2.0 provides details about the STAF Python support by platform.

If you want to use a different version of Python or if you want STAF Python support for a different operating system, you can either:

Note: In STAF V3.2.1+, the extension of the PYSTAF extension module for Windows is now .pyd (instead of .dll) because starting in Python 2.5, the .dll extension is no longer supported as a filename extension for Python extension modules on Windows. If you have both PYSTAF.pyd and PYSTAF.dll on your system (e.g. because you had an earlier version of STAF installed or because you downloaded it from a support request), Python finds PYSTAF.pyd before PYSTAF.dll so make sure that the STAF Python extension module you want to use is PYSTAF.pyd.


2.0 Supported Platforms and Python Versions

2.1 Windows (Intel 32-bit)

Supported Python Version Built with PYSTAF library
2.2.x (default) 2.2.1 bin/python22/PYSTAF.pyd
2.3.x 2.3.5 bin/python23/PYSTAF.pyd
2.4.x 2.4.4 bin/python24/PYSTAF.pyd
2.5.x 2.5.4 bin/python25/PYSTAF.pyd
2.6.x 2.6.2 bin/python26/PYSTAF.pyd
2.7.x 2.7.0 bin/python27/PYSTAF.pyd
3.0.x 3.0.1 bin/python30/PYSTAF.pyd
3.1.x 3.1.2 bin/python31/PYSTAF.pyd
3.2.x 3.2.5 bin/python32/PYSTAF.pyd
3.3.x 3.3.5 bin/python33/PYSTAF.pyd
3.4.x 3.4.3 bin/python34/PYSTAF.pyd

2.3 Windows (AMD64/Opteron)

Supported Python Version Built with PYSTAF library
2.5.x (default) 2.5.4 bin/python25/PYSTAF.pyd
2.6.x 2.6.2 bin/python26/PYSTAF.pyd
2.7.x 2.7.0 bin/python27/PYSTAF.pyd
3.0.x 3.0.1 bin/python30/PYSTAF.pyd
3.1.x 3.1.2 bin/python31/PYSTAF.pyd
3.2.x 3.2.5 bin/python32/PYSTAF.pyd
3.3.x 3.3.5 bin/python33/PYSTAF.pyd
3.4.x 3.4.3 bin/python34/PYSTAF.pyd

2.4 Linux (Intel 32-bit, aka i386 or x86-32)

Supported Python Version Built with PYSTAF library
2.2.x (default) 2.2.2 lib/python22/PYSTAF.so
2.3.x 2.3.5 lib/python23/PYSTAF.so
2.4.x 2.4.4 lib/python24/PYSTAF.so
2.5.x 2.5.4 lib/python25/PYSTAF.so
2.6.x 2.6.2 lib/python26/PYSTAF.so
2.7.x 2.7.0 lib/python27/PYSTAF.so
3.0.x 3.0.1 lib/python30/PYSTAF.so
3.1.x 3.1.2 lib/python31/PYSTAF.so
3.2.x 3.2.6 lib/python32/PYSTAF.so
3.3.x 3.3.6 lib/python33/PYSTAF.so
3.4.x 3.4.3 lib/python34/PYSTAF.so

2.5 Linux (AMD64/Opteron)

Supported Python Version Built with PYSTAF library
2.2.x (default) 2.2.3 lib/python22/PYSTAF.so
2.3.x 2.3.5 lib/python23/PYSTAF.so
2.4.x 2.4.4 lib/python24/PYSTAF.so
2.5.x 2.5.4 lib/python25/PYSTAF.so
2.6.x 2.6.2 lib/python26/PYSTAF.so
2.7.x 2.7.0 lib/python27/PYSTAF.so
3.0.x 3.0.1 lib/python30/PYSTAF.so
3.1.x 3.1.2 lib/python31/PYSTAF.so
3.2.x 3.2.6 lib/python32/PYSTAF.so
3.3.x 3.3.6 lib/python33/PYSTAF.so
3.4.x 3.4.3 lib/python34/PYSTAF.so

2.6 Linux (PPC64-32)

Supported Python Version Built with PYSTAF library
2.2.x (default) 2.2.3 lib/python22/PYSTAF.so
2.3.x 2.3.5 lib/python23/PYSTAF.so
2.4.x 2.4.4 lib/python24/PYSTAF.so
2.5.x 2.5.4 lib/python25/PYSTAF.so
2.6.x 2.6.2 lib/python26/PYSTAF.so
2.7.x 2.7.0 lib/python27/PYSTAF.so
3.0.x 3.0.1 lib/python30/PYSTAF.so
3.1.x 3.1.2 lib/python31/PYSTAF.so

2.7 Linux (PPC64-64)

Supported Python Version Built with PYSTAF library
2.2.x (default) 2.2.3 lib/python22/PYSTAF.so
2.3.x 2.3.5 lib/python23/PYSTAF.so
2.4.x 2.4.4 lib/python24/PYSTAF.so
2.5.x 2.5.4 lib/python25/PYSTAF.so
2.6.x 2.6.2 lib/python26/PYSTAF.so
2.7.x 2.7.0 lib/python27/PYSTAF.so
3.0.x 3.0.1 lib/python30/PYSTAF.so
3.1.x 3.1.2 lib/python31/PYSTAF.so

2.9 Solaris (Sparc 32-bit)

Supported Python Version Built with PYSTAF library
2.2.x (default) 2.2.3 lib/python22/PYSTAF.so
2.3.x 2.3.5 lib/python23/PYSTAF.so
2.4.x 2.4.4 lib/python24/PYSTAF.so
2.5.x 2.5.4 lib/python25/PYSTAF.so
2.6.x 2.6.2 lib/python26/PYSTAF.so
2.7.x 2.7.0 lib/python27/PYSTAF.so
3.0.x 3.0.1 lib/python30/PYSTAF.so
3.1.x 3.1.2 lib/python31/PYSTAF.so

2.10 Solaris (Sparc 64-bit)

Supported Python Version Built with PYSTAF library
2.2.x (default) 2.2.3 lib/python22/PYSTAF.so
2.3.x 2.3.5 lib/python23/PYSTAF.so
2.4.x 2.4.4 lib/python24/PYSTAF.so
2.5.x 2.5.4 lib/python25/PYSTAF.so
2.6.x 2.6.2 lib/python26/PYSTAF.so
2.7.x 2.7.0 lib/python27/PYSTAF.so
3.0.x 3.0.1 lib/python30/PYSTAF.so
3.1.x 3.1.2 lib/python31/PYSTAF.so

2.11 Solaris (AMD64/Opteron x64, aka solaris-x64)

Supported Python Version Built with PYSTAF library
2.2.x (default) 2.2.3 lib/python22/PYSTAF.so
2.3.x 2.3.5 lib/python23/PYSTAF.so
2.4.x 2.4.4 lib/python24/PYSTAF.so
2.5.x 2.5.4 lib/python25/PYSTAF.so
2.6.x 2.6.2 lib/python26/PYSTAF.so
2.7.x 2.7.0 lib/python27/PYSTAF.so
3.0.x 3.0.1 lib/python30/PYSTAF.so
3.1.x 3.1.2 lib/python31/PYSTAF.so

2.12 Solaris (AMD64/Opteron x64, aka solaris-x64-64)

Supported Python Version Built with PYSTAF library
2.2.x (default) 2.2.3 lib/python22/PYSTAF.so
2.3.x 2.3.5 lib/python23/PYSTAF.so
2.4.x 2.4.4 lib/python24/PYSTAF.so
2.5.x 2.5.4 lib/python25/PYSTAF.so
2.6.x 2.6.2 lib/python26/PYSTAF.so
2.7.x 2.7.0 lib/python27/PYSTAF.so
3.0.x 3.0.1 lib/python30/PYSTAF.so
3.1.x 3.1.2 lib/python31/PYSTAF.so

2.13 Solaris (x86 32-bit)

Supported Python Version Built with PYSTAF library
2.2.x (default) 2.2.3 lib/python22/PYSTAF.so
2.3.x 2.3.5 lib/python23/PYSTAF.so
2.4.x 2.4.4 lib/python24/PYSTAF.so
2.5.x 2.5.4 lib/python25/PYSTAF.so
2.6.x 2.6.2 lib/python26/PYSTAF.so
2.7.x 2.7.0 lib/python27/PYSTAF.so
3.0.x 3.0.1 lib/python30/PYSTAF.so
3.1.x 3.1.2 lib/python31/PYSTAF.so

2.14 FreeBSD (i386)

Supported Python Version Built with PYSTAF library
2.2.x 2.2.3 lib/python22/PYSTAF.so
2.3.x 2.3.5 lib/python23/PYSTAF.so
2.4.x (default) 2.4.4 lib/python24/PYSTAF.so
2.5.x 2.5.4 lib/python25/PYSTAF.so
2.6.x 2.6.2 lib/python26/PYSTAF.so
2.7.x 2.7.0 lib/python27/PYSTAF.so
3.0.x 3.0.1 lib/python30/PYSTAF.so
3.1.x 3.1.2 lib/python31/PYSTAF.so
3.2.x 3.2.6 lib/python32/PYSTAF.so
3.3.x 3.3.6 lib/python33/PYSTAF.so
3.4.x 3.4.3 lib/python34/PYSTAF.so

2.15 Mac OS X 10.10+ (Universal binary with support for i386 and x86_64)

Supported Python Version Built with PYSTAF library
2.6.x (default) 2.6.1 lib/python26/PYSTAF.so
2.7.x 2.7.0 lib/python27/PYSTAF.so
3.1.x 3.1.2 lib/python31/PYSTAF.so

If when using STAF Python support for Python 3.1 (or later) provided for Mac OS X (Universal), and /usr/local/Python-3.1/lib/libpython3.1.dylib does not exist (e.g. this Python library it is dependent on was not installed at this location on your Mac OS X system), you may get an error similar to the following when you try to import PySTAF:

Python 3.1.2 (default, Apr 19 2012, 00:55:09) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import PySTAF
Fatal Python error: Interpreter not initialized (version mismatch?)
Abort trap: 6
Run dyldinfo -dylibs on the lib/python27/PYSTAF.so file to see its dylibs dependencies. For example:
# dyldinfo -dylibs /Library/staf/lib/python27/PYSTAF.so
for arch i386:
attributes     dependent dylibs
                /opt/dev/autobuild/build/rel/macosx/staf/retail/lib/libSTAF.dylib
                /usr/local/Python-3.1/lib/libpython3.1.dylib
                /usr/lib/libSystem.B.dylib
                /usr/lib/libc++.1.dylib
for arch x86_64:
attributes     dependent dylibs
                /opt/dev/autobuild/build/rel/macosx/staf/retail/lib/libSTAF.dylib
                /usr/local/Python-3.1/lib/libpython3.1.dylib
                /usr/lib/libSystem.B.dylib
                /usr/lib/libc++.1.dylib                
So, PYSTAF.so requires that /usr/local/Python-3.1/lib/libpython3.1.dylib exists. If it does not (e.g. because the Python 3.1 libraries were installed in a different location on your Mac OS X system), you can resolve this problem by creating a symbolic link in this spot pointing to the actual location of libpython3.1.dylib on your system.

Note that STAF Mac OS X (Universal) binaries provided on SourceForge were built on a Mac OS X 10.10 system where Python 3.1 was built from Python source into /usr/local/Python-3.1 and this resulted in libpython3.1.dylib being created in /usr/local/Python-3.1/lib.

Another workaround would be to build STAF support for Python on your Mac OS X system yourself so that you don't have this issue. See the STAF Developer's Guide for more information on building STAF core and STAF support for Python.

3.0 Installation

To install and configure STAF Python support, perform the following steps:

  1. Install STAF Python support by selecting to install "Python support" during the install. It is installed by default for a "typical" install of STAF if using a STAF installer for the platforms listed in section 2.0 Supported Platforms and Python Versions. Currently, STAF Python support is not provided in the STAF installer files for other operating systems (though you can build STAF Python support yourself). See section 1.0 Introduction above for more information on how to build STAF Python support yourself.

    Once STAF Python support is installed, verify that the STAF Python library file exists:

    The library file for the version of Python that was selected as the default Python version during the STAF installation will either have a link in {STAF/Config/STAFRoot}/lib (on Unix) or a copy in {STAF/Config/STAFRoot}/bin on Windows.

  2. To configure STAF Python support, you need to set or update your PYTHONPATH environment variable as follows:

  3. Verify that the version of Python you have installed matches that version of Python that STAF Python support was built with because the version of Python used to build the STAF Python libraries is usually the only Python version that will work with those STAF Python libraries. See section 1.0 Introduction above for more information on the version of Python used to build the STAF Python libraries if using STAF Python support provided with the STAF installer file and how to build STAF Python support for another version of Python.

  4. Finally, you simply need to import the modules (e.g. PySTAF, PySTAFMon, PySTAFLog) described below that you use. For example, to import the PySTAF module, make sure your Python program contains the following line at the beginning:
      from PySTAF import *
    


4.0 Modules PySTAF, PySTAFv3

The PySTAF module provides the base level of support for Python 2.x scripts to call into STAF.

The PySTAFv3 module provides the base level of support for Python 3.x scripts to call into STAF.

These packages externalize some classes, utility functions, and constants. Whether you are using Python 2.x or Python 3.x, usage of these classes, utility functions, and constants is exactly the same. In your Python script, just make sure to import the version that corresponds to the version of Python you are using (i.e use PySTAF with Python 2.x, and use PySTAFv3 with Python 3.x).

The STAF Python classes that it externalizes are:

The STAF Python functions that it externalizes are:

To use this module with Python 2.x you simply import it like so:

To use this module with Python 3.x you simply import it like so:

Since the STAF Python libraries include several files with "PYSTAF" as their file name (with varying cases, i.e. PySTAF, PYSTAF), make sure that you are not using the "PYTHONCASEOK" environment variable. For example, if you have it set as "PYTHONCASEOK=1" and then try to use the STAF Python libraries, you will get the following error:

Traceback (most recent call last):
  File "TestPython.py", line 9, in ?
    from PySTAF import *
ImportError: dynamic module does not define init function (initPySTAF)

To correct this error, set "PYTHONCASEOK=".

4.1 Primary STAF APIs

These APIs allow you to register/unregister with STAF, submit STAF service requests, and to wrap the values of options used in STAF service requests.

4.1.1 Class STAFHandle

Definition

class STAFHandle(handleNameOrNumber[, handleType])
The STAFHandle class is the primary class used to communicate with STAF. It is used to register with STAF, submit service requests to STAF, and unregister with STAF. Each Python script should generally create one and only one STAFHandle instance object. The act of creating this object registers the Python application with STAF.

There are two ways to create a STAFHandle instance object:

If a STAFHandle instance object could not be created, an instance of STAFException is generated.

The required argument handleNameOrHandle specifies a handle name or number. If you specify a standard handle type (the default), this argument must be a string containing a name for the STAF handle to be created. If you specify a static handle type, this argument must be an integer containing the handle number for an existing static STAF handle.

The optional argument handleType specifies the type of handle. The valid handle types are:

Once you have a valid STAFHandle instance object, you can begin submitting requests to STAF services. To do this, you use the submit() method, to which you specify the machine and service which should handle the request, as well as the request string itself.

Before the Python script exits, it should unregister with STAF by calling the unregister() method.

STAFHandle defines the following member variables. They are initialized by the constructor, and should not be modified directly.

STAFHandle defines the following methods:

STAFHandle defines the following constants:

    # STAFHandle types

    Standard = 0
    Static   = 1

    # Modes for submit call

    Synchronous   = 0
    FireAndForget = 1
    Queue         = 2
    Retain        = 3
    QueueRetain   = 4
Note, you can obtain more information on standard vs. static handles and the various submission modes in the C API reference in the STAF User's Guide.

Examples

The following is a Python 2.x example of a program which registers with STAF, calls a couple of STAF services, and then unregisters with STAF.
    from PySTAF import *
    import sys

    try:
        handle = STAFHandle("MyTest")
    except STAFException, e:
        print "Error registering with STAF, RC: %d" % e.rc
        sys.exit(e.rc)

    result = handle.submit("local", "ping", "ping")

    if (result.rc != 0):
        print "Error submitting request, RC: %d, Result: %s" % (result.rc, result.result)

    result = handle.submit("local", "var", "resolve string {STAF/Config/OS/Name}")

    if (result.rc != 0):
        print "Error submitting request, RC: %d, Result: %s" % (result.rc, result.result)
    else:
        print "OS Name: %s" % result.result

    rc = handle.unregister()

    sys.exit(rc)
The following is the same Python script, written to support Python 3.x.
    from PySTAFv3 import * 
    import sys

    try:
        handle = STAFHandle("MyTest")
    except STAFException as e:
        print("Error registering with STAF, RC: %d" % e.rc)
        sys.exit(e.rc)

    result = handle.submit("local", "ping", "ping")

    if (result.rc != 0):
        print("Error submitting request, RC: %d, Result: %s" % (result.rc, result.result))

    result = handle.submit("local", "var", "resolve string {STAF/Config/OS/Name}")

    if (result.rc != 0):
        print("Error submitting request, RC: %d, Result: %s" % (result.rc, result.result))
    else:
        print("OS Name: %s" % result.result)

    rc = handle.unregister()

    sys.exit(rc)
Note that the remaining examples in this document will show scripts that support Python 2.x.

The following is an example of a program which registers with STAF using a static STAF handle. Note that a static handle is a handle which can be shared by several processes on the same system. See the STAF User's Guide for more information on static handles. Generally, you don't need to use a static STAF handle.

    from PySTAF import *

    # First, need to obtain an existing static handle number from somewhere.
    # This example assumes that this Python program will be run by submitting
    # a START request to the STAF PROCESS service using the STATICHANDLENAME
    # option.  This allows it to obtain the STAF static handle number from
    # environment variable STAF_STATIC_HANDLE.
    #
    # Note:  Another way a static handle could have been obtained is if it
    # is passed to the Python program as an argument.  The static handle
    # could have been created by another program (which submitted a CREATE
    # HANDLE request to the HANDLE service).

    import os
    staticHandleNumber = os.environ.get('STAF_STATIC_HANDLE')

    if staticHandleNumber == None:
        print "Error:  STAF_STATIC_HANDLE environment variable does not exist"
        sys.exit(1)

    staticHandleNumber = int(staticHandleNumber)
    print "Using STAF static handle %s" % (staticHandleNumber)
    
    # Register with STAF using a static STAF handle that already exists

    try:
        handle = STAFHandle(staticHandleNumber, STAFHandle.Static)
    except STAFException, e:
        print "Error registering with STAF, RC: %d" % e.rc
        sys.exit(e.rc)

    result = handle.submit("local", "ping", "ping")

    if (result.rc != 0):
        print "Error submitting request, RC: %d, Result: %s" % (result.rc, result.result)

4.1.2 Class STAFResult

Definition

This class encapsulates the result of a STAF service request (made via the STAFHandle.submit() method). This class also contains a set of constants representing the various common STAF return codes. It has two constructors:

class STAFResult([rc][, result])
The optional argument rc specifies the numeric return code of the service request. The default is 0.
The optional argument result specifies a string result buffer returned from the service request. The default is "".

class STAFResult(rc,result,doUnmarshallResult)
The required argument rc specifies the numeric return code of the service request.
The required argument result specifies a string result buffer returned from the service request.
The optional argument doUnmarshallResult specifies whether to enable auto-unmarshalling. Set to a true value (e.g. 1) to enable or a false value (e.g. 0) to disable. Note: This argument was added in STAF V3.3.1.

STAFResult defines the following member variables. They are initialized by the constructor, and should not be modified directly.

STAFResult defines the following constants representing the various common STAF return codes.

    Ok                          = 0
    InvalidAPI                  = 1
    UnknownService              = 2
    InvalidHandle               = 3
    HandleAlreadyExists         = 4
    HandleDoesNotExist          = 5
    UnknownError                = 6
    InvalidRequestString        = 7
    InvalidServiceResult        = 8
    REXXError                   = 9
    BaseOSError                 = 10
    ProcessAlreadyComplete      = 11
    ProcessNotComplete          = 12
    VariableDoesNotExist        = 13
    UnResolvableString          = 14
    InvalidResolveString        = 15
    NoPathToMachine             = 16
    FileOpenError               = 17
    FileReadError               = 18
    FileWriteError              = 19
    FileDeleteError             = 20
    STAFNotRunning              = 21
    CommunicationError          = 22
    TrusteeDoesNotExist         = 23
    InvalidTrustLevel           = 24
    AccessDenied                = 25
    STAFRegistrationError       = 26
    ServiceConfigurationError   = 27
    QueueFull                   = 28
    NoQueueElement              = 29
    NotifieeDoesNotExist        = 30
    InvalidAPILevel             = 31
    ServiceNotUnregisterable    = 32
    ServiceNotAvailable         = 33
    SemaphoreDoesNotExist       = 34
    NotSemaphoreOwner           = 35
    SemaphoreHasPendingRequests = 36
    Timeout                     = 37
    JavaError                   = 38
    ConverterError              = 39
    InvalidObject               = 41
    InvalidParm                 = 42
    RequestNumberNotFound       = 43
    InvalidAsynchOption         = 44
    RequestNotComplete          = 45
    ProcessAuthenticationDenied = 46
    InvalidValue                = 47
    DoesNotExist                = 48
    AlreadyExists               = 49
    DirectoryNotEmpty           = 50
    DirectoryCopyError          = 51
    DiagnosticsNotEnabled       = 52
    HandleAuthenticationDenied  = 53
    HandleAlreadyAuthenticated  = 54
    InvalidSTAFVersion          = 55
    RequestCancelled            = 56
    CreateThreadError           = 57
    MaximumSizeExceeded         = 58
    MaximumHandlesExceeded      = 59

Examples

  1. The following example shows the use of the STAFResult class when submitting a request to a STAF service that returns a single string result.

  2. The following example shows the use of the STAFResult class when submitting a request to a STAF service that returns a marshalled result string.

4.1.3 Class STAFException

Definition

class STAFException([rc][, result])
This class is the base exception class used by the STAF modules. Currently, this class is only used when trying to register or unregister with STAF.

The optional argument rc specifies the numeric return code which is the basis of the exception. The default is 0.
The optional argument result specifies a string which further describes the exception. The default is "".

STAFException defines the following member variables. They are initialized by the constructor, and should not be modified directly.

Examples

The following is an example of a program which shows how to deal with a STAFException when registering with STAF.

4.1.4 Function wrapData

Definition

Note that STAFWrapData is an alias for the wrapData function and can be used instead to call this function.

Examples

This example creates a colon-length-colon delimited version of string Hello world. The resulting string is :11:Hello world. This example uses the wrapData function for the semaphore name (which contains spaces) in an EVENT POST request to the SEM service.

4.2 Marshalling APIs

These APIs allow you to define, manipulate, and (un)marshall data structures, and print marshalled data in human-readable ("pretty" print) format.

STAF supports the automatic marshalling and unmarshalling of structured data. The act of marshalling takes a data structure and converts it into a string-based representation. The act of unmarshalling reverses this and converts the string-based representation back into a data structure. See Section 6.1, "Marshalling Structured Data" in the STAF User's Guide for more information.

4.2.1 Class STAFMapClassDefinition

Definition

class STAFMapClassDefinition([name])
A class which provides the metadata associated with a map class. In particular, it defines the keys associated with the map class. This class is used to create and/or access a STAF map class definition which can be useful if you want to generate a STAF marshalling context with map classes. The map class definition is used to reduce the size of a marshalling map class in comparison to a map containing the same data. It also contains information about how to display instances of the map class, such as the order in which to display the keys and the display names to use for the keys. You get and set map class definitions using the STAFMarshallingContext class setMapClassDefinition and getMapClassDefinition functions.

The optional keyword argument name specifies the name of the STAF map class definition. The default is None.

STAFMapClassDefinition defines the following methods:

Example

The following is an example of how to create a map class definition named 'Test/MyMap' containing four keys, each with a display name, and one with a short display name. This example prints the following:

4.2.2 Class STAFMarshallingContext

Definition

class STAFMarshallingContext([obj])
A class is used to create and/or access a STAF marshalling context which is used by STAF to help in marshalling and unmarshalling data. A marshalling context is simply a container for map class definitions and a data structure that uses (or is defined in terms of) them.

In order to use a map class when marshalling data, you must add the map class definition to the marshalling context, set the root object of the marshalling context to the object you want to marshall, and then marshall the marshalling context itself. When you unmarshall a data structure, you will always receive a marshalling context. Any map class definitions referenced by map classes within the data structure will be present in the marshalling context.

The primary use of this class is to represent multi-valued results that consist of a data structure (e.g. results from a QUERY/LIST service request, etc.) as a string that can also be converted back into the data structure. This string can be assigned to the string result buffer returned from the service request.

The optional keyword argument obj specifies the root object to be marshalled. The default is None.

STAFMarshallingContext defines the following methods:

Examples

The following is an example of how to create a marshalling context containing one map class definition named 'Test/MyMap' and a root object which is a list of maps defined by the map class definition. Then it shows how to marshall and unmarshall the marshalling context. This example prints the following:

4.2.3 Function isMarshalledData

Definition

Example

4.2.4 Function marshall

Definition

Examples

This example converts a Python dictionary and converts it into a string-based marshalled representation. It also shows how to convert this marshalled string back into the original Python dictionary.

This example creates a marshalling context with one map class definition and a Python list where each entry is a map. It then creates a string-based marshalled representation of it.

4.2.5 Function unmarshall

Definition

Example

4.2.6 Function formatObject

Definition

Examples

This example that prints a Python dictionary in a "pretty" verbose format: This could result in the following output printed:

This example prints the result from a FS QUERY ENTRY request in a "pretty" verbose format:

If successful, this could result in the following output printed: However, note that specifying instead of specifying formatObject(result.resultObj, result.resultContext), you could just print the marshalling context itself, result.resultContext, to get the same output as the str function for a STAFMarshallingContext object calls the formatObject function. For example:

4.3 Private Data Manipulation APIs

These APIs allow you to handle private data. See Section 7.3, "Private Data" in the STAF User's Guide for more information about handling private data.

4.3.1 Function addPrivacyDelimiters

Definition

Note that STAFAddPrivacyDelimiters is an alias for the addPrivacyDelimiters function and can be used instead to call this function.

Examples

This example adds privacy delimiters to 'passw0rd' used in the PASSWORD option when starting a process as another user. This example adds privacy delimiters to password 'secret' used in the COMMAND option when starting a process.

4.3.2 Function escapePrivacyDelimiters

Definition

Note that STAFEscapePrivacyDelimiters is an alias for the escapePrivacyDelimiters function and can be used instead to call this function.

Examples

This example escapes privacy delimiters in password 'passw@!!d' before adding privacy delimiters to it and then uses the password in the PASSWORD option when starting a process as another user.

4.3.3 Function removePrivacyDelimiters

Definition

Note that STAFRemovePrivacyDelimiters is an alias for the removePrivacyDelimiters function and can be used instead to call this function.

Examples

This example removes privacy delimiters from protected password '!!@secret@!!' and assigns 'secret' as the password.

4.3.4 Function maskPrivateData

Definition

Note that STAFMaskPrivateData is an alias for the maskPrivateData function and can be used instead to call this function.

Examples

This example masks any private data indicated by privacy delimiters in a request string before displaying it. This example prints:
START COMMAND C:/tests/TestA.exe USERNAME Test1 PASSWORD **************

4.4 Constants

Constants defined in this module include:


5.0 Module PySTAFMon

The PySTAFMon module provides a class to ease the use of the Monitor service.  To use this module you simply import it like so:
    from PySTAFMon import *

5.1 Class STAFMonitor

Definition

class STAFMonitor(stafHandle[, system][, service])
A class used to provide a wrapper around the Monitor service. It also contains a set of constants representing the Monitor service return codes. The primary method of interest is the log() method which allows you to log a message to the STAF Monitor service.

The required keyword argument stafHandle specifies the STAF handle to use to submit the request to the Monitor service.

The optional keyword argument system specifies the endpoint of the machine to submit the request to. The default is "local".

The optional keyword argument service specifies the registered name of the STAF Monitor service. The default is "Monitor".

Note: By default, the STAFMonitor class will use the service named "Monitor" on the local system. This can be changed by explicitly specifying the system and/or service when you construct the STAFMonitor object.

STAFMonitor defines the following methods:

Examples

The following example shows the use of the STAFMonitor class to log a status message to the Monitor service.
    from PySTAFMon import *

    # The variable "handle" is an instance of the STAFHandle class that was
    # previously instantiated

    monitor = STAFMonitor(handle)
    result = monitor.log("Beginning section ABC of test")


6.0 Module PySTAFLog

The PySTAFLog module provides a class to ease the use of the Log service.  To use this module you simply import it like so:
    from PySTAFLog import *

6.1 Class STAFLog

Definition

class STAFLog(handle, logType, name[, monitorMask][, system][, service])
A class used to provide a wrapper around the Log service. It provides a log() method for logging a message to the Log service. It also contains constants for the various log file types and logging levels, as well as the return codes returned by the Log service. This wrapper also allows you to specify a set of log levels for which the messages logged to the Log service will also be logged to the Monitor service.

The required keyword argument handle specifies the STAF handle to use to submit the request to the Log service.

The required keyword argument logType specifies the log type (e.g. STAFLog.Global, STAFLog.Machine, or STAFLog.Handle)

The required keyword argument name specifies the name of the log.

The optional keyword argument monitorMask specifies a mask that consists of a list of logging levels. This list controls which messages are also sent to the Monitor service. The default is to enable "Fatal", "Error", "Warning", "Start", "Stop", "Pass", and "Fail". If there is an error logging, STAFLog will also try to send an error to the Monitor service.

The optional keyword argument system specifies the endpoint of the machine to submit the log request to. The default is "local".

The optional keyword argument service specifies the registered name of the STAF Log service. The default is "Log".

Note: By default, the STAFLog class will use the service named "Log" on the local system. This can be changed by explicitly specifying the system and/or service when you construct the STAFLog object.

STAFLog defines the following methods:

STAFLog defines the following constants:

    # Log type constants

    Global  = "GLOBAL"
    Machine = "MACHINE"
    Handle  = "HANDLE"

    # Log level constants

    Fatal     = "Fatal"
    Error     = "Error"
    Warning   = "Warning"
    Info      = "Info"
    Trace     = "Trace"
    Trace2    = "Trace2"
    Trace3    = "Trace3"
    Debug     = "Debug"
    Debug2    = "Debug2"
    Debug3    = "Debug3"
    Start     = "Start"
    Stop      = "Stop"
    Pass      = "Pass"
    Fail      = "Fail"
    Status    = "Status"
    User1     = "User1"
    User2     = "User2"
    User3     = "User3"
    User4     = "User4"
    User5     = "User5"
    User6     = "User6"
    User7     = "User7"
    User8     = "User8"

    # Log service return codes

    InvalidLevel                = 4004
    InvalidLogFileFormat        = 4007
    PurgeFailure                = 4008

Examples

The following example shows the use of the STAFLog class to log some messages.

7.0 Python Program Examples

7.1 Example 1

This example shows a Python program that tests most of the STAF Python APIs.

The source code for this program is provided in the STAF source code directory tree at src/staf/lang/python/TestPython.py. See section "Obtaining the STAF Source Code" in the STAF Developer's Guide for instructions on how to download STAF source code.

Note that a version of this script for Python 3.x is provided in the STAF source code directory tree at src/staf/lang/python/TestPythonV3.py.

This example could print the following when run:

*** End of Document ***