Skip to content

Commit c9943a2

Browse files
committed
lib/dtutils/string - Added logging capability with the ability to
set library logging level from a user script
1 parent 612ecaf commit c9943a2

File tree

1 file changed

+88
-29
lines changed

1 file changed

+88
-29
lines changed

lib/dtutils/string.lua

Lines changed: 88 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
local dtutils_string = {}
22

33
local dt = require "darktable"
4+
local du = require "lib/dtutils"
5+
local log = require "lib/dtutils.log"
6+
7+
local DEFAULT_LOG_LEVEL = log.error
8+
9+
dtutils_string.log_level = DEFAULT_LOG_LEVEL
410

511
dtutils_string.libdoc = {
612
Name = [[dtutils.string]],
@@ -48,6 +54,8 @@ dtutils_string.libdoc.functions["strip_accents"] = {
4854
}
4955

5056
function dtutils_string.strip_accents( str )
57+
local old_log_level = log.log_level()
58+
log.log_level(dtutils_string.log_level)
5159
local tableAccents = {}
5260
tableAccents["à"] = "a"
5361
tableAccents["á"] = "a"
@@ -111,6 +119,7 @@ function dtutils_string.strip_accents( str )
111119
end
112120
end
113121

122+
log.log_level(old_log_level)
114123
return normalizedString
115124

116125
end
@@ -136,13 +145,16 @@ dtutils_string.libdoc.functions["escape_xml_characters"] = {
136145

137146
-- Keep & first, otherwise it will double escape other characters
138147
function dtutils_string.escape_xml_characters( str )
148+
local old_log_level = log.log_level()
149+
log.log_level(dtutils_string.log_level)
139150

140151
str = string.gsub(str,"&", "&")
141152
str = string.gsub(str,"\"", """)
142153
str = string.gsub(str,"'", "'")
143154
str = string.gsub(str,"<", "&lt;")
144155
str = string.gsub(str,">", "&gt;")
145156

157+
log.log_level(old_log_level)
146158
return str
147159
end
148160

@@ -165,11 +177,14 @@ dtutils_string.libdoc.functions["urlencode"] = {
165177
}
166178

167179
function dtutils_string.urlencode(str)
180+
local old_log_level = log.log_level()
181+
log.log_level(dtutils_string.log_level)
168182
if (str) then
169183
str = string.gsub (str, "\n", "\r\n")
170184
str = string.gsub (str, "([^%w ])", function (c) return string.format ("%%%02X", string.byte(c)) end)
171185
str = string.gsub (str, " ", "+")
172186
end
187+
log.log_level(old_log_level)
173188
return str
174189
end
175190

@@ -192,38 +207,52 @@ dtutils_string.libdoc.functions["is_not_sanitized"] = {
192207
}
193208

194209
local function _is_not_sanitized_posix(str)
195-
-- A sanitized string must be quoted.
196-
if not string.match(str, "^'.*'$") then
197-
return true
210+
local old_log_level = log.log_level()
211+
log.log_level(dtutils_string.log_level)
212+
-- A sanitized string must be quoted.
213+
if not string.match(str, "^'.*'$") then
214+
log.log_level(old_log_level)
215+
return true
198216
-- A quoted string containing no quote characters within is sanitized.
199-
elseif string.match(str, "^'[^']*'$") then
200-
return false
201-
end
217+
elseif string.match(str, "^'[^']*'$") then
218+
log.log_level(old_log_level)
219+
return false
220+
end
202221

203-
-- Any quote characters within a sanitized string must be properly
204-
-- escaped.
205-
local quotesStripped = string.sub(str, 2, -2)
206-
local escapedQuotesRemoved = string.gsub(quotesStripped, "'\\''", "")
207-
if string.find(escapedQuotesRemoved, "'") then
208-
return true
209-
else
210-
return false
211-
end
222+
-- Any quote characters within a sanitized string must be properly
223+
-- escaped.
224+
local quotesStripped = string.sub(str, 2, -2)
225+
local escapedQuotesRemoved = string.gsub(quotesStripped, "'\\''", "")
226+
if string.find(escapedQuotesRemoved, "'") then
227+
log.log_level(old_log_level)
228+
return true
229+
else
230+
log.log_level(old_log_level)
231+
return false
232+
end
212233
end
213234

214235
local function _is_not_sanitized_windows(str)
215-
if not string.match(str, "^\".*\"$") then
216-
return true
217-
else
218-
return false
219-
end
236+
local old_log_level = log.log_level()
237+
log.log_level(dtutils_string.log_level)
238+
if not string.match(str, "^\".*\"$") then
239+
log.log_level(old_log_level)
240+
return true
241+
else
242+
log.log_level(old_log_level)
243+
return false
244+
end
220245
end
221246

222247
function dtutils_string.is_not_sanitized(str)
248+
local old_log_level = log.log_level()
249+
log.log_level(dtutils_string.log_level)
223250
if dt.configuration.running_os == "windows" then
224-
return _is_not_sanitized_windows(str)
251+
log.log_level(old_log_level)
252+
return _is_not_sanitized_windows(str)
225253
else
226-
return _is_not_sanitized_posix(str)
254+
log.log_level(old_log_level)
255+
return _is_not_sanitized_posix(str)
227256
end
228257
end
229258

@@ -246,26 +275,38 @@ dtutils_string.libdoc.functions["sanitize"] = {
246275
}
247276

248277
local function _sanitize_posix(str)
278+
local old_log_level = log.log_level()
279+
log.log_level(dtutils_string.log_level)
249280
if _is_not_sanitized_posix(str) then
250-
return "'" .. string.gsub(str, "'", "'\\''") .. "'"
281+
log.log_level(old_log_level)
282+
return "'" .. string.gsub(str, "'", "'\\''") .. "'"
251283
else
252-
return str
284+
log.log_level(old_log_level)
285+
return str
253286
end
254287
end
255288

256289
local function _sanitize_windows(str)
290+
local old_log_level = log.log_level()
291+
log.log_level(dtutils_string.log_level)
257292
if _is_not_sanitized_windows(str) then
258-
return "\"" .. string.gsub(str, "\"", "\"^\"\"") .. "\""
293+
log.log_level(old_log_level)
294+
return "\"" .. string.gsub(str, "\"", "\"^\"\"") .. "\""
259295
else
260-
return str
296+
log.log_level(old_log_level)
297+
return str
261298
end
262299
end
263300

264301
function dtutils_string.sanitize(str)
302+
local old_log_level = log.log_level()
303+
log.log_level(dtutils_string.log_level)
265304
if dt.configuration.running_os == "windows" then
266-
return _sanitize_windows(str)
305+
log.log_level(old_log_level)
306+
return _sanitize_windows(str)
267307
else
268-
return _sanitize_posix(str)
308+
log.log_level(old_log_level)
309+
return _sanitize_posix(str)
269310
end
270311
end
271312

@@ -288,9 +329,12 @@ dtutils_string.libdoc.functions["sanitize_lua"] = {
288329
}
289330

290331
function dtutils_string.sanitize_lua(str)
332+
local old_log_level = log.log_level()
333+
log.log_level(dtutils_string.log_level)
291334
str = string.gsub(str, "%-", "%%-")
292335
str = string.gsub(str, "%(", "%%(")
293336
str = string.gsub(str, "%)", "%%)")
337+
log.log_level(old_log_level)
294338
return str
295339
end
296340

@@ -313,7 +357,9 @@ dtutils_string.libdoc.functions["split_filepath"] = {
313357
}
314358

315359
function dtutils_string.split_filepath(str)
316-
-- strip out single quotes from quoted pathnames
360+
local old_log_level = log.log_level()
361+
log.log_level(dtutils_string.log_level)
362+
-- strip out single quotes from quoted pathnames
317363
str = string.gsub(str, "'", "")
318364
str = string.gsub(str, '"', '')
319365
local result = {}
@@ -323,6 +369,7 @@ function dtutils_string.split_filepath(str)
323369
result["basename"] = result["filetype"]
324370
result["filetype"] = ""
325371
end
372+
log.log_level(old_log_level)
326373
return result
327374
end
328375

@@ -344,7 +391,10 @@ dtutils_string.libdoc.functions["get_path"] = {
344391
}
345392

346393
function dtutils_string.get_path(str)
394+
local old_log_level = log.log_level()
395+
log.log_level(dtutils_string.log_level)
347396
local parts = dtutils_string.split_filepath(str)
397+
log.log_level(old_log_level)
348398
return parts["path"]
349399
end
350400

@@ -366,7 +416,10 @@ dtutils_string.libdoc.functions["get_filename"] = {
366416
}
367417

368418
function dtutils_string.get_filename(str)
419+
local old_log_level = log.log_level()
420+
log.log_level(dtutils_string.log_level)
369421
local parts = dtutils_string.split_filepath(str)
422+
log.log_level(old_log_level)
370423
return parts["filename"]
371424
end
372425

@@ -389,7 +442,10 @@ dtutils_string.libdoc.functions["get_basename"] = {
389442
}
390443

391444
function dtutils_string.get_basename(str)
445+
local old_log_level = log.log_level()
446+
log.log_level(dtutils_string.log_level)
392447
local parts = dtutils_string.split_filepath(str)
448+
log.log_level(old_log_level)
393449
return parts["basename"]
394450
end
395451

@@ -411,7 +467,10 @@ dtutils_string.libdoc.functions["get_filetype"] = {
411467
}
412468

413469
function dtutils_string.get_filetype(str)
470+
local old_log_level = log.log_level()
471+
log.log_level(dtutils_string.log_level)
414472
local parts = dtutils_string.split_filepath(str)
473+
log.log_level(old_log_level)
415474
return parts["filetype"]
416475
end
417476

0 commit comments

Comments
 (0)