From 7b82fce064b2cc7cc36fb9cbd93bc0a3d724540a Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 15 Apr 2012 15:14:53 +0000 Subject: [PATCH] * allow passing of variables directly into the groovy shell --- .../sourceforge/filebot/cli/ArgumentBean.java | 4 ++++ .../filebot/cli/ArgumentProcessor.java | 24 +++++++++++++++++-- .../sourceforge/filebot/cli/ScriptShell.java | 18 ++++++++++---- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/source/net/sourceforge/filebot/cli/ArgumentBean.java b/source/net/sourceforge/filebot/cli/ArgumentBean.java index 1b7d94b0..fbe081bd 100644 --- a/source/net/sourceforge/filebot/cli/ArgumentBean.java +++ b/source/net/sourceforge/filebot/cli/ArgumentBean.java @@ -13,6 +13,7 @@ import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.logging.Level; import org.kohsuke.args4j.Argument; @@ -104,6 +105,9 @@ public class ArgumentBean { @Argument public List arguments = new ArrayList(); + // optional parameters + public Map parameters; + public boolean runCLI() { return rename || getSubtitles || getMissingSubtitles || check || list || mediaInfo || extract || script != null; diff --git a/source/net/sourceforge/filebot/cli/ArgumentProcessor.java b/source/net/sourceforge/filebot/cli/ArgumentProcessor.java index cee0a3cd..b57299f9 100644 --- a/source/net/sourceforge/filebot/cli/ArgumentProcessor.java +++ b/source/net/sourceforge/filebot/cli/ArgumentProcessor.java @@ -8,8 +8,12 @@ import static net.sourceforge.tuned.FileUtilities.*; import java.io.File; import java.security.AccessController; +import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; import java.util.logging.Level; import javax.script.Bindings; @@ -28,8 +32,23 @@ public class ArgumentProcessor { final ArgumentBean bean = new ArgumentBean(); if (args != null && args.length > 0) { + List arguments = new ArrayList(); + Map parameters = new HashMap(); + + for (String it : args) { + if (it.startsWith("-X")) { + String[] pair = it.substring(2).split("=", 2); + if (pair.length == 2) { + parameters.put(pair[0], pair[1]); + } + } else { + arguments.add(it); + } + } + CmdLineParser parser = new CmdLineParser(bean); - parser.parseArgument(args); + parser.parseArgument(arguments); + bean.parameters = parameters; } return bean; @@ -92,7 +111,7 @@ public class ArgumentProcessor { bindings.put("args", args.getFiles(false)); Analytics.trackEvent("CLI", "ExecuteScript", args.getScriptLocation().getScheme()); - ScriptShell shell = new ScriptShell(cli, args, args.trustScript, AccessController.getContext()); + ScriptShell shell = new ScriptShell(cli, args, args.parameters, args.trustScript, AccessController.getContext()); shell.run(args.getScriptLocation(), bindings); } @@ -108,6 +127,7 @@ public class ArgumentProcessor { public void printHelp(ArgumentBean argumentBean) { new CmdLineParser(argumentBean).printUsage(System.out); + System.out.println(" -X= : Define script variable"); } } diff --git a/source/net/sourceforge/filebot/cli/ScriptShell.java b/source/net/sourceforge/filebot/cli/ScriptShell.java index 2cad7ca0..fee44a6c 100644 --- a/source/net/sourceforge/filebot/cli/ScriptShell.java +++ b/source/net/sourceforge/filebot/cli/ScriptShell.java @@ -23,6 +23,8 @@ import java.security.Permissions; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; import java.security.ProtectionDomain; +import java.util.Map; +import java.util.Map.Entry; import java.util.PropertyPermission; import javax.script.Bindings; @@ -51,12 +53,12 @@ class ScriptShell { private final boolean trustScript; - public ScriptShell(CmdlineInterface cli, ArgumentBean args, boolean trustScript, AccessControlContext acc) throws ScriptException { + public ScriptShell(CmdlineInterface cli, ArgumentBean args, Map parameters, boolean trustScript, AccessControlContext acc) throws ScriptException { this.trustScript = trustScript; // setup script context ScriptContext context = new SimpleScriptContext(); - context.setBindings(initializeBindings(cli, args, acc), ScriptContext.GLOBAL_SCOPE); + context.setBindings(initializeBindings(cli, args, parameters, acc), ScriptContext.GLOBAL_SCOPE); engine.setContext(context); // import additional functions into the shell environment @@ -65,9 +67,14 @@ class ScriptShell { } - protected Bindings initializeBindings(CmdlineInterface cli, ArgumentBean args, AccessControlContext acc) { + protected Bindings initializeBindings(CmdlineInterface cli, ArgumentBean args, Map parameters, AccessControlContext acc) { Bindings bindings = new SimpleBindings(); + // bind external parameters + for (Entry it : parameters.entrySet()) { + bindings.put(it.getKey(), it.getValue()); + } + // bind API objects bindings.put("_cli", PrivilegedInvocation.newProxy(CmdlineInterface.class, cli, acc)); bindings.put("_script", new File(args.script)); @@ -77,8 +84,9 @@ class ScriptShell { bindings.put("_log", CLILogger); // bind Java properties and environment variables - bindings.put("_prop", new AssociativeScriptObject(System.getProperties())); - bindings.put("_env", new AssociativeScriptObject(System.getenv())); + bindings.put("_parameter", new AssociativeScriptObject(parameters)); + bindings.put("_system", new AssociativeScriptObject(System.getProperties())); + bindings.put("_environment", new AssociativeScriptObject(System.getenv())); // bind console object bindings.put("console", System.console());