Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
ruff :/ #43
Browse files Browse the repository at this point in the history
  • Loading branch information
lbarraga committed Mar 17, 2024
1 parent 0055979 commit 5fe25ab
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
from __future__ import annotations

import os
from typing import Literal, Optional
from pathlib import Path
from typing import Literal

from pydantic import BaseModel

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 DirectoryConstraint(BaseModel):
type: Literal["directory_constraint"]
name: str
sub_constraints: list[DirectoryConstraint | FileConstraint | NotPresentConstraint]

def validate_constraint(self, path: str):
dir_path = os.path.join(path, self.name)
if not os.path.isdir(dir_path):
def validate_constraint(self, path: Path) -> ValidationResult:
dir_path = path / self.name
if not Path.is_dir(dir_path):
return ErrorResult(f"Directory '{self.name}' not present.")

sub_results = [constraint.validate_constraint(dir_path) for constraint in self.sub_constraints]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import os
from pathlib import Path
from typing import Literal

from pydantic import BaseModel

from domain.simple_submission_checks.validation_result import ValidationResult, ErrorResult, OkResult
from domain.simple_submission_checks.validation_result import ErrorResult, OkResult, ValidationResult


class FileConstraint(BaseModel):
type: Literal["file_constraint"]
name: str

def validate_constraint(self, path: str) -> ValidationResult:
def validate_constraint(self, path: Path) -> ValidationResult:
directory = os.listdir(path)

if self.name not in directory:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import os
from pathlib import Path
from typing import Literal

from pydantic import BaseModel

from domain.simple_submission_checks.validation_result import ErrorResult, OkResult
from domain.simple_submission_checks.validation_result import ErrorResult, OkResult, ValidationResult


class NotPresentConstraint(BaseModel):
type: Literal["not_present_constraint"]
name: str

def validate_constraint(self, path: str):
def validate_constraint(self, path: Path) -> ValidationResult:
directory = os.listdir(path)

if self.name in directory:
Expand Down
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)
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)
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import Path

from domain.simple_submission_checks.constraints.zip_constraint import ZipConstraint

json_string = {
Expand All @@ -10,54 +12,53 @@
"sub_constraints": [
{
"type": "file_constraint",
"name": "Resume.pdf"
"name": "Resume.pdf",
},
{
"type": "file_constraint",
"name": "CoverLetter.docx"
"name": "CoverLetter.docx",
},
{
"type": "file_constraint",
"name": "Transcript.pdf"
}
]
"name": "Transcript.pdf",
},
],
},
{
"type": "directory_constraint",
"name": "Images",
"sub_constraints": [
{
"type": "file_constraint",
"name": "Vacation.jpg"
"name": "Vacation.jpg",
},
{
"type": "file_constraint",
"name": "ProfilePicture.jpg"
}
]
"name": "ProfilePicture.jpg",
},
],
},
{
"type": "directory_constraint",
"name": "Videos",
"sub_constraints": [
{
"type": "file_constraint",
"name": "Graduation.mp4"
}
]
"name": "Graduation.mp4",
},
],
},
{
"type": "not_present_constraint",
"name": "file4.txt"
}
]
"name": "file4.txt",
},
],
}

# Parse the JSON to a Pydantic model
constraint = ZipConstraint.parse_obj(json_string)

# Path to the directory to validate
path_to_directory = "/home/lukasbt/"
path_to_directory = Path("/home/lukasbt/")
# Validate the directory and print the result
validation_result = constraint.validate_constraint(path_to_directory)
print(validation_result)
15 changes: 9 additions & 6 deletions backend/domain/simple_submission_checks/validation_result.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@


class ValidationResult:
def __init__(self, is_valid: bool, message: str, sub_results: list):
self.is_valid = is_valid
self.message = message
self.sub_results = sub_results

def __str__(self, level=0):
raise NotImplementedError("Subclass must implement __str__ method")
def __str__(self, level: int = 0):
raise NotImplementedError


class ErrorResult(ValidationResult):
def __init__(self, message):
def __init__(self, message: str):
super().__init__(False, message, [])

def __str__(self, indent=0):
def __str__(self, indent: int = 0):
return f"{' ' * indent}\u2718 [FAIL] {self.message}"


class OkResult(ValidationResult):
def __init__(self, message, results=[]):
def __init__(self, message: str, results: list[ValidationResult] | None = None):
results = results or []
super().__init__(True, message, results)

def __str__(self, level=0):
def __str__(self, level: int = 0):
ret = f"{'\t' * level}\u2714 [OK] {self.message}"
if self.sub_results:
sub_results_str = "\n".join(sub_result.__str__(level + 1) for sub_result in self.sub_results)
Expand Down
1 change: 1 addition & 0 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ ignore = [
"PT027",
"FBT003",
"DTZ005",
"ANN204", # Init function of object should not need return type annotation.
]

# Allow fix for all enabled rules (when `--fix`) is provided.
Expand Down

0 comments on commit 5fe25ab

Please sign in to comment.