Re: Using gnome components with Python



Jason Stokes <jstok@bluedog.apana.org.au> writes:

> Does anyone know of a tutorial or instructional code for manipulating
> Bonobo components with Python?  I'm also asking on Python mailing
> lists.  I'm coming from the standpoint of knowing nothing about
> components whatsoever, and I find Corba to be particularly
> intimidating.  I've looked at the binding between the Orbit orb and
> Python, but I'm still not very clear on the process.

I did some experiments last winter. Here's a script which worked then
with orbit-python and gnumeric. Since then, Bonobo and Gnumeric have
switched from using Goad to Oaf. This means that you'll have to find
the object reference to gnumeric in a different way. oafd (the Oaf
daemon) writes its stringified object reference to standard output
when it starts. You'll have to get the object reference to gnumeric
from oaf, but I do not know the details.


----

#!/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 orbit-python.
"""
# Standard/built-in modules.
import sys

# omniORB modules.
import CORBA

def locate_name_service(orb):
    try:
        ior = open("/tmp/naming.ref").read()
    except:
        raise "Name service not found."

    nameservice = orb.string_to_object(ior)
    return nameservice

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 resolve_goad_object(nameservice, id):
    """Resolve a name with the standard Gnome prefix."""
    nc = CosNaming.NameComponent
    prefix = [nc(id = 'GNOME',   kind = 'subcontext'),
              nc(id = 'Servers', kind = 'subcontext')]
    name   = prefix + [nc(id = id, kind = 'object')]
    return nameservice.resolve(name)
                                       
# Initialise the ORB.
orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)

CORBA.load_idl("/usr/share/idl/Gnumeric.idl", "-D__ORBIT_IDL__")
CORBA.load_idl("/usr/share/idl/name-service.idl")

# Locate the name service.
try:
    nameservice = locate_name_service(orb)
except:
    print 'Please write the IOR of the name service to "/tmp/naming.ref"'
    sys.exit(1)

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 factory.__repo_id != 'IDL:GNOME/Gnumeric/WorkbookFactory:1.0':
    raise 'This is not a Gnumeric WorkbookFactory'


#existing = factory.read("/home/jk/invest/aksjefon.gnumeric")
#mysheet=existing.sheet_lookup("AKSJEFON")
#val=mysheet.cell_get_value(8, 2)
#print val

newbook = factory.read("/home/jk/tmp/test.gnumeric")
sc=newbook.sheet_current()
# hmm. how do we get sheet name? Missing!
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)
# This raises AttributeError: VALUE_INT
# v=GNOME.Gnumeric.Value(GNOME.Gnumeric.VALUE_INT, 1.3)
# sc.cell_set_value(1, 2, 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)

----

> Also, can someone explain the relationship between the Orbit C bindings
> and Gnorba?  I've read the tutorial but it's not very clear.  Is the
> purpose of Gnorba to wrap the Corba interface behind a more friendly C
> library?

Partly that, but it also wraps locating the naming service and
includes a simple authentication hack. Anyway, it is being phased out,
and all Bonobo using applications have now swiched to oaf.

Regards

Jon Kåre





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