Skip to content

Commit 4ff1111

Browse files
committed
tools/script_manaager added sanitized io.popen and os.execute functions
for windows character and space issues. Cleaned up translatable strings to make future translation easier.
1 parent 9088bc9 commit 4ff1111

File tree

1 file changed

+29
-40
lines changed

1 file changed

+29
-40
lines changed

tools/script_manager.lua

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
--[[
22
This file is part of darktable,
3-
copyright (c) 2018, 2020, 2023 Bill Ferguson <[email protected]>
3+
copyright (c) 2018, 2020, 2023, 2024 Bill Ferguson <[email protected]>
44
55
darktable is free software: you can redistribute it and/or modify
66
it under the terms of the GNU General Public License as published by
@@ -60,7 +60,7 @@ local gettext = dt.gettext
6060

6161

6262
-- Tell gettext where to find the .mo file translating messages for a particular domain
63-
dt.gettext.bindtextdomain("script_manager",dt.configuration.config_dir.."/lua/locale/")
63+
dt.gettext.bindtextdomain("script_manager", dt.configuration.config_dir .. "/lua/locale/")
6464

6565
local function _(msgid)
6666
return gettext.dgettext("script_manager", msgid)
@@ -241,7 +241,7 @@ end
241241
local function get_repo_status(repo)
242242
local old_log_level = set_log_level(sm.log_level)
243243

244-
local p = io.popen("cd " .. repo .. CS .. "git status")
244+
local p = dtsys.io_popen("cd " .. repo .. CS .. "git status")
245245

246246
if p then
247247
local data = p:read("*a")
@@ -259,7 +259,7 @@ local function get_current_repo_branch(repo)
259259

260260
local branch = nil
261261

262-
local p = io.popen("cd " .. repo .. CS .. "git branch --all")
262+
local p = dtsys.io_popen("cd " .. repo .. CS .. "git branch --all")
263263

264264
if p then
265265
local data = p:read("*a")
@@ -289,7 +289,7 @@ local function get_repo_branches(repo)
289289
local old_log_level = set_log_level(sm.log_level)
290290

291291
local branches = {}
292-
local p = io.popen("cd " .. repo .. CS .. "git pull --all" .. CS .. "git branch --all")
292+
local p = dtsys.io_popen("cd " .. repo .. CS .. "git pull --all" .. CS .. "git branch --all")
293293

294294
if p then
295295
local data = p:read("*a")
@@ -329,7 +329,7 @@ local function checkout_repo_branch(repo, branch)
329329

330330
log.msg(log.info, "checkout out branch " .. branch .. " from repository " .. repo)
331331

332-
os.execute("cd " .. repo .. CS .. "git checkout " .. branch)
332+
dtsys.os_execute("cd " .. repo .. CS .. "git checkout " .. branch)
333333

334334
restore_log_level(old_log_level)
335335
end
@@ -417,7 +417,7 @@ local function get_script_metadata(script)
417417
-- grab the script_data.metadata table
418418
description = string.match(content, "script_data%.metadata = %{\r?\n(.-)\r?\n%}")
419419
else
420-
log.msg(log.error, _("Cant read from " .. script))
420+
log.msg(log.error, "cant read from " .. script)
421421
end
422422

423423
if description then
@@ -459,7 +459,7 @@ local function get_script_doc(script)
459459
-- assume that the second block comment is the documentation
460460
description = string.match(content, "%-%-%[%[.-%]%].-%-%-%[%[(.-)%]%]")
461461
else
462-
log.msg(log.error, _("Cant read from " .. script))
462+
log.msg(log.error, "can't read from " .. script)
463463
end
464464
if description then
465465
restore_log_level(old_log_level)
@@ -489,7 +489,7 @@ local function activate(script)
489489

490490
if status then
491491
pref_write(script.script_name, "bool", true)
492-
log.msg(log.screen, _("Loaded ") .. script.script_name)
492+
log.msg(log.screen, _(string.format("loaded %s", script.script_name)))
493493
script.running = true
494494

495495
if err ~= true then
@@ -503,9 +503,9 @@ local function activate(script)
503503
end
504504

505505
else
506-
log.msg(log.screen, script.script_name .. _(" failed to load"))
507-
log.msg(log.error, "Error loading " .. script.script_name)
508-
log.msg(log.error, "Error message: " .. err)
506+
log.msg(log.screen, _(string.format("%s failed to load", script.script_name)))
507+
log.msg(log.error, "error loading " .. script.script_name)
508+
log.msg(log.error, "error message: " .. err)
509509
end
510510

511511
else -- script is a lib and loaded but hidden and the user wants to reload
@@ -549,13 +549,13 @@ local function deactivate(script)
549549
end
550550

551551
log.msg(log.info, "turned off " .. script.script_name)
552-
log.msg(log.screen, script.name .. _(" stopped"))
552+
log.msg(log.screen, _(string.format("%s stopped", script.name)))
553553

554554
else
555555
script.running = false
556556

557557
log.msg(log.info, "setting " .. script.script_name .. " to not start")
558-
log.msg(log.screen, script.name .. _(" will not start when darktable is restarted"))
558+
log.msg(log.screen, _(string.format("%s will not start when darktable is restarted", script.name)))
559559
end
560560

561561
restore_log_level(old_log_level)
@@ -665,7 +665,7 @@ local function scan_scripts(script_dir)
665665
log.msg(log.debug, "find command is " .. find_cmd)
666666

667667
-- scan the scripts
668-
local output = io.popen(find_cmd)
668+
local output = dtsys.io_popen(find_cmd)
669669
for line in output:lines() do
670670
log.msg(log.debug, "line is " .. line)
671671
local l = string.gsub(line, ds.sanitize_lua(LUA_DIR) .. PS, "") -- strip the lua dir off
@@ -695,7 +695,7 @@ local function update_scripts()
695695
local git = sm.executables.git
696696

697697
if not git then
698-
dt.print(_("ERROR: git not found. Install or specify the location of the git executable."))
698+
log.msg(log.screen, _("ERROR: git not found. Install or specify the location of the git executable."))
699699
return
700700
end
701701

@@ -709,7 +709,7 @@ local function update_scripts()
709709
end
710710

711711
if result == 0 then
712-
dt.print(_("lua scripts successfully updated"))
712+
log.msg(log.screen, _("lua scripts successfully updated"))
713713
end
714714

715715
restore_log_level(old_log_level)
@@ -749,9 +749,9 @@ local function scan_repositories()
749749
find_cmd = "dir /b/s /a:d " .. LUA_DIR .. PS .. "*.git | sort"
750750
end
751751

752-
log.msg(log.debug, _("find command is ") .. find_cmd)
752+
log.msg(log.debug, "find command is " .. find_cmd)
753753

754-
local output = io.popen(find_cmd)
754+
local output = dtsys.io_popen(find_cmd)
755755

756756
for line in output:lines() do
757757
local l = string.gsub(line, ds.sanitize_lua(LUA_DIR) .. PS, "") -- strip the lua dir off
@@ -799,7 +799,7 @@ local function install_scripts()
799799
local folder = sm.widgets.new_folder.text
800800

801801
if string.match(du.join(sm.folders, " "), ds.sanitize_lua(folder)) then
802-
log.msg(log.screen, _("folder ") .. folder .. _(" is already in use. Please specify a different folder name."))
802+
log.msg(log.screen, _(string.format("folder %s is already in use. Please specify a different folder name.", folder)))
803803
log.msg(log.error, "folder " .. folder .. " already exists, returning...")
804804
restore_log_level(old_log_level)
805805
return
@@ -810,7 +810,7 @@ local function install_scripts()
810810
local git = sm.executables.git
811811

812812
if not git then
813-
dt.print(_("ERROR: git not found. Install or specify the location of the git executable."))
813+
log.msg(log.screen, _("ERROR: git not found. Install or specify the location of the git executable."))
814814
restore_log_level(old_log_level)
815815
return
816816
end
@@ -849,12 +849,12 @@ local function install_scripts()
849849
sm.widgets.new_folder.text = ""
850850
sm.widgets.main_menu.selected = 3
851851
else
852-
dt.print(_("No scripts found to install"))
852+
log.msg(log.screen, _("No scripts found to install"))
853853
log.msg(log.error, "scan_scripts returned " .. count .. " scripts found. Not adding to folder_selector")
854854
end
855855

856856
else
857-
dt.print(_("failed to download scripts"))
857+
log.msg(log.screen, _("failed to download scripts"))
858858
end
859859

860860
restore_log_level(old_log_level)
@@ -1006,7 +1006,7 @@ local function paginate(direction)
10061006
last = first + sm.page_status.num_buttons - 1
10071007
end
10081008

1009-
sm.widgets.page_status.label = _("Page ") .. cur_page .. _(" of ") .. max_pages
1009+
sm.widgets.page_status.label = _(string.format("page %d of %d", cur_page, max_pages))
10101010

10111011
populate_buttons(folder, first, last)
10121012

@@ -1177,17 +1177,6 @@ local function install_module()
11771177
log.msg(log.debug, "set run to true, loading preferences")
11781178
load_preferences()
11791179
scan_repositories()
1180-
--[[dt.print_log("\n\nsetting sm visible false\n\n")
1181-
dt.gui.libs["script_manager"].visible = false
1182-
dt.control.sleep(5000)
1183-
dt.print_log("setting sm visible true")
1184-
dt.gui.libs["script_manager"].visible = true
1185-
--[[dt.control.sleep(5000)
1186-
dt.print_log("setting sm expanded false")
1187-
dt.gui.libs["script_manager"].expanded = false
1188-
dt.control.sleep(5000)
1189-
dt.print_log("setting sm expanded true")
1190-
dt.gui.libs["script_manager"].expanded = true]]
11911180

11921181
restore_log_level(old_log_level)
11931182
end
@@ -1217,7 +1206,7 @@ if check_for_updates then
12171206
-- probably upgraded from an earlier api version so get back to master
12181207
-- to use the latest version of script_manager to get the proper API
12191208
checkout_repo_branch(repo, "master")
1220-
log.msg(log.screen, "lua API version reset, please restart darktable")
1209+
log.msg(log.screen, _("lua API version reset, please restart darktable"))
12211210

12221211
elseif LUA_API_VER == current_branch then
12231212
-- do nothing, we are fine
@@ -1353,7 +1342,7 @@ sm.widgets.disable_scripts = dt.new_widget("button"){
13531342
local LUARC = dt.configuration.config_dir .. PS .. "luarc"
13541343
df.file_move(LUARC, LUARC .. ".disabled")
13551344
log.msg(log.info, "lua scripts disabled")
1356-
dt.print(_("lua scripts will not run the next time darktable is started"))
1345+
log.msg(log.screen, _("lua scripts will not run the next time darktable is started"))
13571346
end
13581347
}
13591348

@@ -1414,7 +1403,7 @@ end
14141403
local page_back = "<"
14151404
local page_forward = ">"
14161405

1417-
sm.widgets.page_status = dt.new_widget("label"){label = _("Page:")}
1406+
sm.widgets.page_status = dt.new_widget("label"){label = _("page:")}
14181407

14191408
sm.widgets.page_back = dt.new_widget("button"){
14201409
label = page_back,
@@ -1445,7 +1434,7 @@ sm.widgets.scripts = dt.new_widget("box"){
14451434
orientation = vertical,
14461435
dt.new_widget("section_label"){label = _(" ")},
14471436
dt.new_widget("label"){label = " "},
1448-
dt.new_widget("label"){label = _("Scripts")},
1437+
dt.new_widget("label"){label = _("scripts")},
14491438
sm.widgets.folder_selector,
14501439
sm.widgets.page_control,
14511440
table.unpack(sm.widgets.boxes),
@@ -1529,7 +1518,7 @@ else
15291518
function(event, old_view, new_view)
15301519
if new_view.name == "lighttable" and old_view.name == "darkroom" then
15311520
install_module()
1532-
end
1521+
end
15331522
end
15341523
)
15351524
sm.event_registered = true

0 commit comments

Comments
 (0)