Skip to content

Commit

Permalink
Extract method.
Browse files Browse the repository at this point in the history
  • Loading branch information
khatchad committed Apr 25, 2024
1 parent 8560264 commit e369a23
Showing 1 changed file with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,7 @@ public CAstNode visitImport(Import imp) throws Exception {
Optional<SourceModule> localModule = getLocalModule(moduleName);

for (File pathEntry : pythonPath) {
Path modulePath =
localModule
.map(SourceModule::getURL)
.map(URL::getFile)
.map(Path::of)
.orElseThrow(IllegalStateException::new);
LOGGER.finer("Found module path: " + modulePath);
Path modulePath = getPath(localModule);

if (modulePath.startsWith(pathEntry.toPath())) {
// Found it.
Expand Down Expand Up @@ -151,6 +145,27 @@ public CAstNode visitImport(Import imp) throws Exception {
return super.visitImport(imp);
}

/**
* Returns the {@link Path} corresponding to the given {@link SourceModule}. If a {@link
* SourceModule} is not supplied, an {@link IllegalStateException} is thrown.
*
* @param module The {@link SourceModule} for which to extract a {@link Path}.
* @return The {@link Path} corresponding to the given {@link SourceModule}.
* @throws IllegalStateException If the given {@link SourceModule} is not present.
* @implNote The discovered {@link Path} will be logged.
*/
private Path getPath(Optional<SourceModule> module) {
Path path =
module
.map(SourceModule::getURL)
.map(URL::getFile)
.map(Path::of)
.orElseThrow(IllegalStateException::new);

LOGGER.finer("Found path: " + path);
return path;
}

@Override
public CAstNode visitImportFrom(ImportFrom importFrom) throws Exception {
Optional<String> s =
Expand Down Expand Up @@ -181,13 +196,7 @@ public CAstNode visitImportFrom(ImportFrom importFrom) throws Exception {
Optional<SourceModule> localModule = getLocalModule(moduleName);

for (File pathEntry : pythonPath) {
Path modulePath =
localModule
.map(SourceModule::getURL)
.map(URL::getFile)
.map(Path::of)
.orElseThrow(IllegalStateException::new);
LOGGER.finer("Found module path: " + modulePath);
Path modulePath = getPath(localModule);

if (modulePath.startsWith(pathEntry.toPath())) {
// Found it.
Expand Down

0 comments on commit e369a23

Please sign in to comment.