From 96182e778a1fca3d5d7b30270f9257cd6c64f567 Mon Sep 17 00:00:00 2001 From: Ian Chua Date: Fri, 17 Jul 2026 13:35:51 +0800 Subject: [PATCH] fix: merge pythonjsonutils into pluginfsutils --- src/slic3r/CMakeLists.txt | 1 - src/slic3r/plugin/PluginFsUtils.hpp | 68 +++++++++++++++++++++ src/slic3r/plugin/PyPluginTrampoline.hpp | 2 +- src/slic3r/plugin/PythonJsonUtils.hpp | 75 ------------------------ src/slic3r/plugin/PythonPluginBridge.cpp | 2 +- src/slic3r/plugin/host/PluginHostUi.cpp | 2 +- 6 files changed, 71 insertions(+), 79 deletions(-) delete mode 100644 src/slic3r/plugin/PythonJsonUtils.hpp diff --git a/src/slic3r/CMakeLists.txt b/src/slic3r/CMakeLists.txt index a5a26d84d3..7c26f0e5b5 100644 --- a/src/slic3r/CMakeLists.txt +++ b/src/slic3r/CMakeLists.txt @@ -621,7 +621,6 @@ set(SLIC3R_GUI_SOURCES plugin/PluginFsUtils.hpp plugin/PluginConfig.cpp plugin/PluginConfig.hpp - plugin/PythonJsonUtils.hpp plugin/PluginLoader.cpp plugin/PluginLoader.hpp plugin/PluginDescriptor.hpp diff --git a/src/slic3r/plugin/PluginFsUtils.hpp b/src/slic3r/plugin/PluginFsUtils.hpp index 8fb4b30687..ea6cacd367 100644 --- a/src/slic3r/plugin/PluginFsUtils.hpp +++ b/src/slic3r/plugin/PluginFsUtils.hpp @@ -2,8 +2,12 @@ #include "PluginDescriptor.hpp" +#include +#include + #include +#include #include #include @@ -13,6 +17,70 @@ namespace Slic3r { extern const char* const INSTALL_STATE_FILE; +// JSON <-> Python conversion shared by the plugin bindings. The caller must hold the GIL. +// Plugin config and orca.host.ui payloads both cross the boundary as plain JSON-compatible +// values, so both go through these. + +inline pybind11::object json_to_py(const nlohmann::json& j) +{ + namespace py = pybind11; + using json = nlohmann::json; + + switch (j.type()) { + case json::value_t::null: return py::none(); + case json::value_t::boolean: return py::bool_(j.get()); + case json::value_t::number_integer: return py::int_(j.get()); + case json::value_t::number_unsigned: return py::int_(j.get()); + case json::value_t::number_float: return py::float_(j.get()); + case json::value_t::string: return py::str(j.get()); + case json::value_t::array: { + py::list lst; + for (const auto& e : j) + lst.append(json_to_py(e)); + return lst; + } + case json::value_t::object: { + py::dict d; + for (auto it = j.begin(); it != j.end(); ++it) + d[py::str(it.key())] = json_to_py(it.value()); + return d; + } + default: return py::none(); + } +} + +inline nlohmann::json py_to_json(const pybind11::handle& o) +{ + namespace py = pybind11; + using json = nlohmann::json; + + if (o.is_none()) + return json(nullptr); + if (py::isinstance(o)) // bool before int (bool subclasses int in Python) + return o.cast(); + if (py::isinstance(o)) + return o.cast(); + if (py::isinstance(o)) + return o.cast(); + if (py::isinstance(o)) + return o.cast(); + if (py::isinstance(o)) + return o.cast(); + if (py::isinstance(o)) { + json obj = json::object(); + for (auto item : py::reinterpret_borrow(o)) + obj[py::str(item.first).cast()] = py_to_json(item.second); + return obj; + } + if (py::isinstance(o) || py::isinstance(o)) { + json arr = json::array(); + for (auto e : o) + arr.push_back(py_to_json(e)); + return arr; + } + return py::str(o).cast(); // fallback: str() +} + // Returns the cloud plugin install/scan directory for a given user_id. // Path: {data_dir}/orca_plugins/_subscribed/{user_id}/ std::string get_cloud_plugin_dir(const std::string& user_id); diff --git a/src/slic3r/plugin/PyPluginTrampoline.hpp b/src/slic3r/plugin/PyPluginTrampoline.hpp index 3ed6827e98..e36c0b375c 100644 --- a/src/slic3r/plugin/PyPluginTrampoline.hpp +++ b/src/slic3r/plugin/PyPluginTrampoline.hpp @@ -10,7 +10,7 @@ #include "PythonPluginInterface.hpp" #include "PythonInterpreter.hpp" -#include "PythonJsonUtils.hpp" +#include "PluginFsUtils.hpp" #include "PluginAuditManager.hpp" // Trampoline variants of pybind11's override macros. Every C++->Python plugin call crosses a diff --git a/src/slic3r/plugin/PythonJsonUtils.hpp b/src/slic3r/plugin/PythonJsonUtils.hpp deleted file mode 100644 index 3caaf51041..0000000000 --- a/src/slic3r/plugin/PythonJsonUtils.hpp +++ /dev/null @@ -1,75 +0,0 @@ -#pragma once - -#include -#include - -#include -#include - -namespace Slic3r { - -// JSON <-> Python conversion shared by the plugin bindings. The caller must hold the GIL. -// Plugin config and orca.host.ui payloads both cross the boundary as plain JSON-compatible -// values, so both go through these. - -inline pybind11::object json_to_py(const nlohmann::json& j) -{ - namespace py = pybind11; - using json = nlohmann::json; - - switch (j.type()) { - case json::value_t::null: return py::none(); - case json::value_t::boolean: return py::bool_(j.get()); - case json::value_t::number_integer: return py::int_(j.get()); - case json::value_t::number_unsigned: return py::int_(j.get()); - case json::value_t::number_float: return py::float_(j.get()); - case json::value_t::string: return py::str(j.get()); - case json::value_t::array: { - py::list lst; - for (const auto& e : j) - lst.append(json_to_py(e)); - return lst; - } - case json::value_t::object: { - py::dict d; - for (auto it = j.begin(); it != j.end(); ++it) - d[py::str(it.key())] = json_to_py(it.value()); - return d; - } - default: return py::none(); - } -} - -inline nlohmann::json py_to_json(const pybind11::handle& o) -{ - namespace py = pybind11; - using json = nlohmann::json; - - if (o.is_none()) - return json(nullptr); - if (py::isinstance(o)) // bool before int (bool subclasses int in Python) - return o.cast(); - if (py::isinstance(o)) - return o.cast(); - if (py::isinstance(o)) - return o.cast(); - if (py::isinstance(o)) - return o.cast(); - if (py::isinstance(o)) - return o.cast(); - if (py::isinstance(o)) { - json obj = json::object(); - for (auto item : py::reinterpret_borrow(o)) - obj[py::str(item.first).cast()] = py_to_json(item.second); - return obj; - } - if (py::isinstance(o) || py::isinstance(o)) { - json arr = json::array(); - for (auto e : o) - arr.push_back(py_to_json(e)); - return arr; - } - return py::str(o).cast(); // fallback: str() -} - -} // namespace Slic3r diff --git a/src/slic3r/plugin/PythonPluginBridge.cpp b/src/slic3r/plugin/PythonPluginBridge.cpp index 14dc84087a..328ebbace1 100644 --- a/src/slic3r/plugin/PythonPluginBridge.cpp +++ b/src/slic3r/plugin/PythonPluginBridge.cpp @@ -13,7 +13,7 @@ #include #include "PythonInterpreter.hpp" -#include "PythonJsonUtils.hpp" +#include "PluginFsUtils.hpp" #include "PluginConfig.hpp" #include "host/PluginHost.hpp" #include "PyPluginPackage.hpp" diff --git a/src/slic3r/plugin/host/PluginHostUi.cpp b/src/slic3r/plugin/host/PluginHostUi.cpp index 70efb3137a..4857287f5e 100644 --- a/src/slic3r/plugin/host/PluginHostUi.cpp +++ b/src/slic3r/plugin/host/PluginHostUi.cpp @@ -2,7 +2,7 @@ #include "slic3r/plugin/PluginAuditManager.hpp" #include "slic3r/plugin/PythonInterpreter.hpp" // PythonGILState -#include "slic3r/plugin/PythonJsonUtils.hpp" // json_to_py / py_to_json +#include "slic3r/plugin/PluginFsUtils.hpp" // json_to_py / py_to_json #include #include