Ok I have a working solution.
Thanks @mikekaganski for you help.
from __future__ import annotations
from typing import Any, Tuple
import contextlib
import uno
from com.sun.star.container import XElementAccess
from com.sun.star.container import XIndexAccess
from com.sun.star.uno import XInterface
from com.sun.star.beans import PropertyValue
import unohelper
from ooodev.loader import Lo
from ooodev.calc import CalcDoc
class IndexAccess(unohelper.Base, XIndexAccess, XElementAccess, XInterface):
__pyunointerface__: str = "com.sun.star.container.XIndexAccess"
def __init__(self, data: Tuple[Tuple[PropertyValue, ...]]):
super().__init__()
self._data = data
self._types = None
# region XInterface
def acquire(self) -> None:
pass
def release(self) -> None:
pass
def queryInterface(self, a_type: Any) -> Any:
with contextlib.suppress(Exception):
if a_type in self.getTypes():
return self
return None
# end region XInterface
# region XIndexAccess
def getCount(self):
return len(self._data)
def getByIndex(self, index):
return self._data[index]
# endregion XIndexAccess
# region XElementAccess
def hasElements(self):
return self.getCount() > 0
def getElementType(self) -> Any:
"""
Gets te Element Type
"""
t = uno.getTypeByName("[]com.sun.star.beans.PropertyValue")
return t
# endregion XElementAccess
# region XTypeProvider
def getImplementationId(self) -> Any:
"""
Obsolete unique identifier.
"""
return b""
def getTypes(self) -> Tuple[Any, ...]:
"""
returns a sequence of all types (usually interface types) provided by the object.
"""
if self._types is None:
types = []
types.append(uno.getTypeByName("com.sun.star.uno.XInterface"))
types.append(uno.getTypeByName("com.sun.star.lang.XTypeProvider"))
types.append(uno.getTypeByName("com.sun.star.container.XElementAccess"))
types.append(uno.getTypeByName("com.sun.star.container.XIndexAccess"))
self._types = tuple(types)
return self._types
def test_index_access():
data = ((PropertyValue("A", 1), PropertyValue("B", 2)), (PropertyValue("C", 3), PropertyValue("D", 4)))
ia = IndexAccess(data)
ia_type = uno.getTypeByName("com.sun.star.container.XIndexAccess")
assert ia.queryInterface(ia_type) is ia
assert ia.getCount() == 2
assert ia.getByIndex(0) == data[0]
def main():
loader = Lo.load_office(connector=Lo.ConnectPipe())
doc = CalcDoc.create_doc(loader=loader, visible=True)
try:
test_index_access()
Lo.delay(3_000) # wait 3 seconds
finally:
doc.close()
Lo.close_office()
if __name__ == "__main__":
main()