Add a minimal Python extension

This is a minimal Python extension. It is possible to run main.py from:

1. Inside a LibreOffice extension
2. Inside LibreOffice, and also via APSO
3. Outside LibreOffice, as an external process, and also in a Python IDE

The provided Python script is very useful for debugging extensions as an
example. That is because it is possible to debug it in an IDE before
packaging it as a LibreOffice extension.

Python file is checked with:

$ flake8 --ignore E501 main.py

Change-Id: I24d9aefdfda29264bf6b5f9403a40fae35e610f6
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/159938
Tested-by: Jenkins
Reviewed-by: Hossein <hossein@libreoffice.org>
This commit is contained in:
Hossein 2023-11-24 18:45:47 +00:00
parent 1bddc6ad3d
commit b9a36edc6d
9 changed files with 157 additions and 0 deletions

View File

@ -28,6 +28,7 @@ my_example_dirs = \
cpp/counter \
cpp/remoteclient \
python/toolpanel \
python/minimal-extension \
# cpp/custompanel \

View File

@ -551,6 +551,13 @@ $(eval $(call gb_Package_add_files_with_dir,odk_examples,$(SDKDIRNAME)/examples,
python/toolpanel/toolpanel.component \
python/toolpanel/toolpanel.py \
python/toolpanel/toolpanels/poc.xdl \
python/minimal-extension/Makefile \
python/minimal-extension/main.py \
python/minimal-extension/description.xml \
python/minimal-extension/Addons.xcu \
python/minimal-extension/registration/license.txt \
python/minimal-extension/pkg-description/pkg-description.en \
python/minimal-extension/META-INF/manifest.xml \
python/DocumentHandling/DocumentConverter.py \
python/DocumentHandling/DocumentLoader.py \
python/DocumentHandling/DocumentPrinter.py \

View File

@ -0,0 +1,28 @@
<oor:component-data xmlns:oor="http://openoffice.org/2001/registry" xmlns:xs="http://www.w3.org/2001/XMLSchema" oor:name="Addons" oor:package="org.openoffice.Office">
<node oor:name="AddonUI">
<node oor:name="OfficeMenuBar">
<node oor:name="org.extension.sample.menubar" oor:op="replace">
<prop oor:name="Title" oor:type="xs:string">
<value xml:lang="en-US">Sample Extension</value>
</prop>
<prop oor:name="Context" oor:type="xs:string">
<value>com.sun.star.text.TextDocument</value>
</prop>
<node oor:name="Submenu">
<node oor:name="M1" oor:op="replace">
<prop oor:name="Title">
<value xml:lang="en-US">Example item</value>
</prop>
<prop oor:name="URL">
<value>service:org.extension.sample.do?FirstMenuItem</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,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE manifest:manifest PUBLIC "-//OpenOffice.org//DTD Manifest 1.0//EN" "Manifest.dtd">
<manifest:manifest xmlns:manifest="http://openoffice.org/2001/manifest">
<manifest:file-entry manifest:media-type="application/vnd.sun.star.uno-component;type=Python" manifest:full-path="main.py" />
<manifest:file-entry manifest:full-path="pkg-desc/pkg-description.en" manifest:media-type="application/vnd.sun.star.package-bundle-description;locale=en"/>
<manifest:file-entry manifest:full-path="Addons.xcu" manifest:media-type="application/vnd.sun.star.configuration-data"/>
</manifest:manifest>

View File

@ -0,0 +1,25 @@
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
PRJ = ../../..
SETTINGS = $(PRJ)/settings
include $(SETTINGS)/settings.mk
include $(SETTINGS)/std.mk
FILES = \
Addons.xcu \
META-INF/manifest.xml \
description.xml \
pkg-description/pkg-description.en \
registration/license.txt \
main.py
$(OUT_BIN)/minimal-python.$(UNOOXT_EXT): $(FILES)
-$(MKDIR) $(subst /,$(PS),$(@D))
$(SDK_ZIP) $@ $^

View File

@ -0,0 +1,19 @@
<?xml version='1.0' encoding='UTF-8'?>
<description
xmlns="http://openoffice.org/extensions/description/2006"
xmlns:dep="http://openoffice.org/extensions/description/2006"
xmlns:xlink="http://www.w3.org/1999/xlink">
<identifier value="org.extension.sample"/>
<version value="1.0"/>
<registration>
<simple-license accept-by="admin" default-license-id="ID0" suppress-on-update="true" >
<license-text xlink:href="registration/license.txt" lang="en" license-id="ID0" />
</simple-license>
</registration>
<publisher>
<name xlink:href="mailto:developer@mywebsite.org">Developer</name>
</publisher>
<display-name>
<name>Minimal Python extension</name>
</display-name>
</description>

View File

@ -0,0 +1,68 @@
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
import sys
import unohelper
import officehelper
from com.sun.star.task import XJobExecutor
# The MainJob is a UNO component derived from unohelper.Base class
# and also the XJobExecutor, the implemented interface
class MainJob(unohelper.Base, XJobExecutor):
def __init__(self, ctx):
self.ctx = ctx
# handling different situations (inside LibreOffice or other process)
try:
self.sm = ctx.getServiceManager()
self.desktop = XSCRIPTCONTEXT.getDesktop()
except NameError:
self.sm = ctx.ServiceManager
self.desktop = self.ctx.getServiceManager().createInstanceWithContext(
"com.sun.star.frame.Desktop", self.ctx)
def trigger(self, args):
desktop = self.ctx.ServiceManager.createInstanceWithContext(
"com.sun.star.frame.Desktop", self.ctx)
model = desktop.getCurrentComponent()
if not hasattr(model, "Text"):
model = self.desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, ())
text = model.Text
cursor = text.createTextCursor()
text.insertString(cursor, "Hello Extension argument -> " + args + "\n", 0)
# Starting from Python IDE
def main():
try:
ctx = XSCRIPTCONTEXT
except NameError:
ctx = officehelper.bootstrap()
if ctx is None:
print("ERROR: Could not bootstrap default Office.")
sys.exit(1)
job = MainJob(ctx)
job.trigger("hello")
# Starting from command line
if __name__ == "__main__":
main()
# pythonloader loads a static g_ImplementationHelper variable
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(
MainJob, # UNO object class
"org.extension.sample.do", # implementation name (customize for yourself)
("com.sun.star.task.Job",), ) # implemented services (only 1)
# vim: set shiftwidth=4 softtabstop=4 expandtab:

View File

@ -0,0 +1 @@
Example LibreOffice extension. Copyright (c) Developer 2023.

View File

@ -0,0 +1 @@
A very short license.