import json from typing import Optional from PySide6.QtCore import QByteArray, QObject, QUrl, Signal, Slot, Property from PySide6.QtQml import QmlElement from kodereviewer.data import PullRequest QML_IMPORT_NAME = "org.deprecated.kodereviewer" QML_IMPORT_MAJOR_VERSION = 1 @QmlElement class Project(QObject): """Represents a github project""" _name: str _owner: str _url: QUrl _pull_requests: list[PullRequest] pullRequestsChanged = Signal() def __init__(self, name: str, owner: str, url: QUrl): super().__init__() self._name = name self._owner = owner self._url = url self._pull_requests = [] @Property(str) def name(self) -> str: return self._name @Property(str) def owner(self) -> str: return self._owner @Property(QUrl) def url(self) -> QUrl: return self._url @Property(list) def pullRequests(self) -> list[PullRequest]: return self._pull_requests @Slot(int, result=PullRequest) def pullRequest(self, number: int) -> Optional[PullRequest]: return self.find_pull_request(number) def load_pull_requests(self, response: QByteArray) -> None: data = json.loads(response.toStdString()) self._pull_requests = [ PullRequest(pr, parent=self) for pr in data ] self.pullRequestsChanged.emit() def find_pull_request(self, number: int) -> Optional[PullRequest]: for pr in self._pull_requests: if pr.number == number: return pr return None