From 20839b421631e1c4f7f73509e4d8ce7812abade1 Mon Sep 17 00:00:00 2001 From: Tomasz Kwiatkowski Date: Mon, 2 Dec 2024 16:17:35 +0100 Subject: [PATCH] Bugfixes, refactor --- src/sio3pack/packages/package/model.py | 8 +++--- src/sio3pack/packages/sinolpack/model.py | 32 ++++++++++++++---------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/sio3pack/packages/package/model.py b/src/sio3pack/packages/package/model.py index 08c3689..9438957 100644 --- a/src/sio3pack/packages/package/model.py +++ b/src/sio3pack/packages/package/model.py @@ -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) @@ -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): diff --git a/src/sio3pack/packages/sinolpack/model.py b/src/sio3pack/packages/sinolpack/model.py index c64a4cd..e0bf13e 100644 --- a/src/sio3pack/packages/sinolpack/model.py +++ b/src/sio3pack/packages/sinolpack/model.py @@ -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())) @@ -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"): @@ -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 @@ -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.") @@ -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] @@ -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): @@ -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 @@ -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( @@ -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): @@ -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()