Doc for Inspect

This commit is contained in:
El Mau 2022-03-07 22:45:36 -06:00
parent 969294c2af
commit cf903c4f2d
4 changed files with 123 additions and 11 deletions

View File

@ -1,21 +1,18 @@
Calc Calc
==== ====
Remember, always import library. Remember, always import library.
.. code-block:: python .. code-block:: python
import easymacro as app import easymacro as app
Active document .. toctree::
--------------- :maxdepth: 2
.. code-block:: python calc_doc.rst
calc_sheets.rst
doc = app.active calc_ranges.rst
app.debug(doc.type) calc_ranges2.rst
calc_data.rst
.. _support filters: https://help.libreoffice.org/latest/en-US/text/shared/guide/convertfilters.html

40
docs/source/calc_doc.rst Normal file
View File

@ -0,0 +1,40 @@
Active document
---------------
.. code-block:: python
doc = app.active
app.debug(doc.type)
|
Headers
-------
Hide or show columns and rows headers.
.. code-block:: python
doc = app.active
app.msgbox(doc.headers)
doc.headers = not doc.headers
app.msgbox(doc.headers)
doc.headers = not doc.headers
|
Tabs
----
Hide or show tab sheets.
.. code-block:: python
doc = app.active
app.msgbox(doc.tabs)
doc.tabs = not doc.tabs
app.msgbox(doc.tabs)
doc.tabs = not doc.tabs
|

View File

@ -65,6 +65,7 @@ from com.sun.star.awt import Key, KeyEvent, KeyModifier
from com.sun.star.awt import MessageBoxButtons as MSG_BUTTONS from com.sun.star.awt import MessageBoxButtons as MSG_BUTTONS
from com.sun.star.awt.MessageBoxResults import YES from com.sun.star.awt.MessageBoxResults import YES
from com.sun.star.beans import PropertyValue, NamedValue from com.sun.star.beans import PropertyValue, NamedValue
from com.sun.star.beans.PropertyConcept import ALL
from com.sun.star.datatransfer import XTransferable, DataFlavor from com.sun.star.datatransfer import XTransferable, DataFlavor
from com.sun.star.io import IOException, XOutputStream from com.sun.star.io import IOException, XOutputStream
from com.sun.star.ui.dialogs import TemplateDescription from com.sun.star.ui.dialogs import TemplateDescription
@ -473,6 +474,71 @@ def render(template, data):
# Classes # Classes
class LOInspect():
TYPE_CLASSES = {
'INTERFACE': '-Interface-',
'SEQUENCE': '-Sequence-',
'STRUCT': '-Struct-',
}
introspection = create_instance('com.sun.star.beans.Introspection')
def __init__(self, obj: Any, to_doc: bool=False):
if hasattr(obj, 'obj'):
obj = obj.obj
self._obj = obj
self._result = self.introspection.inspect(obj)
self._properties = self._get_properties()
if to_doc:
doc = LODocuments.new()
sheet = doc[0]
sheet.name = 'Properties'
sheet['A1'].data = self.properties
def _get_value(self, p: Any):
type_class = p.Type.typeClass.value
if type_class in self.TYPE_CLASSES:
return self.TYPE_CLASSES[type_class]
value = ''
try:
value = getattr(self._obj, p.Name)
if type_class == 'ENUM' and value:
value = value.value
elif type_class == 'TYPE':
value = value.typeName
elif value is None:
value = '-void-'
except:
value = '-error-'
return value
def _get_attributes(self, a: Any):
PA = {1 : 'Maybe Void', 16 : 'Read Only'}
attr = ', '.join([PA.get(k, '') for k in PA.keys() if a & k])
return attr
def _get_property(self, p: Any):
name = p.Name
tipo = p.Type.typeName
value = self._get_value(p)
attr = self._get_attributes(p.Attributes)
return name, tipo, value, attr
def _get_properties(self):
if self._result is None:
return []
properties = self._result.getProperties(ALL)
data = [('Name', 'Type', 'Value', 'Attributes')]
data += [self._get_property(p) for p in properties]
return data
@property
def properties(self):
return self._properties
# ~ https://github.com/django/django/blob/main/django/utils/functional.py#L61 # ~ https://github.com/django/django/blob/main/django/utils/functional.py#L61
class classproperty: class classproperty:
@ -2955,6 +3021,7 @@ class LODocCalc(LODocument):
self._sheets = obj.Sheets self._sheets = obj.Sheets
def __getitem__(self, index): def __getitem__(self, index):
"""Index access"""
return LOCalcSheet(self._sheets[index]) return LOCalcSheet(self._sheets[index])
def __len__(self): def __len__(self):
@ -2965,25 +3032,31 @@ class LODocCalc(LODocument):
@property @property
def headers(self): def headers(self):
"""Get true if is visible columns/rows headers"""
return self._cc.ColumnRowHeaders return self._cc.ColumnRowHeaders
@headers.setter @headers.setter
def headers(self, value): def headers(self, value):
"""Set visible columns/rows headers"""
self._cc.ColumnRowHeaders = value self._cc.ColumnRowHeaders = value
@property @property
def tabs(self): def tabs(self):
"""Get true if is visible tab sheets"""
return self._cc.SheetTabs return self._cc.SheetTabs
@tabs.setter @tabs.setter
def tabs(self, value): def tabs(self, value):
"""Set visible tab sheets"""
self._cc.SheetTabs = value self._cc.SheetTabs = value
@property @property
def names(self): def names(self):
"""Get all sheet names"""
names = self.obj.Sheets.ElementNames names = self.obj.Sheets.ElementNames
return names return names
@property @property
def active(self): def active(self):
"""Get active sheet"""
return LOCalcSheet(self._cc.ActiveSheet) return LOCalcSheet(self._cc.ActiveSheet)
@ -3222,6 +3295,7 @@ class LODocuments():
def __getattr__(name): def __getattr__(name):
classes = { classes = {
'inspect': LOInspect,
'dates': Dates, 'dates': Dates,
'json': Json, 'json': Json,
'macro': Macro, 'macro': Macro,

View File

@ -0,0 +1 @@
#!/usr/bin/env python3