Skip to content

Commit a4f6af7

Browse files
committed
tools/script_manager.lua - enabled log levels in callback routines
1 parent fad74d6 commit a4f6af7

File tree

1 file changed

+82
-4
lines changed

1 file changed

+82
-4
lines changed

tools/script_manager.lua

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ sm.event_registered = false
130130
sm.widgets = {}
131131
sm.categories = {}
132132

133+
-- set log level for functions
134+
135+
sm.log_level = DEFAULT_LOG_LEVEL
136+
133137
--[[
134138
135139
sm.scripts is a table of tables for containing the scripts
@@ -192,33 +196,55 @@ sm.run = false
192196
-- F U N C T I O N S
193197
-- - - - - - - - - - - - - - - - - - - - - - - -
194198

199+
-------------------
200+
-- helper functions
201+
-------------------
202+
203+
local function set_log_level(level)
204+
local old_log_level = log.log_level()
205+
log.log_level(level)
206+
return old_log_level
207+
end
208+
209+
local function restore_log_level(level)
210+
log.log_level(level)
211+
end
212+
195213
local function pref_read(name, pref_type)
214+
local old_log_level = set_log_level(sm.log_level)
196215
log.msg(log.debug, "name is " .. name .. " and type is " .. pref_type)
197216
local val = dt.preferences.read(MODULE, name, pref_type)
198-
if not string.match(pref_type, "bool") then
199-
log.msg(log.debug, "read value " .. tostring(val))
200-
end
217+
log.msg(log.debug, "read value " .. tostring(val))
218+
restore_log_level(old_log_level)
201219
return val
202220
end
203221

204222
local function pref_write(name, pref_type, value)
205-
dt.preferences.write(MODULE, name, pref_type, value)
223+
local old_log_level = set_log_level(sm.log_level)
224+
log.msg(log.debug, "writing value " .. tostring(value) .. " for name " .. name)
225+
dt.preferences.write(MODULE, name, pref_type, value)
226+
restore_log_level(old_log_level)
206227
end
207228

229+
----------------
208230
-- git interface
231+
----------------
209232

210233
local function get_repo_status(repo)
234+
local old_log_level = set_log_level(sm.log_level)
211235
local p = io.popen("cd " .. repo .. CS .. "git status")
212236
if p then
213237
local data = p:read("*a")
214238
p:close()
215239
return data
216240
end
217241
log.msg(log.error, "unable to get status of " .. repo)
242+
restore_log_level(old_log_level)
218243
return nil
219244
end
220245

221246
local function get_current_repo_branch(repo)
247+
local old_log_level = set_log_level(sm.log_level)
222248
local branch = nil
223249
local p = io.popen("cd " .. repo .. CS .. "git branch --all")
224250
if p then
@@ -237,10 +263,12 @@ local function get_current_repo_branch(repo)
237263
if not branch then
238264
log.msg(log.error, "no current branch detected in repo_data")
239265
end
266+
restore_log_level(old_log_level)
240267
return nil
241268
end
242269

243270
local function get_repo_branches(repo)
271+
local old_log_level = set_log_level(sm.log_level)
244272
local branches = {}
245273
local p = io.popen("cd " .. repo .. CS .. "git pull --all" .. CS .. "git branch --all")
246274
if p then
@@ -257,26 +285,32 @@ local function get_repo_branches(repo)
257285
end
258286
end
259287
end
288+
restore_log_level(old_log_level)
260289
return branches
261290
end
262291

263292

264293
local function is_repo_clean(repo_data)
294+
local old_log_level = set_log_level(sm.log_level)
265295
if string.match(repo_data, "\n%s-%a.-%a:%s-%a%g-\n") then
266296
log.msg(log.info, "repo is dirty")
267297
return false
268298
else
269299
log.msg(log.info, "repo is clean")
270300
return true
271301
end
302+
restore_log_level(old_log_level)
272303
end
273304

274305
local function checkout_repo_branch(repo, branch)
306+
local old_log_level = set_log_level(sm.log_level)
275307
log.msg(log.info, "checkout out branch " .. branch .. " from repository " .. repo)
276308
os.execute("cd " .. repo .. CS .. "git checkout " .. branch)
309+
restore_log_level(old_log_level)
277310
end
278311

279312
local function update_combobox_choices(combobox, choice_table, selected)
313+
local old_log_level = set_log_level(sm.log_level)
280314
local items = #combobox
281315
local choices = #choice_table
282316
for i, name in ipairs(choice_table) do
@@ -292,11 +326,14 @@ local function update_combobox_choices(combobox, choice_table, selected)
292326
end
293327

294328
combobox.value = selected
329+
restore_log_level(old_log_level)
295330
end
296331

297332
local function string_trim(str)
333+
local old_log_level = set_log_level(sm.log_level)
298334
local result = string.gsub(str, "^%s+", "")
299335
result = string.gsub(result, "%s+$", "")
336+
restore_log_level(old_log_level)
300337
return result
301338
end
302339

@@ -305,14 +342,17 @@ local function string_dequote(str)
305342
end
306343

307344
local function add_script_category(category)
345+
local old_log_level = set_log_level(sm.log_level)
308346
if #sm.categories == 0 or not string.match(du.join(sm.categories, " "), ds.sanitize_lua(category)) then
309347
table.insert(sm.categories, category)
310348
sm.scripts[category] = {}
311349
log.msg(log.debug, "created category " .. category)
312350
end
351+
restore_log_level(old_log_level)
313352
end
314353

315354
local function get_script_doc(script)
355+
local old_log_level = set_log_level(sm.log_level)
316356
local description = nil
317357
f = io.open(LUA_DIR .. PS .. script .. ".lua")
318358
if f then
@@ -325,13 +365,16 @@ local function get_script_doc(script)
325365
log.msg(log.error, _("Cant read from " .. script))
326366
end
327367
if description then
368+
restore_log_level(old_log_level)
328369
return description
329370
else
371+
restore_log_level(old_log_level)
330372
return "No documentation available"
331373
end
332374
end
333375

334376
local function activate(script)
377+
local old_log_level = set_log_level(sm.log_level)
335378
local status = nil -- status of start function
336379
local err = nil -- error message returned if module doesn't start
337380
log.msg(log.info, "activating " .. script.name)
@@ -364,6 +407,7 @@ local function activate(script)
364407
status = true
365408
pref_write(script.script_name, "bool", true)
366409
end
410+
restore_log_level(old_log_level)
367411
return status
368412
end
369413

@@ -376,6 +420,7 @@ local function deactivate(script)
376420
-- and mark them inactive for the next time darktable starts
377421

378422
-- deactivate it....
423+
local old_log_level = set_log_level(sm.log_level)
379424
pref_write(script.script_name, "bool", false)
380425
if script.data then
381426
script.data.destroy()
@@ -397,9 +442,11 @@ local function deactivate(script)
397442
log.msg(log.info, "setting " .. script.script_name .. " to not start")
398443
log.msg(log.screen, script.name .. _(" will not start when darktable is restarted"))
399444
end
445+
restore_log_level(old_log_level)
400446
end
401447

402448
local function add_script_name(name, path, category)
449+
local old_log_level = set_log_level(sm.log_level)
403450
log.msg(log.debug, "category is " .. category)
404451
log.msg(log.debug, "name is " .. name)
405452
local script = {
@@ -414,9 +461,11 @@ local function add_script_name(name, path, category)
414461
if pref_read(script.script_name, "bool") then
415462
activate(script)
416463
end
464+
restore_log_level(old_log_level)
417465
end
418466

419467
local function process_script_data(script_file)
468+
local old_log_level = set_log_level(sm.log_level)
420469

421470
-- the script file supplied is category/filename.filetype
422471
-- the following pattern splits the string into category, path, name, fileename, and filetype
@@ -447,9 +496,11 @@ local function process_script_data(script_file)
447496
if name then
448497
add_script_name(name, path, category)
449498
end
499+
restore_log_level(old_log_level)
450500
end
451501

452502
local function scan_scripts(script_dir)
503+
local old_log_level = set_log_level(sm.log_level)
453504
local script_count = 0
454505
local find_cmd = "find -L " .. script_dir .. " -name \\*.lua -print | sort"
455506
if dt.configuration.running_os == "windows" then
@@ -472,10 +523,12 @@ local function scan_scripts(script_dir)
472523
end
473524
end
474525
end
526+
restore_log_level(old_log_level)
475527
return script_count
476528
end
477529

478530
local function update_scripts()
531+
local old_log_level = set_log_level(sm.log_level)
479532
local result = false
480533

481534
local git = sm.executables.git
@@ -498,10 +551,12 @@ local function update_scripts()
498551
dt.print(_("lua scripts successfully updated"))
499552
end
500553

554+
restore_log_level(old_log_level)
501555
return result
502556
end
503557

504558
local function update_script_update_choices()
559+
local old_log_level = set_log_level(sm.log_level)
505560
local installs = {}
506561
local pref_string = ""
507562
for i, repo in ipairs(sm.installed_repositories) do
@@ -511,9 +566,11 @@ local function update_script_update_choices()
511566
update_combobox_choices(sm.widgets.update_script_choices, installs, 1)
512567
log.msg(log.debug, "repo pref string is " .. pref_string)
513568
pref_write("installed_repos", "string", pref_string)
569+
restore_log_level(old_log_level)
514570
end
515571

516572
local function scan_repositories()
573+
local old_log_level = set_log_level(sm.log_level)
517574
local script_count = 0
518575
local find_cmd = "find -L " .. LUA_DIR .. " -name \\*.git -print | sort"
519576
if dt.configuration.running_os == "windows" then
@@ -548,15 +605,18 @@ local function scan_repositories()
548605
end
549606
end
550607
update_script_update_choices()
608+
restore_log_level(old_log_level)
551609
end
552610

553611
local function install_scripts()
612+
local old_log_level = set_log_level(sm.log_level)
554613
local url = sm.widgets.script_url.text
555614
local category = sm.widgets.new_category.text
556615

557616
if string.match(du.join(sm.categories, " "), ds.sanitize_lua(category)) then
558617
log.msg(log.screen, _("category ") .. category .. _(" is already in use. Please specify a different category name."))
559618
log.msg(log.error, "category " .. category .. " already exists, returning...")
619+
restore_log_level(old_log_level)
560620
return
561621
end
562622

@@ -566,6 +626,7 @@ local function install_scripts()
566626

567627
if not git then
568628
dt.print(_("ERROR: git not found. Install or specify the location of the git executable."))
629+
restore_log_level(old_log_level)
569630
return
570631
end
571632

@@ -607,28 +668,34 @@ local function install_scripts()
607668
dt.print(_("failed to download scripts"))
608669
end
609670

671+
restore_log_level(old_log_level)
610672
return result
611673
end
612674

613675
local function clear_button(number)
676+
local old_log_level = set_log_level(sm.log_level)
614677
local button = sm.widgets.buttons[number]
615678
button.label = ""
616679
button.tooltip = ""
617680
button.sensitive = false
618681
--button.name = ""
682+
restore_log_level(old_log_level)
619683
end
620684

621685
local function find_script(category, name)
686+
local old_log_level = set_log_level(sm.log_level)
622687
log.msg(log.debug, "looking for script " .. name .. " in category " .. category)
623688
for _, script in ipairs(sm.scripts[category]) do
624689
if string.match(script.name, "^" .. ds.sanitize_lua(name) .. "$") then
625690
return script
626691
end
627692
end
693+
restore_log_level(old_log_level)
628694
return nil
629695
end
630696

631697
local function populate_buttons(category, first, last)
698+
local old_log_level = set_log_level(sm.log_level)
632699
log.msg(log.debug, "category is " .. category .. " and first is " .. first .. " and last is " .. last)
633700
local button_num = 1
634701
for i = first, last do
@@ -693,9 +760,11 @@ local function populate_buttons(category, first, last)
693760
clear_button(i)
694761
end
695762
end
763+
restore_log_level(old_log_level)
696764
end
697765

698766
local function paginate(direction)
767+
local old_log_level = set_log_level(sm.log_level)
699768
local category = sm.page_status.category
700769
log.msg(log.debug, "category is " .. category)
701770
local num_scripts = #sm.scripts[category]
@@ -747,9 +816,11 @@ local function paginate(direction)
747816
sm.widgets.page_status.label = _("Page ") .. cur_page .. _(" of ") .. max_pages
748817

749818
populate_buttons(category, first, last)
819+
restore_log_level(old_log_level)
750820
end
751821

752822
local function change_category(category)
823+
local old_log_level = set_log_level(sm.log_level)
753824
if not category then
754825
log.msg(log.debug "setting category to selector value " .. sm.widgets.category_selector.value)
755826
sm.page_status.category = sm.widgets.category_selector.value
@@ -759,9 +830,11 @@ local function change_category(category)
759830
end
760831

761832
paginate(2)
833+
restore_log_level(old_log_level)
762834
end
763835

764836
local function change_num_buttons()
837+
local old_log_level = set_log_level(sm.log_level)
765838
cur_buttons = sm.page_status.num_buttons
766839
new_buttons = sm.widgets.num_buttons.value
767840
pref_write("num_buttons", "integer", new_buttons)
@@ -792,9 +865,11 @@ local function change_num_buttons()
792865
log.msg(log.debug, "num_buttons set to " .. sm.page_status.num_buttons)
793866
paginate(2) -- force the buttons to repopulate
794867
sm.widgets.main_menu.selected = 3 -- jump back to start/stop scripts
868+
restore_log_level(old_log_level)
795869
end
796870

797871
local function load_preferences()
872+
local old_log_level = set_log_level(sm.log_level)
798873
-- load the prefs and update settings
799874
-- update_script_choices
800875
local pref_string = pref_read("installed_repos", "string")
@@ -836,9 +911,11 @@ local function load_preferences()
836911
log.msg(log.debug, "set main menu to val " .. val .. " which is " .. sm.widgets.main_menu.value)
837912

838913
log.msg(log.debug, "set main menu to " .. sm.widgets.main_menu.value)
914+
restore_log_level(old_log_level)
839915
end
840916

841917
local function install_module()
918+
local old_log_level = set_log_level(sm.log_level)
842919
if not sm.module_installed then
843920
dt.register_lib(
844921
"script_manager", -- Module name
@@ -868,6 +945,7 @@ local function install_module()
868945
dt.control.sleep(5000)
869946
dt.print_log("setting sm expanded true")
870947
dt.gui.libs["script_manager"].expanded = true]]
948+
restore_log_level(old_log_level)
871949
end
872950

873951
-- - - - - - - - - - - - - - - - - - - - - - - -

0 commit comments

Comments
 (0)