OpenHands/opendevin/files.py
Alex Bäuerle e6d91affc6
refactor: remove the previously implemented shallow file fetch (#1231)
Removing because the new filetree implementation is fast enough as is and fetching partial trees adds a lot of complexity that we probably don't need. For example, if we still want to automatically open the tree location of a changed file, we need to make sure all the parent folders are fetched first.
2024-04-19 17:25:40 +00:00

43 lines
1.1 KiB
Python

from pathlib import Path
from typing import Any, Dict, List
class WorkspaceFile:
name: str
children: List['WorkspaceFile']
def __init__(self, name: str, children: List['WorkspaceFile']):
self.name = name
self.children = children
def to_dict(self) -> Dict[str, Any]:
"""Converts the File object to a dictionary.
Returns:
The dictionary representation of the File object.
"""
return {
'name': self.name,
'children': [child.to_dict() for child in self.children],
}
def get_folder_structure(workdir: Path) -> WorkspaceFile:
"""Gets the folder structure of a directory.
Args:
workdir: The directory path.
Returns:
The folder structure.
"""
root = WorkspaceFile(name=workdir.name, children=[])
for item in workdir.iterdir():
if item.is_dir():
dir = get_folder_structure(item)
if dir.children:
root.children.append(dir)
else:
root.children.append(WorkspaceFile(name=item.name, children=[]))
return root