Add attribute config via cli

This commit is contained in:
2023-02-20 23:48:49 +01:00
parent 5f72f4545f
commit 156e327943
8 changed files with 46 additions and 42 deletions

View File

@@ -1,15 +1,16 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.validator.*;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.VersionUtil;
import at.pcgamingfreaks.yaml.YAML;
import at.pcgamingfreaks.yaml.YamlInvalidContentException;
import org.apache.commons.cli.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
@@ -18,7 +19,7 @@ import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.CommandLineOptionsU
public class ConfigLoader {
private static final List<ConfigValidator<?>> VALIDATORS = List.of(
new ConfigPathValidator(CONFIG_PATH, true, Path.of("./config.yaml").toFile()),
new ConfigPathValidator(CONFIG_PATH, false),
new PathValidator(LIBRARY, true, null),
new ThreadValidator(THREADS, false, 2),
new MkvToolNixPathValidator(MKV_TOOL_NIX, true, Path.of("C:\\Program Files\\MKVToolNix").toFile()),
@@ -32,6 +33,7 @@ public class ConfigLoader {
public static void initConfig(String[] args) {
HelpFormatter formatter = new HelpFormatter();
formatter.setOptionComparator(null);
YAML yamlConfig = null;
Options options = initOptions();
@@ -44,7 +46,7 @@ public class ConfigLoader {
for (ConfigValidator<?> validator: VALIDATORS) {
results.add(validator.validate(yamlConfig, cmd));
if (yamlConfig == null && Config.getInstance().getConfigPath() != null) {
if (yamlConfig == null) {
try {
yamlConfig = Config.getInstance().getConfigPath() != null
? new YAML(Config.getInstance().getConfigPath())
@@ -59,16 +61,10 @@ public class ConfigLoader {
private static Options initOptions() {
Options options = new Options();
options.addOption(optionOf(HELP, HELP.abrv(), HELP.args()));
options.addOption(optionOf(VERSION, VERSION.abrv(), VERSION.args()));
options.addOption(optionOf(LIBRARY, LIBRARY.abrv(), LIBRARY.args() ));
options.addOption(optionOf(MKV_TOOL_NIX, MKV_TOOL_NIX.abrv(), MKV_TOOL_NIX.args() ));
options.addOption(optionOf(CONFIG_PATH, CONFIG_PATH.abrv(), CONFIG_PATH.args() ));
options.addOption(optionOf(THREADS, THREADS.abrv(), THREADS.args()));
options.addOption(optionOf(SAFE_MODE, SAFE_MODE.abrv(), SAFE_MODE.args() ));
options.addOption(optionOf(FORCED_KEYWORDS, FORCED_KEYWORDS.abrv(), FORCED_KEYWORDS.args()));
options.addOption(optionOf(EXCLUDE_DIRECTORY, FORCED_KEYWORDS.abrv(), FORCED_KEYWORDS.args()));
options.addOption(optionOf(INCLUDE_PATTERN, INCLUDE_PATTERN.abrv(), INCLUDE_PATTERN.args()));
Arrays.stream(ConfigProperty.values())
.filter(prop -> prop.abrv() != null)
.map(prop -> optionOf(prop, prop.abrv(), prop.args()))
.forEach(options::addOption);
return options;
}

View File

@@ -13,6 +13,7 @@ import java.util.stream.Collectors;
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.LanguageValidatorUtil.isLanguageValid;
public class AttributeConfigValidator extends ConfigValidator<List<AttributeConfig>> {
private static final String SEPARATOR = ":";
public AttributeConfigValidator() {
super(ConfigProperty.ATTRIBUTE_CONFIG, true, null);
@@ -25,10 +26,18 @@ public class AttributeConfigValidator extends ConfigValidator<List<AttributeConf
System.out.printf("%s: ", property.prop());
List<AttributeConfig> result;
Function<String, String> audio = key -> yaml.getString(key + ".audio", null);
Function<String, String> subtitle = key -> yaml.getString(key + ".subtitle", null);
if (yaml.getKeysFiltered(property.prop() + ".*").size() > 0) {
if (cmd.hasOption(property.prop())) {
List<String> values = List.of(cmd.getOptionValues(property.prop()));
result = values.stream().anyMatch(pair -> !pair.contains(SEPARATOR))
? List.of()
: values.stream().map(pair -> pair.split(SEPARATOR))
.map(split -> new AttributeConfig(split[0], split[1]))
.collect(Collectors.toList());
} else if(yaml.getKeysFiltered(property.prop() + ".*").size() > 0) {
Function<String, String> audio = key -> yaml.getString(key + ".audio", null);
Function<String, String> subtitle = key -> yaml.getString(key + ".subtitle", null);
result = yaml.getKeysFiltered(".*audio.*").stream()
.sorted()
.map(key -> key.replace(".audio", ""))
@@ -67,7 +76,7 @@ public class AttributeConfigValidator extends ConfigValidator<List<AttributeConf
if (result.isEmpty()) {
return false;
}
boolean isValid = true;
boolean isValid;
for (AttributeConfig attributeConfig : result) {
isValid = isLanguageValid(attributeConfig.getAudioLanguage())
&& isLanguageValid(attributeConfig.getSubtitleLanguage());

View File

@@ -8,8 +8,8 @@ import java.util.Optional;
import java.util.function.BiFunction;
public class ConfigPathValidator extends PathValidator {
public ConfigPathValidator(ConfigProperty property, boolean required, File defaultValue) {
super(property, required, defaultValue);
public ConfigPathValidator(ConfigProperty property, boolean required) {
super(property, required, null);
}
/**

View File

@@ -10,21 +10,21 @@ import java.util.stream.Collectors;
@AllArgsConstructor
public enum ConfigProperty {
CONFIG_PATH("config-path", "Path to config file", "p", 1),
LIBRARY("library", "Path to library", "l", 1),
SAFE_MODE("safe-mode", "Test run (no files will be changes)", "s", 0),
WINDOWS("windows", "Is operating system windows", null, 0),
THREADS("threads", "thread count (default: 2)", "t", 1),
INCLUDE_PATTERN("include-pattern", "Include files matching pattern", "i", 1),
ATTRIBUTE_CONFIG("attribute-config", "Attribute config to decide which tracks to choose when", "a", Option.UNLIMITED_VALUES),
CONFIG_PATH("config-path", "Path to config file", "p", 1),
MKV_TOOL_NIX("mkvtoolnix", "Path to mkv tool nix installation", "m", 1),
SAFE_MODE("safe-mode", "Test run (no files will be changes)", "s", 0),
COHERENT("coherent", "Try to match whole series with same config", "c", 0),
WINDOWS("windows", "Is operating system windows", null, 0),
THREADS("threads", "Thread count (default: 2)", "t", 1),
INCLUDE_PATTERN("include-pattern", "Include files matching pattern (default: \".*\")", "i", 1),
EXCLUDE_DIRECTORY("exclude-directories", "Directories to be excluded, combines with config file", "e", 1),
FORCED_KEYWORDS("forcedKeywords", "Additional keywords to identify forced tracks", "fk", Option.UNLIMITED_VALUES),
COMMENTARY_KEYWORDS("commentary-keywords", "Additional keywords to identify commentary tracks", "ck", Option.UNLIMITED_VALUES),
EXCLUDE_DIRECTORY("exclude-directories", "Directories to be excluded, combines with config file", "e", 1),
COHERENT("coherent", "Try to match whole series with same config", "c", 0),
HELP("help", "\"for help this is\" - Yoda", "h", 0),
VERSION("version", "Display version", "v", 0),
ARGUMENTS("arguments", "List of arguments", null, 0),
ATTRIBUTE_CONFIG("attribute-config", "Attribute config to decide which tracks to choose when", "a", 1);
VERSION("version", "Display version", "v", 0),
HELP("help", "\"For help this is\" - Yoda", "h", 0);
private final String property;
private final String description;