Entry
PyOsp Overview (From OSP5.2 Man Pages)
Aug 21st, 2001 08:47
Engineer TCSI, Darrell Roarty, PyOsp Overview (from OSP5.2 man pages)
The following information was obtained from the OSP5.2 man pages.
== BEGIN PyOsp overview
OVERVIEW
PyOsp offers real-time interpreted interaction with OSP TOMs via the
Python scripting language.
PyOsp is a standalone executable. It is a standard Python interpreter
with custom extensions as follows:
Integrates an OSP command interface to the Operation, Find, Event,
and Lock services
Handles all the low-level details of using OSP-such as Sessions,
Invokers, Proxies and
Params-enabling utilization with minimal OSP knowledge
Processes OSP return values and events, giving access to results in
Python variables
For more details and a tutorial, including an introduction to Python,
please consult the OSP Developer's Guide chapter on Python Scripting.
For information about enabling and programming AO operations using
Python, see the Python_Operations manpage. For information about
Python, see the python manpage (if installed) or http://www.python.org.
Limitations
You cannot manually set the access ID (accessId) for requests to the
TOM. It is automatically set to the UNIX user id of the process that is
running PyOsp.
When building Python itself, edit $PYTHONROOT/Modules/Setup to enable
compiling of the _tkinter module.
Running PyOsp
1. The first step is to invoke MOSC to generate class information using
both py.class.template and tcl.class.template. This produces files
called <unit>.py and <unit>.tcl, where <unit> corresponds to your MOSC
unit name; once created, it will not have to be regenerated unless MOSU
changes are made.
You will want to run the following command in the directory with
<unit>.moslog so that the correct class codes are are used.
($OSPROOT/bin is presumed to be in your shell PATH.)
csh% mosc -template $OSPROOT/config/mosc/py.class.template \\
-template $OSPROOT/config/mosc/tcl.class.template \\
$RPGROOT/include/abCommon <unit>.mosu
2. Set the PYTHONPATH environment variable to point to the generated
Python files, and the Python files provided with OSP. If the Python
files generated above are in the current directory, then use
csh% setenv PYTHONPATH .:$OSPROOT/config/python
For systems built using TMK, you will probably copy the Python files to
the installation config/python directory:
csh% cp *.py $InstallRootDir/config/python
csh% setenv PYTHONPATH \\
$InstallRootDir/config/python:$OSPROOT/config/python
2. Execute PyOsp, which resides in $OSPROOT/bin, specifying a nameserver
using usual OSP client arguments after an initial dash character:
csh% PyOsp - [+i <id>] +ns <id> <host> <port>
As with an ordinary Python interpreter, you may enter standard Python
commands at the >>> prompt.
3. Import the generated Python module from step 1. Each class, along
with its attributes and operations, can then be examined by calling the
._Describe() method of the class. This method shows the attributes and
operations of the class, in Tcl format. You can now interact with a TOM
to manipluate instances of these classes.
>>> import <unit>
Datatypes
In Tcl, all types are handled as strings. Therefore, within PyOsp, types
are meaningful only in
regards to their communication with a TOM.
int, maps to Python int; will perform standard coercions
double, maps to Python float (which is double precision), will
perform standard coercions
string, maps to Python string
boolean, maps to Osp.true and Osp.false; will coerce '0/t/T' and
'1/f/F'
oid, maps to a stub object instance; will coerce tuples or lists of
ints or a Tcl-style string list
in the order (tomId, classId, instLeft, instRite).
oidstring, maps to a tuple of (stub,string); will coerce tuples as
above with an additional
trailing string name
time, maps to a long int or None (for an invalid time)
oidlist, maps to a tuple of oid stubs; will coerce a list
oidstring, maps to a tuple of oidstring tuples, will coerce a list
stringlist, maps to a tuple of strings, will coerce a list
intlist, maps to a tuple of ints, will coerce a list
getlist, maps to a tuple of attribute names
setlist, maps to a tuple of (name,value) tuples
Command Summary
Here is a list of commands and their parameters.
Operations
For all operations, the lockId can be specified by first setting
Osp.Context['LockId'].
Create
<unitName>.<className>(<initialValues>, [<_tom>])
<initialValues>
List of name-value elements in the form name = value. Note
that name is simply the
symbol for the name, not a string (e.g., foo = 10, not foo =
10). All items are
optional, so an empty list is valid unless initial values are
required by the AO.
_tom
Optional TOM ID number. If suppplied, the new AO will be
created in the TOM
indicated. If not supplied, the new AO will be created in some
TOM that manages
the AO class.
Returns: <stub>
<stub>
A stub instance. This can be used as the target for operations
on the Tom AO. This
has a local member _oid, which is a tuple of integers
Example:
>>> guido = Phone.Employee{empId = 99, firstName = 'Guido',
lastName = 'van Rossum')
Operation
<stubInstance>.<opName>(<inParams>)
<stubInstance>
A variable holding a stub, returned as an out parameter (as an
oid, oidstring, oidlist,
etc.) of a Find or an operation.
<opName>
The name of the operation
<inParams>
The in and inout parameters of the operation in order as
specified in the MOSU,
passed as a normal comma separated sequence of paremeters to a
Python method.
Returns: <outParams>
<outParams>
The out and inout parameters of the operation in order as
specifed in the MOSU. If
there are no parameters, None is returned. If there is one
parameter, that parameter
is returned. If there is more than one parameter, a tuple of
parameters is returned.
Example:
MOSU
unit myTom;
class myClass {
modifier Op1(in string x, inout boolean y, out int z);
};
Python
(y, z) = aMyClassInst.Op1('string for x', Osp.true)
Get (single attribute)
<stubInstance>.<attrName> [as rval]
<attrName>
The name of the attribute to be fetched. Exactly as specified
in the MOSU.
Returns: <attrValue>
<attrValue>
The python mapping of the value of the attribute.
Example:
id = guido.empId
Get (multiple attributes)
<Name>.Get(<getlist>)
<getlist>
A list of string names of the attributes to be fetched.
Returns: <GetResult>
<GetResult>
An instance of Osp.Result, which is a class that exports the
elements of the get result
list as attributes of the returned instance
Example:
>>> res = guido.Get('empId', 'lastName')
>>> id = res.empId
>>> name = res.lastName
Set (single attribute)
<stubInstance>.<attrName = <attrValue>
<attrName>
The name of the attribute to be fetched. Exactly as specified
in the MOSU.
<attrValue>
The python mapping of the value of the attribute.
Returns: N/A
Example:
guido.lastName = 'vanRossum'
Set (multiple attributes)
<Name>.Set(<namedValues>)
<namedValues>
List of name-value elements in the form name = value. Note
that name is simply the
symbol for the name, not a string (e.g., foo = 10, not foo =
10). All items are
optional, so an empty list is valid, but does not make sense.
If only one item is being
set, the single attribute form is simpler.
Returns: <SetResult>
<SetResult>
An instance of Osp.Result, which is a class that exports the
new values from the set
result list as attributes of the returned instance.
Example:
guido.Set(firstName = 'Guidoh', lastName = 'VanRossum')
Find Service
OSP.Find(<unit>.<class>, [<sqlClause>], [<tomId>], [<local>],
[<bestEffort>])
Execute a Find request to search the database for AOs of the specified
class.
<unit>.<class>
The OSP unit and class of objects on which to execute the find
<sqlClause>
The OSP Find sql clause. If this does not begin with WHERE,
the where is added
automatically. An empty query finds all instances of the
class.
<tomId>
If a TOM is passed, this TOM is used as the initial provider
(for a global request) or
the sole provider (for a local request). If not provided, then
the system chooses one
of the TOMs that manages this AO class. (See also the <local>
argument.)
<local>
If set to 0 (default), then a global find is performed, where
the find is executed in
parallel in all TOMs that support the AO class. If set to any
other value, then a local
find is done in only one TOM. Specify the TOM to use for a
local find in the
<local> or <tomId> arguments.
Osp.Find(Unit.Class, "query", local=35) # preferred
Osp.Find(Unit.Class, "query", 35, 1) # alternate
<bestEffort>
If set to 0 (default), then the find is done in "atomic" mode,
where an error from any
TOM will cause the whole request to fail. If set to any other
value (1 preferred), then
the request will return results from all successful TOMs, even
if some TOMs failed to
perform the request.
Returns: <oidlist>
Returns an oidlist, which is a list of stub objects You can use the
Python len() method to
check the length of the returned tuple, or for to iterate over the
list.
emplist = Osp.Find(Phone.Employee, "")
howmany = len(emplist)
for emp in emplist:
print emp.name
Lock Service
Lock
Osp.Lock(<oidList> [, <lockType> [, <tomId> [, <bestEffort>]]])
If no tomId is specified, this executes a global Lock to get a
primary lock on the list of
objects, choosing a TOM as the global provider. If a tomId is
specified, this executes a
local lock on the specified tom.
<oidList>
The list of oids or stubs to be locked
<lockType>
The lockType, one of Osp.LockType.Read, Osp.LockType.Write, or
Osp.LockType.Transaction. Default is Read.
<tomId>
The provider tom
<bestEffort>
If set to 0 (default), then the find is done in "atomic" mode,
where an error from any
TOM will cause the whole request to fail. If set to any other
value (1 preferred), then
the request will return results from all successful TOMs, even
if some TOMs failed to
perform the request.
Returns: <lockId>
<lockId>
The id for this primary lock, used for Unlock commands, and
passed to operation
requests via the dictionary entry Osp.Context['LockId'].
Example:
lockid = Osp.Lock((fred, joe, emma))
Osp.Context['LockId'] = lockid
fred.Set(attribute=value)
Osp.SecondayLock(<lockId> [, <oidList> [, <lockType> [,
<bestEffort>]]])
Requests a secondary lock on <oidList> objects, adding them to the
primary lock
specified by <lockId>.
<lockId>
The primary lockId to be used as the basis for the secondary
lock.
<oidList>
The list of oids or stubs to be locked. Defaults to no AOs.
<lockType>
The lockType, one of Osp.LockType.Read, Osp.LockType.Write, or
Osp.LockType.Transaction. Default is Write.
<bestEffort>
If set to 0 (default), then the find is done in "atomic" mode,
where an error from any
TOM will cause the whole request to fail. If set to any other
value (1 preferred), then
the request will return results from all successful TOMs, even
if some TOMs failed to
perform the request.
Returns: <lockId>
<lockId>
The id for this secondary lock, used for Unlock commands, and
passed to operation
requests via dictionary entry Osp.Context['LockId'].
Unlock
Osp.Unlock(<lockId>
Releases the lock
<lockId>
The lockId to be released.
Returns: None
Example:
Osp.Unlock(lockid)
Event Service
Register Event
Osp.RegisterEvent(<Class> | <instance>, <Callback>, <eventType>)
Registers the callback function for a global event against the
eventType and class or
instance.
<Class>
The class of objects on which to register events
<instance>
The oid or stub on which to register events
<Callback>
A user defined function which processes the events. This has a
signature that takes a
single event instance as input, and returns none. The event
instance has members
corresponding to the attributes of the event. However, for
Osp.evMoEnrollEvent,
Osp.evMoAttrChangeEvent, the event attribute attrChangeList is
replaced by
individual attributes for each of the attributes on the list.
<eventType>
The event type. Can be one of the OSP defined events,
Osp.evMoEnrollEvent,
Osp.evMoDeenrollEvent, Osp.evMoAttrChangeEvent, or the string
name for an
application defined event.
Returns: <eventId>
<eventId>
An id used to deregister the sieve. This should be treated as
an opaque value, and
should only be used as an argument to deregister.
DeregisterEvent
DeregisterEvent(<eventId>)
Deregisters the Events registered for under the handle <eventId>
returned by RegisterEvent
<eventId>
The event handle originally returned by RegisterEvent
WaitEvent
WaitEvent(<eventId>)
Suspends execution until an event has come in on the sieve. Events
are counted so that the
call may return immediately if a previously received event had no
corresponding WaitEvent.
<eventId>
The event handle originally returned by RegisterEvent
Miscellaneous
Describe
Describe(<className>)
Prints out the class description as done by TkClient.
COMPILED BYTECODE
If PyOsp has write-access to the directory containing Python (.py)
files, then it will automatically
compile them to bytecode files, with a .pyc suffix. This speeds up
future execution of the Python
code.
== END PyOsp overview
This author is unclear what you mean by "extract and compare object".
PyOsp is an embedded version of the Python interpreter that provides a
scripting binding feature to OSP-based TOM's.
Python itself is a powerful scripting language with object-oriented
features and good performance. PyOsp is a OSP interface that allows you
to interact with any OSP interface/services, providing a bridge between
TCSI components and external products.