Merge pull request #261 from scheckmedia/master

move function to create temp files to dtutils/file.lua
This commit is contained in:
supertobi
2020-06-15 14:08:03 +02:00
committed by GitHub
2 changed files with 45 additions and 30 deletions

View File

@@ -227,24 +227,8 @@ function PHOTILS.attach_tags()
dt.print(_("Tags successfully attached to image")) dt.print(_("Tags successfully attached to image"))
end end
function PHOTILS.get_tmp_file()
local tmp_file = os.tmpname()
if dt.configuration.running_os == "windows" then
tmp_file = dt.configuration.tmp_dir .. tmp_file -- windows os.tmpname() defaults to root directory
end
local f = io.open(tmp_file, "w")
if not f then
dt.print_log(string.format(_("Error writing to `%s`"), tmp_file))
os.remove(tmp_file)
return nil
end
return tmp_file
end
function PHOTILS.get_tags(image, with_export) function PHOTILS.get_tags(image, with_export)
local tmp_file = PHOTILS.get_tmp_file() local tmp_file = df.create_tmp_file()
local in_arg = df.sanitize_filename(tostring(image)) local in_arg = df.sanitize_filename(tostring(image))
local out_arg = df.sanitize_filename(tmp_file) local out_arg = df.sanitize_filename(tmp_file)
local executable = photils_installed local executable = photils_installed
@@ -255,7 +239,7 @@ function PHOTILS.get_tags(image, with_export)
if with_export then if with_export then
dt.print_log("use export to for prediction") dt.print_log("use export to for prediction")
local export_file = PHOTILS.get_tmp_file() local export_file = df.create_tmp_file()
exporter:write_image(image, export_file) exporter:write_image(image, export_file)
in_arg = df.sanitize_filename(tostring(export_file)) in_arg = df.sanitize_filename(tostring(export_file))
end end

View File

@@ -44,7 +44,7 @@ dtutils_file.libdoc.functions["check_if_bin_exists"] = {
bin - string - the binary to check for]], bin - string - the binary to check for]],
Description = [[check_if_bin_exists checks to see if the specified binary exists. Description = [[check_if_bin_exists checks to see if the specified binary exists.
check_if_bin_exists first checks to see if a preference for the binary has been check_if_bin_exists first checks to see if a preference for the binary has been
registered and uses that if found. The presence of the file is verified, then registered and uses that if found. The presence of the file is verified, then
quoted and returned. If no preference is specified and the operating system is quoted and returned. If no preference is specified and the operating system is
linux then the which command is used to check for a binary in the path. If found linux then the which command is used to check for a binary in the path. If found
that path is returned. If no binary is found, false is returned.]], that path is returned. If no binary is found, false is returned.]],
@@ -61,7 +61,7 @@ function dtutils_file.check_if_bin_exists(bin)
local result = false local result = false
local path = nil local path = nil
if string.match(bin, "/") or string.match(bin, "\\") then if string.match(bin, "/") or string.match(bin, "\\") then
path = bin path = bin
else else
path = dtutils_file.get_executable_path_preference(bin) path = dtutils_file.get_executable_path_preference(bin)
@@ -233,7 +233,7 @@ function dtutils_file.check_if_file_exists(filepath)
local p = io.popen("if exist " .. dtutils_file.sanitize_filename(filepath) .. " (echo 'yes') else (echo 'no')") local p = io.popen("if exist " .. dtutils_file.sanitize_filename(filepath) .. " (echo 'yes') else (echo 'no')")
local ans = p:read("*all") local ans = p:read("*all")
p:close() p:close()
if string.match(ans, "yes") then if string.match(ans, "yes") then
result = true result = true
end end
-- result = os.execute('if exist "'..filepath..'" (cmd /c exit 0) else (cmd /c exit 1)') -- result = os.execute('if exist "'..filepath..'" (cmd /c exit 0) else (cmd /c exit 1)')
@@ -383,8 +383,8 @@ dtutils_file.libdoc.functions["filename_increment"] = {
local result = df.filename_increment(filepath) local result = df.filename_increment(filepath)
filepath - string - filename to increment]], filepath - string - filename to increment]],
Description = [[filename_increment solves the problem of filename confllict by adding an Description = [[filename_increment solves the problem of filename confllict by adding an
increment to the filename. If the supplied filename has no increment then increment to the filename. If the supplied filename has no increment then
"01" is added to the basename. If the filename already has an increment, then "01" is added to the basename. If the filename already has an increment, then
1 is added to it and the filename returned.]], 1 is added to it and the filename returned.]],
Return_Value = [[result - string - the incremented filename]], Return_Value = [[result - string - the incremented filename]],
@@ -436,7 +436,7 @@ dtutils_file.libdoc.functions["create_unique_filename"] = {
filepath - string - the path and filename requested]], filepath - string - the path and filename requested]],
Description = [[create_unique_filename takes a requested filepath and checks to see if Description = [[create_unique_filename takes a requested filepath and checks to see if
it exists. If if doesn't then it's returned intact. If it already exists, then a two it exists. If if doesn't then it's returned intact. If it already exists, then a two
digit increment is added to the filename and it is tested again. The increment keeps digit increment is added to the filename and it is tested again. The increment keeps
increasing until either a unique filename is found or there have been 100 attempts.]], increasing until either a unique filename is found or there have been 100 attempts.]],
Return_Value = [[result - string - the incremented filename]], Return_Value = [[result - string - the incremented filename]],
Limitations = [[create_unique_filename will only attempt 100 increments.]], Limitations = [[create_unique_filename will only attempt 100 increments.]],
@@ -515,7 +515,7 @@ dtutils_file.libdoc.functions["executable_path_widget"] = {
local widget = df.executable_path_widget(executables) local widget = df.executable_path_widget(executables)
executables - table - a table of strings that are executable names]], executables - table - a table of strings that are executable names]],
Description = [[executable_path_widget takes a table of executable names Description = [[executable_path_widget takes a table of executable names
and builds a set of file selector widgets to get the path to the executable. and builds a set of file selector widgets to get the path to the executable.
The resulting widgets are wrapped in a box widget and returned.]], The resulting widgets are wrapped in a box widget and returned.]],
Return_Value = [[widget - widget - a widget containing a file selector widget for Return_Value = [[widget - widget - a widget containing a file selector widget for
each executable.]], each executable.]],
@@ -530,10 +530,10 @@ dtutils_file.libdoc.functions["executable_path_widget"] = {
function dtutils_file.executable_path_widget(executables) function dtutils_file.executable_path_widget(executables)
local box_widgets = {} local box_widgets = {}
table.insert(box_widgets, dt.new_widget("section_label"){label = "select executable(s)"}) table.insert(box_widgets, dt.new_widget("section_label"){label = "select executable(s)"})
for _, executable in pairs(executables) do for _, executable in pairs(executables) do
table.insert(box_widgets, dt.new_widget("label"){label = "select " .. executable .. " executable"}) table.insert(box_widgets, dt.new_widget("label"){label = "select " .. executable .. " executable"})
local path = dtutils_file.get_executable_path_preference(executable) local path = dtutils_file.get_executable_path_preference(executable)
if not path then if not path then
path = "" path = ""
end end
table.insert(box_widgets, dt.new_widget("file_chooser_button"){ table.insert(box_widgets, dt.new_widget("file_chooser_button"){
@@ -562,7 +562,7 @@ dtutils_file.libdoc.functions["sanitize_filename"] = {
local sanitized_filename = df.sanitize_filename(filename) local sanitized_filename = df.sanitize_filename(filename)
filename - string - a filepath and filename]], filename - string - a filepath and filename]],
Description = [[sanitize_file places quotes around the filename in an Description = [[sanitize_file places quotes around the filename in an
operating system specific manner. The result is safe to pass as operating system specific manner. The result is safe to pass as
an argument to the operating system.]], an argument to the operating system.]],
Return_Value = [[sanitized_filename - string - quoted filename]], Return_Value = [[sanitized_filename - string - quoted filename]],
Limitations = [[]], Limitations = [[]],
@@ -584,7 +584,7 @@ dtutils_file.libdoc.functions["mkdir"] = {
df.mkdir(path) df.mkdir(path)
path - string - a directory path]], path - string - a directory path]],
Description = [[mkdir creates directories if not already exists. It Description = [[mkdir creates directories if not already exists. It
create whole parents subtree if needed create whole parents subtree if needed
]], ]],
Return_Value = [[path - string - a directory path]], Return_Value = [[path - string - a directory path]],
@@ -596,7 +596,7 @@ dtutils_file.libdoc.functions["mkdir"] = {
Copyright = [[]], Copyright = [[]],
} }
function dtutils_file.mkdir(path) function dtutils_file.mkdir(path)
if not dtutils_file.check_if_file_exists(path) then if not dtutils_file.check_if_file_exists(path) then
local mkdir_cmd = dt.configuration.running_os == "windows" and "mkdir" or "mkdir -p" local mkdir_cmd = dt.configuration.running_os == "windows" and "mkdir" or "mkdir -p"
return dsys.external_command(mkdir_cmd.." "..path) return dsys.external_command(mkdir_cmd.." "..path)
@@ -627,6 +627,37 @@ function dtutils_file.rmdir(path)
return dsys.external_command(rm_cmd.." "..path) return dsys.external_command(rm_cmd.." "..path)
end end
dtutils_file.libdoc.functions["create_tmp_file"] = {
Name = [[create_tmp_file]],
Synopsis = [[creates a temporary file]],
Usage = [[local df = require "lib/dtutils.file
local result = df.create_tmp_file()]],
Description = [[create_tmp_file can be used to create temporary files]],
Return_Value = [[result - string - path to the created temporary file.]],
Limitations = [[]],
Example = [[]],
See_Also = [[]],
Reference = [[]],
License = [[]],
Copyright = [[]],
}
function dtutils_file.create_tmp_file()
local tmp_file = os.tmpname()
if dt.configuration.running_os == "windows" then
tmp_file = dt.configuration.tmp_dir .. tmp_file -- windows os.tmpname() defaults to root directory
end
local f = io.open(tmp_file, "w")
if not f then
log.msg(log.error, string.format("Error writing to `%s`", tmp_file))
os.remove(tmp_file)
return nil
end
return tmp_file
end
return dtutils_file return dtutils_file