File indexing completed on 2024-05-19 05:42:14

0001 // ct_lvtplg_pythonlibrarydispatcher.cpp                             -*-C++-*-
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #include <ct_lvtplg_pythonlibrarydispatcher.h>
0021 
0022 namespace Codethink::lvtplg {
0023 
0024 // Automatically generated by generate_python_hook_bindings.py and available in the <build>/lvtplg/ folder
0025 #include <ct_lvtplg_hookbindings.inc.cpp>
0026 
0027 PythonLibraryDispatcher::PyResolveContext::~PyResolveContext()
0028 {
0029     PythonLibraryDispatcher::PyResolveContext::activeModule = nullptr;
0030 }
0031 
0032 std::unique_ptr<AbstractLibraryDispatcher::ResolveContext>
0033 PythonLibraryDispatcher::resolve(std::string const& functionName)
0034 {
0035     return std::make_unique<PyResolveContext>(this->pyModule, functionName);
0036 }
0037 
0038 std::string PythonLibraryDispatcher::fileName()
0039 {
0040     py::gil_scoped_acquire _;
0041     return this->pyModule.attr("__file__").cast<std::string>();
0042 }
0043 
0044 bool PythonLibraryDispatcher::isValidPlugin(QDir const& pluginDir)
0045 {
0046     auto pluginName = pluginDir.dirName();
0047     return (pluginDir.exists("metadata.json") && pluginDir.exists("README.md") && pluginDir.exists(pluginName + ".py"));
0048 }
0049 
0050 std::unique_ptr<AbstractLibraryDispatcher> PythonLibraryDispatcher::loadSinglePlugin(QDir const& pluginDir)
0051 {
0052     py::gil_scoped_acquire _;
0053 
0054     auto pluginName = pluginDir.dirName().toStdString();
0055     auto pyLib = std::make_unique<PythonLibraryDispatcher>();
0056 
0057     auto pySys = py::module_::import("sys");
0058     pySys.attr("path").attr("append")(pluginDir.path().toStdString());
0059     try {
0060         pyLib->pyModule = py::module_::import(pluginName.c_str());
0061         pyLib->pluginFolder = pluginDir.path().toStdString();
0062     } catch (py::error_already_set const& e) {
0063         // Setting the sys.attr can also cause another throw.
0064         std::cout << "Invalid plugin (" << pluginDir.path().toStdString() << "): " << e.what() << "\n";
0065         try {
0066             // Could not load python module - Cleanup sys path and early return.
0067             pySys.attr("path").attr("remove")(pluginDir.path().toStdString());
0068         } catch (std::exception& e) {
0069             std::cout << "+ Cleanup error: " << e.what() << "\n";
0070         }
0071         return nullptr;
0072     }
0073 
0074     return pyLib;
0075 }
0076 
0077 void PythonLibraryDispatcher::reload()
0078 {
0079     py::gil_scoped_acquire _;
0080     pyModule.reload();
0081 }
0082 
0083 // This method assumes the plugin has already been cleaned up (e.g.: hookTeardown has been called)
0084 void PythonLibraryDispatcher::unload()
0085 {
0086     py::gil_scoped_acquire _;
0087 
0088     try {
0089         auto pySys = py::module_::import("sys");
0090         pySys.attr("path").attr("remove")(pluginFolder.c_str());
0091     } catch (std::exception& e) {
0092         std::cout << "Error unloading python plugin";
0093     }
0094 }
0095 
0096 } // namespace Codethink::lvtplg