I have read somewhere that the OOo API doesn't work properly when you have
more than one process using the API at the same time. Therefore I have been
trying to get a program written in Python to start up its own instance of
OOo. I have written a little function that finds a spare port and then
starts OOo listening on that port. I use the -env:UserInstallation switch to
point it at a temp directory for that instance, then I use UNO to connect to
it and edit documents.
I have run into two problems when doing this. Firstly, I want to avoid
having to have an X server but when I start it with any combination of the
following flags -headless, -invisible or -nodefault, some of my code stops
working. The code in question is a function to search for a given string
inside a cursor. In most cases it does that by using
compareRegionStarts/compareRegionEnds but those don't work for matches
inside tables and generate an exception. When that happens I simply
enumerate all the things inside the cursor and check if any one of them is
the table associated with a match. Without the aformentioned startup flags,
the enumeration of a test document returns 4 items and one of them matches
the table I'm looking for. With the flags, the enumeration only returns 1
item and it's not the one I'm trying to search for. Is there a reason that
tables are ignored/missing from the enumeration when one of those flags is
given? Or is this a bug? The code for the search function is given below:
def findInCursor(self, searchDescriptor, cursor, callback):
"""Finds all text that matches a given search descriptor and for
every
match that lies within the specified cursor, the callback
function
is invoked. If there were matches then true is returned,
otherwise
false is returned"""
document = self.getUnoDocument()
text = document.Text
matches = document.findAll(searchDescriptor)
matched = False
# Iterate over matches
for i in xrange(0, matches.Count):
match = matches.getByIndex(i)
# Check if the match is within the given cursor
try:
if text.compareRegionStarts(match, cursor) <= 0 and \
text.compareRegionEnds(match, cursor) >= 0:
callback(match)
matched = True
except:
# If it's a table then we have to see if the table is in the
# text instead (note, this should probably be recursive)
if match.TextTable != None:
searchEnum = cursor.createEnumeration()
while searchEnum.hasMoreElements():
elem = searchEnum.nextElement()
if elem == match.TextTable:
callback(match)
matched = True
else:
raise
return matched
The second issue that I'm having is that despite running (what I thought
was) two seperate instances using different UserInstallation directories and
listening on different ports, when one copy of OOo exits, it causes the next
API operation on the other copy to generate a RuntimeException with message
"illegal object given!". How do I concurrently run more than one instance of
OOo, or have more than one connection to the API?
The second problem is more important to me as I can work around the first by
using something like Xvfb (although I'd rather not).
Potentially useful information:
OS: Fedora 9 x86_64
Python: 2.5.1
OpenOffice: 2.4.1 (RPMs from Fedora repository)
Thanks,
David Roe