From b093aa4b78947f8a71de693b3cc49ee99634bfaa Mon Sep 17 00:00:00 2001 From: Mateusz Masiarz Date: Sun, 1 Dec 2024 17:23:08 +0100 Subject: [PATCH] Refactor --- src/sio3pack/packages/package/model.py | 20 ++++++------ src/sio3pack/packages/sinolpack/model.py | 39 ++++++++++++------------ 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/sio3pack/packages/package/model.py b/src/sio3pack/packages/package/model.py index 0a863ec..ba5e45b 100644 --- a/src/sio3pack/packages/package/model.py +++ b/src/sio3pack/packages/package/model.py @@ -2,7 +2,7 @@ from sio3pack import LocalFile from sio3pack.files import File -from sio3pack.graph import Graph +from sio3pack.graph import Graph, GraphOperation from sio3pack.packages.exceptions import UnknownPackageType from sio3pack.test import Test from sio3pack.utils.archive import Archive @@ -16,14 +16,8 @@ class Package(RegisteredSubclassesBase): abstract = True - def __init__(self, file: File): + def __init__(self): super().__init__() - self.file = file - if isinstance(file, LocalFile): - if Archive.is_archive(file.path): - self.is_archive = True - else: - self.is_archive = False @classmethod def from_file(cls, file: LocalFile, django_settings=None): @@ -32,6 +26,14 @@ def from_file(cls, file: LocalFile, django_settings=None): return subclass(file, django_settings) raise UnknownPackageType(file.path) + def _from_file(self, file: LocalFile): + self.file = file + if isinstance(file, LocalFile): + if Archive.is_archive(file.path): + self.is_archive = True + else: + self.is_archive = False + def get_task_id(self) -> str: pass @@ -56,7 +58,7 @@ def get_tests(self) -> list[Test]: def get_test(self, test_id: str) -> Test: pass - def get_package_graph(self) -> Graph: + def get_unpack_graph(self) -> GraphOperation | None: pass def get_run_graph(self, file: File, tests: list[Test] | None = None) -> Graph: diff --git a/src/sio3pack/packages/sinolpack/model.py b/src/sio3pack/packages/sinolpack/model.py index d26c87e..419248f 100644 --- a/src/sio3pack/packages/sinolpack/model.py +++ b/src/sio3pack/packages/sinolpack/model.py @@ -54,27 +54,26 @@ def __del__(self): if hasattr(self, "tmpdir"): self.tmpdir.cleanup() - def __init__(self, file: File, django_settings=None): - super().__init__(file) - - if isinstance(file, LocalFile): - if self.is_archive: - archive = Archive(file.path) - self.short_name = self._find_main_dir(archive) - self.tmpdir = tempfile.TemporaryDirectory() - archive.extract(to_path=self.tmpdir.name) - self.rootdir = os.path.join(self.tmpdir.name, self.short_name) - else: - self.short_name = os.path.basename(file.path) - self.rootdir = file.path - - try: - graph_file = self.get_in_root("graph.json") - self.graph_manager = GraphManager.from_file(graph_file) - except FileNotFoundError: - self.has_custom_graph = False + def __init__(self): + super().__init__() + + def _from_file(self, file: LocalFile, django_settings=None): + super()._from_file(file) + if self.is_archive: + archive = Archive(file.path) + self.short_name = self._find_main_dir(archive) + self.tmpdir = tempfile.TemporaryDirectory() + archive.extract(to_path=self.tmpdir.name) + self.rootdir = os.path.join(self.tmpdir.name, self.short_name) else: - raise NotImplementedError() + self.short_name = os.path.basename(file.path) + self.rootdir = file.path + + try: + graph_file = self.get_in_root("graph.json") + self.graph_manager = GraphManager.from_file(graph_file) + except FileNotFoundError: + self.has_custom_graph = False self.django_settings = django_settings