mirror of
https://github.com/RatzzFatzz/MKVAudioSubtitleChanger.git
synced 2026-02-11 02:05:56 +01:00
Add property to include/exclude files by pattern
This commit is contained in:
@@ -41,6 +41,9 @@ public class Main {
|
||||
config.getForcedKeywords().addAll(List.of(cmd.getOptionValues(FORCED_KEYWORDS.prop())));
|
||||
if (cmd.hasOption(EXCLUDE_DIRECTORY.prop()))
|
||||
config.getExcludedDirectories().addAll(List.of(cmd.getOptionValues(EXCLUDE_DIRECTORY.prop())));
|
||||
if (cmd.hasOption(INCLUDE_PATTERN.prop())) {
|
||||
config.setIncludePattern(Config.compilePattern(cmd.getOptionValue(INCLUDE_PATTERN.prop()), INCLUDE_PATTERN));
|
||||
}
|
||||
config.isValid();
|
||||
} catch (ParseException e) {
|
||||
log.error(e);
|
||||
@@ -58,6 +61,7 @@ public class Main {
|
||||
options.addOption(optionOf(SAFE_MODE, "s", false));
|
||||
options.addOption(optionOf(FORCED_KEYWORDS, "k", Option.UNLIMITED_VALUES, false));
|
||||
options.addOption(optionOf(EXCLUDE_DIRECTORY, "e", Option.UNLIMITED_VALUES, false));
|
||||
options.addOption(optionOf(INCLUDE_PATTERN, "i", true));
|
||||
return options;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,9 +15,11 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty.MKV_TOOL_NIX;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty.*;
|
||||
|
||||
@Log4j2
|
||||
@Getter
|
||||
@@ -31,6 +33,7 @@ public class Config {
|
||||
private int threadCount;
|
||||
private Set<String> forcedKeywords = new HashSet<>(Arrays.asList("forced", "signs"));
|
||||
private Set<String> excludedDirectories = new HashSet<>();
|
||||
private Pattern includePattern;
|
||||
@Getter(AccessLevel.NONE)
|
||||
private String mkvtoolnixPath;
|
||||
private String libraryPath;
|
||||
@@ -75,6 +78,7 @@ public class Config {
|
||||
setWindows(System.getProperty("os.name").toLowerCase().contains("windows"));
|
||||
getForcedKeywords().addAll(loadForcedKeywords(config));
|
||||
getExcludedDirectories().addAll(loadExcludeDirectories(config));
|
||||
loadIncludePattern(config);
|
||||
} catch (YamlInvalidContentException | YamlKeyNotFoundException | IOException e) {
|
||||
log.fatal("Config could not be loaded: {}", e.getMessage());
|
||||
}
|
||||
@@ -105,6 +109,23 @@ public class Config {
|
||||
return config.getStringList(ConfigProperty.EXCLUDE_DIRECTORY.prop(), new ArrayList<>());
|
||||
}
|
||||
|
||||
private void loadIncludePattern(YAML config) throws YamlKeyNotFoundException {
|
||||
if (config.isSet(INCLUDE_PATTERN.prop())) {
|
||||
includePattern = compilePattern(config.getString(INCLUDE_PATTERN.prop()), INCLUDE_PATTERN);
|
||||
} else {
|
||||
includePattern = Pattern.compile(".*");
|
||||
}
|
||||
}
|
||||
|
||||
public static Pattern compilePattern(String pattern, ConfigProperty origin) {
|
||||
try {
|
||||
return Pattern.compile(pattern);
|
||||
} catch (PatternSyntaxException e) {
|
||||
log.error("{} is not a valid regex pattern!", origin.prop());
|
||||
throw new RuntimeException(String.format("%s is not a valid regex pattern!%n", origin.prop()));
|
||||
}
|
||||
}
|
||||
|
||||
private String loadMkvToolNixPath(YAML config) throws YamlKeyNotFoundException {
|
||||
return config.isSet(MKV_TOOL_NIX.prop())
|
||||
? config.getString(MKV_TOOL_NIX.prop())
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
import java.io.File;
|
||||
@@ -28,6 +29,7 @@ public class MkvFileCollector implements FileCollector {
|
||||
.filter(Files::isRegularFile)
|
||||
.map(Path::toFile)
|
||||
.filter(f -> f.getAbsolutePath().endsWith(".mkv"))
|
||||
.filter(f -> Config.getInstance().getIncludePattern().matcher(f.getName()).matches())
|
||||
.collect(Collectors.toList());
|
||||
} catch (IOException e) {
|
||||
log.error("Couldn't find file or directory!", e);
|
||||
|
||||
@@ -8,7 +8,8 @@ public enum ConfigProperty {
|
||||
LIBRARY("library", "path to library"),
|
||||
SAFE_MODE("safe-mode", "Test run (no files will be changes)"),
|
||||
HELP("help", "\"for help this is\" - Yoda"),
|
||||
EXCLUDE_DIRECTORY("exclude-directories", "Directories to exclude");
|
||||
EXCLUDE_DIRECTORY("exclude-directories", "Directories to exclude"),
|
||||
INCLUDE_PATTERN("include-pattern", "Include files matching pattern");
|
||||
|
||||
private final String property;
|
||||
private final String description;
|
||||
|
||||
Reference in New Issue
Block a user