| 1 |
import os, os.path, sys |
|---|
| 2 |
|
|---|
| 3 |
def package_home(globals_dict): |
|---|
| 4 |
__name__=globals_dict['__name__'] |
|---|
| 5 |
m=sys.modules[__name__] |
|---|
| 6 |
if hasattr(m,'__path__'): |
|---|
| 7 |
r=m.__path__[0] |
|---|
| 8 |
elif "." in __name__: |
|---|
| 9 |
r=sys.modules[__name__[:__name__.rfind('.')]].__path__[0] |
|---|
| 10 |
else: |
|---|
| 11 |
r=__name__ |
|---|
| 12 |
return os.path.abspath(os.path.join(os.getcwd(), r)) |
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
setup_ini = 'file://%s/uno.ini' % package_home(globals()) |
|---|
| 16 |
|
|---|
| 17 |
import PyUNO |
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
class uno: |
|---|
| 21 |
def __init__ ( self, connection='socket,host=localhost,port=2002;urp', setup=setup_ini ): |
|---|
| 22 |
""" do the bootstrap |
|---|
| 23 |
|
|---|
| 24 |
connection can be one or more of the following: |
|---|
| 25 |
|
|---|
| 26 |
socket, |
|---|
| 27 |
host = localhost | <hostname> | <ip-addr>, |
|---|
| 28 |
port = <port>, |
|---|
| 29 |
service = soffice, |
|---|
| 30 |
user = <username>, |
|---|
| 31 |
password = <password> |
|---|
| 32 |
;urp |
|---|
| 33 |
|
|---|
| 34 |
""" |
|---|
| 35 |
self.XComponentContext = PyUNO.bootstrap ( setup ) |
|---|
| 36 |
self.XUnoUrlResolver, o = \ |
|---|
| 37 |
self.XComponentContext.ServiceManager.createInstanceWithContext ( 'com.sun.star.bridge.UnoUrlResolver', self.XComponentContext ) |
|---|
| 38 |
self.XNamingService, o = self.XUnoUrlResolver.resolve ( 'uno:%s;StarOffice.NamingService' % connection ) |
|---|
| 39 |
self.XMultiServiceFactory, o = self.XNamingService.getRegisteredObject ('StarOffice.ServiceManager') |
|---|
| 40 |
self.XComponentLoader, o = \ |
|---|
| 41 |
self.XMultiServiceFactory.createInstance ( 'com.sun.star.frame.Desktop' ) |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
def new ( self, what, where='_blank', no=0, propertyValues=() ): |
|---|
| 45 |
return self.XComponentLoader.loadComponentFromURL ( |
|---|
| 46 |
what, where, no, propertyValues ) |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
def newIdlStruct ( self, type ): |
|---|
| 50 |
return PyUNO.createIdlStruct ( self.XMultiServiceFactory, type ) |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
def newCalc (self): |
|---|
| 54 |
return self.new ('private:factory/scalc') |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
def newWriter (self): |
|---|
| 58 |
return self.new ('private:factory/swriter') |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
def newImpress (self): |
|---|
| 62 |
return self.new ('private:factory/simpress') |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
def newDraw (self): |
|---|
| 66 |
return self.new ('private:factory/sdraw') |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
def newPropertyValue (self, propertyValue={} ): |
|---|
| 70 |
property = self.newIdlStruct ( 'com.sun.star.beans.PropertyValue' ) |
|---|
| 71 |
|
|---|
| 72 |
if propertyValue.has_key('Name'): |
|---|
| 73 |
property.Name = propertyValue['Name'] |
|---|
| 74 |
|
|---|
| 75 |
if propertyValue.has_key('Value'): |
|---|
| 76 |
property.Value = propertyValue['Value'] |
|---|
| 77 |
|
|---|
| 78 |
if propertyValue.has_key('State'): |
|---|
| 79 |
property.State = propertyValue['State'] |
|---|
| 80 |
|
|---|
| 81 |
if propertyValue.has_key('Handle'): |
|---|
| 82 |
property.Handle = propertyValue['Handle'] |
|---|
| 83 |
|
|---|
| 84 |
return property |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
def newPropertyValues ( self, propertyValues=[] ): |
|---|
| 88 |
list = () |
|---|
| 89 |
|
|---|
| 90 |
l = len(propertyValues) |
|---|
| 91 |
for p in range (l): |
|---|
| 92 |
list = list + ( self.newPropertyValue ( propertyValues.pop(0) ), ) |
|---|
| 93 |
|
|---|
| 94 |
return list |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
def newBoolean ( self, bool=0 ): |
|---|
| 98 |
if bool: |
|---|
| 99 |
return PyUNO.true() |
|---|
| 100 |
else: |
|---|
| 101 |
return PyUNO.false() |
|---|