Finalize coherent feature

This commit is contained in:
2023-03-07 22:55:03 +01:00
parent 8317e97639
commit 6372cc560c
8 changed files with 50 additions and 48 deletions

View File

@@ -37,6 +37,7 @@ public class Config {
private boolean safeMode;
private Integer coherent;
private boolean forceCoherent;
private Set<String> forcedKeywords = new HashSet<>(Arrays.asList("forced", "signs", "songs"));
private Set<String> commentaryKeywords = new HashSet<>(Arrays.asList("commentary", "director"));

View File

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

View File

@@ -121,27 +121,27 @@ public abstract class ConfigValidator<FieldType> {
* @return false if method invocation failed
*/
protected boolean setValue(FieldType result) {
List<Method> methods = Arrays.stream(Config.getInstance().getClass().getDeclaredMethods())
.filter(containsSetterOf(property))
.collect(Collectors.toList());
if (methods.size() != 1) {
return false;
}
for (Method method: Config.getInstance().getClass().getDeclaredMethods()) {
if(containsSetterOf(property).test(method)) {
try {
methods.get(0).invoke(Config.getInstance(), result);
method.invoke(Config.getInstance(), result);
return true;
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
return true;
}
}
return false;
}
protected Predicate<Method> containsSetterOf(ConfigProperty property) {
return method -> StringUtils.containsIgnoreCase(method.getName(), "set")
&& StringUtils.containsIgnoreCase(method.getName(), property.prop().replace("-", ""));
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.containsIgnoreCase(method.getName(), "get")
return method -> StringUtils.startsWith(method.getName(), "get")
&& StringUtils.containsIgnoreCase(method.getName(), property.prop().replace("-", ""));
}

View File

@@ -4,6 +4,7 @@ import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.exceptions.MkvToolNixException;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileCollector;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileProcessor;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfoDto;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ResultStatistic;
import lombok.RequiredArgsConstructor;
@@ -84,7 +85,18 @@ public abstract class AttributeUpdaterKernel {
*
* @param file file or directory to update
*/
abstract void process(File file);
void process(File file) {
FileInfoDto fileInfo = new FileInfoDto(file);
List<FileAttribute> attributes = processor.loadAttributes(file);
List<FileAttribute> nonForcedTracks = processor.retrieveNonForcedTracks(attributes);
List<FileAttribute> nonCommentaryTracks = processor.retrieveNonCommentaryTracks(attributes);
processor.detectDefaultTracks(fileInfo, attributes, nonForcedTracks);
processor.detectDesiredTracks(fileInfo, nonForcedTracks, nonCommentaryTracks);
updateFile(fileInfo);
}
/**
* Persist file changes.
@@ -92,7 +104,6 @@ public abstract class AttributeUpdaterKernel {
* @param fileInfoDto contains information about file and desired configuration.
*/
protected void updateFile(FileInfoDto fileInfoDto) {
statistic.total();
switch (fileInfoDto.getStatus()) {
case CHANGE_NECESSARY:
statistic.shouldChange();
@@ -117,6 +128,7 @@ public abstract class AttributeUpdaterKernel {
}
try {
statistic.total();
processor.update(fileInfo.getFile(), fileInfo);
statistic.success();
log.info("Updated {}", fileInfo.getFile().getAbsolutePath());

View File

@@ -61,6 +61,7 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel {
}
for (AttributeConfig config : Config.getInstance().getAttributeConfig()) {
for (FileInfoDto fileInfo : fileInfos) {
List<FileAttribute> attributes = fileAttributeCache.get(fileInfo);
@@ -72,8 +73,10 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel {
}
if (fileInfos.stream().allMatch(elem -> elem.getDesiredSubtitleLane() != null && elem.getDesiredAudioLane() != null)) {
log.debug("Found {}/{} match for {}", config.getAudioLanguage(), config.getSubtitleLanguage(), file.getAbsolutePath());
break;
log.info("Found {}/{} match for {}", config.getAudioLanguage(), config.getSubtitleLanguage(), file.getAbsolutePath());
statistic.increaseTotalBy(fileInfos.size());
fileInfos.forEach(this::updateFile);
return; // match found, end process here
}
fileInfos.forEach(f -> {
@@ -82,8 +85,13 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel {
});
}
// apply config
// apply default process if nothing was found (if parameter is set)
for(FileInfoDto fileInfo: fileInfos) {
statistic.total();
if (Config.getInstance().isForceCoherent()) {
super.process(fileInfo.getFile());
} else {
statistic.excluded();
}
}
}
}

View File

@@ -3,13 +3,9 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileCollector;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileProcessor;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfoDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
@@ -30,21 +26,4 @@ public class DefaultAttributeUpdaterKernel extends AttributeUpdaterKernel {
.filter(file -> !excludedFiles.contains(file))
.collect(Collectors.toList());
}
/**
* {@inheritDoc}
*/
@Override
void process(File file) {
FileInfoDto fileInfo = new FileInfoDto(file);
List<FileAttribute> attributes = processor.loadAttributes(file);
List<FileAttribute> nonForcedTracks = processor.retrieveNonForcedTracks(attributes);
List<FileAttribute> nonCommentaryTracks = processor.retrieveNonCommentaryTracks(attributes);
processor.detectDefaultTracks(fileInfo, attributes, nonForcedTracks);
processor.detectDesiredTracks(fileInfo, nonForcedTracks, nonCommentaryTracks);
updateFile(fileInfo);
}
}

View File

@@ -10,12 +10,13 @@ import java.util.stream.Collectors;
@AllArgsConstructor
public enum ConfigProperty {
LIBRARY("library", "Path to library", "l", 1),
LIBRARY("library-path", "Path to library", "l", 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", 1),
COHERENT("coherent", "Try to match all files in dir of depth with the same config", "c", 1),
FORCE_COHERENT("force-coherent", "Force coherent and don't update anything if config fits not whole config (default: false)", "cf", 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),

View File

@@ -39,7 +39,7 @@ class PathValidatorTest {
private static Stream<Arguments> provideTestCases() {
return Stream.of(
argumentsOf(LIBRARY, false, null, "library: " + TEST_DIR, new String[]{}, VALID),
argumentsOf(LIBRARY, false, null, "library-path: " + TEST_DIR, new String[]{}, VALID),
argumentsOf(LIBRARY, true, null, "", new String[]{"-l", TEST_FILE}, VALID),
argumentsOf(LIBRARY, false, TEST_DIR, "", new String[]{}, DEFAULT),
@@ -48,7 +48,7 @@ class PathValidatorTest {
argumentsOf(LIBRARY, false, null, "", new String[]{}, NOT_PRESENT),
argumentsOf(LIBRARY, true, null, "", new String[]{"-l", TEST_DIR + "/invalid"}, INVALID),
argumentsOf(LIBRARY, false, null, "library: " + TEST_DIR + "/invalid", new String[]{}, INVALID),
argumentsOf(LIBRARY, false, null, "library-path: " + TEST_DIR + "/invalid", new String[]{}, INVALID),
argumentsOf(LIBRARY, true, TEST_DIR, "", new String[]{"-l", TEST_DIR + "/invalid"}, INVALID)
);
}