Skip to content

Commit

Permalink
Allow @file.groovy syntax in Format Editor and Preset Editor (e…
Browse files Browse the repository at this point in the history
….g. `@/path/to/MyFormat.groovy`)
  • Loading branch information
rednoah committed May 26, 2019
1 parent d6062c9 commit 97aab5e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
Next Release (4.8.6)
====================
* Improved `SelectDialog` with thumbnails and tooltips
* Enhanced `Selection Dialog` with thumbnails and tooltips
* Added `{history}` binding for looking up the original file path of `{f}` (e.g. useful for `-exec` post-processing commands)
* Evaluate `{closures}` automatically in `String.plus(Closure)` constructs (e.g. `{"[" + {n} + " " + {s00e00} + "]"}`)
* Ensure that `ActionPopup` is always displayed on top of the Windows Task Bar
* Improved `-mediainfo -exec` pipeline
* Added `-no-history` CLI option
* Allow `@file.groovy` syntax in `Format Editor` and `Preset Editor` (e.g. `@/path/to/MyFormat.groovy`)
* Allow `*.groovy` files as argument value for `--format`, `--filter` and `--file-filter` CLI options (e.g. `--format /path/to/MyFormat.groovy`)


Expand Down
2 changes: 1 addition & 1 deletion source/net/filebot/format/ExpressionFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ExpressionFilter {

public ExpressionFilter(String expression) throws ScriptException {
this.expression = expression;
this.compiledExpression = new SecureCompiledScript(compileScriptlet(expression));
this.compiledExpression = new SecureCompiledScript(compileScriptlet(asExpression(expression)));
}

public String getExpression() {
Expand Down
21 changes: 20 additions & 1 deletion source/net/filebot/format/ExpressionFormat.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package net.filebot.format;

import static net.filebot.util.ExceptionUtilities.*;
import static net.filebot.util.FileUtilities.*;

import java.io.File;
import java.security.AccessController;
import java.text.FieldPosition;
import java.text.Format;
Expand Down Expand Up @@ -39,7 +41,7 @@ public class ExpressionFormat extends Format {

public ExpressionFormat(String expression) throws ScriptException {
this.expression = expression;
this.compilation = secure(compile(expression));
this.compilation = secure(compile(asExpression(expression)));
}

public String getExpression() {
Expand Down Expand Up @@ -253,6 +255,23 @@ protected static CompiledScript compileScriptlet(String expression) throws Scrip
}
}

protected static String asExpression(String s) {
// try as file path
if (s.startsWith("@") && s.endsWith(".groovy")) {
File f = new File(s.substring(1));
if (f.isFile()) {
try {
return readTextFile(f);
} catch (Exception e) {
throw new IllegalArgumentException("Failed to read text file: " + f, e);
}
}
}

// or default to literal value
return s;
}

private static class Variable extends CompiledScript {

private String name;
Expand Down

0 comments on commit 97aab5e

Please sign in to comment.