| 15 | namespace lfs::python { |
| 16 | |
| 17 | void register_scripts(nb::module_& m) { |
| 18 | auto scripts = m.def_submodule("scripts", "Python script management"); |
| 19 | |
| 20 | using ScriptState = vis::gui::panels::PythonScriptManagerState; |
| 21 | |
| 22 | scripts.def( |
| 23 | "get_scripts", |
| 24 | []() { |
| 25 | auto& state = ScriptState::getInstance(); |
| 26 | nb::list result; |
| 27 | for (const auto& s : state.scriptsSnapshot()) { |
| 28 | nb::dict d; |
| 29 | d["path"] = lfs::core::path_to_utf8(s.path); |
| 30 | d["enabled"] = s.enabled; |
| 31 | d["has_error"] = s.has_error; |
| 32 | d["error_message"] = s.error_message; |
| 33 | result.append(d); |
| 34 | } |
| 35 | return result; |
| 36 | }, |
| 37 | "Get list of loaded scripts with their state"); |
| 38 | |
| 39 | scripts.def( |
| 40 | "set_script_enabled", |
| 41 | [](size_t index, bool enabled) { |
| 42 | ScriptState::getInstance().setScriptEnabled(index, enabled); |
| 43 | }, |
| 44 | nb::arg("index"), nb::arg("enabled"), |
| 45 | "Enable or disable a script by index"); |
| 46 | |
| 47 | scripts.def( |
| 48 | "set_script_error", |
| 49 | [](size_t index, const std::string& error) { |
| 50 | ScriptState::getInstance().setScriptError(index, error); |
| 51 | }, |
| 52 | nb::arg("index"), nb::arg("error"), |
| 53 | "Set error message for a script (empty to clear)"); |
| 54 | |
| 55 | scripts.def( |
| 56 | "clear_errors", |
| 57 | []() { ScriptState::getInstance().clearErrors(); }, |
| 58 | "Clear all script errors"); |
| 59 | |
| 60 | scripts.def( |
| 61 | "clear", |
| 62 | []() { ScriptState::getInstance().clear(); }, |
| 63 | "Clear all scripts"); |
| 64 | |
| 65 | scripts.def( |
| 66 | "run", |
| 67 | [](const std::vector<std::string>& paths) { |
| 68 | std::vector<std::filesystem::path> fs_paths; |
| 69 | fs_paths.reserve(paths.size()); |
| 70 | for (const auto& p : paths) { |
| 71 | fs_paths.emplace_back(lfs::core::utf8_to_path(p)); |
| 72 | } |
| 73 | auto result = lfs::python::run_scripts(fs_paths); |
| 74 | nb::dict ret; |
no test coverage detected