Improving uno.py code style
* Fixing pep8 violations * Improving comments, docstrings, and unifying coding style * Using functionality copied from six library for Python 2/3 compatibility * Using standard traceback formatting Change-Id: I62bd0e8513ffc59202163002fa4adea3d92572c3 Reviewed-on: https://gerrit.libreoffice.org/22848 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Michael Stahl <mstahl@redhat.com> Tested-by: Michael Stahl <mstahl@redhat.com>
This commit is contained in:
committed by
Michael Stahl
parent
2744b00906
commit
deb989dd6d
@@ -16,208 +16,278 @@
|
|||||||
# except in compliance with the License. You may obtain a copy of
|
# except in compliance with the License. You may obtain a copy of
|
||||||
# the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
# the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
||||||
#
|
#
|
||||||
import sys
|
|
||||||
|
|
||||||
import pyuno
|
import pyuno
|
||||||
|
import sys
|
||||||
|
import traceback
|
||||||
|
import warnings
|
||||||
|
|
||||||
try:
|
# since on Windows sal3.dll no longer calls WSAStartup
|
||||||
import __builtin__
|
import socket
|
||||||
except ImportError:
|
|
||||||
import builtins as __builtin__
|
|
||||||
|
|
||||||
try:
|
# Some Python 2/3 compatibility code copied from the six library
|
||||||
unicode
|
PY2 = sys.version_info[0] == 2
|
||||||
except NameError:
|
|
||||||
# Python 3 compatibility
|
|
||||||
unicode = str
|
|
||||||
|
|
||||||
import socket # since on Windows sal3.dll no longer calls WSAStartup
|
if PY2:
|
||||||
|
six_string_types = basestring,
|
||||||
|
else:
|
||||||
|
six_string_types = str,
|
||||||
|
|
||||||
|
# All functions and variables starting with a underscore (_) must be
|
||||||
|
# considered private and can be changed at any time. Don't use them.
|
||||||
|
_component_context = pyuno.getComponentContext()
|
||||||
|
|
||||||
# all functions and variables starting with a underscore (_) must be considered private
|
|
||||||
# and can be changed at any time. Don't use them
|
|
||||||
_g_ctx = pyuno.getComponentContext( )
|
|
||||||
_g_delegatee = __builtin__.__dict__["__import__"]
|
|
||||||
|
|
||||||
def getComponentContext():
|
def getComponentContext():
|
||||||
""" returns the UNO component context, that was used to initialize the python runtime.
|
"""Returns the UNO component context used to initialize the Python runtime."""
|
||||||
|
|
||||||
|
return _component_context
|
||||||
|
|
||||||
|
|
||||||
|
def getCurrentContext():
|
||||||
|
"""Returns the current context.
|
||||||
|
|
||||||
|
See http://udk.openoffice.org/common/man/concept/uno_contexts.html#current_context
|
||||||
|
for an explanation on the current context concept.
|
||||||
"""
|
"""
|
||||||
return _g_ctx
|
|
||||||
|
|
||||||
def getConstantByName( constant ):
|
return pyuno.getCurrentContext()
|
||||||
"Looks up the value of a idl constant by giving its explicit name"
|
|
||||||
return pyuno.getConstantByName( constant )
|
|
||||||
|
|
||||||
def getTypeByName( typeName):
|
|
||||||
""" returns a uno.Type instance of the type given by typeName. In case the
|
def setCurrentContext(newContext):
|
||||||
type does not exist, a com.sun.star.uno.RuntimeException is raised.
|
"""Sets newContext as new UNO context.
|
||||||
|
|
||||||
|
The newContext must implement the XCurrentContext interface. The
|
||||||
|
implementation should handle the desired properties and delegate
|
||||||
|
unknown properties to the old context. Ensure that the old one
|
||||||
|
is reset when you leave your stack, see
|
||||||
|
http://udk.openoffice.org/common/man/concept/uno_contexts.html#current_context
|
||||||
"""
|
"""
|
||||||
return pyuno.getTypeByName( typeName )
|
|
||||||
|
|
||||||
def createUnoStruct( typeName, *args, **kwargs ):
|
return pyuno.setCurrentContext(newContext)
|
||||||
"""creates a uno struct or exception given by typeName. Can be called with:
|
|
||||||
|
|
||||||
|
def getConstantByName(constant):
|
||||||
|
"""Looks up the value of an IDL constant by giving its explicit name."""
|
||||||
|
|
||||||
|
return pyuno.getConstantByName(constant)
|
||||||
|
|
||||||
|
|
||||||
|
def getTypeByName(typeName):
|
||||||
|
"""Returns a `uno.Type` instance of the type given by typeName.
|
||||||
|
|
||||||
|
If the type does not exist, a `com.sun.star.uno.RuntimeException` is raised.
|
||||||
|
"""
|
||||||
|
|
||||||
|
return pyuno.getTypeByName(typeName)
|
||||||
|
|
||||||
|
|
||||||
|
def createUnoStruct(typeName, *args, **kwargs):
|
||||||
|
"""Creates a UNO struct or exception given by typeName.
|
||||||
|
|
||||||
|
Can be called with:
|
||||||
|
|
||||||
1) No additional argument.
|
1) No additional argument.
|
||||||
In this case, you get a default constructed uno structure.
|
In this case, you get a default constructed UNO structure.
|
||||||
( e.g. createUnoStruct( "com.sun.star.uno.Exception" ) )
|
(e.g. `createUnoStruct("com.sun.star.uno.Exception")`)
|
||||||
2) Exactly one additional argument that is an instance of typeName.
|
2) Exactly one additional argument that is an instance of typeName.
|
||||||
In this case, a copy constructed instance of typeName is returned
|
In this case, a copy constructed instance of typeName is returned
|
||||||
( e.g. createUnoStruct( "com.sun.star.uno.Exception" , e ) )
|
(e.g. `createUnoStruct("com.sun.star.uno.Exception" , e)`)
|
||||||
3) As many additional arguments as the number of elements within typeName
|
3) As many additional arguments as the number of elements within typeName
|
||||||
(e.g. createUnoStruct( "com.sun.star.uno.Exception", "foo error" , self) ).
|
(e.g. `createUnoStruct("com.sun.star.uno.Exception", "foo error" , self)`).
|
||||||
4) Keyword arguments to give values for each element of the struct by name.
|
4) Keyword arguments to give values for each element of the struct by name.
|
||||||
5) A mix of 3) and 4), such that each struct element is given a value exactly once,
|
5) A mix of 3) and 4), such that each struct element is given a value exactly once,
|
||||||
either by a positional argument or by a keyword argument.
|
either by a positional argument or by a keyword argument.
|
||||||
|
|
||||||
The additional and/or keyword arguments must match the type of each struct element,
|
The additional and/or keyword arguments must match the type of each struct element,
|
||||||
otherwise an exception is thrown.
|
otherwise an exception is thrown.
|
||||||
"""
|
"""
|
||||||
return getClass(typeName)( *args, **kwargs )
|
|
||||||
|
|
||||||
def getClass( typeName ):
|
return getClass(typeName)(*args, **kwargs)
|
||||||
"""returns the class of a concrete uno exception, struct or interface
|
|
||||||
"""
|
|
||||||
|
def getClass(typeName):
|
||||||
|
"""Returns the class of a concrete UNO exception, struct, or interface."""
|
||||||
|
|
||||||
return pyuno.getClass(typeName)
|
return pyuno.getClass(typeName)
|
||||||
|
|
||||||
def isInterface( obj ):
|
|
||||||
"""returns true, when obj is a class of a uno interface"""
|
def isInterface(obj):
|
||||||
return pyuno.isInterface( obj )
|
"""Returns True, when obj is a class of a UNO interface."""
|
||||||
|
|
||||||
|
return pyuno.isInterface(obj)
|
||||||
|
|
||||||
|
|
||||||
def generateUuid():
|
def generateUuid():
|
||||||
"returns a 16 byte sequence containing a newly generated uuid or guid, see rtl/uuid.h "
|
"""Returns a 16 byte sequence containing a newly generated uuid or guid.
|
||||||
|
|
||||||
|
For more information, see rtl/uuid.h.
|
||||||
|
"""
|
||||||
|
|
||||||
return pyuno.generateUuid()
|
return pyuno.generateUuid()
|
||||||
|
|
||||||
def systemPathToFileUrl( systemPath ):
|
|
||||||
"returns a file-url for the given system path"
|
|
||||||
return pyuno.systemPathToFileUrl( systemPath )
|
|
||||||
|
|
||||||
def fileUrlToSystemPath( url ):
|
def systemPathToFileUrl(systemPath):
|
||||||
"returns a system path (determined by the system, the python interpreter is running on)"
|
"""Returns a file URL for the given system path."""
|
||||||
return pyuno.fileUrlToSystemPath( url )
|
|
||||||
|
|
||||||
def absolutize( path, relativeUrl ):
|
return pyuno.systemPathToFileUrl(systemPath)
|
||||||
"returns an absolute file url from the given urls"
|
|
||||||
return pyuno.absolutize( path, relativeUrl )
|
|
||||||
|
|
||||||
def getCurrentContext():
|
|
||||||
"""Returns the currently valid current context.
|
def fileUrlToSystemPath(url):
|
||||||
see http://udk.openoffice.org/common/man/concept/uno_contexts.html#current_context
|
"""Returns a system path.
|
||||||
for an explanation on the current context concept
|
|
||||||
|
This path is determined by the system that the Python interpreter is running on.
|
||||||
"""
|
"""
|
||||||
return pyuno.getCurrentContext()
|
|
||||||
|
|
||||||
def setCurrentContext( newContext ):
|
return pyuno.fileUrlToSystemPath(url)
|
||||||
"""Sets newContext as new uno current context. The newContext must
|
|
||||||
implement the XCurrentContext interface. The implementation should
|
|
||||||
handle the desired properties and delegate unknown properties to the
|
def absolutize(path, relativeUrl):
|
||||||
old context. Ensure to reset the old one when you leave your stack ...
|
"""Returns an absolute file url from the given urls."""
|
||||||
see http://udk.openoffice.org/common/man/concept/uno_contexts.html#current_context
|
|
||||||
"""
|
return pyuno.absolutize(path, relativeUrl)
|
||||||
return pyuno.setCurrentContext( newContext )
|
|
||||||
|
|
||||||
|
|
||||||
class Enum:
|
class Enum:
|
||||||
"Represents a UNO idl enum, use an instance of this class to explicitly pass a boolean to UNO"
|
"""Represents a UNO enum.
|
||||||
#typeName the name of the enum as a string
|
|
||||||
#value the actual value of this enum as a string
|
Use an instance of this class to explicitly pass an enum to UNO.
|
||||||
def __init__(self,typeName, value):
|
|
||||||
|
:param typeName: The name of the enum as a string.
|
||||||
|
:param value: The actual value of this enum as a string.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, typeName, value):
|
||||||
self.typeName = typeName
|
self.typeName = typeName
|
||||||
self.value = value
|
self.value = value
|
||||||
pyuno.checkEnum( self )
|
pyuno.checkEnum(self)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<uno.Enum %s (%r)>" % (self.typeName, self.value)
|
return "<Enum instance %s (%r)>" % (self.typeName, self.value)
|
||||||
|
|
||||||
def __eq__(self, that):
|
def __eq__(self, that):
|
||||||
if not isinstance(that, Enum):
|
if not isinstance(that, Enum):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return (self.typeName == that.typeName) and (self.value == that.value)
|
return (self.typeName == that.typeName) and (self.value == that.value)
|
||||||
|
|
||||||
|
|
||||||
class Type:
|
class Type:
|
||||||
"Represents a UNO type, use an instance of this class to explicitly pass a boolean to UNO"
|
"""Represents a UNO type.
|
||||||
# typeName # Name of the UNO type
|
|
||||||
# typeClass # python Enum of TypeClass, see com/sun/star/uno/TypeClass.idl
|
Use an instance of this class to explicitly pass a type to UNO.
|
||||||
|
|
||||||
|
:param typeName: Name of the UNO type
|
||||||
|
:param typeClass: Python Enum of TypeClass, see com/sun/star/uno/TypeClass.idl
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, typeName, typeClass):
|
def __init__(self, typeName, typeClass):
|
||||||
self.typeName = typeName
|
self.typeName = typeName
|
||||||
self.typeClass = typeClass
|
self.typeClass = typeClass
|
||||||
pyuno.checkType(self)
|
pyuno.checkType(self)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<Type instance %s (%r)>" % (self.typeName, self.typeClass)
|
return "<Type instance %s (%r)>" % (self.typeName, self.typeClass)
|
||||||
|
|
||||||
def __eq__(self, that):
|
def __eq__(self, that):
|
||||||
if not isinstance(that, Type):
|
if not isinstance(that, Type):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return self.typeClass == that.typeClass and self.typeName == that.typeName
|
return self.typeClass == that.typeClass and self.typeName == that.typeName
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return self.typeName.__hash__()
|
return self.typeName.__hash__()
|
||||||
|
|
||||||
|
|
||||||
class Bool(object):
|
class Bool(object):
|
||||||
"""Represents a UNO boolean, use an instance of this class to explicitly
|
"""Represents a UNO boolean.
|
||||||
pass a boolean to UNO.
|
|
||||||
Note: This class is deprecated. Use python's True and False directly instead
|
Use an instance of this class to explicitly pass a boolean to UNO.
|
||||||
|
|
||||||
|
Note: This class is deprecated. Use Python's True and False directly instead.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __new__(cls, value):
|
def __new__(cls, value):
|
||||||
if isinstance(value, (str, unicode)) and value == "true":
|
message = "The Bool class is deprecated. Use Python's True and False directly instead."
|
||||||
|
warnings.warn(message, DeprecationWarning)
|
||||||
|
|
||||||
|
if isinstance(value, six_string_types) and value == "true":
|
||||||
return True
|
return True
|
||||||
if isinstance(value, (str, unicode)) and value == "false":
|
|
||||||
|
if isinstance(value, six_string_types) and value == "false":
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if value:
|
if value:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
class Char:
|
class Char:
|
||||||
"Represents a UNO char, use an instance of this class to explicitly pass a char to UNO"
|
"""Represents a UNO char.
|
||||||
# @param value pass a Unicode string with length 1
|
|
||||||
def __init__(self,value):
|
Use an instance of this class to explicitly pass a char to UNO.
|
||||||
assert isinstance(value, unicode)
|
|
||||||
assert len(value) == 1
|
For Python 2, this class only works with unicode objects. Creating
|
||||||
self.value=value
|
a Char instance with a normal str object or comparing a Char instance
|
||||||
|
to a normal str object will raise an AssertionError.
|
||||||
|
|
||||||
|
:param value: A Unicode string with length 1
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, value):
|
||||||
|
if PY2:
|
||||||
|
assert isinstance(value, unicode), "Expected unicode object, got %s instead." % type(value)
|
||||||
|
else:
|
||||||
|
assert isinstance(value, str), "Expected str object, got %s instead." % type(value)
|
||||||
|
|
||||||
|
assert len(value) == 1, "Char value must have length of 1."
|
||||||
|
|
||||||
|
self.value = value
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<Char instance %s>" % (self.value, )
|
return "<Char instance %s>" % (self.value,)
|
||||||
|
|
||||||
def __eq__(self, that):
|
def __eq__(self, that):
|
||||||
if isinstance(that, (str, unicode)):
|
if isinstance(that, six_string_types):
|
||||||
if len(that) > 1:
|
if len(that) > 1:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return self.value == that[0]
|
return self.value == that[0]
|
||||||
|
|
||||||
if isinstance(that, Char):
|
if isinstance(that, Char):
|
||||||
return self.value == that.value
|
return self.value == that.value
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Suggested by Christian, but still some open problems which need to be solved first
|
|
||||||
#
|
|
||||||
#class ByteSequence(str):
|
|
||||||
#
|
|
||||||
# def __repr__(self):
|
|
||||||
# return "<ByteSequence instance %s>" % str.__repr__(self)
|
|
||||||
|
|
||||||
# for a little bit compatibility; setting value is not possible as
|
|
||||||
# strings are immutable
|
|
||||||
# def _get_value(self):
|
|
||||||
# return self
|
|
||||||
#
|
|
||||||
# value = property(_get_value)
|
|
||||||
|
|
||||||
class ByteSequence:
|
class ByteSequence:
|
||||||
|
"""Represents a UNO ByteSequence value.
|
||||||
|
|
||||||
|
Use an instance of this class to explicitly pass a byte sequence to UNO.
|
||||||
|
|
||||||
|
:param value: A string or bytesequence
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, value):
|
def __init__(self, value):
|
||||||
if isinstance(value, bytes):
|
if isinstance(value, bytes):
|
||||||
self.value = value
|
self.value = value
|
||||||
elif isinstance(value, str):
|
|
||||||
self.value = value.encode("utf-8") # Python 2 compatibility
|
|
||||||
elif isinstance(value, ByteSequence):
|
elif isinstance(value, ByteSequence):
|
||||||
self.value = value.value
|
self.value = value.value
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise TypeError("expected string or bytesequence")
|
raise TypeError("Expected bytes object or ByteSequence, got %s instead." % type(value))
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<ByteSequence instance '%s'>" % (self.value, )
|
return "<ByteSequence instance '%s'>" % (self.value,)
|
||||||
|
|
||||||
def __eq__(self, that):
|
def __eq__(self, that):
|
||||||
if isinstance( that, ByteSequence):
|
|
||||||
return self.value == that.value
|
|
||||||
if isinstance(that, bytes):
|
if isinstance(that, bytes):
|
||||||
return self.value == that
|
return self.value == that
|
||||||
if isinstance(that, str):
|
|
||||||
return self.value == that.encode("utf-8")
|
if isinstance(that, ByteSequence):
|
||||||
|
return self.value == that.value
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
@@ -226,86 +296,100 @@ class ByteSequence:
|
|||||||
def __getitem__(self, index):
|
def __getitem__(self, index):
|
||||||
return self.value[index]
|
return self.value[index]
|
||||||
|
|
||||||
def __iter__( self ):
|
def __iter__(self):
|
||||||
return self.value.__iter__()
|
return self.value.__iter__()
|
||||||
|
|
||||||
def __add__( self , b ):
|
def __add__(self, b):
|
||||||
if isinstance( b, bytes):
|
if isinstance(b, bytes):
|
||||||
return ByteSequence(self.value + b)
|
return ByteSequence(self.value + b)
|
||||||
elif isinstance( b, str ):
|
|
||||||
return ByteSequence( self.value + b.encode("utf-8") )
|
|
||||||
elif isinstance( b, ByteSequence ):
|
|
||||||
return ByteSequence( self.value + b.value )
|
|
||||||
raise TypeError( "expected string or ByteSequence as operand" )
|
|
||||||
|
|
||||||
def __hash__( self ):
|
elif isinstance(b, ByteSequence):
|
||||||
|
return ByteSequence(self.value + b.value)
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise TypeError("Can't add ByteString and %s." % type(b))
|
||||||
|
|
||||||
|
def __hash__(self):
|
||||||
return self.value.hash()
|
return self.value.hash()
|
||||||
|
|
||||||
|
|
||||||
class Any:
|
class Any:
|
||||||
"use only in connection with uno.invoke() to pass an explicit typed any"
|
"""Represents a UNO Any value.
|
||||||
def __init__(self, type, value ):
|
|
||||||
if isinstance( type, Type ):
|
Use only in connection with uno.invoke() to pass an explicit typed Any.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, type, value):
|
||||||
|
if isinstance(type, Type):
|
||||||
self.type = type
|
self.type = type
|
||||||
else:
|
else:
|
||||||
self.type = getTypeByName( type )
|
self.type = getTypeByName(type)
|
||||||
|
|
||||||
self.value = value
|
self.value = value
|
||||||
|
|
||||||
def invoke( object, methodname, argTuple ):
|
|
||||||
"use this function to pass exactly typed anys to the callee (using uno.Any)"
|
|
||||||
return pyuno.invoke( object, methodname, argTuple )
|
|
||||||
|
|
||||||
#---------------------------------------------------------------------------------------
|
def invoke(object, methodname, argTuple):
|
||||||
# don't use any functions beyond this point, private section, likely to change
|
"""Use this function to pass exactly typed Anys to the callee (using uno.Any)."""
|
||||||
#---------------------------------------------------------------------------------------
|
|
||||||
#def _uno_import( name, globals={}, locals={}, fromlist=[], level=-1 ):
|
return pyuno.invoke(object, methodname, argTuple)
|
||||||
def _uno_import( name, *optargs, **kwargs ):
|
|
||||||
|
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
# Don't use any functions beyond this point; private section, likely to change.
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
_builtin_import = __import__
|
||||||
|
|
||||||
|
|
||||||
|
def _uno_import(name, *optargs, **kwargs):
|
||||||
|
"""Overrides built-in import to allow directly importing LibreOffice classes."""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# print "optargs = " + repr(optargs)
|
return _builtin_import(name, *optargs, **kwargs)
|
||||||
return _g_delegatee( name, *optargs, **kwargs )
|
|
||||||
except ImportError as e:
|
except ImportError as e:
|
||||||
# process optargs
|
# process optargs
|
||||||
globals, locals, fromlist = list(optargs)[:3] + [kwargs.get('globals',{}), kwargs.get('locals',{}), kwargs.get('fromlist',[])][len(optargs):]
|
globals, locals, fromlist = list(optargs)[:3] + [kwargs.get('globals', {}), kwargs.get('locals', {}),
|
||||||
|
kwargs.get('fromlist', [])][len(optargs):]
|
||||||
|
|
||||||
# from import form only, but skip if an uno lookup has already failed
|
# from import form only, but skip if an uno lookup has already failed
|
||||||
if not fromlist or hasattr(e, '_uno_import_failed'):
|
if not fromlist or hasattr(e, '_uno_import_failed'):
|
||||||
raise
|
raise
|
||||||
|
|
||||||
# hang onto exception for possible use on subsequent uno lookup failure
|
# hang onto exception for possible use on subsequent uno lookup failure
|
||||||
py_import_exc = e
|
py_import_exc = e
|
||||||
modnames = name.split( "." )
|
|
||||||
mod = None
|
mod = None
|
||||||
d = sys.modules
|
d = sys.modules
|
||||||
for x in modnames:
|
|
||||||
if x in d:
|
for module in name.split("."):
|
||||||
mod = d[x]
|
if module in d:
|
||||||
|
mod = d[module]
|
||||||
else:
|
else:
|
||||||
mod = pyuno.__class__(x) # How to create a module ??
|
# How to create a module ??
|
||||||
|
mod = pyuno.__class__(module)
|
||||||
|
|
||||||
d = mod.__dict__
|
d = mod.__dict__
|
||||||
|
|
||||||
RuntimeException = pyuno.getClass( "com.sun.star.uno.RuntimeException" )
|
RuntimeException = pyuno.getClass("com.sun.star.uno.RuntimeException")
|
||||||
for x in fromlist:
|
|
||||||
if x not in d:
|
for class_name in fromlist:
|
||||||
|
if class_name not in d:
|
||||||
failed = False
|
failed = False
|
||||||
if x.startswith( "typeOf" ):
|
|
||||||
try:
|
|
||||||
d[x] = pyuno.getTypeByName( name + "." + x[6:len(x)] )
|
|
||||||
except RuntimeException:
|
|
||||||
failed = True
|
|
||||||
else:
|
|
||||||
try:
|
try:
|
||||||
# check for structs, exceptions or interfaces
|
# check for structs, exceptions or interfaces
|
||||||
d[x] = pyuno.getClass( name + "." + x )
|
d[class_name] = pyuno.getClass(name + "." + class_name)
|
||||||
except RuntimeException:
|
except RuntimeException:
|
||||||
# check for enums
|
# check for enums
|
||||||
try:
|
try:
|
||||||
d[x] = Enum( name , x )
|
d[class_name] = Enum(name, class_name)
|
||||||
except RuntimeException:
|
except RuntimeException:
|
||||||
# check for constants
|
# check for constants
|
||||||
try:
|
try:
|
||||||
d[x] = getConstantByName( name + "." + x )
|
d[class_name] = getConstantByName(name + "." + class_name)
|
||||||
except RuntimeException:
|
except RuntimeException:
|
||||||
# check for constant group
|
# check for constant group
|
||||||
try:
|
try:
|
||||||
d[x] = _impl_getConstantGroupByName( name, x )
|
d[class_name] = _impl_getConstantGroupByName(name, class_name)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
failed = True
|
failed = True
|
||||||
|
|
||||||
@@ -332,105 +416,136 @@ def _uno_import( name, *optargs, **kwargs ):
|
|||||||
# preventing us from re-processing our own import errors.
|
# preventing us from re-processing our own import errors.
|
||||||
|
|
||||||
uno_import_exc = ImportError("%s (or '%s.%s' is unknown)" %
|
uno_import_exc = ImportError("%s (or '%s.%s' is unknown)" %
|
||||||
(py_import_exc, name, x))
|
(py_import_exc, name, class_name))
|
||||||
|
|
||||||
if sys.version_info[0] >= 3:
|
if sys.version_info[0] >= 3:
|
||||||
uno_import_exc = uno_import_exc.with_traceback(py_import_exc.__traceback__)
|
uno_import_exc = uno_import_exc.with_traceback(py_import_exc.__traceback__)
|
||||||
|
|
||||||
uno_import_exc._uno_import_failed = True
|
uno_import_exc._uno_import_failed = True
|
||||||
raise uno_import_exc
|
raise uno_import_exc
|
||||||
|
|
||||||
return mod
|
return mod
|
||||||
|
|
||||||
# private
|
|
||||||
class _ConstantGroup(object):
|
|
||||||
__slots__ = ['_constants']
|
|
||||||
def __init__(self, constants):
|
|
||||||
self._constants = constants
|
|
||||||
def __dir__(self):
|
|
||||||
return self._constants.keys()
|
|
||||||
def __getattr__(self,name):
|
|
||||||
if name in self._constants:
|
|
||||||
return self._constants[name]
|
|
||||||
raise AttributeError
|
|
||||||
|
|
||||||
# private
|
try:
|
||||||
def _impl_getConstantGroupByName( module, group ):
|
import __builtin__
|
||||||
CONSTANTS = Enum('com.sun.star.uno.TypeClass', 'CONSTANTS')
|
except ImportError:
|
||||||
ONE = Enum('com.sun.star.reflection.TypeDescriptionSearchDepth', 'ONE')
|
import builtins as __builtin__
|
||||||
tdm = _g_ctx.getValueByName('/singletons/com.sun.star.reflection.theTypeDescriptionManager')
|
|
||||||
tde = tdm.createTypeDescriptionEnumeration(module,(CONSTANTS,),ONE)
|
|
||||||
qualifiedName = module + '.' + group
|
|
||||||
for td in tde:
|
|
||||||
if td.Name == qualifiedName:
|
|
||||||
return_dict = dict()
|
|
||||||
for c in td.Constants:
|
|
||||||
return_dict.update({c.Name.split('.')[-1]: c.ConstantValue})
|
|
||||||
return _ConstantGroup(return_dict)
|
|
||||||
else:
|
|
||||||
raise ValueError
|
|
||||||
|
|
||||||
# private function, don't use
|
|
||||||
def _impl_extractName(name):
|
|
||||||
r = list(range(len(name)-1,0,-1))
|
|
||||||
for i in r:
|
|
||||||
if name[i] == ".":
|
|
||||||
name = name[i+1:len(name)]
|
|
||||||
break
|
|
||||||
return name
|
|
||||||
|
|
||||||
# private, referenced from the pyuno shared library
|
|
||||||
def _uno_struct__init__(self,*args, **kwargs):
|
|
||||||
if len(kwargs) == 0 and len(args) == 1 and hasattr(args[0], "__class__") and args[0].__class__ == self.__class__ :
|
|
||||||
self.__dict__["value"] = args[0]
|
|
||||||
else:
|
|
||||||
struct, used = pyuno._createUnoStructHelper(self.__class__.__pyunostruct__,args,**kwargs)
|
|
||||||
for kw in kwargs.keys():
|
|
||||||
if not (kw in used and used[kw]):
|
|
||||||
RuntimeException = pyuno.getClass( "com.sun.star.uno.RuntimeException" )
|
|
||||||
raise RuntimeException("_uno_struct__init__: unused keyword argument '" + kw + "'", None)
|
|
||||||
self.__dict__["value"] = struct
|
|
||||||
|
|
||||||
# private, referenced from the pyuno shared library
|
|
||||||
def _uno_struct__getattr__(self,name):
|
|
||||||
return __builtin__.getattr(self.__dict__["value"],name)
|
|
||||||
|
|
||||||
# private, referenced from the pyuno shared library
|
|
||||||
def _uno_struct__setattr__(self,name,value):
|
|
||||||
return __builtin__.setattr(self.__dict__["value"],name,value)
|
|
||||||
|
|
||||||
# private, referenced from the pyuno shared library
|
|
||||||
def _uno_struct__repr__(self):
|
|
||||||
return repr(self.__dict__["value"])
|
|
||||||
|
|
||||||
def _uno_struct__str__(self):
|
|
||||||
return str(self.__dict__["value"])
|
|
||||||
|
|
||||||
# private, referenced from the pyuno shared library
|
|
||||||
def _uno_struct__eq__(self,cmp):
|
|
||||||
if hasattr(cmp,"value"):
|
|
||||||
return self.__dict__["value"] == cmp.__dict__["value"]
|
|
||||||
return False
|
|
||||||
|
|
||||||
# referenced from pyuno shared lib and pythonscript.py
|
|
||||||
def _uno_extract_printable_stacktrace( trace ):
|
|
||||||
mod = None
|
|
||||||
try:
|
|
||||||
mod = __import__("traceback")
|
|
||||||
except ImportError as e:
|
|
||||||
pass
|
|
||||||
ret = ""
|
|
||||||
if mod:
|
|
||||||
lst = mod.extract_tb( trace )
|
|
||||||
max = len(lst)
|
|
||||||
for j in range(max):
|
|
||||||
i = lst[max-j-1]
|
|
||||||
ret = ret + " " + str(i[0]) + ":" + \
|
|
||||||
str(i[1]) + " in function " + \
|
|
||||||
str(i[2]) + "() [" + str(i[3]) + "]\n"
|
|
||||||
else:
|
|
||||||
ret = "Couldn't import traceback module"
|
|
||||||
return ret
|
|
||||||
|
|
||||||
# hook into the __import__ chain
|
# hook into the __import__ chain
|
||||||
__builtin__.__dict__["__import__"] = _uno_import
|
__builtin__.__dict__['__import__'] = _uno_import
|
||||||
|
|
||||||
|
|
||||||
|
class _ConstantGroup(object):
|
||||||
|
"""Represents a group of UNOIDL constants."""
|
||||||
|
|
||||||
|
__slots__ = ['_constants']
|
||||||
|
|
||||||
|
def __init__(self, constants):
|
||||||
|
self._constants = constants
|
||||||
|
|
||||||
|
def __dir__(self):
|
||||||
|
return self._constants.keys()
|
||||||
|
|
||||||
|
def __getattr__(self, name):
|
||||||
|
if name in self._constants:
|
||||||
|
return self._constants[name]
|
||||||
|
|
||||||
|
raise AttributeError("The constant '%s' could not be found." % name)
|
||||||
|
|
||||||
|
|
||||||
|
def _impl_getConstantGroupByName(module, group):
|
||||||
|
"""Gets UNOIDL constant group by name."""
|
||||||
|
|
||||||
|
constants = Enum('com.sun.star.uno.TypeClass', 'CONSTANTS')
|
||||||
|
one = Enum('com.sun.star.reflection.TypeDescriptionSearchDepth', 'ONE')
|
||||||
|
type_desc_mgr = _component_context.getValueByName('/singletons/com.sun.star.reflection.theTypeDescriptionManager')
|
||||||
|
type_descs = type_desc_mgr.createTypeDescriptionEnumeration(module, (constants,), one)
|
||||||
|
qualified_name = module + '.' + group
|
||||||
|
|
||||||
|
for type_desc in type_descs:
|
||||||
|
if type_desc.Name == qualified_name:
|
||||||
|
return _ConstantGroup(dict(
|
||||||
|
(c.Name.split('.')[-1], c.ConstantValue)
|
||||||
|
for c in type_desc.Constants))
|
||||||
|
|
||||||
|
raise ValueError("The constant group '%s' could not be found." % qualified_name)
|
||||||
|
|
||||||
|
|
||||||
|
def _uno_struct__init__(self, *args, **kwargs):
|
||||||
|
"""Initializes a UNO struct.
|
||||||
|
|
||||||
|
Referenced from the pyuno shared library.
|
||||||
|
|
||||||
|
This function can be called with either an already constructed UNO struct, which it
|
||||||
|
will then just reference without copying, or with arguments to create a new UNO struct.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Check to see if this function was passed an existing UNO struct
|
||||||
|
if len(kwargs) == 0 and len(args) == 1 and getattr(args[0], "__class__", None) == self.__class__:
|
||||||
|
self.__dict__['value'] = args[0]
|
||||||
|
else:
|
||||||
|
struct, used = pyuno._createUnoStructHelper(self.__class__.__pyunostruct__, args, **kwargs)
|
||||||
|
|
||||||
|
for kwarg in kwargs.keys():
|
||||||
|
if not used.get(kwarg):
|
||||||
|
RuntimeException = pyuno.getClass("com.sun.star.uno.RuntimeException")
|
||||||
|
raise RuntimeException("_uno_struct__init__: unused keyword argument '%s'." % kwarg, None)
|
||||||
|
|
||||||
|
self.__dict__["value"] = struct
|
||||||
|
|
||||||
|
|
||||||
|
def _uno_struct__getattr__(self, name):
|
||||||
|
"""Gets attribute from UNO struct.
|
||||||
|
|
||||||
|
Referenced from the pyuno shared library.
|
||||||
|
"""
|
||||||
|
|
||||||
|
return getattr(self.__dict__["value"], name)
|
||||||
|
|
||||||
|
|
||||||
|
def _uno_struct__setattr__(self, name, value):
|
||||||
|
"""Sets attribute on UNO struct.
|
||||||
|
|
||||||
|
Referenced from the pyuno shared library.
|
||||||
|
"""
|
||||||
|
|
||||||
|
return setattr(self.__dict__["value"], name, value)
|
||||||
|
|
||||||
|
|
||||||
|
def _uno_struct__repr__(self):
|
||||||
|
"""Converts a UNO struct to a printable string.
|
||||||
|
|
||||||
|
Referenced from the pyuno shared library.
|
||||||
|
"""
|
||||||
|
|
||||||
|
return repr(self.__dict__["value"])
|
||||||
|
|
||||||
|
|
||||||
|
def _uno_struct__str__(self):
|
||||||
|
"""Converts a UNO struct to a string."""
|
||||||
|
|
||||||
|
return str(self.__dict__["value"])
|
||||||
|
|
||||||
|
|
||||||
|
def _uno_struct__eq__(self, that):
|
||||||
|
"""Compares two UNO structs.
|
||||||
|
|
||||||
|
Referenced from the pyuno shared library.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if hasattr(that, "value"):
|
||||||
|
return self.__dict__["value"] == that.__dict__["value"]
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def _uno_extract_printable_stacktrace(trace):
|
||||||
|
"""Extracts a printable stacktrace.
|
||||||
|
|
||||||
|
Referenced from pyuno shared lib and pythonscript.py.
|
||||||
|
"""
|
||||||
|
|
||||||
|
return ''.join(traceback.format_tb(trace))
|
||||||
|
|
||||||
# vim:set shiftwidth=4 softtabstop=4 expandtab:
|
# vim:set shiftwidth=4 softtabstop=4 expandtab:
|
||||||
|
Reference in New Issue
Block a user