Fine-tune unwind behaviour

@see https://www.filebot.net/forums/viewtopic.php?f=8&t=5083
This commit is contained in:
Reinhard Pointner 2017-06-05 15:43:47 +08:00
parent ec4ddf40c4
commit 802f9703e4
3 changed files with 37 additions and 33 deletions

View File

@ -176,12 +176,12 @@ public abstract class ScriptShellBaseClass extends Script {
// define global variable: _system // define global variable: _system
public AssociativeScriptObject get_system() { public AssociativeScriptObject get_system() {
return new AssociativeScriptObject(System.getProperties()); return new AssociativeScriptObject(System.getProperties(), property -> null);
} }
// define global variable: _environment // define global variable: _environment
public AssociativeScriptObject get_environment() { public AssociativeScriptObject get_environment() {
return new AssociativeScriptObject(System.getenv()); return new AssociativeScriptObject(System.getenv(), property -> null);
} }
// Complete or session rename history // Complete or session rename history

View File

@ -10,6 +10,7 @@ import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.function.Function;
import groovy.lang.GroovyObjectSupport; import groovy.lang.GroovyObjectSupport;
@ -17,13 +18,33 @@ public class AssociativeScriptObject extends GroovyObjectSupport implements Iter
private final Map<Object, Object> properties; private final Map<Object, Object> properties;
private final Function<String, Object> defaultValue;
public AssociativeScriptObject(Map<?, ?> properties) { public AssociativeScriptObject(Map<?, ?> properties) {
this.properties = new LenientLookup(properties); this.properties = new LenientLookup(properties);
// throw MissingPropertyException
this.defaultValue = super::getProperty;
}
public AssociativeScriptObject(Map<?, ?> properties, Function<String, Object> defaultValue) {
this.properties = new LenientLookup(properties);
this.defaultValue = defaultValue;
} }
@Override @Override
public Object getProperty(String name) { public Object getProperty(String name) {
return properties.get(name); Object value = properties.get(name);
if (value == null) {
return defaultValue.apply(name);
}
return value;
}
public Object getDefaultProperty(String name) {
return super.getProperty(name);
} }
@Override @Override

View File

@ -37,6 +37,7 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.function.Function;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -728,7 +729,7 @@ public class MediaBindingBean {
if (infoObject instanceof Episode) { if (infoObject instanceof Episode) {
SortOrder order = SortOrder.forName(k); SortOrder order = SortOrder.forName(k);
Episode episode = fetchEpisode(getEpisode(), order, null); Episode episode = fetchEpisode(getEpisode(), order, null);
return createBindingObject(null, episode, null); return createBindingObject(episode);
} }
return undefined(k); return undefined(k);
}); });
@ -741,12 +742,12 @@ public class MediaBindingBean {
if (language != null && infoObject instanceof Movie) { if (language != null && infoObject instanceof Movie) {
Movie movie = TheMovieDB.getMovieDescriptor(getMovie(), language.getLocale()); Movie movie = TheMovieDB.getMovieDescriptor(getMovie(), language.getLocale());
return createBindingObject(null, movie, null); return createBindingObject(movie);
} }
if (language != null && infoObject instanceof Episode) { if (language != null && infoObject instanceof Episode) {
Episode episode = fetchEpisode(getEpisode(), null, language.getLocale()); Episode episode = fetchEpisode(getEpisode(), null, language.getLocale());
return createBindingObject(null, episode, null); return createBindingObject(episode);
} }
return undefined(k); return undefined(k);
@ -1061,14 +1062,14 @@ public class MediaBindingBean {
@Define("self") @Define("self")
public AssociativeScriptObject getSelf() { public AssociativeScriptObject getSelf() {
return createBindingObject(mediaFile, infoObject, context); return createBindingObject(mediaFile, infoObject, context, property -> null);
} }
@Define("model") @Define("model")
public List<AssociativeScriptObject> getModel() { public List<AssociativeScriptObject> getModel() {
List<AssociativeScriptObject> result = new ArrayList<AssociativeScriptObject>(); List<AssociativeScriptObject> result = new ArrayList<AssociativeScriptObject>();
for (Entry<File, ?> it : context.entrySet()) { for (Entry<File, ?> it : context.entrySet()) {
result.add(createBindingObject(it.getKey(), it.getValue(), context)); result.add(createBindingObject(it.getKey(), it.getValue(), context, property -> null));
} }
return result; return result;
} }
@ -1221,7 +1222,11 @@ public class MediaBindingBean {
}); });
} }
private AssociativeScriptObject createBindingObject(File file, Object info, Map<File, ?> context) { private AssociativeScriptObject createBindingObject(Object info) {
return createBindingObject(null, info, null, this::undefined);
}
private AssociativeScriptObject createBindingObject(File file, Object info, Map<File, ?> context, Function<String, Object> defaultValue) {
MediaBindingBean mediaBindingBean = new MediaBindingBean(info, file, context) { MediaBindingBean mediaBindingBean = new MediaBindingBean(info, file, context) {
@Override @Override
@ -1231,33 +1236,11 @@ public class MediaBindingBean {
} }
}; };
return new AssociativeScriptObject(new ExpressionBindings(mediaBindingBean)) { return new AssociativeScriptObject(new ExpressionBindings(mediaBindingBean), defaultValue);
@Override
public Object getProperty(String name) {
try {
return super.getProperty(name);
} catch (Exception e) {
return null; // never throw exceptions for empty or null values
}
}
};
} }
private AssociativeScriptObject createPropertyBindings(Object object) { private AssociativeScriptObject createPropertyBindings(Object object) {
return new AssociativeScriptObject(new PropertyBindings(object)) { return new AssociativeScriptObject(new PropertyBindings(object), this::undefined);
@Override
public Object getProperty(String name) {
Object value = super.getProperty(name);
if (value == null) {
return undefined(name);
}
return value;
}
};
} }
private List<AssociativeScriptObject> createMediaInfoBindings(StreamKind kind) { private List<AssociativeScriptObject> createMediaInfoBindings(StreamKind kind) {