Skip to content

Commit f971571

Browse files
committed
Migrate to use NullGitAutograderRepo
This is to support the use of the ignore repo_type
1 parent 89c3d2a commit f971571

File tree

8 files changed

+122
-34
lines changed

8 files changed

+122
-34
lines changed

src/git_autograder/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"GitAutograderWrongAnswerException",
77
"GitAutograderTestLoader",
88
"GitAutograderRepo",
9+
"GitAutograderRepoBase",
910
"GitAutograderStatus",
1011
"GitAutograderOutput",
1112
"GitAutograderBranch",
@@ -24,7 +25,8 @@
2425
from .exercise import GitAutograderExercise
2526
from .output import GitAutograderOutput
2627
from .remote import GitAutograderRemote
27-
from .repo import GitAutograderRepo
28+
from .repo.repo import GitAutograderRepo
29+
from .repo.repo_base import GitAutograderRepoBase
2830
from .status import GitAutograderStatus
2931
from .test_utils import (
3032
GitAutograderTestLoader,

src/git_autograder/exercise.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
)
1717
from git_autograder.exercise_config import ExerciseConfig
1818
from git_autograder.output import GitAutograderOutput
19-
from git_autograder.repo import GitAutograderRepo
19+
from git_autograder.repo.null_repo import NullGitAutograderRepo
20+
from git_autograder.repo.repo import GitAutograderRepo
21+
from git_autograder.repo.repo_base import GitAutograderRepoBase
2022
from git_autograder.status import GitAutograderStatus
2123

2224

@@ -36,12 +38,12 @@ class GitAutograderExercise:
3638
:param exercise_path: Path to a given exercise folder
3739
:type exercise_path: Union[str, os.PathLike]
3840
"""
41+
3942
def __init__(
4043
self,
4144
exercise_path: str | os.PathLike,
4245
) -> None:
43-
"""Constructor method
44-
"""
46+
"""Constructor method"""
4547

4648
# TODO: We should not be starting the grading at the point of initializing, but
4749
# we're keeping this because of the exception system
@@ -63,10 +65,14 @@ def __init__(
6365
# The only edge cases are those where they run git init themselves, but that
6466
# is the purpose of handling the exception where we can display an error on
6567
# their end.
66-
self.repo: GitAutograderRepo = GitAutograderRepo(
67-
self.config.exercise_name,
68-
Path(exercise_path) / self.config.exercise_repo.repo_name,
69-
)
68+
self.repo: GitAutograderRepoBase
69+
if self.config.exercise_repo.repo_type == "ignore":
70+
self.repo = NullGitAutograderRepo()
71+
else:
72+
self.repo = GitAutograderRepo(
73+
self.config.exercise_name,
74+
Path(exercise_path) / self.config.exercise_repo.repo_name,
75+
)
7076
except InvalidGitRepositoryError:
7177
raise GitAutograderInvalidStateException("Exercise is not a Git repository")
7278
self.__answers_parser: Optional[GitAutograderAnswersParser] = None

src/git_autograder/exercise_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class ExerciseConfig:
99
@dataclass
1010
class ExerciseRepoConfig:
11-
repo_type: Literal["local", "remote"]
11+
repo_type: Literal["local", "remote", "ignore"]
1212
repo_name: str
1313
repo_title: Optional[str]
1414
create_fork: Optional[bool]

src/git_autograder/repo.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/git_autograder/repo/__init__.py

Whitespace-only changes.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from git import Repo
2+
3+
from git_autograder.helpers.branch_helper import BranchHelper
4+
from git_autograder.helpers.commit_helper import CommitHelper
5+
from git_autograder.helpers.file_helper import FileHelper
6+
from git_autograder.helpers.remote_helper import RemoteHelper
7+
from git_autograder.repo.repo_base import GitAutograderRepoBase
8+
9+
10+
class NullGitAutograderRepo(GitAutograderRepoBase):
11+
def repo(self) -> Repo:
12+
raise AttributeError(
13+
"Cannot access attribute repo on NullGitAutograderRepo. Check that your repo_type is not 'ignore'."
14+
)
15+
16+
def branches(self) -> BranchHelper:
17+
raise AttributeError(
18+
"Cannot access attribute branches on NullGitAutograderRepo. Check that your repo_type is not 'ignore'."
19+
)
20+
21+
def commits(self) -> CommitHelper:
22+
raise AttributeError(
23+
"Cannot access attribute commits on NullGitAutograderRepo. Check that your repo_type is not 'ignore'."
24+
)
25+
26+
def remotes(self) -> RemoteHelper:
27+
raise AttributeError(
28+
"Cannot access attribute remotes on NullGitAutograderRepo. Check that your repo_type is not 'ignore'."
29+
)
30+
31+
def files(self) -> FileHelper:
32+
raise AttributeError(
33+
"Cannot access attribute files on NullGitAutograderRepo. Check that your repo_type is not 'ignore'."
34+
)
35+
36+
def __getattr__(self, name: str) -> None:
37+
raise AttributeError(
38+
f"Cannot access attribute {name} on NullGitAutograderRepo. Check that your repo_type is not 'ignore'."
39+
)

src/git_autograder/repo/repo.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import os
2+
3+
from git import Repo
4+
5+
from git_autograder.helpers.branch_helper import BranchHelper
6+
from git_autograder.helpers.commit_helper import CommitHelper
7+
from git_autograder.helpers.file_helper import FileHelper
8+
from git_autograder.helpers.remote_helper import RemoteHelper
9+
from git_autograder.repo.repo_base import GitAutograderRepoBase
10+
11+
12+
class GitAutograderRepo(GitAutograderRepoBase):
13+
def __init__(
14+
self,
15+
exercise_name: str,
16+
repo_path: str | os.PathLike,
17+
) -> None:
18+
self.exercise_name = exercise_name
19+
self.repo_path = repo_path
20+
21+
self._repo: Repo = Repo(self.repo_path)
22+
23+
self._branches: BranchHelper = BranchHelper(self._repo)
24+
self._commits: CommitHelper = CommitHelper(self._repo)
25+
self._remotes: RemoteHelper = RemoteHelper(self._repo)
26+
self._files: FileHelper = FileHelper(self._repo)
27+
28+
def repo(self) -> Repo:
29+
return self._repo
30+
31+
def branches(self) -> BranchHelper:
32+
return self._branches
33+
34+
def commits(self) -> CommitHelper:
35+
return self._commits
36+
37+
def remotes(self) -> RemoteHelper:
38+
return self._remotes
39+
40+
def files(self) -> FileHelper:
41+
return self._files
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from abc import ABC, abstractmethod
2+
3+
from git import Repo
4+
5+
from git_autograder.helpers.branch_helper import BranchHelper
6+
from git_autograder.helpers.commit_helper import CommitHelper
7+
from git_autograder.helpers.file_helper import FileHelper
8+
from git_autograder.helpers.remote_helper import RemoteHelper
9+
10+
11+
class GitAutograderRepoBase(ABC):
12+
@abstractmethod
13+
def repo(self) -> Repo: ...
14+
15+
@abstractmethod
16+
def branches(self) -> BranchHelper: ...
17+
18+
@abstractmethod
19+
def commits(self) -> CommitHelper: ...
20+
21+
@abstractmethod
22+
def remotes(self) -> RemoteHelper: ...
23+
24+
@abstractmethod
25+
def files(self) -> FileHelper: ...

0 commit comments

Comments
 (0)