Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to import folders #3086

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ void testDoNotExportFictitiousSwitchesCreatedForDisconnectedTerminals() throws I
assertEquals("true", fictitiousSwitch.getProperty(Conversion.PROPERTY_IS_CREATED_FOR_DISCONNECTED_TERMINAL));

String exportFolder = "/test-terminal-disconnected-fictitious-switch";
String exportFolder1 = "/test-terminal-disconnected-fictitious-switch-closed";
try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) {
// Export to CGMES and add boundary EQ for reimport
Path tmpDir = Files.createDirectory(fs.getPath(exportFolder));
Expand All @@ -283,7 +284,8 @@ void testDoNotExportFictitiousSwitchesCreatedForDisconnectedTerminals() throws I
// And the fictitious switch is not crated when re-importing
fictitiousSwitch.setOpen(false);
String baseName1 = "testTerminalDisconnectedFictitiousSwitchClosedExported";
ReadOnlyDataSource exportedCgmes1 = exportAndAddBoundaries(network, tmpDir, baseName1, ds);
Path tmpDir1 = Files.createDirectory(fs.getPath(exportFolder1));
ReadOnlyDataSource exportedCgmes1 = exportAndAddBoundaries(network, tmpDir1, baseName1, ds);
CgmesModel cgmes1 = CgmesModelFactory.create(exportedCgmes1, TripleStoreFactory.defaultImplementation());
assertTrue(cgmes1.isNodeBreaker());
assertFalse(cgmes1.switches().stream().anyMatch(sw -> sw.getId("Switch").equals(fictitiousSwitchId)));
Expand Down
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 @@ -96,6 +97,8 @@ static DataSource createDataSource(Path directory, String fileNameOrBaseName, Da
} else if (fileNameOrBaseName.endsWith(".bz2")) {
String fileNameWithoutCompressionExtension = fileNameOrBaseName.substring(0, fileNameOrBaseName.length() - 4);
return new Bzip2FileDataSource(directory, getBaseName(fileNameWithoutCompressionExtension), getBaseExtension(fileNameWithoutCompressionExtension), observer);
} else if (Files.isDirectory(directory.resolve(fileNameOrBaseName))) {
return new FileDataSource(directory.resolve(fileNameOrBaseName), getBaseName(fileNameOrBaseName), "", observer);
} else {
return new FileDataSource(directory, getBaseName(fileNameOrBaseName), getBaseExtension(fileNameOrBaseName), observer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,17 @@ 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);
// For a file in a directory, we filter by basename to get similar files only because other files may be unrelated.
// 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)
var isListingAllDirectory = baseExtension.isEmpty(); // we assume all files have extensions so if we have a mainExtension, this means we are not in a clean separate directory
var maybeBaseNameFilteredFilenames = isListingAllDirectory ? filenames
: filenames.filter(name -> name.startsWith(baseName));
return maybeBaseNameFilteredFilenames
// 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 @@ -42,16 +42,16 @@ void listNamesTest() throws IOException {
}

// A file data source created using the complete filename does not return other filenames
Set<String> names = new FileDataSource(testDir, getBaseName() + ".txt").listNames(".*");
Set<String> names = new FileDataSource(testDir, getBaseName(), ".txt").listNames(".*");
assertEquals(1, names.size());
assertTrue(names.contains(validFilename));
assertFalse(names.contains(otherFilename));

// A file data source created using the test folder and the basename sees only the right file
// A file data source created using the test folder and the basename sees all the files
names = new FileDataSource(testDir, getBaseName()).listNames(".*");
assertEquals(1, names.size());
assertEquals(2, names.size());
assertTrue(names.contains(validFilename));
assertFalse(names.contains(otherFilename));
assertTrue(names.contains(otherFilename));
}

@Test
Expand Down Expand Up @@ -81,11 +81,11 @@ void createNewFilesTest() throws IOException {
assertTrue(ds.exists("dummy.txt"));
assertTrue(ds.exists("dummy2.txt"));

// but only the files that contain the basename can be accessed through list names
// all the files can be accessed through list names for basename datasource (no extension)
Set<String> names = ds.listNames(".*");
assertEquals(1, names.size());
assertEquals(3, names.size());
assertTrue(names.contains(getBaseName() + suffix + "." + ext));
assertFalse(names.contains("dummy.txt"));
assertFalse(names.contains("dummy2.txt"));
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
Loading