Merge pull request #512 from wpferguson/rename_images_variable_replacement
Rename images variable replacement
This commit is contained in:
@@ -43,6 +43,7 @@
|
||||
local dt = require "darktable"
|
||||
local du = require "lib/dtutils"
|
||||
local df = require "lib/dtutils.file"
|
||||
local ds = require "lib/dtutils.string"
|
||||
|
||||
du.check_min_api_version("7.0.0", "rename_images")
|
||||
|
||||
@@ -57,12 +58,6 @@ end
|
||||
-- namespace variable
|
||||
local rename = {
|
||||
presets = {},
|
||||
substitutes = {},
|
||||
placeholders = {"ROLL_NAME","FILE_FOLDER","FILE_NAME","FILE_EXTENSION","ID","VERSION","SEQUENCE","YEAR","MONTH","DAY",
|
||||
"HOUR","MINUTE","SECOND","EXIF_YEAR","EXIF_MONTH","EXIF_DAY","EXIF_HOUR","EXIF_MINUTE","EXIF_SECOND",
|
||||
"STARS","LABELS","MAKER","MODEL","TITLE","CREATOR","PUBLISHER","RIGHTS","USERNAME","PICTURES_FOLDER",
|
||||
"HOME","DESKTOP","EXIF_ISO","EXIF_EXPOSURE","EXIF_EXPOSURE_BIAS","EXIF_APERTURE","EXIF_FOCUS_DISTANCE",
|
||||
"EXIF_FOCAL_LENGTH","LONGITUDE","LATITUDE","ELEVATION","LENS","DESCRIPTION","EXIF_CROP"},
|
||||
widgets = {},
|
||||
}
|
||||
rename.module_installed = false
|
||||
@@ -99,83 +94,6 @@ local DESKTOP = HOME .. PS .. "Desktop"
|
||||
-- F U N C T I O N S
|
||||
-- - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
local function build_substitution_list(image, sequence, datetime, username, pic_folder, home, desktop)
|
||||
-- build the argument substitution list from each image
|
||||
-- local datetime = os.date("*t")
|
||||
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 eyear,emon,eday,ehour,emin,esec = string.match(image.exif_datetime_taken, "(%d-):(%d-):(%d-) (%d-):(%d-):(%d-)$")
|
||||
local replacements = {image.film,
|
||||
image.path,
|
||||
df.get_filename(image.filename),
|
||||
string.upper(df.get_filetype(image.filename)),
|
||||
image.id,image.duplicate_index,
|
||||
string.format("%04d", sequence),
|
||||
datetime.year,
|
||||
string.format("%02d", datetime.month),
|
||||
string.format("%02d", datetime.day),
|
||||
string.format("%02d", datetime.hour),
|
||||
string.format("%02d", datetime.min),
|
||||
string.format("%02d", datetime.sec),
|
||||
eyear,
|
||||
emon,
|
||||
eday,
|
||||
ehour,
|
||||
emin,
|
||||
esec,
|
||||
image.rating,
|
||||
labels,
|
||||
image.exif_maker,
|
||||
image.exif_model,
|
||||
image.title,
|
||||
image.creator,
|
||||
image.publisher,
|
||||
image.rights,
|
||||
username,
|
||||
pic_folder,
|
||||
home,
|
||||
desktop,
|
||||
image.exif_iso,
|
||||
image.exif_exposure,
|
||||
image.exif_exposure_bias,
|
||||
image.exif_aperture,
|
||||
image.exif_focus_distance,
|
||||
image.exif_focal_length,
|
||||
image.longitude,
|
||||
image.latitude,
|
||||
image.elevation,
|
||||
image.exif_lens,
|
||||
image.description,
|
||||
image.exif_crop
|
||||
}
|
||||
|
||||
for i=1,#rename.placeholders,1 do rename.substitutes[rename.placeholders[i]] = replacements[i] end
|
||||
end
|
||||
|
||||
local function substitute_list(str)
|
||||
-- replace the substitution variables in a string
|
||||
for match in string.gmatch(str, "%$%(.-%)") do
|
||||
local var = string.match(match, "%$%((.-)%)")
|
||||
if rename.substitutes[var] then
|
||||
str = string.gsub(str, "%$%("..var.."%)", rename.substitutes[var])
|
||||
else
|
||||
dt.print_error("unrecognized variable " .. var)
|
||||
dt.print(string.format(_("unknown variable %s, aborting..."), var))
|
||||
return -1
|
||||
end
|
||||
end
|
||||
return str
|
||||
end
|
||||
|
||||
local function clear_substitute_list()
|
||||
for i=1,#rename.placeholders,1 do rename.substitutes[rename.placeholders[i]] = nil end
|
||||
end
|
||||
|
||||
local function stop_job(job)
|
||||
job.valid = false
|
||||
end
|
||||
@@ -225,14 +143,14 @@ local function do_rename(images)
|
||||
for i, image in ipairs(images) do
|
||||
if job.valid then
|
||||
job.percent = i / #images
|
||||
build_substitution_list(image, i, datetime, USER, PICTURES, HOME, DESKTOP)
|
||||
local new_name = substitute_list(pattern)
|
||||
ds.build_substitution_list(image, i, pattern, USER, PICTURES, HOME, DESKTOP)
|
||||
local new_name = ds.substitute_list(pattern)
|
||||
if new_name == -1 then
|
||||
dt.print(_("unable to do variable substitution, exiting..."))
|
||||
stop_job(job)
|
||||
return
|
||||
end
|
||||
clear_substitute_list()
|
||||
ds.clear_substitute_list()
|
||||
local args = {}
|
||||
local path = string.sub(df.get_path(new_name), 1, -2)
|
||||
if string.len(path) == 0 then
|
||||
@@ -278,50 +196,8 @@ end
|
||||
-- - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
rename.widgets.pattern = dt.new_widget("entry"){
|
||||
tooltip = _("$(ROLL_NAME) - film roll name\n") ..
|
||||
_("$(FILE_FOLDER) - image file folder\n") ..
|
||||
_("$(FILE_NAME) - image file name\n") ..
|
||||
_("$(FILE_EXTENSION) - image file extension\n") ..
|
||||
_("$(ID) - image id\n") ..
|
||||
_("$(VERSION) - version number\n") ..
|
||||
_("$(SEQUENCE) - sequence number of selection\n") ..
|
||||
_("$(YEAR) - current year\n") ..
|
||||
_("$(MONTH) - current month\n") ..
|
||||
_("$(DAY) - current day\n") ..
|
||||
_("$(HOUR) - current hour\n") ..
|
||||
_("$(MINUTE) - current minute\n") ..
|
||||
_("$(SECOND) - current second\n") ..
|
||||
_("$(EXIF_YEAR) - EXIF year\n") ..
|
||||
_("$(EXIF_MONTH) - EXIF month\n") ..
|
||||
_("$(EXIF_DAY) - EXIF day\n") ..
|
||||
_("$(EXIF_HOUR) - EXIF hour\n") ..
|
||||
_("$(EXIF_MINUTE) - EXIF minute\n") ..
|
||||
_("$(EXIF_SECOND) - EXIF seconds\n") ..
|
||||
_("$(EXIF_ISO) - EXIF ISO\n") ..
|
||||
_("$(EXIF_EXPOSURE) - EXIF exposure\n") ..
|
||||
_("$(EXIF_EXPOSURE_BIAS) - EXIF exposure bias\n") ..
|
||||
_("$(EXIF_APERTURE) - EXIF aperture\n") ..
|
||||
_("$(EXIF_FOCAL_LENGTH) - EXIF focal length\n") ..
|
||||
_("$(EXIF_FOCUS_DISTANCE) - EXIF focus distance\n") ..
|
||||
_("$(EXIF_CROP) - EXIF crop\n") ..
|
||||
_("$(LONGITUDE) - longitude\n") ..
|
||||
_("$(LATITUDE) - latitude\n") ..
|
||||
_("$(ELEVATION) - elevation\n") ..
|
||||
_("$(STARS) - star rating\n") ..
|
||||
_("$(LABELS) - color labels\n") ..
|
||||
_("$(MAKER) - camera maker\n") ..
|
||||
_("$(MODEL) - camera model\n") ..
|
||||
_("$(LENS) - lens\n") ..
|
||||
_("$(TITLE) - title from metadata\n") ..
|
||||
_("$(DESCRIPTION) - description from metadata\n") ..
|
||||
_("$(CREATOR) - creator from metadata\n") ..
|
||||
_("$(PUBLISHER) - publisher from metadata\n") ..
|
||||
_("$(RIGHTS) - rights from metadata\n") ..
|
||||
_("$(USERNAME) - username\n") ..
|
||||
_("$(PICTURES_FOLDER) - pictures folder\n") ..
|
||||
_("$(HOME) - user's home directory\n") ..
|
||||
_("$(DESKTOP) - desktop directory"),
|
||||
placeholder = _("enter pattern $(FILE_FOLDER)/$(FILE_NAME)"),
|
||||
tooltip = ds.get_substitution_tooltip(),
|
||||
placeholder = _("enter pattern") .. "$(FILE_FOLDER)/$(FILE_NAME)",
|
||||
text = ""
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,10 @@ local log = require "lib/dtutils.log"
|
||||
|
||||
local DEFAULT_LOG_LEVEL = log.error
|
||||
|
||||
local function _(msg)
|
||||
return dt.gettext.gettext(msg)
|
||||
end
|
||||
|
||||
dtutils_string.log_level = DEFAULT_LOG_LEVEL
|
||||
|
||||
dtutils_string.libdoc = {
|
||||
@@ -688,7 +692,7 @@ end
|
||||
|
||||
-- build the argument substitution list from each image
|
||||
|
||||
function dtutils_string.build_substition_list(image, sequence, variable_string, username, pic_folder, home, desktop)
|
||||
function dtutils_string.build_substitution_list(image, sequence, variable_string, username, pic_folder, home, desktop)
|
||||
local old_log_level = log.log_level()
|
||||
log.log_level(dtutils_string.log_level)
|
||||
|
||||
@@ -814,6 +818,102 @@ function dtutils_string.build_substition_list(image, sequence, variable_string,
|
||||
log.log_level(old_log_level)
|
||||
end
|
||||
|
||||
dtutils_string.libdoc.functions["get_substitution_tooltip"] = {
|
||||
Name = [[get_substitution_tooltip]],
|
||||
Synopsis = [[get a tooltip that lists the substitution variables]],
|
||||
Usage = [[local ds = require "lib/dtutils.string"
|
||||
ds.get_substitution_tooltip()
|
||||
Description = [[get_substitution_tooltip lists the variables with brief explanations]],
|
||||
Return_Value = [[string - the tooltip]],
|
||||
Limitations = [[]],
|
||||
Example = [[]],
|
||||
See_Also = [[https://docs.darktable.org/usermanual/4.6/en/special-topics/variables/]],
|
||||
Reference = [[]],
|
||||
License = [[]],
|
||||
Copyright = [[]],
|
||||
}
|
||||
|
||||
function dtutils_string.get_substitution_tooltip()
|
||||
return string.format("$(ROLL.NAME) - %s\n", _("film roll of the input image")) ..
|
||||
string.format("$(FILE.FOLDER) - %s\n", _("folder containing the input image")) ..
|
||||
string.format("$(FILE.NAME) - %s\n", _("basename of the input image")) ..
|
||||
string.format("$(FILE.EXTENSION) - %s\n", _("extension of the input image")) ..
|
||||
string.format("$(ID) - %s\n", _("the image id")) ..
|
||||
string.format("$(VERSION) - %s\n", _("the duplicate version number")) ..
|
||||
string.format("$(VERSION.IF.MULTI) - %s\n", _("same as $(VERSION) but null string if only one version exists")) ..
|
||||
string.format("$(VERSION.NAME) - %s\n", _("version name from metadata")) ..
|
||||
string.format("$(DARKTABLE.VERSION) - %s\n", _("the version of the running darktable instance")) ..
|
||||
--string.format("$(DARKTABLE.NAME) - %s\n", _("")) .. -- Not Implemented
|
||||
string.format("$(SEQUENCE[n,m]) - %s\n", _("a sequence number within an export job with n digits and starting with m\nparameters are optional, default is [4,1]")) ..
|
||||
string.format("$(WIDTH.SENSOR) - %s\n", _("width of RAW data in pixels before RAW crop")) ..
|
||||
string.format("$(HEIGHT.SENSOR) - %s\n", _("height of RAW data in pixels before RAW crop")) ..
|
||||
string.format("$(WIDTH.RAW) - %s\n", _("width of RAW data in pixels after RAW crop")) ..
|
||||
string.format("$(HEIGHT.RAW) - %s\n", _("height of RAW data in pixels after RAW crop")) ..
|
||||
string.format("$(WIDTH.CROP) - %s\n", _("image width in pixels at the end of the pixelpipe, but before export resize")) ..
|
||||
string.format("$(HEIGHT.CROP) - %s\n", _("image height in pixels at the end of the pixelpipe, but before export resize")) ..
|
||||
string.format("$(WIDTH.EXPORT) - %s\n", _("image width in pixels at the end of the pixelpipe and after export resize")) ..
|
||||
string.format("$(HEIGHT.EXPORT) - %s\n", _("image height in pixels at the end of the pixelpipe and after export resize")) ..
|
||||
--string.format("$(WIDTH.MAX) - %s\n", _("")) .. -- Not Implemented
|
||||
--string.format("$(HEIGHT.MAX) - %s\n", _("")) .. -- Not Implemented
|
||||
string.format("$(YEAR) - %s\n", _("current year")) ..
|
||||
string.format("$(YEAR.SHORT) - %s\n", _("current two digit year")) ..
|
||||
string.format("$(MONTH) - %s\n", _("current numeric (1-12) month")) ..
|
||||
string.format("$(MONTH.LONG) - %s\n", _("full current month name")) ..
|
||||
string.format("$(MONTH.SHORT) - %s\n", _("abbreviated current month name")) ..
|
||||
string.format("$(DAY) - %s\n", _("current day")) ..
|
||||
string.format("$(HOUR) - %s\n", _("current hour")) ..
|
||||
string.format("$(MINUTE) - %s\n", _("current minute")) ..
|
||||
string.format("$(SECOND) - %s\n", _("current second")) ..
|
||||
string.format("$(MSEC) - %s\n", _("current millisecond")) ..
|
||||
string.format("$(EXIF.YEAR) - EXIF %s\n", _("year")) ..
|
||||
string.format("$(EXIF.YEAR.SHORT) - EXIF %s\n", _("year, two-digit version")) ..
|
||||
string.format("$(EXIF.MONTH) - EXIF %s\n", _("month, numeric")) ..
|
||||
string.format("$(EXIF.MONTH.LONG) - EXIF %s\n", _("month, full name")) ..
|
||||
string.format("$(EXIF.MONTH.SHORT) - EXIF %s\n", _("month, abbreviated name")) ..
|
||||
string.format("$(EXIF.DAY) - EXIF %s\n", _("day")) ..
|
||||
string.format("$(EXIF.HOUR) - EXIF %s\n", _("hour")) ..
|
||||
string.format("$(EXIF.MINUTE) - EXIF %s\n", _("minute")) ..
|
||||
string.format("$(EXIF.SECOND) - EXIF %s\n", _("second")) ..
|
||||
string.format("$(EXIF.MSEC) - EXIF %s\n", _("millisecond")) ..
|
||||
--string.format("$(EXIF.DATE.REGIONAL) - %s\n", _("")) .. -- Not Implemented
|
||||
--string.format("$(EXIF.TIME.REGIONAL) - %s\n", _("")) .. -- Not Implemented
|
||||
string.format("$(EXIF.ISO) - EXIF ISO %s\n", _("value")) ..
|
||||
string.format("$(EXIF.EXPOSURE) - EXIF %s\n", _("exposure")) ..
|
||||
string.format("$(EXIF.EXPOSURE.BIAS) - EXIF %s\n", _("exposure bias")) ..
|
||||
string.format("$(EXIF.APERTURE) - EXIF %s\n", _("aperture")) ..
|
||||
string.format("$(EXIF.CROP.FACTOR) - EXIF %s\n", _("crop factor")) ..
|
||||
string.format("$(EXIF.FOCAL.LENGTH) - EXIF %s\n", _("focal length")) ..
|
||||
string.format("$(EXIF.FOCAL.LENGTH.EQUIV) - EXIF 35mm %s\n", _("equivalent focal length")) .. -- Not Implemented
|
||||
string.format("$(EXIF.FOCUS.DISTANCE) - EXIF %s\n", _("focus distance")) ..
|
||||
--string.format("$(IMAGE.EXIF) - %s\n", _("")) .. -- Not Implemented
|
||||
string.format("$(LONGITUDE) - %s\n", _("longitude")) ..
|
||||
string.format("$(LATITUDE) - %s\n", _("latitude")) ..
|
||||
string.format("$(ELEVATION) - %s\n", _("elevation")) ..
|
||||
--string.format("$(GPS.LOCATION) - %s\n", _("")) .. -- Not Implemented
|
||||
string.format("$(STARS) - %s\n", _("star rating")) ..
|
||||
--string.format("$(RATING.ICONS) - %s\n", _("")) .. -- Not Implemented
|
||||
string.format("$(LABELS) - %s\n", _("colorlabels")) ..
|
||||
--string.format("$(LABELS.ICONS) - %s\n", _("")) .. -- Not Implemented
|
||||
string.format("$(MAKER) - %s\n", _("camera maker")) ..
|
||||
string.format("$(MODEL) - %s\n", _("camera model")) ..
|
||||
string.format("$(LENS) - %s\n", _("lens")) ..
|
||||
string.format("$(TITLE) - %s\n", _("title from metadata")) ..
|
||||
string.format("$(DESCRIPTION) - %s\n", _("description from metadata")) ..
|
||||
string.format("$(CREATOR) - %s\n", _("creator from metadata")) ..
|
||||
string.format("$(PUBLISHER) - %s\n", _("publisher from metadata")) ..
|
||||
string.format("$(RIGHTS) - %s\n", _("rights from metadata")) ..
|
||||
--string.format("$(TAGS) - %s\n", _("")) .. -- Not Implemented
|
||||
string.format("$(CATEGORY[n,category]) - %s\n", _("tag name of level n [0,9] of selected category (or tag)")) ..
|
||||
--string.format("$(SIDECAR.TXT) - %s\n", _("")) .. -- Not Implemented
|
||||
string.format("$(FOLDER.PICTURES) - %s\n", _("pictures folder")) ..
|
||||
string.format("$(FOLDER.HOME) - %s\n", _("home folder")) ..
|
||||
string.format("$(FOLDER.DESKTOP) - %s\n", _("desktop folder")) ..
|
||||
--string.format("$(OPENCL.ACTIVATED) - %s\n", _("")) .. -- Not Implemented
|
||||
string.format("$(USERNAME) - %s\n", _("user name defined by OS"))
|
||||
--string.format("$(NL) - %s\n", _("")) .. -- Not Implemented
|
||||
--string.format("$(JOBCODE) - %s", _("")) -- Not Implemented
|
||||
end
|
||||
|
||||
-- handle different versions of names
|
||||
|
||||
local function check_legacy_vars(var_name)
|
||||
|
||||
Reference in New Issue
Block a user