Added __getitem__ to allow easy directory traversal.

This commit is contained in:
2025-11-08 17:50:51 +01:00
parent 89ba272003
commit d6b58f0b51

View File

@@ -1,4 +1,5 @@
from __future__ import annotations from __future__ import annotations
import pathlib
import requests import requests
import urllib.parse import urllib.parse
from uuid import UUID from uuid import UUID
@@ -299,5 +300,15 @@ class Item():
self._children = self.get_child_items() self._children = self.get_child_items()
return self._children return self._children
def __getitem__(self, key: str) -> Item:
if self.git_object_type != "tree": # type: ignore[attr-defined]
raise ValueError("Child items can only be accessed for folder items.")
if not key.startswith("/"):
key = pathlib.Path(self.path).joinpath(key).absolute().as_posix()
for child in self.children:
if child.path == key:
return child
raise KeyError(f"Child item with path '{key}' not found.")
def __str__(self): def __str__(self):
return f"Item(path=\"{self._path}\" type={self.git_object_type})" # type: ignore[attr-defined] return f"Item(path=\"{self._path}\" type={self.git_object_type})" # type: ignore[attr-defined]