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 Jun 26, 2024
1 parent 2dbe40a commit f91ce66
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 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 @@ -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

0 comments on commit f91ce66

Please sign in to comment.