<commit_before>local LrTasks = import 'LrTasks'
local LrFileUtils = import 'LrFileUtils'
local LrApplication = import 'LrApplication'

local logger = import 'LrLogger'( 'IncameraFujiRatingConverter' )
logger:enable( 'print' )
logger:info('-------------------- Execution start --------------------')

-- TODO: create a settings panel in plugin manager to set this value
local exiftoolPath
if (MAC_ENV) then
  exiftoolPath = '/usr/local/bin/exiftool'
else
  exiftoolPath = 'C:\\\Windows\\\exiftool.exe'
end

function main()
  local catalog = LrApplication.activeCatalog()
  local targetPhotos = catalog.targetPhotos
  for i, photo in ipairs(targetPhotos) do
    -- TODO: show some kind of progress bar and/or notification once the ratings are converted
    LrTasks.startAsyncTask(function()
      local filePath = photo.path
      local fileWithoutExtension = GetFilePathWithoutExtension(filePath);
      setRatingForJpgAndRaf(fileWithoutExtension)
    end)
  end
end

function GetFilePathWithoutExtension(url)
  return url:match("^([^.]+)")
end

function setRatingForJpgAndRaf(fileWithoutExtension)
  local jpgFile = GetJpgFile(fileWithoutExtension)

  if jpgFile then
    local rafFile = GetRafFile(fileWithoutExtension)
    local cmdToGetRating = getCmdForFetchingIncameraRating(jpgFile)
    setRating(jpgFile, cmdToGetRating)

    if rafFile then
      local xmpFile = fileWithoutExtension .. '.xmp'
      setRating(xmpFile, cmdToGetRating)
    end
  end
end

function GetJpgFile(fileWithoutExtension)
  -- TODO: make this work by ignoring letter case
  local supportedJpgFileExtensions = {'.JPG', '.jpg'}
  return GetFile(fileWithoutExtension, supportedJpgFileExtensions)
end

function GetRafFile(fileWithoutExtension)
  -- TODO: make this work by ignoring letter case
  local supportedRafFileExtensions = {'.RAF', '.raf'}
  return GetFile(fileWithoutExtension, supportedRafFileExtensions)
end

function GetFile(fileWithoutExtension, extensions)
  for i, extension in ipairs(extensions) do
    local fileWithExtension = fileWithoutExtension .. extension
		if LrFileUtils.exists(fileWithExtension) and LrFileUtils.isReadable(fileWithExtension) then
      logger:info('Found file: ', fileWithExtension)
      return fileWithExtension
    end
  end
end

function getCmdForFetchingIncameraRating(jpgFile)
  -- FIXME: this is not going to work on windows
  return exiftoolPath .. ' -rating "' .. jpgFile .. '" | grep -o "[^ ]*$"'
end

function setRating(file, cmdToGetRating)
  -- TODO: find a way to get the results of cmdToGetRating so we don't have to repeat/nest the bash statement
  logger:info('Setting rating for file: ', file)
  -- FIXME: this is not going to work on windows (nested bash command)
  LrTasks.execute(exiftoolPath .. ' -overwrite_original -rating=$('.. cmdToGetRating ..') "' .. file .. '"')
end

-- THIS FUNCTION DOES NOT WORK
-- LrTasks.execute does not return the output of exiftool
-- TODO: how to read output from LrTasks.execute?
function GetRatingFromFile(file)
  local cmd = exiftoolPath .. ' -rating ' .. file .. ' | grep -o "[^ ]*$"'
  local rating = LrTasks.execute(cmd)
  logger:info('Found rating: ', rating)
  return rating
end

main()<commit_msg>Read jpg rating once by using tmp file, this should work on Windows too<commit_after>local LrTasks = import 'LrTasks'
local LrFileUtils = import 'LrFileUtils'
local LrApplication = import 'LrApplication'

local logger = import 'LrLogger'('IncameraFujiRatingConverter')
logger:enable('print')
logger:info(' -------------------- Execution start --------------------')

-- TODO: create a settings panel in plugin manager to set this value
local exiftoolPath
if (MAC_ENV) then
  exiftoolPath = '/usr/local/bin/exiftool'
else
  exiftoolPath = 'C:\\\Windows\\\exiftool.exe'
end

function main()
  local catalog = LrApplication.activeCatalog()
  local targetPhotos = catalog.targetPhotos
  for i, photo in ipairs(targetPhotos) do
    -- TODO: show some kind of progress bar and/or notification once the ratings are converted
    LrTasks.startAsyncTask(function()
      local filePath = photo.path
      local fileWithoutExtension = GetFilePathWithoutExtension(filePath);
      setRatingForJpgAndRaf(fileWithoutExtension)
    end)
  end
end

function GetFilePathWithoutExtension(url)
  return url:match("^([^.]+)")
end

function setRatingForJpgAndRaf(fileWithoutExtension)
  local jpgFile = GetJpgFile(fileWithoutExtension)

  if jpgFile then
    local rating = GetRatingFromFile(jpgFile)
    if rating then
      local rafFile = GetRafFile(fileWithoutExtension)
      setRating(jpgFile, rating)

      if rafFile then
        local xmpFile = fileWithoutExtension .. '.xmp'
        setRating(xmpFile, rating)
      end
    end
  end
end

function GetJpgFile(fileWithoutExtension)
  -- TODO: make this work by ignoring letter case
  local supportedJpgFileExtensions = {'.JPG', '.jpg'}
  return GetFile(fileWithoutExtension, supportedJpgFileExtensions)
end

function GetRafFile(fileWithoutExtension)
  -- TODO: make this work by ignoring letter case
  local supportedRafFileExtensions = {'.RAF', '.raf'}
  return GetFile(fileWithoutExtension, supportedRafFileExtensions)
end

function GetFile(fileWithoutExtension, extensions)
  for i, extension in ipairs(extensions) do
    local fileWithExtension = fileWithoutExtension .. extension
		if LrFileUtils.exists(fileWithExtension) and LrFileUtils.isReadable(fileWithExtension) then
      logger:info(' Found file: ', fileWithExtension)
      return fileWithExtension
    end
  end
end

function setRating(file, rating)
  logger:info(' Setting rating for file: ', file, ' rating: ', rating)
  LrTasks.execute(exiftoolPath .. ' -overwrite_original -rating=' .. rating .. ' "' .. file .. '"')
end

function GetRatingFromFile(file)
  local tmpFileForRatingOutput = os.tmpname()
  local result = LrTasks.execute(exiftoolPath .. ' -rating "' .. file .. '" > ' .. tmpFileForRatingOutput)
  if (result ~= 0) then
    logger:error(' Error when writing rating to tmp file', result)
    return nil
  else
    local exiftoolOutput = readFile(tmpFileForRatingOutput)
    return tonumber(exiftoolOutput:sub(-2))
  end
end

function readFile(path)
    local file = io.open(path, "rb")
    if not file then return nil end
    local content = file:read "*a"
    file:close()
    return content
end

main()
{'fraction_non_alphanumeric': 0.10401141588710956, 'fraction_numerical': 0.00031710797526557794, 'mean_word_length': 4.070739549839228, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '16719', 'n_tokens_mistral': 1889, 'n_tokens_neox': 1833, 'n_words': 548}