Skip to content

Commit

Permalink
fix: use iterative accumulation to define module path
Browse files Browse the repository at this point in the history
  • Loading branch information
manishshettym committed Sep 18, 2024
1 parent 5e2d256 commit 2c762e3
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions r2e/models/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,31 @@ class Module(BaseModel):

@property
def local_path(self) -> str:
path = self.module_id.identifier.replace(".", "/")
if self.module_type == ModuleTypeEnum.PACKAGE:
return f"{self.repo.repo_path}/{path}"
else:
return f"{self.repo.repo_path}/{path}.py"
parts = self.module_id.identifier.split(".")
path = self.repo.repo_path
segment = ""

for i, part in enumerate(parts):
# accumulate a path segment until full path exists
segment = f"{segment}.{part}" if segment else part
current_path = os.path.join(path, segment)

# if the path exists, reset segment
if os.path.exists(current_path):
path = current_path
segment = ""

elif i == len(parts) - 1:
# if module is a file, check if it exists
if self.module_type != ModuleTypeEnum.PACKAGE:
py_path = f"{current_path}.py"
if os.path.exists(py_path):
return py_path

# if a package, return directory
return os.path.join(path, segment)

return path

@property
def relative_module_path(self) -> str:
Expand Down

0 comments on commit 2c762e3

Please sign in to comment.