This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
53 additions
and
43 deletions.
There are no files selected for viewing
12 changes: 6 additions & 6 deletions
12
backend/domain/simple_submission_checks/constraints/directory_constraint.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
backend/domain/simple_submission_checks/constraints/file_constraint.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
backend/domain/simple_submission_checks/constraints/not_present_constraint.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 5 additions & 2 deletions
7
backend/domain/simple_submission_checks/constraints/submission_constraint.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
from pathlib import Path | ||
|
||
from pydantic import BaseModel | ||
|
||
from domain.simple_submission_checks.constraints.file_constraint import FileConstraint | ||
from domain.simple_submission_checks.constraints.zip_constraint import ZipConstraint | ||
from domain.simple_submission_checks.validation_result import ValidationResult | ||
|
||
|
||
class SubmissionConstraint(BaseModel): | ||
root_constraint: ZipConstraint | FileConstraint | ||
|
||
def validate(self, path: str): | ||
self.root_constraint.validate_constraint(path) | ||
def validate(self, path: Path) -> ValidationResult: | ||
return self.root_constraint.validate_constraint(path) |
16 changes: 8 additions & 8 deletions
16
backend/domain/simple_submission_checks/constraints/zip_constraint.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,36 @@ | ||
import os | ||
import tempfile | ||
import zipfile | ||
from pathlib import Path | ||
from typing import Literal | ||
|
||
from pydantic import BaseModel | ||
|
||
from domain.simple_submission_checks.constraints.directory_constraint import DirectoryConstraint | ||
from domain.simple_submission_checks.constraints.file_constraint import FileConstraint | ||
from domain.simple_submission_checks.constraints.not_present_constraint import NotPresentConstraint | ||
from domain.simple_submission_checks.validation_result import ErrorResult, OkResult | ||
from domain.simple_submission_checks.validation_result import ErrorResult, OkResult, ValidationResult | ||
|
||
|
||
class ZipConstraint(BaseModel): | ||
type: Literal["zip_constraint"] | ||
name: str | ||
sub_constraints: list[DirectoryConstraint | FileConstraint | NotPresentConstraint] | ||
|
||
def validate_constraint(self, path: str): | ||
def validate_constraint(self, path: Path) -> ValidationResult: | ||
directory = os.listdir(path) | ||
|
||
# Check if file is present. | ||
if self.name not in directory: | ||
return ErrorResult(f"Zip file '{self.name}' not present.") | ||
|
||
# Check if file is a zip file. | ||
zip_path = os.path.join(path, self.name) | ||
zip_path = path / self.name | ||
if not zipfile.is_zipfile(zip_path): | ||
return ErrorResult(f"File '{self.name}' is not a zip file.") | ||
|
||
# Extract file into a Temp directory and validate sub constraints. | ||
with tempfile.TemporaryDirectory() as tmp_dir: | ||
with zipfile.ZipFile(zip_path, 'r') as zip_ref: | ||
zip_ref.extractall(tmp_dir) | ||
sub_constraints = [constraint.validate_constraint(tmp_dir) for constraint in self.sub_constraints] | ||
return OkResult(f"Zip file '{self.name}' present.", sub_constraints) | ||
with tempfile.TemporaryDirectory() as tmp_dir, zipfile.ZipFile(zip_path, "r") as zip_ref: | ||
zip_ref.extractall(tmp_dir) | ||
sub_constraints = [constraint.validate_constraint(Path(tmp_dir)) for constraint in self.sub_constraints] | ||
return OkResult(f"Zip file '{self.name}' present.", sub_constraints) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 9 additions & 6 deletions
15
backend/domain/simple_submission_checks/validation_result.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters