Scripting with Python and omniORB



Hi,

I've made some experiments with Gnome CORBA objects and omniORB for
Python. omniORB is fast, even with Python, as you kan see in a
separate article.

omniidl is the idl compiler of omniORB. To compile Gnome IDL and
produce Python stubs and skeletons, you need the following flags:

   omniidl -bpython -Wbinline <idlfile>

I used the latest omniORBpy with the official omniORB 2.8.0
release. According to the announcement, omniORB 3.0 should now also work.

You need the latest Bonobo from CVS: We used some constructs which
weren't standard, and omniORB is strict about checking. The IDL in CVS
is now compatible with omniORB.

The Gnorba authentication mechanism isn't portable, so in order to
experiment with a "foreign" ORB, you have to disable authentication. I
hacked gnome_ORBit_request_validate in gnome-libs/libgnorba/orbitgtk.c
to always return ORBIT_MESSAGE_ALLOW_ALL. This is something you only
want to do behind a firewall where you can trust everybody.

Here's a small script to walk the naming tree, and a script to
exercise Gnumeric. You have to write the IOR of the gnome name service
to /tmp/naming.ref before trying these scripts. And you must change
the file name given to factory.read() in the Gnumeric script.

Jon Kåre

--

--
#!/usr/bin/env python

# nsbrowse.py
#
# Author: Jon K Hellan <hellan@acm.org>
#
# Do what you wish with this script. No warranties - if it breaks, you get
# keep both pieces.

"""Script to walk the naming tree of a CORBA name service.
Uses omniORBpy. Can locate the Gnome Name Service."""

# Standard/built-in modules.
import sys

# omniORB modules.
from omniORB import CORBA
import CosNaming

## gnames find the Gnome Name Service in the same way as gnome apps do.
#try:
#    import gnames     
#except:
#    print """Did not find the "gnames" extension module.
#Use orb.resolve_initial_references or read the IOR from a file."""

def name_to_string(name):
    """Convert CosNaming Name to a list of (id, kind) tuples."""
    res = []
    for i in range(len(name)):
        binding_name = name[i]
        res = res + [(binding_name.id, binding_name.kind)]
    return `res`

def walk(sofar, nameservice, visitor):
    """Walk the naming tree. Call visitor at object nodes."""
    (bl, bi) = nameservice.list(10000)
    print "%d bindings at naming context %s" % (len(bl), name_to_string(sofar))
    print "binding iterator: " + `bi`

    for i in range(len(bl)):
        binding = bl[i]
        try:
            obj = nameservice.resolve(binding.binding_name)
        except CosNaming.NamingContext.NotFound:
            print "Could not find %s" % name_to_string(sofar
                                                       + binding.binding_name)
            continue
        
        if binding.binding_type is CosNaming.ncontext:
            walk(sofar + binding.binding_name, obj, visitor)
        else:
            visitor(sofar, binding, obj)

def walk_print(nameservice):
    """Walk the naming tree. Print object nodes."""
    def printit(sofar, binding, obj):
        """Print object node."""
        print name_to_string(sofar + binding.binding_name)
        print obj

    walk([], nameservice, printit)
            
# Initialise the ORB.
orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)

# Locate the name service.
# This is how it's supposed to work:
#nameservice=orb.resolve_initial_references("NameService")
# This is the last resort - have the name server write IOR to file.
ior = open("/tmp/naming.ref").read()
# This works with the Gnome name service and the 'gnames' extension module.
#ior = gnames.get_ior()
nameservice = orb.string_to_object(ior)

# Breaks with normal libgnorba:
walk_print(nameservice)
                
--
#!/usr/bin/env python

# gntest.py
#
# Author: Jon K Hellan <hellan@acm.org>
#
# Do what you wish with this script. No warranties - if it breaks, you get
# keep both pieces.

"""Script to explore the CORBA interface of Gnumeric.
Uses omniORBpy.

Gnome uses an authentication mechanism for CORBA interactions which only
works with the ORBit ORB. To use other ORBs, you have to disable
authentication. Change gnome_ORBit_request_validate in
gnome-libs/libgnorba/orbitgtk.c to always return ORBIT_MESSAGE_ALLOW_ALL.
Needless to say, you should only do this if trust *everybody* who can
access your system.
"""
# Standard/built-in modules.
import sys

# omniORB modules.
from omniORB import CORBA
import CosNaming
import GNOME

## gnames find the Gnome Name Service in the same way as gnome apps do.
#try:
#    import gnames     
#except:
#    print """Did not find the "gnames" extension module.
#Use orb.resolve_initial_references or read the IOR from a file."""

def name_to_string(name):
    """Convert CosNaming Name to a list of (id, kind) tuples."""
    res = []
    for i in range(len(name)):
        binding_name = name[i]
        res = res + [(binding_name.id, binding_name.kind)]
    return `res`

# Initialise the ORB.
orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)

# Locate the name service.
# This is how it's supposed to work:
#nameservice=orb.resolve_initial_references("NameService")
# This is the last resort - have the name server write IOR to file.
ior = open("/tmp/naming.ref").read()
## This works with the Gnome name service and the 'gnames' extension module.
#ior = gnames.get_ior()
nameservice = orb.string_to_object(ior)

def resolve_goad_object(nameservice, id):
    """Resolve a name with the standard Gnome prefix."""
    nc = CosNaming.NameComponent
    prefix = [nc('GNOME', 'subcontext'), nc('Servers', 'subcontext')]
    name   = prefix + [nc(id, 'object')]
    return nameservice.resolve(name)
                                       
# Breaks with normal libgnorba:
#walk_print(nameservice)
try:
    factory = resolve_goad_object(nameservice,
                                  'GOADID:GNOME:Gnumeric:WorkbookFactory:1.0')
except CosNaming.NamingContext.NotFound:
    print 'Gnumeric not found'
    sys.exit()
#Other exceptions ...

if not factory._is_a('IDL:GNOME/Gnumeric/WorkbookFactory:1.0'):
    raise 'This is not a Gnumeric WorkbookFactory'

newbook = factory.read("/home/jk/tmp/test.gnumeric")
sc=newbook.sheet_current()

sc.cursor_set(4, 4, 1, 1, 5, 5)

sc.make_cell_visible(4, 500)
sc.cursor_move(3, 500)
sc.cursor_move(1, 1)
sc.make_cell_visible(1, 1)
sc.make_cell_visible(0, 0)

v=GNOME.Gnumeric.Value(GNOME.Gnumeric.VALUE_FLOAT, 1)
sc.cell_set_value(1, 1, v)
v=GNOME.Gnumeric.Value(GNOME.Gnumeric.VALUE_BOOLEAN, 1)
sc.cell_set_value(1, 3, v)
v=GNOME.Gnumeric.Value(GNOME.Gnumeric.VALUE_FLOAT, 3.1415926)
sc.cell_set_value(1, 4, v)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]