Skip to content

Commit

Permalink
Merge pull request #137 from ImogenBits/import_machinery
Browse files Browse the repository at this point in the history
Fix problem importing
  • Loading branch information
Benezivas authored Oct 9, 2023
2 parents 4451360 + 8ee192d commit c2ee22c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
2 changes: 2 additions & 0 deletions algobattle/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ def init(
gitignore += f"{res_path.relative_to(target)}/\n"
target.joinpath(".gitignore").write_text(gitignore)

if not parsed_config.problem.location.is_absolute():
parsed_config.problem.location = target / parsed_config.problem.location
problem_obj = parsed_config.loaded_problem
if schemas:
instance: type[Instance] = problem_obj.instance_cls
Expand Down
9 changes: 0 additions & 9 deletions algobattle/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,15 +624,6 @@ class AlgobattleConfig(BaseModel):

model_config = ConfigDict(revalidate_instances="always")

@model_validator(mode="after")
def check_problem_defined(self) -> Self:
"""Validates that the specified problem is either installed or dynamically specified."""
prob = self.match.problem
if not self.problem.location.is_file() and prob not in Problem.available():
raise ValueError(f"The specified problem {prob} cannot be found")
else:
return self

@cached_property
def loaded_problem(self) -> Problem:
"""The problem this config uses."""
Expand Down
15 changes: 9 additions & 6 deletions algobattle/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,15 @@ def score(
def load_file(cls, name: str, file: Path) -> Self:
"""Loads the problem from the specified file."""
existing_problems = cls._problems.copy()
import_file_as_module(file, "__algobattle_problem__")
new_problems = {n: p for n, p in cls._problems.items() if n not in existing_problems}
if name not in new_problems:
raise ValueError(f"The {name} problem is not defined in {file}")
else:
return cls._problems[name]
cls._problems = {}
try:
import_file_as_module(file, "__algobattle_problem__")
if name not in cls._problems:
raise ValueError(f"The {name} problem is not defined in {file}")
else:
return cls._problems[name]
finally:
cls._problems = existing_problems

@classmethod
def load(cls, name: str, file: Path | None = None) -> Self:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "pdm.backend"

[project]
name = "algobattle-base"
version = "4.0.0"
version = "4.0.1"
description = "The Algobattle lab course package."
readme = "README.md"
requires-python = ">=3.11"
Expand Down

0 comments on commit c2ee22c

Please sign in to comment.