Skip to content

Commit

Permalink
Allow to import folders
Browse files Browse the repository at this point in the history
Signed-off-by: HARPER Jon <jon.harper87@gmail.com>
  • Loading branch information
jonenst committed Jul 5, 2024
1 parent 2dbe40a commit d10bb45
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public interface DataSource extends ReadOnlyDataSource {

static DataSource fromPath(Path file) {
Objects.requireNonNull(file);
if (!Files.isRegularFile(file)) {
throw new PowsyblException("File " + file + " does not exist or is not a regular file");
if (!Files.exists(file)) {
throw new PowsyblException("File " + file + " does not exist");
}
Path absFile = file.toAbsolutePath();
return fromPath(absFile.getParent(), absFile.getFileName().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package com.powsybl.commons.datasource;

import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
Expand Down Expand Up @@ -77,6 +78,15 @@ static DataSource createDataSource(Path directory, String fileNameOrBaseName, Da
Objects.requireNonNull(directory);
Objects.requireNonNull(fileNameOrBaseName);

// For a directory, we list all files because we assume everything is related.
// And this allows to still have the directory name as the basename (for example for writing new files named like the directory)
if (Files.isDirectory(directory.resolve(fileNameOrBaseName))) {
return new FileDataSource(directory.resolve(fileNameOrBaseName), getBaseName(fileNameOrBaseName), observer);
}

// For a file in a directory, we filter by basename to get similar files only because other files may be unrelated
// Additionnally, we give the base extension to allow to know the exact file that was used to specify this datasource to avoid importing
// a similarly named unrelated network.
if (fileNameOrBaseName.endsWith(".zst")) {
String fileNameWithoutCompressionExtension = fileNameOrBaseName.substring(0, fileNameOrBaseName.length() - 4);
return new ZstdFileDataSource(directory, getBaseName(fileNameWithoutCompressionExtension), getBaseExtension(fileNameWithoutCompressionExtension), observer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,43 @@ public class FileDataSource implements DataSource {

private final String baseExtension;

private final boolean isolatedDirectory;

private final DataSourceObserver observer;

public FileDataSource(Path directory, String baseName) {
this(directory, baseName, "", null);
this(directory, baseName, "", false, null);
}

public FileDataSource(Path directory, String baseName, DataSourceObserver observer) {
this(directory, baseName, "", observer);
this(directory, baseName, "", false, observer);
}

public FileDataSource(Path directory, String baseName, String baseExtension) {
this(directory, baseName, baseExtension, null);
this(directory, baseName, baseExtension, false, null);
}

public FileDataSource(Path directory, String baseName, String baseExtension, DataSourceObserver observer) {
this(directory, baseName, baseExtension, false, observer);
}

public FileDataSource(Path directory, String baseName, boolean filterDirectory) {
this(directory, baseName, "", filterDirectory, null);
}

public FileDataSource(Path directory, String baseName, boolean filterDirectory, DataSourceObserver observer) {
this(directory, baseName, "", filterDirectory, observer);
}

public FileDataSource(Path directory, String baseName, String baseExtension, boolean filterDirectory) {
this(directory, baseName, baseExtension, filterDirectory, null);
}

public FileDataSource(Path directory, String baseName, String baseExtension, boolean isolatedDirectory, DataSourceObserver observer) {
this.directory = Objects.requireNonNull(directory);
this.baseName = Objects.requireNonNull(baseName);
this.baseExtension = Objects.requireNonNull(baseExtension);
this.isolatedDirectory = isolatedDirectory;
this.observer = observer;
}

Expand Down Expand Up @@ -120,11 +139,14 @@ public Set<String> listNames(String regex) throws IOException {
Pattern p = Pattern.compile(regex);
int maxDepth = 1;
try (Stream<Path> paths = Files.walk(directory, maxDepth)) {
return paths

var filenames = paths
.filter(Files::isRegularFile)
.map(Path::getFileName)
.map(Path::toString)
.filter(name -> name.startsWith(baseName))
.map(Path::toString);
var maybeFilteredFilenames = isolatedDirectory ? filenames
: filenames.filter(name -> name.startsWith(baseName));
return maybeFilteredFilenames
// Return names after removing the compression extension
.map(name -> name.replace(getCompressionExt(), ""))
.filter(s -> p.matcher(s).matches())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,12 @@ void createNewFilesTest() throws IOException {
assertTrue(names.contains(getBaseName() + suffix + "." + ext));
assertFalse(names.contains("dummy.txt"));
assertFalse(names.contains("dummy2.txt"));

// but all the files are listed through list names for the isolated directory FileDataSource
DataSource dsUnfiltered = new FileDataSource(testDir, getBaseName(), true);
assertEquals(3, names.size());
assertTrue(names.contains(getBaseName() + suffix + "." + ext));
assertTrue(names.contains("dummy.txt"));
assertTrue(names.contains("dummy2.txt"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ void checkFailsWhenNetworkFileNotFound() {
"--case-file", "wrongFile.xiidm",
"--factors-file", "factors.json",
"--output-file", "output.csv"},
"com.powsybl.commons.PowsyblException: File wrongFile.xiidm does not exist or is not a regular file");
"com.powsybl.commons.PowsyblException: File wrongFile.xiidm does not exist");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void checkFailsWhenInputFileNotFound() {

@Test
void checkFailsWhenNetworkFileNotFound() {
assertCommandErrorMatch(new String[] {COMMAND_NAME, "--input-file", "input.txt", "--case-file", "wrongFile.uct"}, "com.powsybl.commons.PowsyblException: File wrongFile.uct does not exist or is not a regular file");
assertCommandErrorMatch(new String[] {COMMAND_NAME, "--input-file", "input.txt", "--case-file", "wrongFile.uct"}, "com.powsybl.commons.PowsyblException: File wrongFile.uct does not exist");
}

@Test
Expand Down

0 comments on commit d10bb45

Please sign in to comment.