mirror of
https://github.com/RatzzFatzz/MKVAudioSubtitleChanger.git
synced 2026-02-11 02:05:56 +01:00
Fix last-execution file saving
This commit is contained in:
@@ -72,17 +72,22 @@ public class Config {
|
|||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return new StringJoiner(", ", Config.class.getSimpleName() + "[", "]")
|
return new StringJoiner(", ", Config.class.getSimpleName() + "[", "]")
|
||||||
.add("parser=" + parser).add("\n")
|
.add("parser=" + parser)
|
||||||
.add("formatter=" + formatter).add("\n")
|
.add("formatter=" + formatter)
|
||||||
.add("configPath=" + configPath).add("\n")
|
.add("configPath=" + configPath)
|
||||||
.add("libraryPath=" + libraryPath).add("\n")
|
.add("libraryPath=" + libraryPath)
|
||||||
.add("isSafeMode=" + safeMode).add("\n")
|
.add("mkvToolNix=" + mkvToolNix)
|
||||||
.add("forcedKeywords=" + forcedKeywords).add("\n")
|
.add("threads=" + threads)
|
||||||
.add("commentaryKeywords=" + commentaryKeywords).add("\n")
|
.add("includePattern=" + includePattern)
|
||||||
.add("excludedDirectories=" + excludedDirectories).add("\n")
|
.add("safeMode=" + safeMode)
|
||||||
.add("threadCount=" + threads).add("\n")
|
.add("coherent=" + coherent)
|
||||||
.add("includePattern=" + includePattern).add("\n")
|
.add("forceCoherent=" + forceCoherent)
|
||||||
.add("mkvToolNixPath='" + mkvToolNix + "'").add("\n")
|
.add("onlyNewFiles=" + onlyNewFiles)
|
||||||
|
.add("filterDate=" + filterDate)
|
||||||
|
.add("forcedKeywords=" + forcedKeywords)
|
||||||
|
.add("commentaryKeywords=" + commentaryKeywords)
|
||||||
|
.add("excludedDirectories=" + excludedDirectories)
|
||||||
|
.add("preferredSubtitles=" + preferredSubtitles)
|
||||||
.add("attributeConfig=" + attributeConfig)
|
.add("attributeConfig=" + attributeConfig)
|
||||||
.toString();
|
.toString();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,155 +6,165 @@ import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty;
|
|||||||
import at.pcgamingfreaks.yaml.YAML;
|
import at.pcgamingfreaks.yaml.YAML;
|
||||||
import at.pcgamingfreaks.yaml.YamlKeyNotFoundException;
|
import at.pcgamingfreaks.yaml.YamlKeyNotFoundException;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.cli.CommandLine;
|
import org.apache.commons.cli.CommandLine;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.StringJoiner;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public abstract class ConfigValidator<FieldType> {
|
public abstract class ConfigValidator<FieldType> {
|
||||||
protected final ConfigProperty property;
|
protected final ConfigProperty property;
|
||||||
protected final boolean required;
|
protected final boolean required;
|
||||||
protected final FieldType defaultValue;
|
protected final FieldType defaultValue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate the user input. Parameters of cmd are prioritised.
|
* Validate the user input. Parameters of cmd are prioritised.
|
||||||
*
|
*
|
||||||
* @param yaml config file
|
* @param yaml config file
|
||||||
* @param cmd command line parameters
|
* @param cmd command line parameters
|
||||||
* @return {@link ValidationResult} containing validity of input.
|
* @return {@link ValidationResult} containing validity of input.
|
||||||
*/
|
*/
|
||||||
public ValidationResult validate(YAML yaml, CommandLine cmd) {
|
public ValidationResult validate(YAML yaml, CommandLine cmd) {
|
||||||
System.out.printf("%s: ", property.prop());
|
System.out.printf("%s: ", property.prop());
|
||||||
FieldType result;
|
FieldType result;
|
||||||
|
|
||||||
Optional<FieldType> cmdResult = provideDataCmd().apply(cmd, property);
|
Optional<FieldType> cmdResult = provideDataCmd().apply(cmd, property);
|
||||||
Optional<FieldType> yamlResult = provideDataYaml().apply(yaml, property);
|
Optional<FieldType> yamlResult = provideDataYaml().apply(yaml, property);
|
||||||
|
|
||||||
if (isOverwritingNecessary()){
|
if (isOverwritingNecessary()) {
|
||||||
result = overwriteValue();
|
result = overwriteValue();
|
||||||
} else if (cmdResult.isPresent()) {
|
} else if (cmdResult.isPresent()) {
|
||||||
result = cmdResult.get();
|
result = cmdResult.get();
|
||||||
} else if (yamlResult.isPresent()) {
|
} else if (yamlResult.isPresent()) {
|
||||||
result = yamlResult.get();
|
result = yamlResult.get();
|
||||||
} else {
|
} else {
|
||||||
if (defaultValue != null) {
|
if (defaultValue != null) {
|
||||||
if (setValue(defaultValue)) {
|
if (setValue(defaultValue)) {
|
||||||
System.out.println("default");
|
System.out.println("default");
|
||||||
return ValidationResult.DEFAULT;
|
return ValidationResult.DEFAULT;
|
||||||
} else {
|
} else {
|
||||||
System.out.println("invalid");
|
System.out.println("invalid");
|
||||||
return ValidationResult.INVALID;
|
return ValidationResult.INVALID;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (required) {
|
if (required) {
|
||||||
System.out.println("missing");
|
System.out.println("missing");
|
||||||
return ValidationResult.MISSING;
|
return ValidationResult.MISSING;
|
||||||
} else {
|
} else {
|
||||||
System.out.println("ok");
|
System.out.println("ok");
|
||||||
return ValidationResult.NOT_PRESENT;
|
return ValidationResult.NOT_PRESENT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isValid(result) || !setValue(result)) {
|
if (!isValid(result) || !setValue(result)) {
|
||||||
System.out.println("invalid");
|
System.out.println("invalid");
|
||||||
return ValidationResult.INVALID;
|
return ValidationResult.INVALID;
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("ok");
|
System.out.println("ok");
|
||||||
return ValidationResult.VALID;
|
return ValidationResult.VALID;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return parsed input of yaml config for property
|
|
||||||
*/
|
|
||||||
protected BiFunction<YAML, ConfigProperty, Optional<FieldType>> provideDataYaml() {
|
|
||||||
return (yaml, property) -> {
|
|
||||||
if (yaml.isSet(property.prop())) {
|
|
||||||
try {
|
|
||||||
return Optional.of(parse(yaml.getString(property.prop())));
|
|
||||||
} catch (YamlKeyNotFoundException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Optional.empty();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return parsed input of command line parameters config for property
|
|
||||||
*/
|
|
||||||
protected BiFunction<CommandLine, ConfigProperty, Optional<FieldType>> provideDataCmd() {
|
|
||||||
return (cmd, property) -> {
|
|
||||||
if (cmd.hasOption(property.prop())) {
|
|
||||||
return Optional.of(parse(cmd.getOptionValue(property.prop())));
|
|
||||||
}
|
|
||||||
return Optional.empty();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
protected boolean isOverwritingNecessary() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected FieldType overwriteValue() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse input parameter to desired format.
|
|
||||||
*
|
|
||||||
* @param value input parameter
|
|
||||||
* @return parsed property
|
|
||||||
*/
|
|
||||||
abstract FieldType parse(String value);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validate if the data has the desired and allowed format.
|
|
||||||
*
|
|
||||||
* @param result parsed property
|
|
||||||
* @return true if data is in desired format.
|
|
||||||
*/
|
|
||||||
abstract boolean isValid(FieldType result);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets valid properties to {@link Config} via reflections.
|
|
||||||
*
|
|
||||||
* @param result parsed property
|
|
||||||
* @return false if method invocation failed
|
|
||||||
*/
|
|
||||||
protected boolean setValue(FieldType result) {
|
|
||||||
for (Method method: Config.getInstance().getClass().getDeclaredMethods()) {
|
|
||||||
if(containsSetterOf(property).test(method)) {
|
|
||||||
try {
|
|
||||||
method.invoke(Config.getInstance(), result);
|
|
||||||
return true;
|
|
||||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Predicate<Method> containsSetterOf(ConfigProperty property) {
|
/**
|
||||||
return method -> StringUtils.startsWith(method.getName(), "set")
|
* @return parsed input of yaml config for property
|
||||||
&& StringUtils.equalsIgnoreCase(method.getName().replace("set", ""), property.prop().replace("-", ""));
|
*/
|
||||||
}
|
protected BiFunction<YAML, ConfigProperty, Optional<FieldType>> provideDataYaml() {
|
||||||
|
return (yaml, property) -> {
|
||||||
|
if (yaml.isSet(property.prop())) {
|
||||||
|
try {
|
||||||
|
return Optional.of(parse(yaml.getString(property.prop())));
|
||||||
|
} catch (YamlKeyNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Optional.empty();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
protected Predicate<Method> containsGetterOf(ConfigProperty property) {
|
/**
|
||||||
return method -> StringUtils.startsWith(method.getName(), "get")
|
* @return parsed input of command line parameters config for property
|
||||||
&& StringUtils.equalsIgnoreCase(method.getName().replace("get", ""), property.prop().replace("-", ""));
|
*/
|
||||||
}
|
protected BiFunction<CommandLine, ConfigProperty, Optional<FieldType>> provideDataCmd() {
|
||||||
|
return (cmd, property) -> {
|
||||||
|
if (cmd.hasOption(property.prop())) {
|
||||||
|
return Optional.of(parse(cmd.getOptionValue(property.prop())));
|
||||||
|
}
|
||||||
|
return Optional.empty();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
public int getWeight() {
|
protected boolean isOverwritingNecessary() {
|
||||||
return 50;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected FieldType overwriteValue() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse input parameter to desired format.
|
||||||
|
*
|
||||||
|
* @param value input parameter
|
||||||
|
* @return parsed property
|
||||||
|
*/
|
||||||
|
abstract FieldType parse(String value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate if the data has the desired and allowed format.
|
||||||
|
*
|
||||||
|
* @param result parsed property
|
||||||
|
* @return true if data is in desired format.
|
||||||
|
*/
|
||||||
|
abstract boolean isValid(FieldType result);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets valid properties to {@link Config} via reflections.
|
||||||
|
*
|
||||||
|
* @param result parsed property
|
||||||
|
* @return false if method invocation failed
|
||||||
|
*/
|
||||||
|
protected boolean setValue(FieldType result) {
|
||||||
|
for (Method method : Config.getInstance().getClass().getDeclaredMethods()) {
|
||||||
|
if (containsSetterOf(property).test(method)) {
|
||||||
|
try {
|
||||||
|
method.invoke(Config.getInstance(), result);
|
||||||
|
return true;
|
||||||
|
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Predicate<Method> containsSetterOf(ConfigProperty property) {
|
||||||
|
return method -> StringUtils.startsWith(method.getName(), "set")
|
||||||
|
&& StringUtils.equalsIgnoreCase(method.getName().replace("set", ""), property.prop().replace("-", ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Predicate<Method> containsGetterOf(ConfigProperty property) {
|
||||||
|
return method -> StringUtils.startsWith(method.getName(), "get")
|
||||||
|
&& StringUtils.equalsIgnoreCase(method.getName().replace("get", ""), property.prop().replace("-", ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWeight() {
|
||||||
|
return 50;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return new StringJoiner(", ", ConfigValidator.class.getSimpleName() + "[", "]")
|
||||||
|
.add("property=" + property)
|
||||||
|
.add("required=" + required)
|
||||||
|
.add("defaultValue=" + defaultValue)
|
||||||
|
.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import java.util.Date;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class DateValidator extends ConfigValidator<Date> {
|
public class DateValidator extends ConfigValidator<Date> {
|
||||||
private static final Date INVALID_DATE = new Date(0);
|
private static final Date INVALID_DATE = new Date(0);
|
||||||
private static final Date DEFAULT_DATE = new Date(1);
|
private static final Date DEFAULT_DATE = new Date(1000);
|
||||||
|
|
||||||
public DateValidator(ConfigProperty property, boolean required) {
|
public DateValidator(ConfigProperty property, boolean required) {
|
||||||
super(property, required, null);
|
super(property, required, null);
|
||||||
@@ -37,7 +37,7 @@ public class DateValidator extends ConfigValidator<Date> {
|
|||||||
return DEFAULT_DATE;
|
return DEFAULT_DATE;
|
||||||
}
|
}
|
||||||
YAML yaml = new YAML(lastExecutionFile);
|
YAML yaml = new YAML(lastExecutionFile);
|
||||||
return parse(yaml.getString(Config.getInstance().getLibraryPath().getAbsolutePath(), DateUtils.convert(DEFAULT_DATE)));
|
return parse(yaml.getString(Config.getInstance().getLibraryPath().getAbsolutePath().replace("\\", "/"), DateUtils.convert(DEFAULT_DATE)));
|
||||||
} catch (YamlInvalidContentException | IOException e) {
|
} catch (YamlInvalidContentException | IOException e) {
|
||||||
log.error("Couldn't open last-execution.properties");
|
log.error("Couldn't open last-execution.properties");
|
||||||
return INVALID_DATE;
|
return INVALID_DATE;
|
||||||
|
|||||||
@@ -150,6 +150,10 @@ public abstract class AttributeUpdaterKernel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void endProcess() {
|
protected void endProcess() {
|
||||||
|
if (Config.getInstance().isSafeMode()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String filePath = AppDirsFactory.getInstance().getUserConfigDir(ProjectUtil.getProjectName(), null, null);
|
String filePath = AppDirsFactory.getInstance().getUserConfigDir(ProjectUtil.getProjectName(), null, null);
|
||||||
|
|
||||||
@@ -162,7 +166,7 @@ public abstract class AttributeUpdaterKernel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
YAML yaml = new YAML(lastExecutionFile);
|
YAML yaml = new YAML(lastExecutionFile);
|
||||||
yaml.set(Config.getInstance().getLibraryPath().getAbsolutePath(), DateUtils.convert(new Date()));
|
yaml.set(Config.getInstance().getLibraryPath().getAbsolutePath().replace("\\", "/"), DateUtils.convert(new Date()));
|
||||||
yaml.save(lastExecutionFile);
|
yaml.save(lastExecutionFile);
|
||||||
} catch (IOException | YamlInvalidContentException e) {
|
} catch (IOException | YamlInvalidContentException e) {
|
||||||
log.error("last-execution.yml could not be created: ", e); // TODO
|
log.error("last-execution.yml could not be created: ", e); // TODO
|
||||||
|
|||||||
Reference in New Issue
Block a user