From c9943a206519352b156b7312b22a3707d87cb3dc Mon Sep 17 00:00:00 2001 From: Bill Ferguson Date: Sat, 25 Mar 2023 17:02:48 -0400 Subject: [PATCH 01/10] lib/dtutils/string - Added logging capability with the ability to set library logging level from a user script --- lib/dtutils/string.lua | 117 +++++++++++++++++++++++++++++++---------- 1 file changed, 88 insertions(+), 29 deletions(-) diff --git a/lib/dtutils/string.lua b/lib/dtutils/string.lua index f7159a2..6b71692 100644 --- a/lib/dtutils/string.lua +++ b/lib/dtutils/string.lua @@ -1,6 +1,12 @@ local dtutils_string = {} local dt = require "darktable" +local du = require "lib/dtutils" +local log = require "lib/dtutils.log" + +local DEFAULT_LOG_LEVEL = log.error + +dtutils_string.log_level = DEFAULT_LOG_LEVEL dtutils_string.libdoc = { Name = [[dtutils.string]], @@ -48,6 +54,8 @@ dtutils_string.libdoc.functions["strip_accents"] = { } function dtutils_string.strip_accents( str ) + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) local tableAccents = {} tableAccents["à"] = "a" tableAccents["á"] = "a" @@ -111,6 +119,7 @@ function dtutils_string.strip_accents( str ) end end + log.log_level(old_log_level) return normalizedString end @@ -136,6 +145,8 @@ dtutils_string.libdoc.functions["escape_xml_characters"] = { -- Keep & first, otherwise it will double escape other characters function dtutils_string.escape_xml_characters( str ) + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) str = string.gsub(str,"&", "&") str = string.gsub(str,"\"", """) @@ -143,6 +154,7 @@ function dtutils_string.escape_xml_characters( str ) str = string.gsub(str,"<", "<") str = string.gsub(str,">", ">") + log.log_level(old_log_level) return str end @@ -165,11 +177,14 @@ dtutils_string.libdoc.functions["urlencode"] = { } function dtutils_string.urlencode(str) + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) if (str) then str = string.gsub (str, "\n", "\r\n") str = string.gsub (str, "([^%w ])", function (c) return string.format ("%%%02X", string.byte(c)) end) str = string.gsub (str, " ", "+") end + log.log_level(old_log_level) return str end @@ -192,38 +207,52 @@ dtutils_string.libdoc.functions["is_not_sanitized"] = { } local function _is_not_sanitized_posix(str) - -- A sanitized string must be quoted. - if not string.match(str, "^'.*'$") then - return true + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) + -- A sanitized string must be quoted. + if not string.match(str, "^'.*'$") then + log.log_level(old_log_level) + return true -- A quoted string containing no quote characters within is sanitized. - elseif string.match(str, "^'[^']*'$") then - return false - end + elseif string.match(str, "^'[^']*'$") then + log.log_level(old_log_level) + return false + end - -- Any quote characters within a sanitized string must be properly - -- escaped. - local quotesStripped = string.sub(str, 2, -2) - local escapedQuotesRemoved = string.gsub(quotesStripped, "'\\''", "") - if string.find(escapedQuotesRemoved, "'") then - return true - else - return false - end + -- Any quote characters within a sanitized string must be properly + -- escaped. + local quotesStripped = string.sub(str, 2, -2) + local escapedQuotesRemoved = string.gsub(quotesStripped, "'\\''", "") + if string.find(escapedQuotesRemoved, "'") then + log.log_level(old_log_level) + return true + else + log.log_level(old_log_level) + return false + end end local function _is_not_sanitized_windows(str) - if not string.match(str, "^\".*\"$") then - return true - else - return false - end + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) + if not string.match(str, "^\".*\"$") then + log.log_level(old_log_level) + return true + else + log.log_level(old_log_level) + return false + end end function dtutils_string.is_not_sanitized(str) + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) if dt.configuration.running_os == "windows" then - return _is_not_sanitized_windows(str) + log.log_level(old_log_level) + return _is_not_sanitized_windows(str) else - return _is_not_sanitized_posix(str) + log.log_level(old_log_level) + return _is_not_sanitized_posix(str) end end @@ -246,26 +275,38 @@ dtutils_string.libdoc.functions["sanitize"] = { } local function _sanitize_posix(str) + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) if _is_not_sanitized_posix(str) then - return "'" .. string.gsub(str, "'", "'\\''") .. "'" + log.log_level(old_log_level) + return "'" .. string.gsub(str, "'", "'\\''") .. "'" else - return str + log.log_level(old_log_level) + return str end end local function _sanitize_windows(str) + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) if _is_not_sanitized_windows(str) then - return "\"" .. string.gsub(str, "\"", "\"^\"\"") .. "\"" + log.log_level(old_log_level) + return "\"" .. string.gsub(str, "\"", "\"^\"\"") .. "\"" else - return str + log.log_level(old_log_level) + return str end end function dtutils_string.sanitize(str) + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) if dt.configuration.running_os == "windows" then - return _sanitize_windows(str) + log.log_level(old_log_level) + return _sanitize_windows(str) else - return _sanitize_posix(str) + log.log_level(old_log_level) + return _sanitize_posix(str) end end @@ -288,9 +329,12 @@ dtutils_string.libdoc.functions["sanitize_lua"] = { } function dtutils_string.sanitize_lua(str) + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) str = string.gsub(str, "%-", "%%-") str = string.gsub(str, "%(", "%%(") str = string.gsub(str, "%)", "%%)") + log.log_level(old_log_level) return str end @@ -313,7 +357,9 @@ dtutils_string.libdoc.functions["split_filepath"] = { } function dtutils_string.split_filepath(str) - -- strip out single quotes from quoted pathnames + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) + -- strip out single quotes from quoted pathnames str = string.gsub(str, "'", "") str = string.gsub(str, '"', '') local result = {} @@ -323,6 +369,7 @@ function dtutils_string.split_filepath(str) result["basename"] = result["filetype"] result["filetype"] = "" end + log.log_level(old_log_level) return result end @@ -344,7 +391,10 @@ dtutils_string.libdoc.functions["get_path"] = { } function dtutils_string.get_path(str) + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) local parts = dtutils_string.split_filepath(str) + log.log_level(old_log_level) return parts["path"] end @@ -366,7 +416,10 @@ dtutils_string.libdoc.functions["get_filename"] = { } function dtutils_string.get_filename(str) + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) local parts = dtutils_string.split_filepath(str) + log.log_level(old_log_level) return parts["filename"] end @@ -389,7 +442,10 @@ dtutils_string.libdoc.functions["get_basename"] = { } function dtutils_string.get_basename(str) + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) local parts = dtutils_string.split_filepath(str) + log.log_level(old_log_level) return parts["basename"] end @@ -411,7 +467,10 @@ dtutils_string.libdoc.functions["get_filetype"] = { } function dtutils_string.get_filetype(str) + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) local parts = dtutils_string.split_filepath(str) + log.log_level(old_log_level) return parts["filetype"] end From 5331a38f10fdbbd85894ec6b9cd096e242370aa4 Mon Sep 17 00:00:00 2001 From: Bill Ferguson Date: Sat, 25 Mar 2023 17:05:11 -0400 Subject: [PATCH 02/10] lib/dtutils/string - added % and + to sanitize_lua() to prevent pattern matching problems --- lib/dtutils/string.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/dtutils/string.lua b/lib/dtutils/string.lua index 6b71692..b988731 100644 --- a/lib/dtutils/string.lua +++ b/lib/dtutils/string.lua @@ -331,9 +331,11 @@ dtutils_string.libdoc.functions["sanitize_lua"] = { function dtutils_string.sanitize_lua(str) local old_log_level = log.log_level() log.log_level(dtutils_string.log_level) + str = string.gsub(str, "%%", "%%%%") str = string.gsub(str, "%-", "%%-") str = string.gsub(str, "%(", "%%(") str = string.gsub(str, "%)", "%%)") + str = string.gsub(str, "+", "%%+") log.log_level(old_log_level) return str end From ad10542b3025a2a4e32d87cc3bd1ca61351a0fdb Mon Sep 17 00:00:00 2001 From: Bill Ferguson Date: Sun, 26 Mar 2023 14:55:40 -0400 Subject: [PATCH 03/10] lib/dtutils/string - Added variable substitution functions that are equivalent to the darktable variable substitution functions. --- lib/dtutils/string.lua | 411 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 411 insertions(+) diff --git a/lib/dtutils/string.lua b/lib/dtutils/string.lua index b988731..e315eda 100644 --- a/lib/dtutils/string.lua +++ b/lib/dtutils/string.lua @@ -476,6 +476,417 @@ function dtutils_string.get_filetype(str) return parts["filetype"] end +local substitutes = {} + +-- - - - - - - - - - - - - - - - - - - - - - - - +-- C O N S T A N T S +-- - - - - - - - - - - - - - - - - - - - - - - - + +local PLACEHOLDERS = {"ROLL.NAME", + "FILE.FOLDER", + "FILE.NAME", + "FILE.EXTENSION", + "ID", + "VERSION", + "VERSION.IF.MULTI", -- Not Implemented + "VERSION.NAME", + "DARKTABLE.VERSION", + "DARKTABLE.NAME", -- Not Implemented + "SEQUENCE", + "WIDTH.SENSOR", + "HEIGHT.SENSOR", + "WIDTH.RAW", + "HEIGHT.RAW", + "WIDTH.CROP", + "HEIGHT.CROP", + "WIDTH.EXPORT", + "HEIGHT.EXPORT", + "WIDTH.MAX", -- Not Implemented + "HEIGHT.MAX", -- Not Implemented + "YEAR", + "MONTH", + "DAY", + "HOUR", + "MINUTE", + "SECOND", + "MSEC", + "EXIF.YEAR", + "EXIF.MONTH", + "EXIF.DAY", + "EXIF.HOUR", + "EXIF.MINUTE", + "EXIF.SECOND", + "EXIF.MSEC", + "EXIF.DATE.REGIONAL", -- Not Implemented + "EXIF.TIME.REGIONAL", -- Not Implemented + "EXIF.ISO", + "EXIF.EXPOSURE", + "EXIF.EXPOSURE.BIAS", + "EXIF.APERTURE", + "EXIF.FOCAL.LENGTH", + "EXIF.FOCUS.DISTANCE", + "LONGITUDE", + "LATITUDE", + "ALTITUDE", + "STARS", + "RATING.ICONS", -- Not Implemented + "LABELS", + "LABELS.ICONS", -- Not Implemented + "MAKER", + "MODEL", + "TITLE", + "DESCRIPTION", + "CREATOR", + "PUBLISHER", + "RIGHTS", + "TAGS", -- Not Implemented + "CATEGORY", -- Not Implemented + "SIDECAR.TXT", -- Not Implemented + "FOLDER.PICTURES", + "FOLDER.HOME", + "FOLDER.DESKTOP", + "OPENCL.ACTIVATED", -- Not Implemented + "USERNAME", + "NL", + "JOBCODE" -- Not Implemented +} + +local PS = dt.configuration.running_os == "windows" and "\\" or "/" +local USER = os.getenv("USERNAME") +local HOME = dt.configuration.running_os == "windows" and os.getenv("HOMEPATH") or os.getenv("HOME") +local PICTURES = HOME .. PS .. (dt.configuration.running_os == "windows" and "My Pictures" or "Pictures") +local DESKTOP = HOME .. PS .. "Desktop" + +local function get_colorlabels(image) + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) + local colorlabels = {} + if image.red then table.insert(colorlabels, "red") end + if image.yellow then table.insert(colorlabels, "yellow") end + if image.green then table.insert(colorlabels, "green") end + if image.blue then table.insert(colorlabels, "blue") end + if image.purple then table.insert(colorlabels, "purple") end + local labels = #colorlabels == 1 and colorlabels[1] or du.join(colorlabels, ",") + log.log_level(old_log_level) + return labels +end + +dtutils_string.libdoc.functions["build_substitution_list"] = { + Name = [[build_substitution_list]], + Synopsis = [[build a list of variable substitutions]], + Usage = [[local ds = require "lib/dtutils.string" + ds.build_substitution_list(image, sequence, [username], [pic_folder], [home], [desktop]) + image - dt_lua_image_t - the image being processed + sequence - integer - the sequence number of the image + [username] - string - optional - user name. Will be determined if not supplied + [pic_folder] - string - optional - pictures folder name. Will be determined if not supplied + [home] - string - optional - home directory. Will be determined if not supplied + [desktop] - string - optional - desktop directory. Will be determined if not supplied]], + Description = [[build_substitution_list populates variables with values from the arguments + and determined from the system and darktable.]], + Return_Value = [[]], + Limitations = [[If the value for a variable can not be determined, or if it is not supported, + then an empty string is used for the value.]], + Example = [[]], + See_Also = [[https://docs.darktable.org/usermanual/4.2/en/special-topics/variables/]], + Reference = [[]], + License = [[]], + Copyright = [[]], +} + +function dtutils_string.build_substition_list(image, sequence, username, pic_folder, home, desktop) + -- build the argument substitution list from each image + + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) + + local is_api_9_1 = true + if dt.configuration.api_version_string < "9.1.0" then + is_api_9_1 = false + end + + local datetime = os.date("*t") + local user_name = username or USER + local pictures_folder = pic_folder or PICTURES + local home_folder = home or HOME + log.msg(log.info, "home is " .. tostring(home) .. " and HOME is " .. tostring(HOME) .. " and home_folder is " .. tostring(home_folder)) + local desktop_folder = desktop or DESKTOP + + local labels = get_colorlabels(image) + log.msg(log.info, "image date time taken is " .. image.exif_datetime_taken) + local eyear, emon, eday, ehour, emin, esec, emsec + if dt.preferences.read("darktable", "lighttable/ui/milliseconds", "bool") and is_api_9_1 then + eyear, emon, eday, ehour, emin, esec, emsec = + string.match(image.exif_datetime_taken, "(%d+):(%d+):(%d+) (%d+):(%d+):(%d+)%.(%d+)$") + else + emsec = "0" + eyear, emon, eday, ehour, emin, esec = + string.match(image.exif_datetime_taken, "(%d+):(%d+):(%d+) (%d+):(%d+):(%d+)$") + end + local replacements = {image.film, -- ROLL.NAME + image.path, -- FILE.FOLDER + image.filename, -- FILE.NAME + dtutils_string.get_filetype(image.filename),-- FILE.EXTENSION + image.id, -- ID + image.duplicate_index, -- VERSION + "", -- VERSION.IF_MULTI + image.version_name, -- VERSION.NAME + dt.configuration.version, -- DARKTABLE.VERSION + "", -- DARKTABLE.NAME + sequence, -- SEQUENCE + image.width, -- WIDTH.SENSOR + image.height, -- HEIGHT.SENSOR + is_api_9_1 and image.p_width or "", -- WIDTH.RAW + is_api_9_1 and image.p_height or "", -- HEIGHT.RAW + is_api_9_1 and image.final_width or "", -- WIDTH.CROP + is_api_9_1 and image.final_height or "", -- HEIGHT.CROP + is_api_9_1 and image.final_width or "", -- WIDTH.EXPORT + is_api_9_1 and image.final_height or "", -- HEIGHT.EXPORT + "", -- WIDTH.MAX + "", -- HEIGHT.MAX + string.format("%4d", datetime.year), -- YEAR + string.format("%02d", datetime.month), -- MONTH + string.format("%02d", datetime.day), -- DAY + string.format("%02d", datetime.hour), -- HOUR + string.format("%02d", datetime.min), -- MINUTE + string.format("%02d", datetime.sec), -- SECOND + "", -- MSEC + eyear, -- EXIF.YEAR + emon, -- EXIF.MONTH + eday, -- EXIF.DAY + ehour, -- EXIF.HOUR + emin, -- EXIF.MINUTE + esec, -- EXIF.SECOND + emsec, -- EXIF.MSEC + "", -- EXIF.DATE.REGIONAL + "", -- EXIF.TIME.REGIONAL + string.format("%d", image.exif_iso), -- EXIF.ISO + string.format("1/%.0f", 1./image.exif_exposure), -- EXIF.EXPOSURE + image.exif_exposure_bias, -- EXIF.EXPOSURE.BIAS + string.format("%.01f", image.exif_aperture), -- EXIF.APERTURE + string.format("%.0f", image.exif_focal_length), -- EXIF.FOCAL.LENGTH + image.exif_focus_distance, -- EXIF.FOCUS.DISTANCE + image.longitude or "", -- LONGITUDE + image.latitude or "", -- LATITUDE + image.elevation or "", -- ALTITUDE + image.rating, -- STARS + "", -- RATING.ICONS + labels, -- LABELS + "", -- LABELS.ICONS + image.exif_maker, -- MAKER + image.exif_model, -- MODEL + image.title, -- TITLE + image.description, -- DESCRIPTION + image.creator, -- CREATOR + image.publisher, -- PUBLISHER + image.rights, -- RIGHTS + "", -- TAGS + "", -- CATEGORYn + "", -- SIDECAR.TXT + pictures_folder, -- FOLDER.PICTURES + home_folder, -- FOLDER.HOME + desktop_folder, -- FOLDER.DESKTOP + "", -- OPENCL.ACTIVATED + user_name, -- USERNAME + "\n", -- NL + "" -- JOBCODE + } + + for i = 1, #PLACEHOLDERS, 1 do + substitutes[PLACEHOLDERS[i]] = replacements[i] + log.msg(log.info, "setting " .. PLACEHOLDERS[i] .. " to " .. tostring(replacements[i])) + end + log.log_level(old_log_level) +end + +local function check_legacy_vars(var_name) + local var = var_name + if string.match(var, "_") then + var = string.gsub(var, "_", ".") + end + if string.match(var, "^HOME$") then var = "FOLDER.HOME" end + if string.match(var, "^PICTURES.FOLDER$") then var = "FOLDER.PICTURES" end + if string.match(var, "^DESKTOP$") then var = "FOLDER.DESKTOP" end + return var +end + +local function treat(var_string) + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) + local ret_val = "" + -- remove the var from the string + local var = string.match(var_string, "[%a%._]+") + var = check_legacy_vars(var) + log.msg(log.info, "var_string is " .. tostring(var_string) .. " and var is " .. tostring(var)) + ret_val = substitutes[var] + if not ret_val then + log.msg(log.error, "variable " .. var .. " is not an allowed variable, returning empty value") + log.log_level(old_log_level) + return "" + end + local args = string.gsub(var_string, var, "") + log.msg(log.info, "args is " .. tostring(args)) + if string.len(args) > 0 then + if string.match(args, '^%^%^') then + ret_val = string.upper(ret_val) + elseif string.match(args, "^%^") then + ret_val = string.gsub(ret_val, "^%a", string.upper, 1) + elseif string.match(args, "^,,") then + ret_val = string.lower(ret_val) + elseif string.match(args, "^,") then + ret_val = string.gsub(ret_val, "^%a", string.lower, 1) + elseif string.match(args, "^:%-?%d+:%-?%d+") then + local soffset, slen = string.match(args, ":(%-?%d+):(%-?%d+)") + log.msg(log.info, "soffset is " .. soffset .. " and slen is " .. slen) + if tonumber(soffset) >= 0 then + soffset = soffset + 1 + end + log.msg(log.info, "soffset is " .. soffset .. " and slen is " .. slen) + if tonumber(soffset) < 0 and tonumber(slen) < 0 then + local temp = soffset + soffset = slen + slen = temp + end + log.msg(log.info, "soffset is " .. soffset .. " and slen is " .. slen) + ret_val = string.sub(ret_val, soffset, slen) + log.msg(log.info, "ret_val is " .. ret_val) + elseif string.match(args, "^:%-?%d+") then + local soffset= string.match(args, ":(%-?%d+)") + if tonumber(soffset) >= 0 then + soffset = soffset + 1 + end + ret_val = string.sub(ret_val, soffset, -1) + elseif string.match(args, "^-%$%(.-%)") then + local replacement = string.match(args, "-%$%(([%a%._]+)%)") + replacement = check_legacy_vars(replacement) + if string.len(ret_val) == 0 then + ret_val = substitutes[replacement] + end + elseif string.match(args, "^-.+$") then + local replacement = string.match(args, "-(.+)$") + if string.len(ret_val) == 0 then + ret_val = replacement + end + elseif string.match(args, "^+.+") then + local replacement = string.match(args, "+(.+)") + if string.len(ret_val) > 0 then + ret_val = replacement + end + elseif string.match(args, "^#.+") then + local pattern = string.match(args, "#(.+)") + log.msg(log.info, "pattern to remove is " .. tostring(pattern)) + ret_val = string.gsub(ret_val, "^" .. dtutils_string.sanitize_lua(pattern), "") + elseif string.match(args, "^%%.+") then + local pattern = string.match(args, "%%(.+)") + ret_val = string.gsub(ret_val, pattern .. "$", "") + elseif string.match(args, "^//.-/.+") then + local pattern, replacement = string.match(args, "//(.-)/(.+)") + ret_val = string.gsub(ret_val, pattern, replacement) + elseif string.match(args, "^/#.+/.+") then + local pattern, replacement = string.match(args, "/#(.+)/(.+)") + ret_val = string.gsub(ret_val, "^" .. pattern, replacement, 1) + elseif string.match(args, "^/%%.-/.+") then + local pattern, replacement = string.match(args, "/%%(.-)/(.+)") + ret_val = string.gsub(ret_val, pattern .. "$", replacement) + elseif string.match(args, "^/.-/.+") then + log.msg(log.info, "took replacement branch") + local pattern, replacement = string.match(args, "/(.-)/(.+)") + ret_val = string.gsub(ret_val, pattern, replacement, 1) + end + end + log.log_level(old_log_level) + return ret_val +end + +dtutils_string.libdoc.functions["substitute_list"] = { + Name = [[substitute_list]], + Synopsis = [[Replace variables in a string with their computed values]], + Usage = [[local ds = require "lib/dtutils.string" + local result = ds.substitute_list(str) + str - string - the string containing the variables to be substituted for]], + Description = [[substitute_list replaces the variables in the supplied string with + values computed in build_substitution_list().]], + Return_Value = [[result - string - the input string with values substituted for the variables]], + Limitations = [[]], + Example = [[]], + See_Also = [[]], + Reference = [[]], + License = [[]], + Copyright = [[]], +} + +function dtutils_string.substitute_list(str) + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) + -- replace the substitution variables in a string + for match in string.gmatch(str, "%$%(.-%)?%)") do + local var = string.match(match, "%$%((.-%)?)%)") + local treated_var = treat(var) + log.msg(log.info, "var is " .. var .. " and treated var is " .. tostring(treated_var)) + str = string.gsub(str, "%$%(".. dtutils_string.sanitize_lua(var) .."%)", tostring(treated_var)) + log.msg(log.info, "str after replacement is " .. str) + end + log.log_level(old_log_level) + return str +end + +dtutils_string.libdoc.functions["clear_substitute_list"] = { + Name = [[clear_substitute_list]], + Synopsis = [[Clear the computed list of variable substitution values]], + Usage = [[local ds = require "lib/dtutils.string" + ds.clear_substitute_list()]], + Description = [[clear_substitute_list resets the list of variable replacement values]], + Return_Value = [[]], + Limitations = [[]], + Example = [[]], + See_Also = [[]], + Reference = [[]], + License = [[]], + Copyright = [[]], +} + +function dtutils_string.clear_substitute_list() + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) + for i = 1, #PLACEHOLDERS, 1 do + substitutes[PLACEHOLDERS[i]] = nil + end + log.log_level(old_log_level) +end + +dtutils_string.libdoc.functions["substitute"] = { + Name = [[substitute]], + Synopsis = [[Check if a string has been sanitized]], + Usage = [[local ds = require "lib/dtutils.string" + ds.substitute(image, sequence, [username], [pic_folder], [home], [desktop]) + image - dt_lua_image_t - the image being processed + sequence - integer - the sequence number of the image + [username] - string - optional - user name. Will be determined if not supplied + [pic_folder] - string - optional - pictures folder name. Will be determined if not supplied + [home] - string - optional - home directory. Will be determined if not supplied + [desktop] - string - optional - desktop directory. Will be determined if not supplied]], + Description = [[substitute initializes the substitution list by calling clear_substitute_list(), + then builds the substitutions by calling build_substitution_list() and finally does the + substitution by calling substitute_list(), then returns the result string.]], + Return_Value = [[result - string - the input string with values substituted for the variables]], + Limitations = [[]], + Example = [[]], + See_Also = [[]], + Reference = [[]], + License = [[]], + Copyright = [[]], +} + +function dtutils_string.substitute(image, sequence, variable_string, username, pic_folder, home, desktop) + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) + dtutils_string.clear_substitute_list() + dtutils_string.build_substition_list(image, sequence, username, pic_folder, home, desktop) + local str = dtutils_string.substitute_list(variable_string) + log.log_level(old_log_level) + return str +end + return dtutils_string From 3e2afffed82ff9488c0805bcfd389d8361269c7c Mon Sep 17 00:00:00 2001 From: Bill Ferguson Date: Sat, 18 May 2024 21:59:11 -0400 Subject: [PATCH 04/10] lib/dtutils/string - Added categories to substitution functionality. This makes it feature complete. Some substitutions aren't implemented since the current use case for this is creating export directories. The results from the not implemented substitutions wouldn't work well in path/file names (such as comma separated lists or newlines). --- lib/dtutils/string.lua | 291 +++++++++++++++++++++++++++-------------- 1 file changed, 195 insertions(+), 96 deletions(-) diff --git a/lib/dtutils/string.lua b/lib/dtutils/string.lua index e315eda..1cece46 100644 --- a/lib/dtutils/string.lua +++ b/lib/dtutils/string.lua @@ -477,6 +477,7 @@ function dtutils_string.get_filetype(str) end local substitutes = {} +local category_substitutes = {} -- - - - - - - - - - - - - - - - - - - - - - - - -- C O N S T A N T S @@ -488,7 +489,7 @@ local PLACEHOLDERS = {"ROLL.NAME", "FILE.EXTENSION", "ID", "VERSION", - "VERSION.IF.MULTI", -- Not Implemented + "VERSION.IF.MULTI", "VERSION.NAME", "DARKTABLE.VERSION", "DARKTABLE.NAME", -- Not Implemented @@ -504,14 +505,20 @@ local PLACEHOLDERS = {"ROLL.NAME", "WIDTH.MAX", -- Not Implemented "HEIGHT.MAX", -- Not Implemented "YEAR", + "YEAR.SHORT", "MONTH", + "MONTH.LONG", + "MONTH.SHORT", "DAY", "HOUR", "MINUTE", "SECOND", "MSEC", "EXIF.YEAR", + "EXIF.YEAR.SHORT", "EXIF.MONTH", + "EXIF.MONTH.LONG", + "EXIF.MONTH.SHORT", "EXIF.DAY", "EXIF.HOUR", "EXIF.MINUTE", @@ -523,17 +530,22 @@ local PLACEHOLDERS = {"ROLL.NAME", "EXIF.EXPOSURE", "EXIF.EXPOSURE.BIAS", "EXIF.APERTURE", + "EXIF.CROP.FACTOR", "EXIF.FOCAL.LENGTH", + "EXIF.FOCAL.LENGTH.EQUIV", -- Not Implemented "EXIF.FOCUS.DISTANCE", + "IMAGE.EXIF", -- Not Implemented "LONGITUDE", "LATITUDE", - "ALTITUDE", + "ELEVATION", + "GPS.LOCATION", -- Not Implemented "STARS", "RATING.ICONS", -- Not Implemented "LABELS", "LABELS.ICONS", -- Not Implemented "MAKER", "MODEL", + "LENS", "TITLE", "DESCRIPTION", "CREATOR", @@ -547,7 +559,7 @@ local PLACEHOLDERS = {"ROLL.NAME", "FOLDER.DESKTOP", "OPENCL.ACTIVATED", -- Not Implemented "USERNAME", - "NL", + "NL", -- Not Implemented "JOBCODE" -- Not Implemented } @@ -560,14 +572,19 @@ local DESKTOP = HOME .. PS .. "Desktop" local function get_colorlabels(image) local old_log_level = log.log_level() log.log_level(dtutils_string.log_level) + local colorlabels = {} + if image.red then table.insert(colorlabels, "red") end if image.yellow then table.insert(colorlabels, "yellow") end if image.green then table.insert(colorlabels, "green") end if image.blue then table.insert(colorlabels, "blue") end if image.purple then table.insert(colorlabels, "purple") end - local labels = #colorlabels == 1 and colorlabels[1] or du.join(colorlabels, ",") + + local labels = #colorlabels == 1 and colorlabels[1] or du.join(colorlabels, "_") + log.log_level(old_log_level) + return labels end @@ -575,9 +592,10 @@ dtutils_string.libdoc.functions["build_substitution_list"] = { Name = [[build_substitution_list]], Synopsis = [[build a list of variable substitutions]], Usage = [[local ds = require "lib/dtutils.string" - ds.build_substitution_list(image, sequence, [username], [pic_folder], [home], [desktop]) + ds.build_substitution_list(image, sequence, variable_string, [username], [pic_folder], [home], [desktop]) image - dt_lua_image_t - the image being processed sequence - integer - the sequence number of the image + variable_string - string - the substitution variable string [username] - string - optional - user name. Will be determined if not supplied [pic_folder] - string - optional - pictures folder name. Will be determined if not supplied [home] - string - optional - home directory. Will be determined if not supplied @@ -594,7 +612,46 @@ dtutils_string.libdoc.functions["build_substitution_list"] = { Copyright = [[]], } -function dtutils_string.build_substition_list(image, sequence, username, pic_folder, home, desktop) +local function build_category_substitution_list(image, variable_string) + -- scan the variable string for CATEGORYn(tag name) entries and build the substitutions + local old_log_level = log.log_level() + -- log.log_level(dtutils_string.log_level) + --log.log_level(log.debug) + + for match in string.gmatch(variable_string, "%$%(.-%)?%)") do + log.msg(log.info, "match is " .. match) + local var = string.match(match, "%$%((.-%)?)%)") + log.msg(log.info, "var is " .. var) + if string.match(var, "CATEGORY%d") then + local element, tag = string.match(var, "CATEGORY(%d)%((.-)%)") + element = element + 1 + log.msg(log.debug, "element is " .. element .. " and tag is " .. tag) + local tags = image:get_tags() + log.msg(log.debug, "got " .. #tags .. " from image " .. image.filename) + for _, image_tag in ipairs(tags) do + log.msg(log.debug, "checking tag " .. image_tag.name) + if string.match(image_tag.name, tag) then + parts = du.split(image_tag.name, "|") + substitutes[var] = parts[element] + log.msg(log.info, "set substitute for " .. var .. " to " .. parts[element]) + log.msg(log.info, "double check substitutes[" .. var .. "] is " .. substitutes[var]) + end + end + end + end + -- scan the variable string looking for CATEGORYn + -- retrieve the tag and compute the replacement string + -- add the found string as a PLACEHOLDER + -- add the replacement to the corresponding spot in the replacement table + log.log_level(old_log_level) +end + +local function exiftime2systime(exiftime) + local yr,mo,dy,h,m,s = string.match(exiftime, "(%d-):(%d-):(%d-) (%d-):(%d-):(%d+)") + return(os.time{year=yr, month=mo, day=dy, hour=h, min=m, sec=s}) +end + +function dtutils_string.build_substition_list(image, sequence, variable_string, username, pic_folder, home, desktop) -- build the argument substitution list from each image local old_log_level = log.log_level() @@ -606,6 +663,8 @@ function dtutils_string.build_substition_list(image, sequence, username, pic_fol end local datetime = os.date("*t") + local long_month = os.date("%B") + local short_month = os.date("%b") local user_name = username or USER local pictures_folder = pic_folder or PICTURES local home_folder = home or HOME @@ -629,7 +688,8 @@ function dtutils_string.build_substition_list(image, sequence, username, pic_fol dtutils_string.get_filetype(image.filename),-- FILE.EXTENSION image.id, -- ID image.duplicate_index, -- VERSION - "", -- VERSION.IF_MULTI + -- #image:get_group_members() > 1 and image.duplicate_index or "", -- VERSION.IF_MULTI + "", -- VERSION.IF_MULTI image.version_name, -- VERSION.NAME dt.configuration.version, -- DARKTABLE.VERSION "", -- DARKTABLE.NAME @@ -645,57 +705,71 @@ function dtutils_string.build_substition_list(image, sequence, username, pic_fol "", -- WIDTH.MAX "", -- HEIGHT.MAX string.format("%4d", datetime.year), -- YEAR + string.sub(datetime.year, 3), -- YEAR.SHORT string.format("%02d", datetime.month), -- MONTH + long_month, -- MONTH.LONG + short_month, -- MONTH.SHORT string.format("%02d", datetime.day), -- DAY string.format("%02d", datetime.hour), -- HOUR string.format("%02d", datetime.min), -- MINUTE string.format("%02d", datetime.sec), -- SECOND - "", -- MSEC + 0, -- MSEC eyear, -- EXIF.YEAR + string.sub(eyear, 3), -- EXIF.YEAR.SHORT emon, -- EXIF.MONTH + os.date("%B", exiftime2systime(image.exif_datetime_taken)), -- EXIF.MONTH.LONG + os.date("%b", exiftime2systime(image.exif_datetime_taken)), -- EXIF.MONTH.SHORT eday, -- EXIF.DAY ehour, -- EXIF.HOUR emin, -- EXIF.MINUTE esec, -- EXIF.SECOND emsec, -- EXIF.MSEC - "", -- EXIF.DATE.REGIONAL - "", -- EXIF.TIME.REGIONAL + "", -- EXIF.DATE.REGIONAL - wont be implemented + "", -- EXIF.TIME.REGIONAL - wont be implemented string.format("%d", image.exif_iso), -- EXIF.ISO - string.format("1/%.0f", 1./image.exif_exposure), -- EXIF.EXPOSURE + string.format("%.0f", 1./image.exif_exposure), -- EXIF.EXPOSURE image.exif_exposure_bias, -- EXIF.EXPOSURE.BIAS string.format("%.01f", image.exif_aperture), -- EXIF.APERTURE + string.format("%.01f", image.exif_crop),-- EXIF.CROP_FACTOR string.format("%.0f", image.exif_focal_length), -- EXIF.FOCAL.LENGTH + string.format("%.0f", image.exif_focal_length * image.exif_crop), -- EXIF.FOCAL.LENGTH.EQUIV image.exif_focus_distance, -- EXIF.FOCUS.DISTANCE + "", -- IMAGE.EXIF image.longitude or "", -- LONGITUDE image.latitude or "", -- LATITUDE - image.elevation or "", -- ALTITUDE + image.elevation or "", -- ELEVATION + "", -- GPS.LOCATION - wont be implemented image.rating, -- STARS - "", -- RATING.ICONS + "", -- RATING.ICONS - wont be implemented labels, -- LABELS - "", -- LABELS.ICONS + "", -- LABELS.ICONS - wont be implemented image.exif_maker, -- MAKER image.exif_model, -- MODEL + image.exif_lens, -- LENS image.title, -- TITLE image.description, -- DESCRIPTION image.creator, -- CREATOR image.publisher, -- PUBLISHER image.rights, -- RIGHTS - "", -- TAGS - "", -- CATEGORYn - "", -- SIDECAR.TXT + "", -- TAGS - wont be implemented + "", -- CATEGORY + "", -- SIDECAR.TXT - wont be implemented pictures_folder, -- FOLDER.PICTURES home_folder, -- FOLDER.HOME desktop_folder, -- FOLDER.DESKTOP - "", -- OPENCL.ACTIVATED + "", -- OPENCL.ACTIVATED - wont be implemented user_name, -- USERNAME - "\n", -- NL - "" -- JOBCODE + "", -- NL - wont be implemented + "" -- JOBCODE - wont be implemented } for i = 1, #PLACEHOLDERS, 1 do substitutes[PLACEHOLDERS[i]] = replacements[i] log.msg(log.info, "setting " .. PLACEHOLDERS[i] .. " to " .. tostring(replacements[i])) end + + build_category_substitution_list(image, variable_string) + log.log_level(old_log_level) end @@ -712,86 +786,105 @@ end local function treat(var_string) local old_log_level = log.log_level() - log.log_level(dtutils_string.log_level) + --log.log_level(dtutils_string.log_level) + --log.log_level(log.info) local ret_val = "" -- remove the var from the string local var = string.match(var_string, "[%a%._]+") var = check_legacy_vars(var) log.msg(log.info, "var_string is " .. tostring(var_string) .. " and var is " .. tostring(var)) - ret_val = substitutes[var] - if not ret_val then + if string.match(var_string, "CATEGORY%d") then + log.msg(log.info, "substituting for " .. var_string) + ret_val = substitutes[var_string] + log.msg(log.info, "ret_val is " .. ret_val) + else + ret_val = substitutes[var] + end + local valid_var = false + + if ret_val then + valid_var = true + --elseif string.match(var, "CATEGORY%d") then + --valid_var = true + end + + if not valid_var then log.msg(log.error, "variable " .. var .. " is not an allowed variable, returning empty value") log.log_level(old_log_level) return "" end - local args = string.gsub(var_string, var, "") - log.msg(log.info, "args is " .. tostring(args)) - if string.len(args) > 0 then - if string.match(args, '^%^%^') then - ret_val = string.upper(ret_val) - elseif string.match(args, "^%^") then - ret_val = string.gsub(ret_val, "^%a", string.upper, 1) - elseif string.match(args, "^,,") then - ret_val = string.lower(ret_val) - elseif string.match(args, "^,") then - ret_val = string.gsub(ret_val, "^%a", string.lower, 1) - elseif string.match(args, "^:%-?%d+:%-?%d+") then - local soffset, slen = string.match(args, ":(%-?%d+):(%-?%d+)") - log.msg(log.info, "soffset is " .. soffset .. " and slen is " .. slen) - if tonumber(soffset) >= 0 then - soffset = soffset + 1 + if string.match(var, "CATEGORY%d") then + ret_val = process_category(image, var, var_string) + else + local args = string.gsub(var_string, var, "") + log.msg(log.info, "args is " .. tostring(args)) + if string.len(args) > 0 then + if string.match(args, '^%^%^') then + ret_val = string.upper(ret_val) + elseif string.match(args, "^%^") then + ret_val = string.gsub(ret_val, "^%a", string.upper, 1) + elseif string.match(args, "^,,") then + ret_val = string.lower(ret_val) + elseif string.match(args, "^,") then + ret_val = string.gsub(ret_val, "^%a", string.lower, 1) + elseif string.match(args, "^:%-?%d+:%-?%d+") then + local soffset, slen = string.match(args, ":(%-?%d+):(%-?%d+)") + log.msg(log.info, "soffset is " .. soffset .. " and slen is " .. slen) + if tonumber(soffset) >= 0 then + soffset = soffset + 1 + end + log.msg(log.info, "soffset is " .. soffset .. " and slen is " .. slen) + if tonumber(soffset) < 0 and tonumber(slen) < 0 then + local temp = soffset + soffset = slen + slen = temp + end + log.msg(log.info, "soffset is " .. soffset .. " and slen is " .. slen) + ret_val = string.sub(ret_val, soffset, slen) + log.msg(log.info, "ret_val is " .. ret_val) + elseif string.match(args, "^:%-?%d+") then + local soffset= string.match(args, ":(%-?%d+)") + if tonumber(soffset) >= 0 then + soffset = soffset + 1 + end + ret_val = string.sub(ret_val, soffset, -1) + elseif string.match(args, "^-%$%(.-%)") then + local replacement = string.match(args, "-%$%(([%a%._]+)%)") + replacement = check_legacy_vars(replacement) + if string.len(ret_val) == 0 then + ret_val = substitutes[replacement] + end + elseif string.match(args, "^-.+$") then + local replacement = string.match(args, "-(.+)$") + if string.len(ret_val) == 0 then + ret_val = replacement + end + elseif string.match(args, "^+.+") then + local replacement = string.match(args, "+(.+)") + if string.len(ret_val) > 0 then + ret_val = replacement + end + elseif string.match(args, "^#.+") then + local pattern = string.match(args, "#(.+)") + log.msg(log.info, "pattern to remove is " .. tostring(pattern)) + ret_val = string.gsub(ret_val, "^" .. dtutils_string.sanitize_lua(pattern), "") + elseif string.match(args, "^%%.+") then + local pattern = string.match(args, "%%(.+)") + ret_val = string.gsub(ret_val, pattern .. "$", "") + elseif string.match(args, "^//.-/.+") then + local pattern, replacement = string.match(args, "//(.-)/(.+)") + ret_val = string.gsub(ret_val, pattern, replacement) + elseif string.match(args, "^/#.+/.+") then + local pattern, replacement = string.match(args, "/#(.+)/(.+)") + ret_val = string.gsub(ret_val, "^" .. pattern, replacement, 1) + elseif string.match(args, "^/%%.-/.+") then + local pattern, replacement = string.match(args, "/%%(.-)/(.+)") + ret_val = string.gsub(ret_val, pattern .. "$", replacement) + elseif string.match(args, "^/.-/.+") then + log.msg(log.info, "took replacement branch") + local pattern, replacement = string.match(args, "/(.-)/(.+)") + ret_val = string.gsub(ret_val, pattern, replacement, 1) end - log.msg(log.info, "soffset is " .. soffset .. " and slen is " .. slen) - if tonumber(soffset) < 0 and tonumber(slen) < 0 then - local temp = soffset - soffset = slen - slen = temp - end - log.msg(log.info, "soffset is " .. soffset .. " and slen is " .. slen) - ret_val = string.sub(ret_val, soffset, slen) - log.msg(log.info, "ret_val is " .. ret_val) - elseif string.match(args, "^:%-?%d+") then - local soffset= string.match(args, ":(%-?%d+)") - if tonumber(soffset) >= 0 then - soffset = soffset + 1 - end - ret_val = string.sub(ret_val, soffset, -1) - elseif string.match(args, "^-%$%(.-%)") then - local replacement = string.match(args, "-%$%(([%a%._]+)%)") - replacement = check_legacy_vars(replacement) - if string.len(ret_val) == 0 then - ret_val = substitutes[replacement] - end - elseif string.match(args, "^-.+$") then - local replacement = string.match(args, "-(.+)$") - if string.len(ret_val) == 0 then - ret_val = replacement - end - elseif string.match(args, "^+.+") then - local replacement = string.match(args, "+(.+)") - if string.len(ret_val) > 0 then - ret_val = replacement - end - elseif string.match(args, "^#.+") then - local pattern = string.match(args, "#(.+)") - log.msg(log.info, "pattern to remove is " .. tostring(pattern)) - ret_val = string.gsub(ret_val, "^" .. dtutils_string.sanitize_lua(pattern), "") - elseif string.match(args, "^%%.+") then - local pattern = string.match(args, "%%(.+)") - ret_val = string.gsub(ret_val, pattern .. "$", "") - elseif string.match(args, "^//.-/.+") then - local pattern, replacement = string.match(args, "//(.-)/(.+)") - ret_val = string.gsub(ret_val, pattern, replacement) - elseif string.match(args, "^/#.+/.+") then - local pattern, replacement = string.match(args, "/#(.+)/(.+)") - ret_val = string.gsub(ret_val, "^" .. pattern, replacement, 1) - elseif string.match(args, "^/%%.-/.+") then - local pattern, replacement = string.match(args, "/%%(.-)/(.+)") - ret_val = string.gsub(ret_val, pattern .. "$", replacement) - elseif string.match(args, "^/.-/.+") then - log.msg(log.info, "took replacement branch") - local pattern, replacement = string.match(args, "/(.-)/(.+)") - ret_val = string.gsub(ret_val, pattern, replacement, 1) end end log.log_level(old_log_level) @@ -848,9 +941,10 @@ dtutils_string.libdoc.functions["clear_substitute_list"] = { function dtutils_string.clear_substitute_list() local old_log_level = log.log_level() log.log_level(dtutils_string.log_level) - for i = 1, #PLACEHOLDERS, 1 do - substitutes[PLACEHOLDERS[i]] = nil - end + + substitutes = {} + category_substitutes = {} + log.log_level(old_log_level) end @@ -858,9 +952,10 @@ dtutils_string.libdoc.functions["substitute"] = { Name = [[substitute]], Synopsis = [[Check if a string has been sanitized]], Usage = [[local ds = require "lib/dtutils.string" - ds.substitute(image, sequence, [username], [pic_folder], [home], [desktop]) + ds.substitute(image, sequence, variable_string, [username], [pic_folder], [home], [desktop]) image - dt_lua_image_t - the image being processed sequence - integer - the sequence number of the image + variable_string - string - the substitution variable string [username] - string - optional - user name. Will be determined if not supplied [pic_folder] - string - optional - pictures folder name. Will be determined if not supplied [home] - string - optional - home directory. Will be determined if not supplied @@ -880,13 +975,17 @@ dtutils_string.libdoc.functions["substitute"] = { function dtutils_string.substitute(image, sequence, variable_string, username, pic_folder, home, desktop) local old_log_level = log.log_level() log.log_level(dtutils_string.log_level) + dtutils_string.clear_substitute_list() - dtutils_string.build_substition_list(image, sequence, username, pic_folder, home, desktop) + + dtutils_string.build_substition_list(image, sequence, variable_string, username, pic_folder, home, desktop) + local str = dtutils_string.substitute_list(variable_string) + log.log_level(old_log_level) + return str end - return dtutils_string From 7b2018412a7180c09ab502062dac2d8939c8dec0 Mon Sep 17 00:00:00 2001 From: Bill Ferguson Date: Sat, 18 May 2024 22:01:10 -0400 Subject: [PATCH 05/10] lib/dtutils/string - Cleaned up code to make it more readable. Fixed VERSION_IF_MULTI. Added safety check in category to make sure we don't try and read a tag field that doesn't exist. --- lib/dtutils/string.lua | 495 +++++++++++++++++++++++------------------ 1 file changed, 279 insertions(+), 216 deletions(-) diff --git a/lib/dtutils/string.lua b/lib/dtutils/string.lua index 1cece46..dad0d55 100644 --- a/lib/dtutils/string.lua +++ b/lib/dtutils/string.lua @@ -476,118 +476,6 @@ function dtutils_string.get_filetype(str) return parts["filetype"] end -local substitutes = {} -local category_substitutes = {} - --- - - - - - - - - - - - - - - - - - - - - - - - --- C O N S T A N T S --- - - - - - - - - - - - - - - - - - - - - - - - - -local PLACEHOLDERS = {"ROLL.NAME", - "FILE.FOLDER", - "FILE.NAME", - "FILE.EXTENSION", - "ID", - "VERSION", - "VERSION.IF.MULTI", - "VERSION.NAME", - "DARKTABLE.VERSION", - "DARKTABLE.NAME", -- Not Implemented - "SEQUENCE", - "WIDTH.SENSOR", - "HEIGHT.SENSOR", - "WIDTH.RAW", - "HEIGHT.RAW", - "WIDTH.CROP", - "HEIGHT.CROP", - "WIDTH.EXPORT", - "HEIGHT.EXPORT", - "WIDTH.MAX", -- Not Implemented - "HEIGHT.MAX", -- Not Implemented - "YEAR", - "YEAR.SHORT", - "MONTH", - "MONTH.LONG", - "MONTH.SHORT", - "DAY", - "HOUR", - "MINUTE", - "SECOND", - "MSEC", - "EXIF.YEAR", - "EXIF.YEAR.SHORT", - "EXIF.MONTH", - "EXIF.MONTH.LONG", - "EXIF.MONTH.SHORT", - "EXIF.DAY", - "EXIF.HOUR", - "EXIF.MINUTE", - "EXIF.SECOND", - "EXIF.MSEC", - "EXIF.DATE.REGIONAL", -- Not Implemented - "EXIF.TIME.REGIONAL", -- Not Implemented - "EXIF.ISO", - "EXIF.EXPOSURE", - "EXIF.EXPOSURE.BIAS", - "EXIF.APERTURE", - "EXIF.CROP.FACTOR", - "EXIF.FOCAL.LENGTH", - "EXIF.FOCAL.LENGTH.EQUIV", -- Not Implemented - "EXIF.FOCUS.DISTANCE", - "IMAGE.EXIF", -- Not Implemented - "LONGITUDE", - "LATITUDE", - "ELEVATION", - "GPS.LOCATION", -- Not Implemented - "STARS", - "RATING.ICONS", -- Not Implemented - "LABELS", - "LABELS.ICONS", -- Not Implemented - "MAKER", - "MODEL", - "LENS", - "TITLE", - "DESCRIPTION", - "CREATOR", - "PUBLISHER", - "RIGHTS", - "TAGS", -- Not Implemented - "CATEGORY", -- Not Implemented - "SIDECAR.TXT", -- Not Implemented - "FOLDER.PICTURES", - "FOLDER.HOME", - "FOLDER.DESKTOP", - "OPENCL.ACTIVATED", -- Not Implemented - "USERNAME", - "NL", -- Not Implemented - "JOBCODE" -- Not Implemented -} - -local PS = dt.configuration.running_os == "windows" and "\\" or "/" -local USER = os.getenv("USERNAME") -local HOME = dt.configuration.running_os == "windows" and os.getenv("HOMEPATH") or os.getenv("HOME") -local PICTURES = HOME .. PS .. (dt.configuration.running_os == "windows" and "My Pictures" or "Pictures") -local DESKTOP = HOME .. PS .. "Desktop" - -local function get_colorlabels(image) - local old_log_level = log.log_level() - log.log_level(dtutils_string.log_level) - - local colorlabels = {} - - if image.red then table.insert(colorlabels, "red") end - if image.yellow then table.insert(colorlabels, "yellow") end - if image.green then table.insert(colorlabels, "green") end - if image.blue then table.insert(colorlabels, "blue") end - if image.purple then table.insert(colorlabels, "purple") end - - local labels = #colorlabels == 1 and colorlabels[1] or du.join(colorlabels, "_") - - log.log_level(old_log_level) - - return labels -end - dtutils_string.libdoc.functions["build_substitution_list"] = { Name = [[build_substitution_list]], Synopsis = [[build a list of variable substitutions]], @@ -612,51 +500,175 @@ dtutils_string.libdoc.functions["build_substitution_list"] = { Copyright = [[]], } -local function build_category_substitution_list(image, variable_string) - -- scan the variable string for CATEGORYn(tag name) entries and build the substitutions - local old_log_level = log.log_level() - -- log.log_level(dtutils_string.log_level) - --log.log_level(log.debug) +local substitutes = {} +local category_substitutes = {} - for match in string.gmatch(variable_string, "%$%(.-%)?%)") do +-- - - - - - - - - - - - - - - - - - - - - - - - +-- C O N S T A N T S +-- - - - - - - - - - - - - - - - - - - - - - - - + +local PLACEHOLDERS = {"ROLL.NAME", + "FILE.FOLDER", + "FILE.NAME", + "FILE.EXTENSION", + "ID", + "VERSION", + "VERSION.IF.MULTI", + "VERSION.NAME", + "DARKTABLE.VERSION", + "DARKTABLE.NAME", -- Not Implemented + "SEQUENCE", + "WIDTH.SENSOR", + "HEIGHT.SENSOR", + "WIDTH.RAW", + "HEIGHT.RAW", + "WIDTH.CROP", + "HEIGHT.CROP", + "WIDTH.EXPORT", + "HEIGHT.EXPORT", + "WIDTH.MAX", -- Not Implemented + "HEIGHT.MAX", -- Not Implemented + "YEAR", + "YEAR.SHORT", + "MONTH", + "MONTH.LONG", + "MONTH.SHORT", + "DAY", + "HOUR", + "MINUTE", + "SECOND", + "MSEC", + "EXIF.YEAR", + "EXIF.YEAR.SHORT", + "EXIF.MONTH", + "EXIF.MONTH.LONG", + "EXIF.MONTH.SHORT", + "EXIF.DAY", + "EXIF.HOUR", + "EXIF.MINUTE", + "EXIF.SECOND", + "EXIF.MSEC", + "EXIF.DATE.REGIONAL", -- Not Implemented + "EXIF.TIME.REGIONAL", -- Not Implemented + "EXIF.ISO", + "EXIF.EXPOSURE", + "EXIF.EXPOSURE.BIAS", + "EXIF.APERTURE", + "EXIF.CROP.FACTOR", + "EXIF.FOCAL.LENGTH", + "EXIF.FOCAL.LENGTH.EQUIV", -- Not Implemented + "EXIF.FOCUS.DISTANCE", + "IMAGE.EXIF", -- Not Implemented + "LONGITUDE", + "LATITUDE", + "ELEVATION", + "GPS.LOCATION", -- Not Implemented + "STARS", + "RATING.ICONS", -- Not Implemented + "LABELS", + "LABELS.ICONS", -- Not Implemented + "MAKER", + "MODEL", + "LENS", + "TITLE", + "DESCRIPTION", + "CREATOR", + "PUBLISHER", + "RIGHTS", + "TAGS", -- Not Implemented + "SIDECAR.TXT", -- Not Implemented + "FOLDER.PICTURES", + "FOLDER.HOME", + "FOLDER.DESKTOP", + "OPENCL.ACTIVATED", -- Not Implemented + "USERNAME", + "NL", -- Not Implemented + "JOBCODE" -- Not Implemented +} + +local PS = dt.configuration.running_os == "windows" and "\\" or "/" +local USER = os.getenv("USERNAME") +local HOME = dt.configuration.running_os == "windows" and os.getenv("HOMEPATH") or os.getenv("HOME") +local PICTURES = HOME .. PS .. (dt.configuration.running_os == "windows" and "My Pictures" or "Pictures") +local DESKTOP = HOME .. PS .. "Desktop" + +local function get_colorlabels(image) + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) + + local colorlabels = {} + + if image.red then table.insert(colorlabels, "red") end + if image.yellow then table.insert(colorlabels, "yellow") end + if image.green then table.insert(colorlabels, "green") end + if image.blue then table.insert(colorlabels, "blue") end + if image.purple then table.insert(colorlabels, "purple") end + + local labels = #colorlabels == 1 and colorlabels[1] or du.join(colorlabels, "_") + + log.log_level(old_log_level) + + return labels +end + +-- find the $CATEGORYn requests and add them to the substitute list + +local function build_category_substitution_list(image, variable_string) + local old_log_level = log.log_level() + log.log_level(dtutils_string.log_level) + + for match in string.gmatch(variable_string, "%$%(.-%)?%)") do -- grab each complete variable log.msg(log.info, "match is " .. match) - local var = string.match(match, "%$%((.-%)?)%)") + + local var = string.match(match, "%$%((.-%)?)%)") -- strip of the leading $( and trailing ) log.msg(log.info, "var is " .. var) + if string.match(var, "CATEGORY%d") then - local element, tag = string.match(var, "CATEGORY(%d)%((.-)%)") - element = element + 1 + local element, tag = string.match(var, "CATEGORY(%d)%((.-)%)") -- get the element number and the tag to match + + element = element + 1 -- add one to element since lua arrays are 1 based log.msg(log.debug, "element is " .. element .. " and tag is " .. tag) + local tags = image:get_tags() log.msg(log.debug, "got " .. #tags .. " from image " .. image.filename) + for _, image_tag in ipairs(tags) do log.msg(log.debug, "checking tag " .. image_tag.name) + if string.match(image_tag.name, tag) then - parts = du.split(image_tag.name, "|") - substitutes[var] = parts[element] - log.msg(log.info, "set substitute for " .. var .. " to " .. parts[element]) - log.msg(log.info, "double check substitutes[" .. var .. "] is " .. substitutes[var]) + fields = du.split(image_tag.name, "|") + + if element <= #fields then + substitutes[var] = fields[element] + else + substitutes[var] = "" + log.msg(log.warn, "requested field for tag " .. tag .. " doesn't exist") + end + + log.msg(log.info, "set substitute for " .. var .. " to " .. fields[element]) + end end end end - -- scan the variable string looking for CATEGORYn - -- retrieve the tag and compute the replacement string - -- add the found string as a PLACEHOLDER - -- add the replacement to the corresponding spot in the replacement table log.log_level(old_log_level) end +-- convert image.exif_datetime_taken to system time + local function exiftime2systime(exiftime) local yr,mo,dy,h,m,s = string.match(exiftime, "(%d-):(%d-):(%d-) (%d-):(%d-):(%d+)") return(os.time{year=yr, month=mo, day=dy, hour=h, min=m, sec=s}) end -function dtutils_string.build_substition_list(image, sequence, variable_string, username, pic_folder, home, desktop) - -- build the argument substitution list from each image +-- build the argument substitution list from each image +function dtutils_string.build_substition_list(image, sequence, variable_string, username, pic_folder, home, desktop) local old_log_level = log.log_level() log.log_level(dtutils_string.log_level) + -- is time millisecond aware? Implemented in API 9.1.0 + local is_api_9_1 = true if dt.configuration.api_version_string < "9.1.0" then is_api_9_1 = false @@ -668,11 +680,10 @@ function dtutils_string.build_substition_list(image, sequence, variable_string, local user_name = username or USER local pictures_folder = pic_folder or PICTURES local home_folder = home or HOME - log.msg(log.info, "home is " .. tostring(home) .. " and HOME is " .. tostring(HOME) .. " and home_folder is " .. tostring(home_folder)) local desktop_folder = desktop or DESKTOP local labels = get_colorlabels(image) - log.msg(log.info, "image date time taken is " .. image.exif_datetime_taken) + local eyear, emon, eday, ehour, emin, esec, emsec if dt.preferences.read("darktable", "lighttable/ui/milliseconds", "bool") and is_api_9_1 then eyear, emon, eday, ehour, emin, esec, emsec = @@ -682,14 +693,16 @@ function dtutils_string.build_substition_list(image, sequence, variable_string, eyear, emon, eday, ehour, emin, esec = string.match(image.exif_datetime_taken, "(%d+):(%d+):(%d+) (%d+):(%d+):(%d+)$") end + + local version_multi = #image:get_group_members() > 1 and image.version or "" + local replacements = {image.film, -- ROLL.NAME image.path, -- FILE.FOLDER image.filename, -- FILE.NAME dtutils_string.get_filetype(image.filename),-- FILE.EXTENSION image.id, -- ID image.duplicate_index, -- VERSION - -- #image:get_group_members() > 1 and image.duplicate_index or "", -- VERSION.IF_MULTI - "", -- VERSION.IF_MULTI + version_multi, -- VERSION.IF_MULTI image.version_name, -- VERSION.NAME dt.configuration.version, -- DARKTABLE.VERSION "", -- DARKTABLE.NAME @@ -698,12 +711,12 @@ function dtutils_string.build_substition_list(image, sequence, variable_string, image.height, -- HEIGHT.SENSOR is_api_9_1 and image.p_width or "", -- WIDTH.RAW is_api_9_1 and image.p_height or "", -- HEIGHT.RAW - is_api_9_1 and image.final_width or "", -- WIDTH.CROP + is_api_9_1 and image.final_width or "", -- WIDTH.CROP is_api_9_1 and image.final_height or "", -- HEIGHT.CROP is_api_9_1 and image.final_width or "", -- WIDTH.EXPORT is_api_9_1 and image.final_height or "", -- HEIGHT.EXPORT - "", -- WIDTH.MAX - "", -- HEIGHT.MAX + "", -- WIDTH.MAX -- from export module + "", -- HEIGHT.MAX -- from export module string.format("%4d", datetime.year), -- YEAR string.sub(datetime.year, 3), -- YEAR.SHORT string.format("%02d", datetime.month), -- MONTH @@ -717,8 +730,8 @@ function dtutils_string.build_substition_list(image, sequence, variable_string, eyear, -- EXIF.YEAR string.sub(eyear, 3), -- EXIF.YEAR.SHORT emon, -- EXIF.MONTH - os.date("%B", exiftime2systime(image.exif_datetime_taken)), -- EXIF.MONTH.LONG - os.date("%b", exiftime2systime(image.exif_datetime_taken)), -- EXIF.MONTH.SHORT + os.date("%B", exiftime2systime(image.exif_datetime_taken)), -- EXIF.MONTH.LONG + os.date("%b", exiftime2systime(image.exif_datetime_taken)), -- EXIF.MONTH.SHORT eday, -- EXIF.DAY ehour, -- EXIF.HOUR emin, -- EXIF.MINUTE @@ -752,7 +765,6 @@ function dtutils_string.build_substition_list(image, sequence, variable_string, image.publisher, -- PUBLISHER image.rights, -- RIGHTS "", -- TAGS - wont be implemented - "", -- CATEGORY "", -- SIDECAR.TXT - wont be implemented pictures_folder, -- FOLDER.PICTURES home_folder, -- FOLDER.HOME @@ -763,36 +775,50 @@ function dtutils_string.build_substition_list(image, sequence, variable_string, "" -- JOBCODE - wont be implemented } + -- populate the substitution list + for i = 1, #PLACEHOLDERS, 1 do substitutes[PLACEHOLDERS[i]] = replacements[i] log.msg(log.info, "setting " .. PLACEHOLDERS[i] .. " to " .. tostring(replacements[i])) end + -- do category substitutions separately + build_category_substitution_list(image, variable_string) log.log_level(old_log_level) end +-- handle different versions of names + local function check_legacy_vars(var_name) local var = var_name + if string.match(var, "_") then var = string.gsub(var, "_", ".") end + if string.match(var, "^HOME$") then var = "FOLDER.HOME" end if string.match(var, "^PICTURES.FOLDER$") then var = "FOLDER.PICTURES" end if string.match(var, "^DESKTOP$") then var = "FOLDER.DESKTOP" end + return var end +-- get the substitution and do any string manipulations requested + local function treat(var_string) local old_log_level = log.log_level() - --log.log_level(dtutils_string.log_level) - --log.log_level(log.info) + log.log_level(dtutils_string.log_level) + local ret_val = "" + -- remove the var from the string local var = string.match(var_string, "[%a%._]+") + var = check_legacy_vars(var) log.msg(log.info, "var_string is " .. tostring(var_string) .. " and var is " .. tostring(var)) + if string.match(var_string, "CATEGORY%d") then log.msg(log.info, "substituting for " .. var_string) ret_val = substitutes[var_string] @@ -800,12 +826,11 @@ local function treat(var_string) else ret_val = substitutes[var] end + local valid_var = false if ret_val then valid_var = true - --elseif string.match(var, "CATEGORY%d") then - --valid_var = true end if not valid_var then @@ -813,79 +838,109 @@ local function treat(var_string) log.log_level(old_log_level) return "" end - if string.match(var, "CATEGORY%d") then - ret_val = process_category(image, var, var_string) - else - local args = string.gsub(var_string, var, "") - log.msg(log.info, "args is " .. tostring(args)) - if string.len(args) > 0 then - if string.match(args, '^%^%^') then - ret_val = string.upper(ret_val) - elseif string.match(args, "^%^") then - ret_val = string.gsub(ret_val, "^%a", string.upper, 1) - elseif string.match(args, "^,,") then - ret_val = string.lower(ret_val) - elseif string.match(args, "^,") then - ret_val = string.gsub(ret_val, "^%a", string.lower, 1) - elseif string.match(args, "^:%-?%d+:%-?%d+") then - local soffset, slen = string.match(args, ":(%-?%d+):(%-?%d+)") - log.msg(log.info, "soffset is " .. soffset .. " and slen is " .. slen) - if tonumber(soffset) >= 0 then - soffset = soffset + 1 - end - log.msg(log.info, "soffset is " .. soffset .. " and slen is " .. slen) - if tonumber(soffset) < 0 and tonumber(slen) < 0 then - local temp = soffset - soffset = slen - slen = temp - end - log.msg(log.info, "soffset is " .. soffset .. " and slen is " .. slen) - ret_val = string.sub(ret_val, soffset, slen) - log.msg(log.info, "ret_val is " .. ret_val) - elseif string.match(args, "^:%-?%d+") then - local soffset= string.match(args, ":(%-?%d+)") - if tonumber(soffset) >= 0 then - soffset = soffset + 1 - end - ret_val = string.sub(ret_val, soffset, -1) - elseif string.match(args, "^-%$%(.-%)") then - local replacement = string.match(args, "-%$%(([%a%._]+)%)") - replacement = check_legacy_vars(replacement) - if string.len(ret_val) == 0 then - ret_val = substitutes[replacement] - end - elseif string.match(args, "^-.+$") then - local replacement = string.match(args, "-(.+)$") - if string.len(ret_val) == 0 then - ret_val = replacement - end - elseif string.match(args, "^+.+") then - local replacement = string.match(args, "+(.+)") - if string.len(ret_val) > 0 then - ret_val = replacement - end - elseif string.match(args, "^#.+") then - local pattern = string.match(args, "#(.+)") - log.msg(log.info, "pattern to remove is " .. tostring(pattern)) - ret_val = string.gsub(ret_val, "^" .. dtutils_string.sanitize_lua(pattern), "") - elseif string.match(args, "^%%.+") then - local pattern = string.match(args, "%%(.+)") - ret_val = string.gsub(ret_val, pattern .. "$", "") - elseif string.match(args, "^//.-/.+") then - local pattern, replacement = string.match(args, "//(.-)/(.+)") - ret_val = string.gsub(ret_val, pattern, replacement) - elseif string.match(args, "^/#.+/.+") then - local pattern, replacement = string.match(args, "/#(.+)/(.+)") - ret_val = string.gsub(ret_val, "^" .. pattern, replacement, 1) - elseif string.match(args, "^/%%.-/.+") then - local pattern, replacement = string.match(args, "/%%(.-)/(.+)") - ret_val = string.gsub(ret_val, pattern .. "$", replacement) - elseif string.match(args, "^/.-/.+") then - log.msg(log.info, "took replacement branch") - local pattern, replacement = string.match(args, "/(.-)/(.+)") - ret_val = string.gsub(ret_val, pattern, replacement, 1) + + -- string modifications + + local args = string.gsub(var_string, var, "") + log.msg(log.info, "args is " .. tostring(args)) + + if string.len(args) > 0 then + + if string.match(args, '^%^%^') then + ret_val = string.upper(ret_val) + + elseif string.match(args, "^%^") then + ret_val = string.gsub(ret_val, "^%a", string.upper, 1) + + elseif string.match(args, "^,,") then + ret_val = string.lower(ret_val) + + elseif string.match(args, "^,") then + ret_val = string.gsub(ret_val, "^%a", string.lower, 1) + + elseif string.match(args, "^:%-?%d+:%-?%d+") then + + local soffset, slen = string.match(args, ":(%-?%d+):(%-?%d+)") + log.msg(log.info, "soffset is " .. soffset .. " and slen is " .. slen) + + if tonumber(soffset) >= 0 then + soffset = soffset + 1 end + log.msg(log.info, "soffset is " .. soffset .. " and slen is " .. slen) + + if tonumber(soffset) < 0 and tonumber(slen) < 0 then + local temp = soffset + soffset = slen + slen = temp + end + log.msg(log.info, "soffset is " .. soffset .. " and slen is " .. slen) + + ret_val = string.sub(ret_val, soffset, slen) + log.msg(log.info, "ret_val is " .. ret_val) + + elseif string.match(args, "^:%-?%d+") then + + local soffset= string.match(args, ":(%-?%d+)") + if tonumber(soffset) >= 0 then + soffset = soffset + 1 + end + ret_val = string.sub(ret_val, soffset, -1) + + elseif string.match(args, "^-%$%(.-%)") then + + local replacement = string.match(args, "-%$%(([%a%._]+)%)") + replacement = check_legacy_vars(replacement) + if string.len(ret_val) == 0 then + ret_val = substitutes[replacement] + end + + elseif string.match(args, "^-.+$") then + + local replacement = string.match(args, "-(.+)$") + if string.len(ret_val) == 0 then + ret_val = replacement + end + + elseif string.match(args, "^+.+") then + + local replacement = string.match(args, "+(.+)") + if string.len(ret_val) > 0 then + ret_val = replacement + end + + elseif string.match(args, "^#.+") then + + local pattern = string.match(args, "#(.+)") + log.msg(log.info, "pattern to remove is " .. tostring(pattern)) + ret_val = string.gsub(ret_val, "^" .. dtutils_string.sanitize_lua(pattern), "") + + elseif string.match(args, "^%%.+") then + + local pattern = string.match(args, "%%(.+)") + ret_val = string.gsub(ret_val, pattern .. "$", "") + + elseif string.match(args, "^//.-/.+") then + + local pattern, replacement = string.match(args, "//(.-)/(.+)") + ret_val = string.gsub(ret_val, pattern, replacement) + + elseif string.match(args, "^/#.+/.+") then + + local pattern, replacement = string.match(args, "/#(.+)/(.+)") + ret_val = string.gsub(ret_val, "^" .. pattern, replacement, 1) + + elseif string.match(args, "^/%%.-/.+") then + + local pattern, replacement = string.match(args, "/%%(.-)/(.+)") + ret_val = string.gsub(ret_val, pattern .. "$", replacement) + + elseif string.match(args, "^/.-/.+") then + + log.msg(log.info, "took replacement branch") + local pattern, replacement = string.match(args, "/(.-)/(.+)") + ret_val = string.gsub(ret_val, pattern, replacement, 1) end + end log.log_level(old_log_level) return ret_val @@ -911,15 +966,22 @@ dtutils_string.libdoc.functions["substitute_list"] = { function dtutils_string.substitute_list(str) local old_log_level = log.log_level() log.log_level(dtutils_string.log_level) + -- replace the substitution variables in a string for match in string.gmatch(str, "%$%(.-%)?%)") do + local var = string.match(match, "%$%((.-%)?)%)") + local treated_var = treat(var) log.msg(log.info, "var is " .. var .. " and treated var is " .. tostring(treated_var)) + str = string.gsub(str, "%$%(".. dtutils_string.sanitize_lua(var) .."%)", tostring(treated_var)) log.msg(log.info, "str after replacement is " .. str) + end + log.log_level(old_log_level) + return str end @@ -988,4 +1050,5 @@ function dtutils_string.substitute(image, sequence, variable_string, username, p end + return dtutils_string From 97e33672ea79d29073aef37364c6c523bd6d5bad Mon Sep 17 00:00:00 2001 From: Bill Ferguson Date: Sun, 19 May 2024 23:04:01 -0400 Subject: [PATCH 06/10] lib/dtutils/string - fixed default formatting of sequence --- lib/dtutils/string.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dtutils/string.lua b/lib/dtutils/string.lua index dad0d55..c172ff6 100644 --- a/lib/dtutils/string.lua +++ b/lib/dtutils/string.lua @@ -706,7 +706,7 @@ function dtutils_string.build_substition_list(image, sequence, variable_string, image.version_name, -- VERSION.NAME dt.configuration.version, -- DARKTABLE.VERSION "", -- DARKTABLE.NAME - sequence, -- SEQUENCE + string.format("%04d", sequence), -- SEQUENCE image.width, -- WIDTH.SENSOR image.height, -- HEIGHT.SENSOR is_api_9_1 and image.p_width or "", -- WIDTH.RAW From 3ed4219e6c2f92cdc01eabb9d88227b972c83948 Mon Sep 17 00:00:00 2001 From: Bill Ferguson Date: Mon, 20 May 2024 00:56:01 -0400 Subject: [PATCH 07/10] lib/dtutils/string - added [] to sanitize_lua(). added 4.8 syntax to CATEGORY added 4.8 SEQUENCE changes (syntax, start, width) --- lib/dtutils/string.lua | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/dtutils/string.lua b/lib/dtutils/string.lua index c172ff6..2e4b7d8 100644 --- a/lib/dtutils/string.lua +++ b/lib/dtutils/string.lua @@ -335,6 +335,8 @@ function dtutils_string.sanitize_lua(str) str = string.gsub(str, "%-", "%%-") str = string.gsub(str, "%(", "%%(") str = string.gsub(str, "%)", "%%)") + str = string.gsub(str, "%[", "%%[") + str = string.gsub(str, "%]", "%%]") str = string.gsub(str, "+", "%%+") log.log_level(old_log_level) return str @@ -623,8 +625,15 @@ local function build_category_substitution_list(image, variable_string) local var = string.match(match, "%$%((.-%)?)%)") -- strip of the leading $( and trailing ) log.msg(log.info, "var is " .. var) - if string.match(var, "CATEGORY%d") then - local element, tag = string.match(var, "CATEGORY(%d)%((.-)%)") -- get the element number and the tag to match + if string.match(var, "CATEGORY%d") or string.match(var, "CATEGORY%[") then + local element + local tag + + if string.match(var, "CATEGORY%d") then + element, tag = string.match(var, "CATEGORY(%d)%((.-)%)") -- get the element number and the tag to match + else + element, tag = string.match(var, "%[(%d),(.-)%]") -- new syntax + end element = element + 1 -- add one to element since lua arrays are 1 based log.msg(log.debug, "element is " .. element .. " and tag is " .. tag) @@ -696,7 +705,7 @@ function dtutils_string.build_substition_list(image, sequence, variable_string, local version_multi = #image:get_group_members() > 1 and image.version or "" - local replacements = {image.film, -- ROLL.NAME + local replacements = {image.film.path, -- ROLL.NAME image.path, -- FILE.FOLDER image.filename, -- FILE.NAME dtutils_string.get_filetype(image.filename),-- FILE.EXTENSION @@ -819,10 +828,18 @@ local function treat(var_string) var = check_legacy_vars(var) log.msg(log.info, "var_string is " .. tostring(var_string) .. " and var is " .. tostring(var)) - if string.match(var_string, "CATEGORY%d") then + if string.match(var_string, "CATEGORY%d") or string.match(var_string, "CATEGORY%[") then log.msg(log.info, "substituting for " .. var_string) ret_val = substitutes[var_string] log.msg(log.info, "ret_val is " .. ret_val) + + elseif string.match(var_string, "SEQUENCE%[") then + local start, width = string.match(var_string, "(%d+),(%d)") + local seq_val = tonumber(substitutes[var]) + local pat = "%0" .. width .. "d" + substitutes[var_string] = string.format(pat, start + (seq_val - 1)) + ret_val = substitutes[var_string] + else ret_val = substitutes[var] end @@ -943,6 +960,7 @@ local function treat(var_string) end log.log_level(old_log_level) + dt.print_log("returning ret_val of " .. ret_val) return ret_val end @@ -965,7 +983,8 @@ dtutils_string.libdoc.functions["substitute_list"] = { function dtutils_string.substitute_list(str) local old_log_level = log.log_level() - log.log_level(dtutils_string.log_level) + -- log.log_level(dtutils_string.log_level) + log.log_level(log.info) -- replace the substitution variables in a string for match in string.gmatch(str, "%$%(.-%)?%)") do @@ -975,6 +994,8 @@ function dtutils_string.substitute_list(str) local treated_var = treat(var) log.msg(log.info, "var is " .. var .. " and treated var is " .. tostring(treated_var)) + dt.print_log("str is " .. str) + str = string.gsub(str, "%$%(".. dtutils_string.sanitize_lua(var) .."%)", tostring(treated_var)) log.msg(log.info, "str after replacement is " .. str) From 232baab86bd559684d97aab3c154da6c240d5deb Mon Sep 17 00:00:00 2001 From: Bill Ferguson Date: Mon, 20 May 2024 10:07:13 -0400 Subject: [PATCH 08/10] lib/dtutils/string - removed debugging statements --- lib/dtutils/string.lua | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/dtutils/string.lua b/lib/dtutils/string.lua index 2e4b7d8..7690d59 100644 --- a/lib/dtutils/string.lua +++ b/lib/dtutils/string.lua @@ -960,7 +960,6 @@ local function treat(var_string) end log.log_level(old_log_level) - dt.print_log("returning ret_val of " .. ret_val) return ret_val end @@ -983,8 +982,7 @@ dtutils_string.libdoc.functions["substitute_list"] = { function dtutils_string.substitute_list(str) local old_log_level = log.log_level() - -- log.log_level(dtutils_string.log_level) - log.log_level(log.info) + log.log_level(dtutils_string.log_level) -- replace the substitution variables in a string for match in string.gmatch(str, "%$%(.-%)?%)") do @@ -994,8 +992,6 @@ function dtutils_string.substitute_list(str) local treated_var = treat(var) log.msg(log.info, "var is " .. var .. " and treated var is " .. tostring(treated_var)) - dt.print_log("str is " .. str) - str = string.gsub(str, "%$%(".. dtutils_string.sanitize_lua(var) .."%)", tostring(treated_var)) log.msg(log.info, "str after replacement is " .. str) From 417c165d16dc7d65ccfd2be5906ab05e0bb12c85 Mon Sep 17 00:00:00 2001 From: Bill Ferguson Date: Mon, 20 May 2024 18:18:46 -0400 Subject: [PATCH 09/10] lib/dtutils/string - updated documentation --- lib/dtutils/string.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/dtutils/string.lua b/lib/dtutils/string.lua index 7690d59..f3236fc 100644 --- a/lib/dtutils/string.lua +++ b/lib/dtutils/string.lua @@ -484,7 +484,7 @@ dtutils_string.libdoc.functions["build_substitution_list"] = { Usage = [[local ds = require "lib/dtutils.string" ds.build_substitution_list(image, sequence, variable_string, [username], [pic_folder], [home], [desktop]) image - dt_lua_image_t - the image being processed - sequence - integer - the sequence number of the image + sequence - integer - the sequence number of the image being processed (exported) variable_string - string - the substitution variable string [username] - string - optional - user name. Will be determined if not supplied [pic_folder] - string - optional - pictures folder name. Will be determined if not supplied @@ -496,7 +496,7 @@ dtutils_string.libdoc.functions["build_substitution_list"] = { Limitations = [[If the value for a variable can not be determined, or if it is not supported, then an empty string is used for the value.]], Example = [[]], - See_Also = [[https://docs.darktable.org/usermanual/4.2/en/special-topics/variables/]], + See_Also = [[https://docs.darktable.org/usermanual/4.6/en/special-topics/variables/]], Reference = [[]], License = [[]], Copyright = [[]], @@ -613,7 +613,7 @@ local function get_colorlabels(image) return labels end --- find the $CATEGORYn requests and add them to the substitute list +-- find the $CATEGORYn and $CATEGORY[n,m] requests and add them to the substitute list local function build_category_substitution_list(image, variable_string) local old_log_level = log.log_level() @@ -974,7 +974,7 @@ dtutils_string.libdoc.functions["substitute_list"] = { Return_Value = [[result - string - the input string with values substituted for the variables]], Limitations = [[]], Example = [[]], - See_Also = [[]], + See_Also = [[https://docs.darktable.org/usermanual/4.6/en/special-topics/variables/]], Reference = [[]], License = [[]], Copyright = [[]], @@ -1033,7 +1033,7 @@ dtutils_string.libdoc.functions["substitute"] = { Usage = [[local ds = require "lib/dtutils.string" ds.substitute(image, sequence, variable_string, [username], [pic_folder], [home], [desktop]) image - dt_lua_image_t - the image being processed - sequence - integer - the sequence number of the image + sequence - integer - the number of the image being processed (exported) variable_string - string - the substitution variable string [username] - string - optional - user name. Will be determined if not supplied [pic_folder] - string - optional - pictures folder name. Will be determined if not supplied @@ -1045,7 +1045,7 @@ dtutils_string.libdoc.functions["substitute"] = { Return_Value = [[result - string - the input string with values substituted for the variables]], Limitations = [[]], Example = [[]], - See_Also = [[]], + See_Also = [[https://docs.darktable.org/usermanual/4.6/en/special-topics/variables/]], Reference = [[]], License = [[]], Copyright = [[]], From dcbe575e9c50665209c429842edbe309a8191192 Mon Sep 17 00:00:00 2001 From: Bill Ferguson Date: Mon, 20 May 2024 22:13:29 -0400 Subject: [PATCH 10/10] lib/dtutils/string - updated embedded documentation --- lib/dtutils/string.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/dtutils/string.lua b/lib/dtutils/string.lua index f3236fc..342a576 100644 --- a/lib/dtutils/string.lua +++ b/lib/dtutils/string.lua @@ -478,11 +478,11 @@ function dtutils_string.get_filetype(str) return parts["filetype"] end -dtutils_string.libdoc.functions["build_substitution_list"] = { - Name = [[build_substitution_list]], +dtutils_string.libdoc.functions["build_substitute_list"] = { + Name = [[build_substitute_list]], Synopsis = [[build a list of variable substitutions]], Usage = [[local ds = require "lib/dtutils.string" - ds.build_substitution_list(image, sequence, variable_string, [username], [pic_folder], [home], [desktop]) + ds.build_substitute_list(image, sequence, variable_string, [username], [pic_folder], [home], [desktop]) image - dt_lua_image_t - the image being processed sequence - integer - the sequence number of the image being processed (exported) variable_string - string - the substitution variable string @@ -490,7 +490,7 @@ dtutils_string.libdoc.functions["build_substitution_list"] = { [pic_folder] - string - optional - pictures folder name. Will be determined if not supplied [home] - string - optional - home directory. Will be determined if not supplied [desktop] - string - optional - desktop directory. Will be determined if not supplied]], - Description = [[build_substitution_list populates variables with values from the arguments + Description = [[build_substitute_list populates variables with values from the arguments and determined from the system and darktable.]], Return_Value = [[]], Limitations = [[If the value for a variable can not be determined, or if it is not supported, @@ -1057,7 +1057,7 @@ function dtutils_string.substitute(image, sequence, variable_string, username, p dtutils_string.clear_substitute_list() - dtutils_string.build_substition_list(image, sequence, variable_string, username, pic_folder, home, desktop) + dtutils_string.build_substitute_list(image, sequence, variable_string, username, pic_folder, home, desktop) local str = dtutils_string.substitute_list(variable_string)