From 3e2d4c956d74e1f388edd93ea9488752bb7ce0ea Mon Sep 17 00:00:00 2001 From: Bill Ferguson Date: Thu, 22 Jun 2023 16:04:26 -0400 Subject: [PATCH 1/8] tools/script_manager.lua - Documented more fields for the sm.scripts table Removed extra declaration of check_update preference --- tools/script_manager.lua | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/tools/script_manager.lua b/tools/script_manager.lua index 7befa037..b17ec572 100644 --- a/tools/script_manager.lua +++ b/tools/script_manager.lua @@ -104,17 +104,6 @@ dt.preferences.register(MODULE, "check_update", "bool", local check_for_updates = dt.preferences.read(MODULE, "check_update", "bool") --- - - - - - - - - - - - - - - - - - - - - - - - --- P R E F E R E N C E S --- - - - - - - - - - - - - - - - - - - - - - - - - -dt.preferences.register(MODULE, "check_update", "bool", - "check for updated scripts on start up", - "automatically update scripts to correct version", - true) - -local check_for_updates = dt.preferences.read(MODULE, "check_update", "bool") - -- - - - - - - - - - - - - - - - - - - - - - - - -- L O G L E V E L -- - - - - - - - - - - - - - - - - - - - - - - - @@ -170,6 +159,8 @@ sm.categories = {} storage_name name of the exporter (in the exporter storage menu) has_action true if it creates an action action_name name on the button + has_select true if it creates a select + select_name name on the button has_event true if it creates an event handler event_type type of event, shortcut, post-xxx, pre-xxx callback name of the callback routine From fad74d6438963a11f3ccf0e7491955e724f57430 Mon Sep 17 00:00:00 2001 From: Bill Ferguson Date: Fri, 23 Jun 2023 19:09:57 -0400 Subject: [PATCH 2/8] tools/script_manager.lua - cleaned up deactivate comments --- tools/script_manager.lua | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tools/script_manager.lua b/tools/script_manager.lua index b17ec572..84e6f0eb 100644 --- a/tools/script_manager.lua +++ b/tools/script_manager.lua @@ -368,10 +368,12 @@ local function activate(script) end local function deactivate(script) - -- presently the lua api doesn't support unloading gui elements however, we - -- can hide libs, so we just mark those as hidden and hide the gui - -- can delete storage - --therefore we just mark then inactive for the next time darktable starts + -- presently the lua api doesn't support unloading lib elements however, we + -- can hide libs, so we just mark those as hidden and hide the gui + -- can delete storages + -- can delete actions + -- can delete selects + -- and mark them inactive for the next time darktable starts -- deactivate it.... pref_write(script.script_name, "bool", false) From a4f6af73ec340700ca613a42fcd832487008c646 Mon Sep 17 00:00:00 2001 From: Bill Ferguson Date: Sat, 24 Jun 2023 19:56:51 -0400 Subject: [PATCH 3/8] tools/script_manager.lua - enabled log levels in callback routines --- tools/script_manager.lua | 86 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 4 deletions(-) diff --git a/tools/script_manager.lua b/tools/script_manager.lua index 84e6f0eb..1a3b2a27 100644 --- a/tools/script_manager.lua +++ b/tools/script_manager.lua @@ -130,6 +130,10 @@ sm.event_registered = false sm.widgets = {} sm.categories = {} +-- set log level for functions + +sm.log_level = DEFAULT_LOG_LEVEL + --[[ sm.scripts is a table of tables for containing the scripts @@ -192,22 +196,42 @@ sm.run = false -- F U N C T I O N S -- - - - - - - - - - - - - - - - - - - - - - - - +------------------- +-- helper functions +------------------- + +local function set_log_level(level) + local old_log_level = log.log_level() + log.log_level(level) + return old_log_level +end + +local function restore_log_level(level) + log.log_level(level) +end + local function pref_read(name, pref_type) + local old_log_level = set_log_level(sm.log_level) log.msg(log.debug, "name is " .. name .. " and type is " .. pref_type) local val = dt.preferences.read(MODULE, name, pref_type) - if not string.match(pref_type, "bool") then - log.msg(log.debug, "read value " .. tostring(val)) - end + log.msg(log.debug, "read value " .. tostring(val)) + restore_log_level(old_log_level) return val end local function pref_write(name, pref_type, value) - dt.preferences.write(MODULE, name, pref_type, value) + local old_log_level = set_log_level(sm.log_level) + log.msg(log.debug, "writing value " .. tostring(value) .. " for name " .. name) + dt.preferences.write(MODULE, name, pref_type, value) + restore_log_level(old_log_level) end +---------------- -- git interface +---------------- local function get_repo_status(repo) + local old_log_level = set_log_level(sm.log_level) local p = io.popen("cd " .. repo .. CS .. "git status") if p then local data = p:read("*a") @@ -215,10 +239,12 @@ local function get_repo_status(repo) return data end log.msg(log.error, "unable to get status of " .. repo) + restore_log_level(old_log_level) return nil end local function get_current_repo_branch(repo) + local old_log_level = set_log_level(sm.log_level) local branch = nil local p = io.popen("cd " .. repo .. CS .. "git branch --all") if p then @@ -237,10 +263,12 @@ local function get_current_repo_branch(repo) if not branch then log.msg(log.error, "no current branch detected in repo_data") end + restore_log_level(old_log_level) return nil end local function get_repo_branches(repo) + local old_log_level = set_log_level(sm.log_level) local branches = {} local p = io.popen("cd " .. repo .. CS .. "git pull --all" .. CS .. "git branch --all") if p then @@ -257,11 +285,13 @@ local function get_repo_branches(repo) end end end + restore_log_level(old_log_level) return branches end local function is_repo_clean(repo_data) + local old_log_level = set_log_level(sm.log_level) if string.match(repo_data, "\n%s-%a.-%a:%s-%a%g-\n") then log.msg(log.info, "repo is dirty") return false @@ -269,14 +299,18 @@ local function is_repo_clean(repo_data) log.msg(log.info, "repo is clean") return true end + restore_log_level(old_log_level) end local function checkout_repo_branch(repo, branch) + local old_log_level = set_log_level(sm.log_level) log.msg(log.info, "checkout out branch " .. branch .. " from repository " .. repo) os.execute("cd " .. repo .. CS .. "git checkout " .. branch) + restore_log_level(old_log_level) end local function update_combobox_choices(combobox, choice_table, selected) + local old_log_level = set_log_level(sm.log_level) local items = #combobox local choices = #choice_table for i, name in ipairs(choice_table) do @@ -292,11 +326,14 @@ local function update_combobox_choices(combobox, choice_table, selected) end combobox.value = selected + restore_log_level(old_log_level) end local function string_trim(str) + local old_log_level = set_log_level(sm.log_level) local result = string.gsub(str, "^%s+", "") result = string.gsub(result, "%s+$", "") + restore_log_level(old_log_level) return result end @@ -305,14 +342,17 @@ local function string_dequote(str) end local function add_script_category(category) + local old_log_level = set_log_level(sm.log_level) if #sm.categories == 0 or not string.match(du.join(sm.categories, " "), ds.sanitize_lua(category)) then table.insert(sm.categories, category) sm.scripts[category] = {} log.msg(log.debug, "created category " .. category) end + restore_log_level(old_log_level) end local function get_script_doc(script) + local old_log_level = set_log_level(sm.log_level) local description = nil f = io.open(LUA_DIR .. PS .. script .. ".lua") if f then @@ -325,13 +365,16 @@ local function get_script_doc(script) log.msg(log.error, _("Cant read from " .. script)) end if description then + restore_log_level(old_log_level) return description else + restore_log_level(old_log_level) return "No documentation available" end end local function activate(script) + local old_log_level = set_log_level(sm.log_level) local status = nil -- status of start function local err = nil -- error message returned if module doesn't start log.msg(log.info, "activating " .. script.name) @@ -364,6 +407,7 @@ local function activate(script) status = true pref_write(script.script_name, "bool", true) end + restore_log_level(old_log_level) return status end @@ -376,6 +420,7 @@ local function deactivate(script) -- and mark them inactive for the next time darktable starts -- deactivate it.... + local old_log_level = set_log_level(sm.log_level) pref_write(script.script_name, "bool", false) if script.data then script.data.destroy() @@ -397,9 +442,11 @@ local function deactivate(script) log.msg(log.info, "setting " .. script.script_name .. " to not start") log.msg(log.screen, script.name .. _(" will not start when darktable is restarted")) end + restore_log_level(old_log_level) end local function add_script_name(name, path, category) + local old_log_level = set_log_level(sm.log_level) log.msg(log.debug, "category is " .. category) log.msg(log.debug, "name is " .. name) local script = { @@ -414,9 +461,11 @@ local function add_script_name(name, path, category) if pref_read(script.script_name, "bool") then activate(script) end + restore_log_level(old_log_level) end local function process_script_data(script_file) + local old_log_level = set_log_level(sm.log_level) -- the script file supplied is category/filename.filetype -- the following pattern splits the string into category, path, name, fileename, and filetype @@ -447,9 +496,11 @@ local function process_script_data(script_file) if name then add_script_name(name, path, category) end + restore_log_level(old_log_level) end local function scan_scripts(script_dir) + local old_log_level = set_log_level(sm.log_level) local script_count = 0 local find_cmd = "find -L " .. script_dir .. " -name \\*.lua -print | sort" if dt.configuration.running_os == "windows" then @@ -472,10 +523,12 @@ local function scan_scripts(script_dir) end end end + restore_log_level(old_log_level) return script_count end local function update_scripts() + local old_log_level = set_log_level(sm.log_level) local result = false local git = sm.executables.git @@ -498,10 +551,12 @@ local function update_scripts() dt.print(_("lua scripts successfully updated")) end + restore_log_level(old_log_level) return result end local function update_script_update_choices() + local old_log_level = set_log_level(sm.log_level) local installs = {} local pref_string = "" for i, repo in ipairs(sm.installed_repositories) do @@ -511,9 +566,11 @@ local function update_script_update_choices() update_combobox_choices(sm.widgets.update_script_choices, installs, 1) log.msg(log.debug, "repo pref string is " .. pref_string) pref_write("installed_repos", "string", pref_string) + restore_log_level(old_log_level) end local function scan_repositories() + local old_log_level = set_log_level(sm.log_level) local script_count = 0 local find_cmd = "find -L " .. LUA_DIR .. " -name \\*.git -print | sort" if dt.configuration.running_os == "windows" then @@ -548,15 +605,18 @@ local function scan_repositories() end end update_script_update_choices() + restore_log_level(old_log_level) end local function install_scripts() + local old_log_level = set_log_level(sm.log_level) local url = sm.widgets.script_url.text local category = sm.widgets.new_category.text if string.match(du.join(sm.categories, " "), ds.sanitize_lua(category)) then log.msg(log.screen, _("category ") .. category .. _(" is already in use. Please specify a different category name.")) log.msg(log.error, "category " .. category .. " already exists, returning...") + restore_log_level(old_log_level) return end @@ -566,6 +626,7 @@ local function install_scripts() if not git then dt.print(_("ERROR: git not found. Install or specify the location of the git executable.")) + restore_log_level(old_log_level) return end @@ -607,28 +668,34 @@ local function install_scripts() dt.print(_("failed to download scripts")) end + restore_log_level(old_log_level) return result end local function clear_button(number) + local old_log_level = set_log_level(sm.log_level) local button = sm.widgets.buttons[number] button.label = "" button.tooltip = "" button.sensitive = false --button.name = "" + restore_log_level(old_log_level) end local function find_script(category, name) + local old_log_level = set_log_level(sm.log_level) log.msg(log.debug, "looking for script " .. name .. " in category " .. category) for _, script in ipairs(sm.scripts[category]) do if string.match(script.name, "^" .. ds.sanitize_lua(name) .. "$") then return script end end + restore_log_level(old_log_level) return nil end local function populate_buttons(category, first, last) + local old_log_level = set_log_level(sm.log_level) log.msg(log.debug, "category is " .. category .. " and first is " .. first .. " and last is " .. last) local button_num = 1 for i = first, last do @@ -693,9 +760,11 @@ local function populate_buttons(category, first, last) clear_button(i) end end + restore_log_level(old_log_level) end local function paginate(direction) + local old_log_level = set_log_level(sm.log_level) local category = sm.page_status.category log.msg(log.debug, "category is " .. category) local num_scripts = #sm.scripts[category] @@ -747,9 +816,11 @@ local function paginate(direction) sm.widgets.page_status.label = _("Page ") .. cur_page .. _(" of ") .. max_pages populate_buttons(category, first, last) + restore_log_level(old_log_level) end local function change_category(category) + local old_log_level = set_log_level(sm.log_level) if not category then log.msg(log.debug "setting category to selector value " .. sm.widgets.category_selector.value) sm.page_status.category = sm.widgets.category_selector.value @@ -759,9 +830,11 @@ local function change_category(category) end paginate(2) + restore_log_level(old_log_level) end local function change_num_buttons() + local old_log_level = set_log_level(sm.log_level) cur_buttons = sm.page_status.num_buttons new_buttons = sm.widgets.num_buttons.value pref_write("num_buttons", "integer", new_buttons) @@ -792,9 +865,11 @@ local function change_num_buttons() log.msg(log.debug, "num_buttons set to " .. sm.page_status.num_buttons) paginate(2) -- force the buttons to repopulate sm.widgets.main_menu.selected = 3 -- jump back to start/stop scripts + restore_log_level(old_log_level) end local function load_preferences() + local old_log_level = set_log_level(sm.log_level) -- load the prefs and update settings -- update_script_choices local pref_string = pref_read("installed_repos", "string") @@ -836,9 +911,11 @@ local function load_preferences() log.msg(log.debug, "set main menu to val " .. val .. " which is " .. sm.widgets.main_menu.value) log.msg(log.debug, "set main menu to " .. sm.widgets.main_menu.value) + restore_log_level(old_log_level) end local function install_module() + local old_log_level = set_log_level(sm.log_level) if not sm.module_installed then dt.register_lib( "script_manager", -- Module name @@ -868,6 +945,7 @@ local function install_module() dt.control.sleep(5000) dt.print_log("setting sm expanded true") dt.gui.libs["script_manager"].expanded = true]] + restore_log_level(old_log_level) end -- - - - - - - - - - - - - - - - - - - - - - - - From 236b25a235e733e108103f3203eaa4bf987ae378 Mon Sep 17 00:00:00 2001 From: Bill Ferguson Date: Mon, 3 Jul 2023 23:42:47 -0400 Subject: [PATCH 4/8] tools/script_manager.lua - clean up formatting and documentation --- tools/script_manager.lua | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/tools/script_manager.lua b/tools/script_manager.lua index 1a3b2a27..0ace7796 100644 --- a/tools/script_manager.lua +++ b/tools/script_manager.lua @@ -1,6 +1,6 @@ --[[ This file is part of darktable, - copyright (c) 2018, 2020 Bill Ferguson + copyright (c) 2018, 2020, 2023 Bill Ferguson darktable is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -289,7 +289,6 @@ local function get_repo_branches(repo) return branches end - local function is_repo_clean(repo_data) local old_log_level = set_log_level(sm.log_level) if string.match(repo_data, "\n%s-%a.-%a:%s-%a%g-\n") then @@ -309,6 +308,10 @@ local function checkout_repo_branch(repo, branch) restore_log_level(old_log_level) end +-------------------- +-- utility functions +-------------------- + local function update_combobox_choices(combobox, choice_table, selected) local old_log_level = set_log_level(sm.log_level) local items = #combobox @@ -341,6 +344,10 @@ local function string_dequote(str) return string.gsub(str, "['\"]", "") end +------------------ +-- script handling +------------------ + local function add_script_category(category) local old_log_level = set_log_level(sm.log_level) if #sm.categories == 0 or not string.match(du.join(sm.categories, " "), ds.sanitize_lua(category)) then @@ -555,6 +562,10 @@ local function update_scripts() return result end +-------------- +-- UI handling +-------------- + local function update_script_update_choices() local old_log_level = set_log_level(sm.log_level) local installs = {} @@ -678,7 +689,7 @@ local function clear_button(number) button.label = "" button.tooltip = "" button.sensitive = false ---button.name = "" + --button.name = "" restore_log_level(old_log_level) end @@ -1235,6 +1246,7 @@ sm.widgets.main_box = dt.new_widget("box"){ } script_manager_running_script = nil + -- - - - - - - - - - - - - - - - - - - - - - - - -- D A R K T A B L E I N T E G R A T I O N -- - - - - - - - - - - - - - - - - - - - - - - - From 2fc8ed78e446ced4b352da2bc6281c6567dcce87 Mon Sep 17 00:00:00 2001 From: Bill Ferguson Date: Mon, 3 Jul 2023 23:44:13 -0400 Subject: [PATCH 5/8] tools/script_manager.lua - added check to ensure script_data.show() runction was passed as part of the script_data table before trying to use it. Fixes #418. --- tools/script_manager.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/script_manager.lua b/tools/script_manager.lua index 0ace7796..78909fd9 100644 --- a/tools/script_manager.lua +++ b/tools/script_manager.lua @@ -397,7 +397,7 @@ local function activate(script) if err ~= true then log.msg(log.debug, "got lib data") script.data = err - if script.data.destroy_method and script.data.destroy_method == "hide" then + if script.data.destroy_method and script.data.destroy_method == "hide" and script.data.show then script.data.show() end else From b4626f278768f93be4da283e70d96862f1c7c076 Mon Sep 17 00:00:00 2001 From: Bill Ferguson Date: Tue, 4 Jul 2023 15:57:12 -0400 Subject: [PATCH 6/8] lib/dtutils.lua - changed log message to error on fail to load. Added printing of the returned error. --- lib/dtutils.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/dtutils.lua b/lib/dtutils.lua index 0a6d4416..8aae188d 100644 --- a/lib/dtutils.lua +++ b/lib/dtutils.lua @@ -180,11 +180,12 @@ dtutils.libdoc.functions["prequire"] = { } function dtutils.prequire(req_name) - local status, lib = pcall(require, req_name) + local status, lib = pcall(require, san_req_name) if status then log.msg(log.info, "Loaded " .. req_name) else - log.msg(log.info, "Error loading " .. req_name) + log.msg(log.error, "Error loading " .. req_name) + log.msg(log.error, "Error returned is " .. lib) end return status, lib end From 68ae9326bf3fd402b3bbcc5f3cf98a14f944146f Mon Sep 17 00:00:00 2001 From: Bill Ferguson Date: Wed, 12 Jul 2023 12:37:14 -0400 Subject: [PATCH 7/8] tools/script_manager.lua - added translation to preference setting --- tools/script_manager.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/script_manager.lua b/tools/script_manager.lua index 78909fd9..c4546b65 100644 --- a/tools/script_manager.lua +++ b/tools/script_manager.lua @@ -98,8 +98,8 @@ local LUA_API_VER = "API-" .. dt.configuration.api_version_string -- - - - - - - - - - - - - - - - - - - - - - - - dt.preferences.register(MODULE, "check_update", "bool", - "check for updated scripts on start up", - "automatically update scripts to correct version", + _("check for updated scripts on start up"), + _("automatically update scripts to correct version"), true) local check_for_updates = dt.preferences.read(MODULE, "check_update", "bool") From 24b552d61983d51cec6e140ab03f728e6d866e47 Mon Sep 17 00:00:00 2001 From: Bill Ferguson Date: Wed, 12 Jul 2023 13:18:01 -0400 Subject: [PATCH 8/8] lib/dtutils.lua - fix typo from testing --- lib/dtutils.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dtutils.lua b/lib/dtutils.lua index 8aae188d..5ff6c9f7 100644 --- a/lib/dtutils.lua +++ b/lib/dtutils.lua @@ -180,7 +180,7 @@ dtutils.libdoc.functions["prequire"] = { } function dtutils.prequire(req_name) - local status, lib = pcall(require, san_req_name) + local status, lib = pcall(require, req_name) if status then log.msg(log.info, "Loaded " .. req_name) else