1*cdf0e10cSrcweir#************************************************************************* 2*cdf0e10cSrcweir# 3*cdf0e10cSrcweir# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir# 5*cdf0e10cSrcweir# Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir# 7*cdf0e10cSrcweir# OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir# 9*cdf0e10cSrcweir# This file is part of OpenOffice.org. 10*cdf0e10cSrcweir# 11*cdf0e10cSrcweir# OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir# it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir# only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir# 15*cdf0e10cSrcweir# OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir# but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir# GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir# (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir# 21*cdf0e10cSrcweir# You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir# version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir# <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir# for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir# 26*cdf0e10cSrcweir#************************************************************************* 27*cdf0e10cSrcweirimport sys 28*cdf0e10cSrcweir 29*cdf0e10cSrcweirimport pyuno 30*cdf0e10cSrcweirimport __builtin__ 31*cdf0e10cSrcweirimport socket # since on Windows sal3.dll no longer calls WSAStartup 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir# all functions and variables starting with a underscore (_) must be considered private 34*cdf0e10cSrcweir# and can be changed at any time. Don't use them 35*cdf0e10cSrcweir_g_ctx = pyuno.getComponentContext( ) 36*cdf0e10cSrcweir_g_delegatee = __builtin__.__dict__["__import__"] 37*cdf0e10cSrcweir 38*cdf0e10cSrcweirdef getComponentContext(): 39*cdf0e10cSrcweir """ returns the UNO component context, that was used to initialize the python runtime. 40*cdf0e10cSrcweir """ 41*cdf0e10cSrcweir return _g_ctx 42*cdf0e10cSrcweir 43*cdf0e10cSrcweirdef getConstantByName( constant ): 44*cdf0e10cSrcweir "Looks up the value of a idl constant by giving its explicit name" 45*cdf0e10cSrcweir return pyuno.getConstantByName( constant ) 46*cdf0e10cSrcweir 47*cdf0e10cSrcweirdef getTypeByName( typeName): 48*cdf0e10cSrcweir """ returns a uno.Type instance of the type given by typeName. In case the 49*cdf0e10cSrcweir type does not exist, a com.sun.star.uno.RuntimeException is raised. 50*cdf0e10cSrcweir """ 51*cdf0e10cSrcweir return pyuno.getTypeByName( typeName ) 52*cdf0e10cSrcweir 53*cdf0e10cSrcweirdef createUnoStruct( typeName, *args ): 54*cdf0e10cSrcweir """creates a uno struct or exception given by typeName. The parameter args may 55*cdf0e10cSrcweir 1) be empty. In this case, you get a default constructed uno structure. 56*cdf0e10cSrcweir ( e.g. createUnoStruct( "com.sun.star.uno.Exception" ) ) 57*cdf0e10cSrcweir 2) be a sequence with exactly one element, that contains an instance of typeName. 58*cdf0e10cSrcweir In this case, a copy constructed instance of typeName is returned 59*cdf0e10cSrcweir ( e.g. createUnoStruct( "com.sun.star.uno.Exception" , e ) ) 60*cdf0e10cSrcweir 3) be a sequence, where the length of the sequence must match the number of 61*cdf0e10cSrcweir elements within typeName (e.g. 62*cdf0e10cSrcweir createUnoStruct( "com.sun.star.uno.Exception", "foo error" , self) ). The 63*cdf0e10cSrcweir elements with in the sequence must match the type of each struct element, 64*cdf0e10cSrcweir otherwise an exception is thrown. 65*cdf0e10cSrcweir """ 66*cdf0e10cSrcweir return getClass(typeName)( *args ) 67*cdf0e10cSrcweir 68*cdf0e10cSrcweirdef getClass( typeName ): 69*cdf0e10cSrcweir """returns the class of a concrete uno exception, struct or interface 70*cdf0e10cSrcweir """ 71*cdf0e10cSrcweir return pyuno.getClass(typeName) 72*cdf0e10cSrcweir 73*cdf0e10cSrcweirdef isInterface( obj ): 74*cdf0e10cSrcweir """returns true, when obj is a class of a uno interface""" 75*cdf0e10cSrcweir return pyuno.isInterface( obj ) 76*cdf0e10cSrcweir 77*cdf0e10cSrcweirdef generateUuid(): 78*cdf0e10cSrcweir "returns a 16 byte sequence containing a newly generated uuid or guid, see rtl/uuid.h " 79*cdf0e10cSrcweir return pyuno.generateUuid() 80*cdf0e10cSrcweir 81*cdf0e10cSrcweirdef systemPathToFileUrl( systemPath ): 82*cdf0e10cSrcweir "returns a file-url for the given system path" 83*cdf0e10cSrcweir return pyuno.systemPathToFileUrl( systemPath ) 84*cdf0e10cSrcweir 85*cdf0e10cSrcweirdef fileUrlToSystemPath( url ): 86*cdf0e10cSrcweir "returns a system path (determined by the system, the python interpreter is running on)" 87*cdf0e10cSrcweir return pyuno.fileUrlToSystemPath( url ) 88*cdf0e10cSrcweir 89*cdf0e10cSrcweirdef absolutize( path, relativeUrl ): 90*cdf0e10cSrcweir "returns an absolute file url from the given urls" 91*cdf0e10cSrcweir return pyuno.absolutize( path, relativeUrl ) 92*cdf0e10cSrcweir 93*cdf0e10cSrcweirdef getCurrentContext(): 94*cdf0e10cSrcweir """Returns the currently valid current context. 95*cdf0e10cSrcweir see http://udk.openoffice.org/common/man/concept/uno_contexts.html#current_context 96*cdf0e10cSrcweir for an explanation on the current context concept 97*cdf0e10cSrcweir """ 98*cdf0e10cSrcweir return pyuno.getCurrentContext() 99*cdf0e10cSrcweir 100*cdf0e10cSrcweirdef setCurrentContext( newContext ): 101*cdf0e10cSrcweir """Sets newContext as new uno current context. The newContext must 102*cdf0e10cSrcweir implement the XCurrentContext interface. The implemenation should 103*cdf0e10cSrcweir handle the desired properties and delegate unknown properties to the 104*cdf0e10cSrcweir old context. Ensure to reset the old one when you leave your stack ... 105*cdf0e10cSrcweir see http://udk.openoffice.org/common/man/concept/uno_contexts.html#current_context 106*cdf0e10cSrcweir """ 107*cdf0e10cSrcweir return pyuno.setCurrentContext( newContext ) 108*cdf0e10cSrcweir 109*cdf0e10cSrcweir 110*cdf0e10cSrcweirclass Enum: 111*cdf0e10cSrcweir "Represents a UNO idl enum, use an instance of this class to explicitly pass a boolean to UNO" 112*cdf0e10cSrcweir #typeName the name of the enum as a string 113*cdf0e10cSrcweir #value the actual value of this enum as a string 114*cdf0e10cSrcweir def __init__(self,typeName, value): 115*cdf0e10cSrcweir self.typeName = typeName 116*cdf0e10cSrcweir self.value = value 117*cdf0e10cSrcweir pyuno.checkEnum( self ) 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir def __repr__(self): 120*cdf0e10cSrcweir return "<uno.Enum %s (%r)>" % (self.typeName, self.value) 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir def __eq__(self, that): 123*cdf0e10cSrcweir if not isinstance(that, Enum): 124*cdf0e10cSrcweir return False 125*cdf0e10cSrcweir return (self.typeName == that.typeName) and (self.value == that.value) 126*cdf0e10cSrcweir 127*cdf0e10cSrcweirclass Type: 128*cdf0e10cSrcweir "Represents a UNO type, use an instance of this class to explicitly pass a boolean to UNO" 129*cdf0e10cSrcweir# typeName # Name of the UNO type 130*cdf0e10cSrcweir# typeClass # python Enum of TypeClass, see com/sun/star/uno/TypeClass.idl 131*cdf0e10cSrcweir def __init__(self, typeName, typeClass): 132*cdf0e10cSrcweir self.typeName = typeName 133*cdf0e10cSrcweir self.typeClass = typeClass 134*cdf0e10cSrcweir pyuno.checkType(self) 135*cdf0e10cSrcweir def __repr__(self): 136*cdf0e10cSrcweir return "<Type instance %s (%r)>" % (self.typeName, self.typeClass) 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir def __eq__(self, that): 139*cdf0e10cSrcweir if not isinstance(that, Type): 140*cdf0e10cSrcweir return False 141*cdf0e10cSrcweir return self.typeClass == that.typeClass and self.typeName == that.typeName 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir def __hash__(self): 144*cdf0e10cSrcweir return self.typeName.__hash__() 145*cdf0e10cSrcweir 146*cdf0e10cSrcweirclass Bool(object): 147*cdf0e10cSrcweir """Represents a UNO boolean, use an instance of this class to explicitly 148*cdf0e10cSrcweir pass a boolean to UNO. 149*cdf0e10cSrcweir Note: This class is deprecated. Use python's True and False directly instead 150*cdf0e10cSrcweir """ 151*cdf0e10cSrcweir def __new__(cls, value): 152*cdf0e10cSrcweir if isinstance(value, (str, unicode)) and value == "true": 153*cdf0e10cSrcweir return True 154*cdf0e10cSrcweir if isinstance(value, (str, unicode)) and value == "false": 155*cdf0e10cSrcweir return False 156*cdf0e10cSrcweir if value: 157*cdf0e10cSrcweir return True 158*cdf0e10cSrcweir return False 159*cdf0e10cSrcweir 160*cdf0e10cSrcweirclass Char: 161*cdf0e10cSrcweir "Represents a UNO char, use an instance of this class to explicitly pass a char to UNO" 162*cdf0e10cSrcweir # @param value pass a Unicode string with length 1 163*cdf0e10cSrcweir def __init__(self,value): 164*cdf0e10cSrcweir assert isinstance(value, unicode) 165*cdf0e10cSrcweir assert len(value) == 1 166*cdf0e10cSrcweir self.value=value 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir def __repr__(self): 169*cdf0e10cSrcweir return "<Char instance %s>" % (self.value, ) 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir def __eq__(self, that): 172*cdf0e10cSrcweir if isinstance(that, (str, unicode)): 173*cdf0e10cSrcweir if len(that) > 1: 174*cdf0e10cSrcweir return False 175*cdf0e10cSrcweir return self.value == that[0] 176*cdf0e10cSrcweir if isinstance(that, Char): 177*cdf0e10cSrcweir return self.value == that.value 178*cdf0e10cSrcweir return False 179*cdf0e10cSrcweir 180*cdf0e10cSrcweir# Suggested by Christian, but still some open problems which need to be solved first 181*cdf0e10cSrcweir# 182*cdf0e10cSrcweir#class ByteSequence(str): 183*cdf0e10cSrcweir# 184*cdf0e10cSrcweir# def __repr__(self): 185*cdf0e10cSrcweir# return "<ByteSequence instance %s>" % str.__repr__(self) 186*cdf0e10cSrcweir 187*cdf0e10cSrcweir # for a little bit compatitbility; setting value is not possible as 188*cdf0e10cSrcweir # strings are immutable 189*cdf0e10cSrcweir# def _get_value(self): 190*cdf0e10cSrcweir# return self 191*cdf0e10cSrcweir# 192*cdf0e10cSrcweir# value = property(_get_value) 193*cdf0e10cSrcweir 194*cdf0e10cSrcweirclass ByteSequence: 195*cdf0e10cSrcweir def __init__(self, value): 196*cdf0e10cSrcweir if isinstance(value, str): 197*cdf0e10cSrcweir self.value = value 198*cdf0e10cSrcweir elif isinstance(value, ByteSequence): 199*cdf0e10cSrcweir self.value = value.value 200*cdf0e10cSrcweir else: 201*cdf0e10cSrcweir raise TypeError("expected string or bytesequence") 202*cdf0e10cSrcweir 203*cdf0e10cSrcweir def __repr__(self): 204*cdf0e10cSrcweir return "<ByteSequence instance '%s'>" % (self.value, ) 205*cdf0e10cSrcweir 206*cdf0e10cSrcweir def __eq__(self, that): 207*cdf0e10cSrcweir if isinstance( that, ByteSequence): 208*cdf0e10cSrcweir return self.value == that.value 209*cdf0e10cSrcweir if isinstance(that, str): 210*cdf0e10cSrcweir return self.value == that 211*cdf0e10cSrcweir return False 212*cdf0e10cSrcweir 213*cdf0e10cSrcweir def __len__(self): 214*cdf0e10cSrcweir return len(self.value) 215*cdf0e10cSrcweir 216*cdf0e10cSrcweir def __getitem__(self, index): 217*cdf0e10cSrcweir return self.value[index] 218*cdf0e10cSrcweir 219*cdf0e10cSrcweir def __iter__( self ): 220*cdf0e10cSrcweir return self.value.__iter__() 221*cdf0e10cSrcweir 222*cdf0e10cSrcweir def __add__( self , b ): 223*cdf0e10cSrcweir if isinstance( b, str ): 224*cdf0e10cSrcweir return ByteSequence( self.value + b ) 225*cdf0e10cSrcweir elif isinstance( b, ByteSequence ): 226*cdf0e10cSrcweir return ByteSequence( self.value + b.value ) 227*cdf0e10cSrcweir raise TypeError( "expected string or ByteSequence as operand" ) 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir def __hash__( self ): 230*cdf0e10cSrcweir return self.value.hash() 231*cdf0e10cSrcweir 232*cdf0e10cSrcweir 233*cdf0e10cSrcweirclass Any: 234*cdf0e10cSrcweir "use only in connection with uno.invoke() to pass an explicit typed any" 235*cdf0e10cSrcweir def __init__(self, type, value ): 236*cdf0e10cSrcweir if isinstance( type, Type ): 237*cdf0e10cSrcweir self.type = type 238*cdf0e10cSrcweir else: 239*cdf0e10cSrcweir self.type = getTypeByName( type ) 240*cdf0e10cSrcweir self.value = value 241*cdf0e10cSrcweir 242*cdf0e10cSrcweirdef invoke( object, methodname, argTuple ): 243*cdf0e10cSrcweir "use this function to pass exactly typed anys to the callee (using uno.Any)" 244*cdf0e10cSrcweir return pyuno.invoke( object, methodname, argTuple ) 245*cdf0e10cSrcweir 246*cdf0e10cSrcweir#--------------------------------------------------------------------------------------- 247*cdf0e10cSrcweir# don't use any functions beyond this point, private section, likely to change 248*cdf0e10cSrcweir#--------------------------------------------------------------------------------------- 249*cdf0e10cSrcweir#def _uno_import( name, globals={}, locals={}, fromlist=[], level=-1 ): 250*cdf0e10cSrcweirdef _uno_import( name, *optargs, **kwargs ): 251*cdf0e10cSrcweir try: 252*cdf0e10cSrcweir# print "optargs = " + repr(optargs) 253*cdf0e10cSrcweir return _g_delegatee( name, *optargs, **kwargs ) 254*cdf0e10cSrcweir except ImportError: 255*cdf0e10cSrcweir # process optargs 256*cdf0e10cSrcweir globals, locals, fromlist = list(optargs)[:3] + [kwargs.get('globals',{}), kwargs.get('locals',{}), kwargs.get('fromlist',[])][len(optargs):] 257*cdf0e10cSrcweir if not fromlist: 258*cdf0e10cSrcweir raise 259*cdf0e10cSrcweir modnames = name.split( "." ) 260*cdf0e10cSrcweir mod = None 261*cdf0e10cSrcweir d = sys.modules 262*cdf0e10cSrcweir for x in modnames: 263*cdf0e10cSrcweir if d.has_key(x): 264*cdf0e10cSrcweir mod = d[x] 265*cdf0e10cSrcweir else: 266*cdf0e10cSrcweir mod = pyuno.__class__(x) # How to create a module ?? 267*cdf0e10cSrcweir d = mod.__dict__ 268*cdf0e10cSrcweir 269*cdf0e10cSrcweir RuntimeException = pyuno.getClass( "com.sun.star.uno.RuntimeException" ) 270*cdf0e10cSrcweir for x in fromlist: 271*cdf0e10cSrcweir if not d.has_key(x): 272*cdf0e10cSrcweir if x.startswith( "typeOf" ): 273*cdf0e10cSrcweir try: 274*cdf0e10cSrcweir d[x] = pyuno.getTypeByName( name + "." + x[6:len(x)] ) 275*cdf0e10cSrcweir except RuntimeException,e: 276*cdf0e10cSrcweir raise ImportError( "type " + name + "." + x[6:len(x)] +" is unknown" ) 277*cdf0e10cSrcweir else: 278*cdf0e10cSrcweir try: 279*cdf0e10cSrcweir # check for structs, exceptions or interfaces 280*cdf0e10cSrcweir d[x] = pyuno.getClass( name + "." + x ) 281*cdf0e10cSrcweir except RuntimeException,e: 282*cdf0e10cSrcweir # check for enums 283*cdf0e10cSrcweir try: 284*cdf0e10cSrcweir d[x] = Enum( name , x ) 285*cdf0e10cSrcweir except RuntimeException,e2: 286*cdf0e10cSrcweir # check for constants 287*cdf0e10cSrcweir try: 288*cdf0e10cSrcweir d[x] = getConstantByName( name + "." + x ) 289*cdf0e10cSrcweir except RuntimeException,e3: 290*cdf0e10cSrcweir # no known uno type ! 291*cdf0e10cSrcweir raise ImportError( "type "+ name + "." +x + " is unknown" ) 292*cdf0e10cSrcweir return mod 293*cdf0e10cSrcweir 294*cdf0e10cSrcweir# hook into the __import__ chain 295*cdf0e10cSrcweir__builtin__.__dict__["__import__"] = _uno_import 296*cdf0e10cSrcweir 297*cdf0e10cSrcweir# private function, don't use 298*cdf0e10cSrcweirdef _impl_extractName(name): 299*cdf0e10cSrcweir r = range (len(name)-1,0,-1) 300*cdf0e10cSrcweir for i in r: 301*cdf0e10cSrcweir if name[i] == ".": 302*cdf0e10cSrcweir name = name[i+1:len(name)] 303*cdf0e10cSrcweir break 304*cdf0e10cSrcweir return name 305*cdf0e10cSrcweir 306*cdf0e10cSrcweir# private, referenced from the pyuno shared library 307*cdf0e10cSrcweirdef _uno_struct__init__(self,*args): 308*cdf0e10cSrcweir if len(args) == 1 and hasattr(args[0], "__class__") and args[0].__class__ == self.__class__ : 309*cdf0e10cSrcweir self.__dict__["value"] = args[0] 310*cdf0e10cSrcweir else: 311*cdf0e10cSrcweir self.__dict__["value"] = pyuno._createUnoStructHelper(self.__class__.__pyunostruct__,args) 312*cdf0e10cSrcweir 313*cdf0e10cSrcweir# private, referenced from the pyuno shared library 314*cdf0e10cSrcweirdef _uno_struct__getattr__(self,name): 315*cdf0e10cSrcweir return __builtin__.getattr(self.__dict__["value"],name) 316*cdf0e10cSrcweir 317*cdf0e10cSrcweir# private, referenced from the pyuno shared library 318*cdf0e10cSrcweirdef _uno_struct__setattr__(self,name,value): 319*cdf0e10cSrcweir return __builtin__.setattr(self.__dict__["value"],name,value) 320*cdf0e10cSrcweir 321*cdf0e10cSrcweir# private, referenced from the pyuno shared library 322*cdf0e10cSrcweirdef _uno_struct__repr__(self): 323*cdf0e10cSrcweir return repr(self.__dict__["value"]) 324*cdf0e10cSrcweir 325*cdf0e10cSrcweirdef _uno_struct__str__(self): 326*cdf0e10cSrcweir return str(self.__dict__["value"]) 327*cdf0e10cSrcweir 328*cdf0e10cSrcweir# private, referenced from the pyuno shared library 329*cdf0e10cSrcweirdef _uno_struct__eq__(self,cmp): 330*cdf0e10cSrcweir if hasattr(cmp,"value"): 331*cdf0e10cSrcweir return self.__dict__["value"] == cmp.__dict__["value"] 332*cdf0e10cSrcweir return False 333*cdf0e10cSrcweir 334*cdf0e10cSrcweir# referenced from pyuno shared lib and pythonscript.py 335*cdf0e10cSrcweirdef _uno_extract_printable_stacktrace( trace ): 336*cdf0e10cSrcweir mod = None 337*cdf0e10cSrcweir try: 338*cdf0e10cSrcweir mod = __import__("traceback") 339*cdf0e10cSrcweir except ImportError,e: 340*cdf0e10cSrcweir pass 341*cdf0e10cSrcweir ret = "" 342*cdf0e10cSrcweir if mod: 343*cdf0e10cSrcweir lst = mod.extract_tb( trace ) 344*cdf0e10cSrcweir max = len(lst) 345*cdf0e10cSrcweir for j in range(max): 346*cdf0e10cSrcweir i = lst[max-j-1] 347*cdf0e10cSrcweir ret = ret + " " + str(i[0]) + ":" + \ 348*cdf0e10cSrcweir str(i[1]) + " in function " + \ 349*cdf0e10cSrcweir str(i[2]) + "() [" + str(i[3]) + "]\n" 350*cdf0e10cSrcweir else: 351*cdf0e10cSrcweir ret = "Couldn't import traceback module" 352*cdf0e10cSrcweir return ret 353