odk/CustomTarget_build-examples_java.mk | 2 odk/Package_examples.mk | 5 odk/examples/DevelopersGuide/OfficeDev/PathSubstitution/java/Makefile | 2 odk/examples/DevelopersGuide/OfficeDev/PathSubstitution/python/path_substitution_test.py | 69 ++++++++++ 4 files changed, 74 insertions(+), 4 deletions(-)
New commits: commit 21d7936b320e084a08ef70211a3a51106503ab98 Author: RMZeroFour <ritobrot...@gmail.com> AuthorDate: Fri Apr 19 00:19:00 2024 +0530 Commit: Hossein <hoss...@libreoffice.org> CommitDate: Tue Jun 4 15:16:38 2024 +0200 tdf#143123 Port Java PathSubstitution example to Python This commit ports the PathSubstitution example in odk/examples/DevelopersGuide/OfficeDev/ to Python. The original Java example code was moved to a java/ subdirectory, and the new Python code was added to the python/ subdirectory. Necessary changes were made in the corresponding Makefiles. The example can be run with: ./instdir/program/python \ ./odk/examples/DevelopersGuide/OfficeDev/PathSubstitution/\ python/path_substitution_test.py Change-Id: I6c7a8af86cee2dc9cb5384e42bd9644847f28767 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/166264 Reviewed-by: Hossein <hoss...@libreoffice.org> Tested-by: Jenkins diff --git a/odk/CustomTarget_build-examples_java.mk b/odk/CustomTarget_build-examples_java.mk index 7b4f9c8eb234..45fe7db2a0d4 100644 --- a/odk/CustomTarget_build-examples_java.mk +++ b/odk/CustomTarget_build-examples_java.mk @@ -34,7 +34,7 @@ my_example_dirs_java = \ DevelopersGuide/OfficeDev/FilterDevelopment/FlatXmlFilter_java \ DevelopersGuide/OfficeDev/Linguistic \ DevelopersGuide/OfficeDev/PathSettings \ - DevelopersGuide/OfficeDev/PathSubstitution \ + DevelopersGuide/OfficeDev/PathSubstitution/java \ DevelopersGuide/OfficeDev/TerminationTest/java \ DevelopersGuide/ProfUNO/InterprocessConn \ DevelopersGuide/ProfUNO/Lifetime \ diff --git a/odk/Package_examples.mk b/odk/Package_examples.mk index f8a29340f53c..bb4aff66ec25 100644 --- a/odk/Package_examples.mk +++ b/odk/Package_examples.mk @@ -320,8 +320,9 @@ $(eval $(call gb_Package_add_files_with_dir,odk_examples,$(SDKDIRNAME)/examples, DevelopersGuide/OfficeDev/OfficeConnect.java \ DevelopersGuide/OfficeDev/PathSettings/Makefile \ DevelopersGuide/OfficeDev/PathSettings/PathSettingsTest.java \ - DevelopersGuide/OfficeDev/PathSubstitution/Makefile \ - DevelopersGuide/OfficeDev/PathSubstitution/PathSubstitutionTest.java \ + DevelopersGuide/OfficeDev/PathSubstitution/java/Makefile \ + DevelopersGuide/OfficeDev/PathSubstitution/java/PathSubstitutionTest.java \ + DevelopersGuide/OfficeDev/PathSubstitution/python/path_substitution_test.py \ DevelopersGuide/OfficeDev/TerminationTest/cxx/Makefile \ DevelopersGuide/OfficeDev/TerminationTest/cxx/TerminationTest.cxx \ DevelopersGuide/OfficeDev/TerminationTest/java/Makefile \ diff --git a/odk/examples/DevelopersGuide/OfficeDev/PathSubstitution/Makefile b/odk/examples/DevelopersGuide/OfficeDev/PathSubstitution/java/Makefile similarity index 99% rename from odk/examples/DevelopersGuide/OfficeDev/PathSubstitution/Makefile rename to odk/examples/DevelopersGuide/OfficeDev/PathSubstitution/java/Makefile index 20bb65687613..6e0a6f18eaef 100644 --- a/odk/examples/DevelopersGuide/OfficeDev/PathSubstitution/Makefile +++ b/odk/examples/DevelopersGuide/OfficeDev/PathSubstitution/java/Makefile @@ -34,7 +34,7 @@ # Builds the OfficeDevPathSettingsTest example of the Developers Guide. -PRJ=../../../.. +PRJ=../../../../.. SETTINGS=$(PRJ)/settings include $(SETTINGS)/settings.mk diff --git a/odk/examples/DevelopersGuide/OfficeDev/PathSubstitution/PathSubstitutionTest.java b/odk/examples/DevelopersGuide/OfficeDev/PathSubstitution/java/PathSubstitutionTest.java similarity index 100% rename from odk/examples/DevelopersGuide/OfficeDev/PathSubstitution/PathSubstitutionTest.java rename to odk/examples/DevelopersGuide/OfficeDev/PathSubstitution/java/PathSubstitutionTest.java diff --git a/odk/examples/DevelopersGuide/OfficeDev/PathSubstitution/python/path_substitution_test.py b/odk/examples/DevelopersGuide/OfficeDev/PathSubstitution/python/path_substitution_test.py new file mode 100644 index 000000000000..e2d923cbd825 --- /dev/null +++ b/odk/examples/DevelopersGuide/OfficeDev/PathSubstitution/python/path_substitution_test.py @@ -0,0 +1,69 @@ +# -*- 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 traceback +import officehelper + +from com.sun.star.container import NoSuchElementException + +# This example demonstrates how to access and use the PathSettings service + +# List of pre-defined path variables supported by the path substitution service. +predefined_path_properties = [ + "$(home)", "$(inst)", "$(prog)", "$(temp)", "$(user)", + "$(username)", "$(work)", "$(path)", "$(langid)", "$(vlang)" +] + +def work_with_path_settings(): + try: + # Get a reference to the remote office context + # If necessary, a new office process is started + context = officehelper.bootstrap() + print("Connected to a running office ...") + + # Get a reference to the service manager and path substitution service + srv_mgr = context.getServiceManager() + path_subst_service = srv_mgr.createInstanceWithContext( + "com.sun.star.comp.framework.PathSubstitution", context) + + work_with_path_variables(path_subst_service) + + except Exception as e: + print(str(e)) + traceback.print_exc() + +def work_with_path_variables(path_subst_service): + if path_subst_service is None: + return + + # Retrieve values for path properties from path settings service + for prop in predefined_path_properties: + try: + value = path_subst_service.getSubstituteVariableValue(prop) + print(prop, '=', value) + except NoSuchElementException as e: + print("NoSuchElementException thrown when accessing", prop) + print(str(e)) + traceback.print_exc() + + try: + # Now try the resubstitution function + path = path_subst_service.getSubstituteVariableValue(predefined_path_properties[0]) + path += "/test" # extend the path + print("Path =", path) + resubst_path = path_subst_service.reSubstituteVariables(path) + print("Resubstituted path =", resubst_path) + except NoSuchElementException as e: + print("NoSuchElementException thrown when accessing", predefined_path_properties[0]) + print(str(e)) + traceback.print_exc() + +if __name__ == "__main__": + work_with_path_settings() + +# vim: set shiftwidth=4 softtabstop=4 expandtab: \ No newline at end of file