Add dir loading for coherent feature

This commit is contained in:
2023-03-01 19:13:07 +01:00
parent b07f6894aa
commit 440251c7c9
9 changed files with 66 additions and 12 deletions

View File

@@ -1,7 +1,9 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger; package at.pcgamingfreaks.mkvaudiosubtitlechanger;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ConfigLoader; import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ConfigLoader;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.AttributeUpdaterKernel; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.AttributeUpdaterKernel;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.CoherentAttributeUpdaterKernel;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.DefaultAttributeUpdaterKernel; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.DefaultAttributeUpdaterKernel;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.MkvFileCollector; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.MkvFileCollector;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.MkvFileProcessor; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.MkvFileProcessor;
@@ -11,7 +13,9 @@ import lombok.extern.slf4j.Slf4j;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
ConfigLoader.initConfig(args); ConfigLoader.initConfig(args);
AttributeUpdaterKernel kernel = new DefaultAttributeUpdaterKernel(new MkvFileCollector(), new MkvFileProcessor()); AttributeUpdaterKernel kernel = Config.getInstance().getCoherent() != null
? new CoherentAttributeUpdaterKernel(new MkvFileCollector(), new MkvFileProcessor())
: new DefaultAttributeUpdaterKernel(new MkvFileCollector(), new MkvFileProcessor());
kernel.execute(); kernel.execute();
} }
} }

View File

@@ -28,7 +28,8 @@ public class ConfigLoader {
new SetValidator(FORCED_KEYWORDS, false, true), new SetValidator(FORCED_KEYWORDS, false, true),
new SetValidator(COMMENTARY_KEYWORDS, false, true), new SetValidator(COMMENTARY_KEYWORDS, false, true),
new SetValidator(EXCLUDED_DIRECTORY, false, true), new SetValidator(EXCLUDED_DIRECTORY, false, true),
new AttributeConfigValidator() new AttributeConfigValidator(),
new CoherentConfigValidator(COHERENT, false)
); );
public static void initConfig(String[] args) { public static void initConfig(String[] args) {

View File

@@ -17,6 +17,6 @@ public class CoherentConfigValidator extends ConfigValidator<Integer> {
@Override @Override
boolean isValid(Integer result) { boolean isValid(Integer result) {
return result > 0; return result >= 0;
} }
} }

View File

@@ -10,4 +10,13 @@ public interface FileCollector {
* @return list of all files within the directory * @return list of all files within the directory
*/ */
List<File> loadFiles(String path); List<File> loadFiles(String path);
/**
* Load all directories from path, but only until depth is reached.
*
* @param path leads to a directory which will be loaded recursively until depth
* @param depth limit directory crawling
* @return list of directory until depth
*/
List<File> loadDirectories(String path, int depth);
} }

View File

@@ -16,6 +16,9 @@ import java.util.stream.Stream;
public class MkvFileCollector implements FileCollector { public class MkvFileCollector implements FileCollector {
private static final String[] fileExtensions = new String[]{".mkv", ".mka", ".mks", ".mk3d"}; private static final String[] fileExtensions = new String[]{".mkv", ".mka", ".mks", ".mk3d"};
/**
* {@inheritDoc}
*/
@Override @Override
public List<File> loadFiles(String path) { public List<File> loadFiles(String path) {
try (Stream<Path> paths = Files.walk(Paths.get(path))) { try (Stream<Path> paths = Files.walk(Paths.get(path))) {
@@ -28,4 +31,19 @@ public class MkvFileCollector implements FileCollector {
return new ArrayList<>(); return new ArrayList<>();
} }
} }
/**
* {@inheritDoc}
*/
@Override
public List<File> loadDirectories(String path, int depth) {
try (Stream<Path> paths = Files.walk(Paths.get(path), depth)) {
return paths.map(Path::toFile)
.filter(File::isDirectory)
.collect(Collectors.toList());
} catch (IOException e) {
log.error("Couldn't find file or directory!", e);
return new ArrayList<>();
}
}
} }

View File

@@ -15,10 +15,12 @@ import me.tongfei.progressbar.ProgressBarStyle;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Slf4j @Slf4j
@RequiredArgsConstructor @RequiredArgsConstructor
@@ -57,6 +59,16 @@ public abstract class AttributeUpdaterKernel {
statistic.printResult(); statistic.printResult();
} }
protected List<File> loadExcludedFiles() {
List<File> excludedFiles = Config.getInstance().getExcludedDirectories().stream()
.map(collector::loadFiles)
.flatMap(Collection::stream)
.collect(Collectors.toList());
statistic.increaseTotalBy(excludedFiles.size());
statistic.increaseExcludedBy(excludedFiles.size());
return excludedFiles;
}
/** /**
* Load files or directories to update. * Load files or directories to update.
* Remove excluded directories. * Remove excluded directories.

View File

@@ -1,14 +1,17 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel; package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileCollector; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileCollector;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileProcessor; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileProcessor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@Slf4j @Slf4j
public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel{ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel {
public CoherentAttributeUpdaterKernel(FileCollector collector, FileProcessor processor) { public CoherentAttributeUpdaterKernel(FileCollector collector, FileProcessor processor) {
super(collector, processor); super(collector, processor);
@@ -19,7 +22,19 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel{
*/ */
@Override @Override
List<File> loadFiles(String path) { List<File> loadFiles(String path) {
return null; List<File> excludedFiles = loadExcludedFiles();
List<File> directories = collector.loadDirectories(path, Config.getInstance().getCoherent())
.stream().filter(file -> !excludedFiles.contains(file))
.collect(Collectors.toList());
return directories.stream()
.filter(dir -> isParentDirectory(dir, directories))
.collect(Collectors.toList());
}
private boolean isParentDirectory(File directory, List<File> directories) {
String path = directory.getAbsolutePath();
return directories.stream()
.noneMatch(dir -> dir.getAbsolutePath().contains(path) && !StringUtils.equals(path, dir.getAbsolutePath()));
} }
/** /**

View File

@@ -25,12 +25,7 @@ public class DefaultAttributeUpdaterKernel extends AttributeUpdaterKernel {
*/ */
@Override @Override
List<File> loadFiles(String path) { List<File> loadFiles(String path) {
List<File> excludedFiles = Config.getInstance().getExcludedDirectories().stream() List<File> excludedFiles = loadExcludedFiles();
.map(collector::loadFiles)
.flatMap(Collection::stream)
.collect(Collectors.toList());
statistic.increaseTotalBy(excludedFiles.size());
statistic.increaseExcludedBy(excludedFiles.size());
return collector.loadFiles(Config.getInstance().getLibraryPath().getAbsolutePath()).stream() return collector.loadFiles(Config.getInstance().getLibraryPath().getAbsolutePath()).stream()
.filter(file -> !excludedFiles.contains(file)) .filter(file -> !excludedFiles.contains(file))
.collect(Collectors.toList()); .collect(Collectors.toList());

View File

@@ -15,7 +15,7 @@ public enum ConfigProperty {
CONFIG_PATH("config-path", "Path to config file", "p", 1), CONFIG_PATH("config-path", "Path to config file", "p", 1),
MKV_TOOL_NIX("mkvtoolnix", "Path to mkv tool nix installation", "m", 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), SAFE_MODE("safe-mode", "Test run (no files will be changes)", "s", 0),
COHERENT("coherent", "Try to match whole series with same config", null, 0), COHERENT("coherent", "Try to match whole series with same config", "c", 1),
WINDOWS("windows", "Is operating system windows", null, 0), WINDOWS("windows", "Is operating system windows", null, 0),
THREADS("threads", "Thread count (default: 2)", "t", 1), THREADS("threads", "Thread count (default: 2)", "t", 1),
INCLUDE_PATTERN("include-pattern", "Include files matching pattern (default: \".*\")", "i", 1), INCLUDE_PATTERN("include-pattern", "Include files matching pattern (default: \".*\")", "i", 1),