source: salt-server/trunk/fuentes/py/doooLib.py @ 473

Last change on this file since 473 was 473, checked in by daduve, 3 years ago

Adding files

  • Property svn:executable set to *
File size: 10.1 KB
Line 
1#**********************************************************************
2#
3#   Danny.OOo.OOoLib.py
4#
5#   A module to easily work with OpenOffice.org.
6#
7#**********************************************************************
8#   Copyright (c) 2003-2004 Danny Brewer
9#   d29583@groovegarden.com
10#
11#   This library is free software; you can redistribute it and/or
12#   modify it under the terms of the GNU Lesser General Public
13#   License as published by the Free Software Foundation; either
14#   version 2.1 of the License, or (at your option) any later version.
15#
16#   This library is distributed in the hope that it will be useful,
17#   but WITHOUT ANY WARRANTY; without even the implied warranty of
18#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19#   Lesser General Public License for more details.
20#
21#   You should have received a copy of the GNU Lesser General Public
22#   License along with this library; if not, write to the Free Software
23#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24#
25#   See:  http://www.gnu.org/licenses/lgpl.html
26#
27#**********************************************************************
28#   If you make changes, please append to the change log below.
29#
30#   Change Log
31#   Danny Brewer         Revised 2004-06-07-01
32#
33#**********************************************************************
34
35import string 
36import socket
37# OOo's libraries
38import uno 
39
40
41#------------------------------------------------------------
42#   Uno ServiceManager access
43#   A different version of this routine and global variable
44#    is needed for code running inside a component.
45#------------------------------------------------------------
46
47
48# The ServiceManager of the running OOo.
49# It is cached in a global variable.
50goServiceManager = False 
51def getServiceManager( cHost="localhost", cPort="2002",sMan=False): 
52    """Get the ServiceManager from the running OpenOffice.org.
53        Then retain it in the global variable goServiceManager for future use.
54        This is similar to the GetProcessServiceManager() in OOo Basic.
55    """ 
56    global goServiceManager
57    if not goServiceManager:
58        if not sMan:
59            # Get the uno component context from the PyUNO runtime
60            oLocalContext = uno.getComponentContext() 
61            # Create the UnoUrlResolver on the Python side.
62            oLocalResolver = oLocalContext.ServiceManager.createInstanceWithContext( 
63                                        "com.sun.star.bridge.UnoUrlResolver", oLocalContext ) 
64            # Connect to the running OpenOffice.org and get its context.
65            import socket
66            oContext = oLocalResolver.resolve( "uno:socket,host=" + cHost + ",port=" + cPort + ";urp;StarOffice.ComponentContext" ) 
67            # Get the ServiceManager object
68            goServiceManager = oContext.ServiceManager
69        else:
70            goServiceManager=sMan
71    return goServiceManager
72
73
74#------------------------------------------------------------
75#   Uno convenience functions
76#   The stuff in this section is just to make
77#    python progrmaming of OOo more like using OOo Basic.
78#------------------------------------------------------------
79
80
81# This is the same as ServiceManager.createInstance( ... )
82def createUnoService( cClass ): 
83    """A handy way to create a global objects within the running OOo.
84    Similar to the function of the same name in OOo Basic.
85    """ 
86    oServiceManager = getServiceManager(cPort='2002') 
87    oObj = oServiceManager.createInstance( cClass ) 
88    return oObj
89
90
91# The StarDesktop object.  (global like in OOo Basic)
92# It is cached in a global variable.
93StarDesktop = None 
94def getDesktop(): 
95    """An easy way to obtain the Desktop object from a running OOo.
96    """ 
97    global StarDesktop
98    if StarDesktop == None: 
99        StarDesktop = createUnoService( "com.sun.star.frame.Desktop" ) 
100    return StarDesktop
101# preload the StarDesktop variable.
102#getDesktop()
103
104
105# The CoreReflection object.
106# It is cached in a global variable.
107goCoreReflection = False 
108def getCoreReflection(): 
109    global goCoreReflection
110    if not goCoreReflection: 
111        goCoreReflection = createUnoService( "com.sun.star.reflection.CoreReflection" ) 
112    return goCoreReflection
113
114
115def createUnoStruct( cTypeName ): 
116    """Create a UNO struct and return it.
117    Similar to the function of the same name in OOo Basic.
118    """ 
119    oCoreReflection = getCoreReflection() 
120    # Get the IDL class for the type name
121    oXIdlClass = oCoreReflection.forName( cTypeName ) 
122    # Create the struct.
123    oReturnValue, oStruct = oXIdlClass.createObject( None ) 
124    return oStruct
125
126
127
128#def newConnectionToOOo( cHost="localhost", cPort="8100" ):
129#    """Call this to establish, or re-establish a connection to OOo."""
130#    global goServiceManager
131#    global StarDesktop
132#    global goCoreReflection
133#    goServiceManager = False
134#    StarDesktop = None
135#    goCoreReflection = False
136#    getServiceManager( cHost, cPort )
137#    getDesktop()
138
139
140
141#------------------------------------------------------------
142#   API helpers
143#------------------------------------------------------------
144
145def hasUnoInterface( oObject, cInterfaceName ): 
146    """Similar to Basic's HasUnoInterfaces() function, but singular not plural.""" 
147
148    # Get the Introspection service.
149    oIntrospection = createUnoService( "com.sun.star.beans.Introspection" ) 
150
151    # Now inspect the object to learn about it.   
152    oObjInfo = oIntrospection.inspect( oObject ) 
153   
154    # Obtain an array describing all methods of the object.
155    oMethods = oObjInfo.getMethods( uno.getConstantByName( "com.sun.star.beans.MethodConcept.ALL" ) ) 
156    # Now look at every method.
157    for oMethod in oMethods: 
158        # Check the method's interface to see if
159        #  these aren't the droids you're looking for.
160        cMethodInterfaceName = oMethod.getDeclaringClass().getName() 
161        if cMethodInterfaceName == cInterfaceName: 
162            return True 
163    return False 
164
165def hasUnoInterfaces( oObject, *cInterfaces ): 
166    """Similar to the function of the same name in OOo Basic.""" 
167    for cInterface in cInterfaces: 
168        if not hasUnoInterface( oObject, cInterface ): 
169            return False 
170    return True 
171
172
173
174#------------------------------------------------------------
175#   High level general purpose functions
176#------------------------------------------------------------
177
178
179def makePropertyValue( cName=None, uValue=None, nHandle=None, nState=None ): 
180    """Create a com.sun.star.beans.PropertyValue struct and return it.
181    """ 
182    oPropertyValue = createUnoStruct( "com.sun.star.beans.PropertyValue" ) 
183
184    if cName != None: 
185        oPropertyValue.Name = cName
186    if uValue != None: 
187        oPropertyValue.Value = uValue
188    if nHandle != None: 
189        oPropertyValue.Handle = nHandle
190    if nState != None: 
191        oPropertyValue.State = nState
192
193    return oPropertyValue
194
195
196def makePoint( nX, nY ): 
197    """Create a com.sun.star.awt.Point struct.""" 
198    oPoint = createUnoStruct( "com.sun.star.awt.Point" ) 
199    oPoint.X = nX
200    oPoint.Y = nY
201    return oPoint
202
203
204def makeSize( nWidth, nHeight ): 
205    """Create a com.sun.star.awt.Size struct.""" 
206    oSize = createUnoStruct( "com.sun.star.awt.Size" ) 
207    oSize.Width = nWidth
208    oSize.Height = nHeight
209    return oSize
210
211
212def makeRectangle( nX, nY, nWidth, nHeight ): 
213    """Create a com.sun.star.awt.Rectangle struct.""" 
214    oRect = createUnoStruct( "com.sun.star.awt.Rectangle" ) 
215    oRect.X = nX
216    oRect.Y = nY
217    oRect.Width = nWidth
218    oRect.Height = nHeight
219    return oRect
220
221
222def Array( *args ): 
223    """This is just sugar coating so that code from OOoBasic which
224    contains the Array() function can work perfectly in python.""" 
225    tArray = () 
226    for arg in args: 
227        tArray += (arg,) 
228    return tArray
229
230
231def loadComponentFromURL( cUrl, tProperties=() ): 
232    """Open or Create a document from it's URL.
233    New documents are created from URL's such as:
234        private:factory/sdraw
235        private:factory/swriter
236        private:factory/scalc
237        private:factory/simpress
238    """ 
239    StarDesktop = getDesktop() 
240    oDocument = StarDesktop.loadComponentFromURL( cUrl, "_blank", 0, tProperties ) 
241    return oDocument
242
243
244
245#def makeWriterDocument():
246#    """Create a new OOo Writer document."""
247#    return loadComponentFromURL( "private:factory/swriter" )
248#
249#
250#def makeCalcDocument():
251#    """Create a new OOo Calc document."""
252#    return loadComponentFromURL( "private:factory/scalc" )
253
254
255
256
257#------------------------------------------------------------
258#   Styles
259#------------------------------------------------------------
260
261
262def defineStyle( oDrawDoc, cStyleFamily, cStyleName, cParentStyleName=None ): 
263    """Add a new style to the style catalog if it is not already present.
264    This returns the style object so that you can alter its properties.
265    """ 
266
267    oStyleFamily = oDrawDoc.getStyleFamilies().getByName( cStyleFamily ) 
268
269    # Does the style already exist?
270    if oStyleFamily.hasByName( cStyleName ): 
271        # then get it so we can return it.
272        oStyle = oStyleFamily.getByName( cStyleName ) 
273    else: 
274        # Create new style object.
275        oStyle = oDrawDoc.createInstance( "com.sun.star.style.Style" ) 
276
277        # Set its parent style
278        if cParentStyleName != None: 
279            oStyle.setParentStyle( cParentStyleName ) 
280
281        # Add the new style to the style family.
282        oStyleFamily.insertByName( cStyleName, oStyle ) 
283
284    return oStyle
285
286
287def getStyle( oDrawDoc, cStyleFamily, cStyleName ): 
288    """Lookup and return a style from the document.
289    """ 
290    return oDrawDoc.getStyleFamilies().getByName( cStyleFamily ).getByName( cStyleName ) 
291
292
293
294
295
296#------------------------------------------------------------
297#   General Utility functions
298#------------------------------------------------------------
299
300
301def convertToURL( cPathname ): 
302    """Convert a Windows or Linux pathname into an OOo URL.""" 
303    if len( cPathname ) > 1: 
304        if cPathname[1:2] == ":": 
305            cPathname = "/" + cPathname[0] + "|" + cPathname[2:] 
306    cPathname = string.replace( cPathname, "\\", "/" ) 
307    cPathname = "file://" + cPathname
308    return cPathname
Note: See TracBrowser for help on using the repository browser.