Skip to content

Commit

Permalink
Bugfixes, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
geoff128 committed Dec 2, 2024
1 parent f129825 commit 20839b4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/sio3pack/packages/package/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ def _from_db(self, problem_id: int):
"""
self.problem_id = problem_id

def _setup_django_handler(self, handler: str, problem_id: int):
def _setup_django_handler(self, problem_id: int):
try:
import django

self.django_enabled = True
module_path, class_name = handler.rsplit(".", 1)
module_path, class_name = self.django_handler.rsplit(".", 1)
module = importlib.import_module(module_path)
handler = getattr(module, class_name)
self.django = handler(package=self, problem_id=problem_id)
Expand Down Expand Up @@ -119,10 +119,10 @@ def get_test(self, test_id: str) -> Test:
def get_unpack_graph(self) -> GraphOperation | None:
pass

def get_run_graph(self, file: File, tests: list[Test] | None = None) -> Graph:
def get_run_graph(self, file: File, tests: list[Test] | None = None) -> GraphOperation | None:
pass

def get_save_outs_graph(self, tests: list[Test] | None = None) -> Graph:
def get_save_outs_graph(self, tests: list[Test] | None = None) -> GraphOperation | None:
pass

def save_to_db(self, problem_id: int):
Expand Down
32 changes: 19 additions & 13 deletions src/sio3pack/packages/sinolpack/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Sinolpack(Package):
Represents a OIOIOI's standard problem package.
"""

django_handler = "sio3pack.django.sinolpack.handler.SinolpackDjangoHandler"

@classmethod
def _find_main_dir(cls, archive: Archive) -> str | None:
dirs = list(map(os.path.normcase, archive.dirnames()))
Expand Down Expand Up @@ -61,9 +63,7 @@ def identify_from_db(cls, problem_id: int) -> bool:
"""
from sio3pack.django.sinolpack.models import SinolpackPackage

if SinolpackPackage.objects.filter(problem_id=problem_id).exists():
return True
return False
return SinolpackPackage.objects.filter(problem_id=problem_id).exists()

def __del__(self):
if hasattr(self, "tmpdir"):
Expand All @@ -81,6 +81,7 @@ def _from_file(self, file: LocalFile, django_settings=None):
archive.extract(to_path=self.tmpdir.name)
self.rootdir = os.path.join(self.tmpdir.name, self.short_name)
else:
# FIXME: Won't work in sinol-make.
self.short_name = os.path.basename(file.path)
self.rootdir = file.path

Expand All @@ -92,9 +93,11 @@ def _from_file(self, file: LocalFile, django_settings=None):

self.django_settings = django_settings

self._process_package()

def _from_db(self, problem_id: int):
super()._from_db(problem_id)
super()._setup_django_handler(SinolpackDjangoHandler, problem_id)
super()._setup_django_handler(problem_id)
if not self.django_enabled:
raise ImproperlyConfigured("sio3pack is not installed with Django support.")

Expand Down Expand Up @@ -201,8 +204,8 @@ def _detect_full_name_translations(self):
two-letter language code defined in ``settings.py``), if any such key is given.
"""
self.lang_titles = {}
for lang_code, lang in self._get_from_django_settings("LANGUAGES", [("en", "English")]):
key = "title_%s" % lang_code
for lang_code, _ in self._get_from_django_settings("LANGUAGES", [("en", "English")]):
key = f"title_{lang_code}"
if key in self.config:
self.lang_titles[lang_code] = self.config[key]

Expand All @@ -211,7 +214,7 @@ def get_submittable_extensions(self):
Returns a list of extensions that are submittable.
"""
return self.config.get(
"submittable_langs", self._get_from_django_settings("SUBMITTABLE_LANGUAGES", ["c", "cpp", "cxx", "py"])
"submittable_langs", self._get_from_django_settings("SUBMITTABLE_LANGUAGES", ["c", "cpp", "cc", "cxx", "py"])
)

def get_model_solution_regex(self):
Expand All @@ -225,11 +228,14 @@ def _get_model_solutions(self) -> list[tuple[ModelSolutionKind, str]]:
"""
Returns a list of model solutions, where each element is a tuple of model solution kind and filename.
"""
if not os.path.exists(self.get_prog_dir()):
return []

regex = self.get_model_solution_regex()
model_solutions = []
for file in os.listdir(self.get_prog_dir()):
match = re.match(regex, file)
if re.match(regex, file) and os.path.isfile(os.path.join(self.get_prog_dir(), file)):
if match and os.path.isfile(os.path.join(self.get_prog_dir(), file)):
model_solutions.append((ModelSolutionKind.from_regex(match.group(1)), file))

return model_solutions
Expand Down Expand Up @@ -291,13 +297,13 @@ def _process_statements(self):
return

lang_prefs = [""] + [
"-" + l[0] for l in self._get_from_django_settings("LANGUAGES", [("en", "English"), ("pl", "Polish")])
f"-{lang}" for lang, _ in self._get_from_django_settings("LANGUAGES", [("en", "English"), ("pl", "Polish")])
]

self.lang_statements = {}
for lang in lang_prefs:
try:
htmlzipfile = self.get_in_doc_dir(self.short_name + "zad" + lang + ".html.zip")
htmlzipfile = self.get_in_doc_dir(f"{self.short_name}zad{lang}.html.zip")
# TODO: what to do with html?
# if self._html_disallowed():
# raise ProblemPackageError(
Expand All @@ -317,12 +323,12 @@ def _process_statements(self):
pass

try:
pdffile = self.get_in_doc_dir(self.short_name + "zad" + lang + ".pdf")
pdffile = self.get_in_doc_dir(f"{self.short_name}zad{lang}.pdf")
if lang == "":
self.statement = pdffile
else:
self.lang_statements[lang[1:]] = pdffile
except:
except FileNotFoundError:
pass

def _process_attachments(self):
Expand Down Expand Up @@ -358,7 +364,7 @@ def save_to_db(self, problem_id: int):
Save the package to the database. If sio3pack isn't installed with Django
support, it should raise an ImproperlyConfigured exception.
"""
self._setup_django_handler("sio3pack.django.sinolpack.handler.SinolpackDjangoHandler", problem_id)
self._setup_django_handler(problem_id)
if not self.django_enabled:
raise ImproperlyConfigured("sio3pack is not installed with Django support.")
self.django.save_to_db()

0 comments on commit 20839b4

Please sign in to comment.