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