From ba772e03c84573ff49825f16f4896289466be1ef Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Thu, 5 Jan 2017 17:04:40 +0800 Subject: [PATCH] Catch exceptions and log as warning before the interrupt the program flow (i.e. when using -mediainfo and -list with custom --format options that yield empty strings) --- source/net/filebot/cli/ArgumentProcessor.java | 26 +++++++++++++------ source/net/filebot/cli/CmdlineOperations.java | 11 +++++--- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/source/net/filebot/cli/ArgumentProcessor.java b/source/net/filebot/cli/ArgumentProcessor.java index 7b50a8dd..9d232621 100644 --- a/source/net/filebot/cli/ArgumentProcessor.java +++ b/source/net/filebot/cli/ArgumentProcessor.java @@ -56,22 +56,17 @@ public class ArgumentProcessor { // print episode info if (args.list) { - List lines = cli.fetchEpisodeList(args.getDatasource(), args.getSearchQuery(), args.getExpressionFormat(), args.getExpressionFilter(), args.getSortOrder(), args.getLanguage().getLocale()); - lines.forEach(System.out::println); - return lines.isEmpty() ? 1 : 0; + return print(cli.fetchEpisodeList(args.getDatasource(), args.getSearchQuery(), args.getExpressionFormat(), args.getExpressionFilter(), args.getSortOrder(), args.getLanguage().getLocale())); } // print media info if (args.mediaInfo) { - List lines = cli.getMediaInfo(args.getFiles(true), args.getExpressionFileFilter(), args.getExpressionFormat()); - lines.forEach(System.out::println); - return lines.isEmpty() ? 1 : 0; + return print(cli.getMediaInfo(args.getFiles(true), args.getExpressionFileFilter(), args.getExpressionFormat())); } // revert files if (args.revert) { - List files = cli.revert(args.getFiles(false), args.getExpressionFileFilter(), args.getRenameAction()); - return files.isEmpty() ? 1 : 0; + return cli.revert(args.getFiles(false), args.getExpressionFileFilter(), args.getRenameAction()).isEmpty() ? 1 : 0; } // file operations @@ -103,6 +98,21 @@ public class ArgumentProcessor { return 0; } + private int print(List values) { + int lines = 0; + + for (int i = 0; i < values.size(); i++) { + try { + System.out.println(values.get(i)); + lines++; + } catch (Exception e) { + debug.warning(e::getMessage); + } + } + + return lines == 0 ? 1 : 0; + } + public void runScript(CmdlineInterface cli, ArgumentBean args) throws Throwable { Bindings bindings = new SimpleBindings(); bindings.put(ScriptShell.SHELL_ARGS_BINDING_NAME, args); diff --git a/source/net/filebot/cli/CmdlineOperations.java b/source/net/filebot/cli/CmdlineOperations.java index 6fd017bf..d2387989 100644 --- a/source/net/filebot/cli/CmdlineOperations.java +++ b/source/net/filebot/cli/CmdlineOperations.java @@ -995,9 +995,10 @@ public class CmdlineOperations implements CmdlineInterface { List episodes = applyExpressionFilter(service.getEpisodeList(options.get(0), order, locale), filter); Map context = new EntryList(null, episodes); - return episodes.stream().map(episode -> { - return format != null ? format.format(new MediaBindingBean(episode, null, context)) : EpisodeFormat.SeasonEpisode.format(episode); - }).collect(toList()); + // lazy format + return new FunctionList(episodes, e -> { + return format != null ? format.format(new MediaBindingBean(e, null, context)) : EpisodeFormat.SeasonEpisode.format(e); + }); } @Override @@ -1013,7 +1014,9 @@ public class CmdlineOperations implements CmdlineInterface { ExpressionFormat formatter = format != null ? format : new ExpressionFormat("{fn} [{resolution} {vc} {channels} {ac} {minutes}m]"); // lazy format - return new FunctionList(selection, f -> formatter.format(new MediaBindingBean(xattr.getMetaInfo(f), f))); + return new FunctionList(selection, f -> { + return formatter.format(new MediaBindingBean(xattr.getMetaInfo(f), f)); + }); } @Override