First most basic example

This commit is contained in:
Mauricio Baeza 2021-07-17 12:58:11 -05:00
parent 2b6f687b90
commit bfffab0b19
6 changed files with 190 additions and 0 deletions

BIN
oxt/my_first_extension.oxt Normal file

Binary file not shown.

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<oor:component-data xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:oor="http://openoffice.org/2001/registry" oor:name="Addons" oor:package="org.openoffice.Office">
<node oor:name="AddonUI">
<node oor:name="OfficeMenuBar">
<node oor:name="org.examples.zaz.000" oor:op="replace">
<prop oor:name="Title" oor:type="xs:string">
<value xml:lang="en">My extension</value>
<value xml:lang="es">Mi extensión</value>
</prop>
<prop oor:name="Target" oor:type="xs:string">
<value>_self</value>
</prop>
<node oor:name="Submenu">
<node oor:name="m0" oor:op="replace">
<prop oor:name="Title" oor:type="xs:string">
<value xml:lang="en">Say hello</value>
<value xml:lang="es">Di hola</value>
</prop>
<prop oor:name="Context" oor:type="xs:string">
<value/>
</prop>
<prop oor:name="URL" oor:type="xs:string">
<value>service:org.examples.zaz.000?argument</value>
</prop>
<prop oor:name="Target" oor:type="xs:string">
<value>_self</value>
</prop>
</node>
</node>
</node>
</node>
</node>
</oor:component-data>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" manifest:version="1.2">
<manifest:file-entry manifest:full-path="main.py" manifest:media-type="application/vnd.sun.star.uno-component;type=Python" />
<manifest:file-entry manifest:full-path="Addons.xcu" manifest:media-type="application/vnd.sun.star.configuration-data" />
</manifest:manifest>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<description xmlns="http://openoffice.org/extensions/description/2006" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:d="http://openoffice.org/extensions/description/2006">
<identifier value="org.examples.zaz.000"/>
<version value="0.1.0"/>
<display-name>
<name lang="en">My first extension</name>
<name lang="es">Mi primera extension</name>
</display-name>
<publisher>
<name xlink:href="https://git.cuates.net/elmau" lang="en">El Mau</name>
</publisher>
</description>

View File

@ -0,0 +1,49 @@
import uno
import unohelper
from com.sun.star.task import XJobExecutor
from com.sun.star.awt import MessageBoxButtons as MSG_BUTTONS
OK = MSG_BUTTONS.BUTTONS_OK
ID_EXTENSION = 'org.examples.zaz.000'
SERVICE = ('com.sun.star.task.Job',)
CTX = uno.getComponentContext()
SM = CTX.getServiceManager()
def create_instance(name, with_context=False):
if with_context:
instance = SM.createInstanceWithContext(name, CTX)
else:
instance = SM.createInstance(name)
return instance
def msgbox(message, title='ZAZ Python', buttons=OK, type_msg='infobox'):
""" Create message box
type_msg: infobox, warningbox, errorbox, querybox, messbox
http://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1awt_1_1XMessageBoxFactory.html
"""
toolkit = create_instance('com.sun.star.awt.Toolkit')
parent = toolkit.getDesktopWindow()
box = toolkit.createMessageBox(parent, type_msg, buttons, title, str(message))
return box.execute()
class MyFirstExtension(unohelper.Base, XJobExecutor):
def __init__(self, ctx):
self.ctx = ctx
# ~ XJobExecutor
def trigger(self, event):
print(event)
msgbox('Dammed World!')
return
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(MyFirstExtension, ID_EXTENSION, SERVICE)

91
zaz.py Executable file
View File

@ -0,0 +1,91 @@
#!/usr/bin/env python3
import argparse
import zipfile
import os
from subprocess import call
PATH_SOFFICE = 'soffice'
APP_TEST = 'calc'
UNOPKG = ('unopkg', 'add', '-v', '-f', '-s')
PATH_SOURCE = 'source'
PATH_TARGET = 'oxt'
_join = os.path.join
_abspath = os.path.abspath
_exists = os.path.exists
def _make_oxt(name):
path_oxt = _join(PATH_TARGET, f'{name}.oxt')
path_source = _join(PATH_SOURCE, name)
if not _exists(path_source):
print(f'\tNot found: {path_source}')
return
print('\tStart compress OXT extension...')
z = zipfile.ZipFile(path_oxt, 'w', compression=zipfile.ZIP_DEFLATED)
root_len = len(_abspath(path_source))
for root, dirs, files in os.walk(path_source):
relative = _abspath(root)[root_len:]
for f in files:
fullpath = _join(root, f)
file_name = _join(relative, f)
z.write(fullpath, file_name, zipfile.ZIP_DEFLATED)
z.close()
print('\tOXT extension created successfully...')
return
def _install(name):
path_oxt = _join(PATH_TARGET, f'{name}.oxt')
if not _exists(path_oxt):
print(f'\tNot found: {path_oxt}')
return
print('\tStart install extension...')
call(UNOPKG + (path_oxt,))
print('\tInstall correctly...')
print(f'\tStart LibreOffice {APP_TEST}')
call((PATH_SOFFICE, f'--{APP_TEST}'))
print('\tExtension make successfully...')
return
def main(args):
print(f'Extension: {args.name}')
if args.make_oxt:
_make_oxt(args.name)
if args.install:
_install(args.name)
return
def _process_command_line_arguments():
parser = argparse.ArgumentParser(description='Make LibreOffice extensions')
parser.add_argument('-m', '--make-oxt', action='store_true',
default=False, required=False)
parser.add_argument('-i', '--install', action='store_true',
default=False, required=False)
parser.add_argument('name')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = _process_command_line_arguments()
main(args)