Move commands into classe LO

This commit is contained in:
El Mau 2022-03-02 22:14:23 -06:00
parent 71d40fa6fa
commit 0f2638ebaa
1 changed files with 130 additions and 53 deletions

View File

@ -294,59 +294,6 @@ def save_log(path: str, data: Any) -> None:
return
def _set_app_command(command: str, disable: bool):
NEW_NODE_NAME = f'zaz_disable_command_{command.lower()}'
name = 'com.sun.star.configuration.ConfigurationProvider'
service = 'com.sun.star.configuration.ConfigurationUpdateAccess'
node_name = '/org.openoffice.Office.Commands/Execute/Disabled'
cp = create_instance(name, True)
node = PropertyValue(Name='nodepath', Value=node_name)
update = cp.createInstanceWithArguments(service, (node,))
result = True
try:
if disable:
new_node = update.createInstanceWithArguments(())
new_node.setPropertyValue('Command', command)
update.insertByName(NEW_NODE_NAME, new_node)
else:
update.removeByName(NEW_NODE_NAME)
update.commitChanges()
except Exception as e:
result = False
return result
class commands():
"""Class for disable and enable commands
:param command: UNO Command for disable or enable
:type command: str
`See DispatchCommands <https://wiki.documentfoundation.org/Development/DispatchCommands>`_
"""
def __init__(self, command):
self._command = command
def disable(self):
"""Disable command
:return: True if correctly disable, False if not.
:rtype: bool
"""
return _set_app_command(self._command, True)
def enabled(self):
"""Enable command
:return: True if correctly enable, False if not.
:rtype: bool
"""
return _set_app_command(self._command, False)
def mri(obj: Any) -> None:
"""Inspect object with MRI Extension
@ -1971,6 +1918,134 @@ class Color(object):
COLOR_ON_FOCUS = Color()('LightYellow')
class LO(object):
"""Class for LibreOffice
"""
class _commands():
"""Class for disable and enable commands
`See DispatchCommands <https://wiki.documentfoundation.org/Development/DispatchCommands>`_
"""
@classmethod
def _set_app_command(cls, command: str, disable: bool):
"""Disable or enabled UNO command
:param command: UNO command to disable or enabled
:type command: str
:param disable: True if disable, False if active
:type disable: bool
:return: True if correctly update, False if not.
:rtype: bool
"""
NEW_NODE_NAME = f'zaz_disable_command_{command.lower()}'
name = 'com.sun.star.configuration.ConfigurationProvider'
service = 'com.sun.star.configuration.ConfigurationUpdateAccess'
node_name = '/org.openoffice.Office.Commands/Execute/Disabled'
cp = create_instance(name, True)
node = PropertyValue(Name='nodepath', Value=node_name)
update = cp.createInstanceWithArguments(service, (node,))
result = True
try:
if disable:
new_node = update.createInstanceWithArguments(())
new_node.setPropertyValue('Command', command)
update.insertByName(NEW_NODE_NAME, new_node)
else:
update.removeByName(NEW_NODE_NAME)
update.commitChanges()
except Exception as e:
result = False
return result
@classmethod
def disable(cls, command: str):
"""Disable UNO command
:param command: UNO command to disable
:type command: str
:return: True if correctly disable, False if not.
:rtype: bool
"""
return cls._set_app_command(command, True)
@classmethod
def enabled(cls, command):
"""Enabled UNO command
:param command: UNO command to enabled
:type command: str
:return: True if correctly disable, False if not.
:rtype: bool
"""
return cls._set_app_command(command, False)
@_classproperty
def cmd(cls):
return cls._commands
@_classproperty
def desktop(cls) -> Any:
"""Create desktop instance
:return: Desktop instance
:rtype: pyUno
"""
obj = create_instance('com.sun.star.frame.Desktop', True)
return obj
@classmethod
def dispatch(cls, frame: Any, command: str, args: dict={}) -> None:
"""Call dispatch, used only if not exists directly in API
:param frame: doc or frame instance
:type frame: pyUno
:param command: Command to execute
:type command: str
:param args: Extra argument for command
:type args: dict
`See DispatchCommands <`See DispatchCommands <https://wiki.documentfoundation.org/Development/DispatchCommands>`_>`_
"""
dispatch = create_instance('com.sun.star.frame.DispatchHelper')
if hasattr(frame, 'frame'):
frame = frame.frame
url = command
if not command.startswith('.uno:'):
url = f'.uno:{command}'
opt = dict_to_property(args)
dispatch.executeDispatch(frame, url, '', 0, opt)
return
@classmethod
def fonts(cls):
"""Get all font visibles in LibreOffice
:return: tuple of FontDescriptors
:rtype: tuple
`See API FontDescriptor <https://api.libreoffice.org/docs/idl/ref/structcom_1_1sun_1_1star_1_1awt_1_1FontDescriptor.html>`_
"""
toolkit = create_instance('com.sun.star.awt.Toolkit')
device = toolkit.createScreenCompatibleDevice(0, 0)
return device.FontDescriptors
@classmethod
def filters(cls):
"""Get all support filters
`See Help ConvertFilters <https://help.libreoffice.org/latest/en-US/text/shared/guide/convertfilters.html>`_
`See API FilterFactory <https://api.libreoffice.org/docs/idl/ref/servicecom_1_1sun_1_1star_1_1document_1_1FilterFactory.html>`_
"""
factory = create_instance('com.sun.star.document.FilterFactory')
rows = [data_to_dict(factory[name]) for name in factory]
for row in rows:
row['UINames'] = data_to_dict(row['UINames'])
return rows
def __getattr__(name):
classes = {
'dates': Dates,
@ -1984,6 +2059,8 @@ def __getattr__(name):
'url': Url,
'email': Email,
'color': Color(),
'lo': LO,
'desktop': LO.desktop,
}
if name in classes:
return classes[name]