From 26d3b51a8439e0103ed9b66c547803a626a4b66f Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 20 Apr 2014 13:09:01 +0000 Subject: [PATCH] * support simple rename function call that takes care of all the cmdline logging / rename action / conflict action / history --- source/net/filebot/cli/CmdlineInterface.java | 3 ++ source/net/filebot/cli/CmdlineOperations.java | 8 ++++- .../net/filebot/cli/ScriptShellBaseClass.java | 29 ++++++++++++++++--- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/source/net/filebot/cli/CmdlineInterface.java b/source/net/filebot/cli/CmdlineInterface.java index cfb5ca6d..5d05573a 100644 --- a/source/net/filebot/cli/CmdlineInterface.java +++ b/source/net/filebot/cli/CmdlineInterface.java @@ -4,6 +4,7 @@ import java.io.File; import java.io.FileFilter; import java.util.Collection; import java.util.List; +import java.util.Map; import net.filebot.RenameAction; @@ -11,6 +12,8 @@ public interface CmdlineInterface { List rename(Collection files, RenameAction action, String conflict, String output, String format, String db, String query, String sortOrder, String filter, String lang, boolean strict) throws Exception; + List rename(Map renameMap, RenameAction renameAction, String conflict) throws Exception; + List getSubtitles(Collection files, String db, String query, String lang, String output, String encoding, String format, boolean strict) throws Exception; List getMissingSubtitles(Collection files, String db, String query, String lang, String output, String encoding, String format, boolean strict) throws Exception; diff --git a/source/net/filebot/cli/CmdlineOperations.java b/source/net/filebot/cli/CmdlineOperations.java index 56d48b0f..441650bb 100644 --- a/source/net/filebot/cli/CmdlineOperations.java +++ b/source/net/filebot/cli/CmdlineOperations.java @@ -138,6 +138,12 @@ public class CmdlineOperations implements CmdlineInterface { } } + @Override + public List rename(Map renameMap, RenameAction renameAction, String conflict) throws Exception { + // generic rename function that can be passed any set of files + return renameAll(renameMap, renameAction, ConflictAction.forName(conflict), null); + } + public List renameSeries(Collection files, RenameAction renameAction, ConflictAction conflictAction, File outputDir, ExpressionFormat format, EpisodeListProvider db, String query, SortOrder sortOrder, ExpressionFilter filter, Locale locale, boolean strict) throws Exception { CLILogger.config(format("Rename episodes using [%s]", db.getName())); @@ -554,7 +560,7 @@ public class CmdlineOperations implements CmdlineInterface { public List renameAll(Map renameMap, RenameAction renameAction, ConflictAction conflictAction, List> matches) throws Exception { if (renameMap.isEmpty()) { - throw new Exception("Unable to identify and process any files"); + throw new Exception("Unable to identify or process any files"); } // rename files diff --git a/source/net/filebot/cli/ScriptShellBaseClass.java b/source/net/filebot/cli/ScriptShellBaseClass.java index 56ab37a2..614857cc 100644 --- a/source/net/filebot/cli/ScriptShellBaseClass.java +++ b/source/net/filebot/cli/ScriptShellBaseClass.java @@ -299,6 +299,10 @@ public abstract class ScriptShellBaseClass extends Script { synchronized (cli) { try { + if (input.isEmpty() && !getInputFileMap(parameters).isEmpty()) { + return cli.rename(getInputFileMap(parameters), action, asString(option.get(Option.conflict))); + } + return cli.rename(input, action, asString(option.get(Option.conflict)), asString(option.get(Option.output)), asString(option.get(Option.format)), asString(option.get(Option.db)), asString(option.get(Option.query)), asString(option.get(Option.order)), asString(option.get(Option.filter)), asString(option.get(Option.lang)), strict); } catch (Exception e) { printException(e); @@ -406,18 +410,35 @@ public abstract class ScriptShellBaseClass extends Script { } } - private List getInputFileList(Map map) { - Object file = map.get("file"); + private List getInputFileList(Map parameters) { + Object file = parameters.get("file"); if (file != null) { return FileUtilities.asFileList(file); } - Object folder = map.get("folder"); + Object folder = parameters.get("folder"); if (folder != null) { return FileUtilities.listFiles(FileUtilities.asFileList(folder), 0, false, true, false); } - throw new IllegalArgumentException("file is not set"); + return emptyList(); + } + + private Map getInputFileMap(Map parameters) { + Map map = (Map) parameters.get("map"); + Map files = new LinkedHashMap(); + if (map != null) { + for (Entry it : map.entrySet()) { + List key = FileUtilities.asFileList(it.getKey()); + List value = FileUtilities.asFileList(it.getValue()); + if (key.size() == 1 && value.size() == 1) { + files.put(key.get(0), value.get(0)); + } else { + throw new IllegalArgumentException("Illegal file mapping: " + it); + } + } + } + return files; } private Map getDefaultOptions(Map parameters) throws Exception {