From c553ae364ff866d15eaa38bb679bc9e76bff100c Mon Sep 17 00:00:00 2001 From: vil02 <65706193+vil02@users.noreply.github.com> Date: Sun, 28 Jul 2024 12:57:03 +0200 Subject: [PATCH] refactor: simplify `create_puzzle.create` --- .../configurators/simple/common.py | 1 + .../configurators/simple/simple.py | 6 ++++++ .../configurators/simple/spiced.py | 4 ++++ puzzle_generator/create_puzzle.py | 18 +++++++----------- pyproject.toml | 2 +- 5 files changed, 19 insertions(+), 12 deletions(-) create mode 100644 puzzle_generator/configurators/simple/common.py diff --git a/puzzle_generator/configurators/simple/common.py b/puzzle_generator/configurators/simple/common.py new file mode 100644 index 0000000..a8c219c --- /dev/null +++ b/puzzle_generator/configurators/simple/common.py @@ -0,0 +1 @@ +MODULES = ["hashlib", "itertools", "base64", "json", "sys", "typing"] diff --git a/puzzle_generator/configurators/simple/simple.py b/puzzle_generator/configurators/simple/simple.py index 0937a8a..d6f134e 100644 --- a/puzzle_generator/configurators/simple/simple.py +++ b/puzzle_generator/configurators/simple/simple.py @@ -1,8 +1,11 @@ +import typing + from ...encryption_algorithms import bytestr_utils as bu from ...encryption_algorithms.simple import common from ...encryption_algorithms.simple import simple as se from ...puzzle_data_encryption import decrypt_data from .. import common as cc +from .common import MODULES from ...run_puzzle import run_puzzle @@ -11,6 +14,9 @@ def __init__(self, **kwargs): self._proc_hasher = kwargs.get("proc_hasher", cc.DefaultHasher) self._signature_hasher = kwargs.get("signature_hasher", cc.DefaultHasher) + def get_modules(self) -> typing.List[str]: + return MODULES + def get_encrypt(self): return se.get_encrypt(self._proc_hasher, self._signature_hasher) diff --git a/puzzle_generator/configurators/simple/spiced.py b/puzzle_generator/configurators/simple/spiced.py index 5e6da82..9bff9e9 100644 --- a/puzzle_generator/configurators/simple/spiced.py +++ b/puzzle_generator/configurators/simple/spiced.py @@ -6,6 +6,7 @@ from ...encryption_algorithms.simple import spiced as sse from ...puzzle_data_encryption import decrypt_data from .. import common as cc +from .common import MODULES from ...run_puzzle import run_puzzle @@ -29,6 +30,9 @@ def __init__(self, **kwargs): self._proc_spices = kwargs.get("proc_spices", _get_some_spices()) self._signature_spices = kwargs.get("signature_spices", _get_some_spices()) + def get_modules(self) -> typing.List[str]: + return MODULES + def get_encrypt(self): return sse.get_encrypt( self._proc_hasher, diff --git a/puzzle_generator/create_puzzle.py b/puzzle_generator/create_puzzle.py index 43d7a3a..4940e2b 100644 --- a/puzzle_generator/create_puzzle.py +++ b/puzzle_generator/create_puzzle.py @@ -4,14 +4,16 @@ from .configurators import configurators -def _create_str(in_modules, in_objects, in_encrypted_puzzle, constants: str) -> str: +def _create_str(in_encrypted_puzzle, configurator) -> str: advertisement = """# generated with puzzle-generator # # https://pypi.org/project/puzzle-generator/ # https://github.com/vil02/puzzle_generator/ """ - modules: str = "\n".join("import " + _ for _ in in_modules) + "\n" - objects: str = "\n".join(inspect.getsource(_) for _ in in_objects) + modules: str = "\n".join("import " + _ for _ in configurator.get_modules()) + "\n" + objects: str = "\n".join( + inspect.getsource(_) for _ in configurator.get_needed_objects() + ) puzzle_data: str = f"_PUZZLE = {in_encrypted_puzzle}" call: str = "run_puzzle(_PUZZLE, _DECRYPT, input)" @@ -22,7 +24,7 @@ def _create_str(in_modules, in_objects, in_encrypted_puzzle, constants: str) -> modules, objects, puzzle_data, - constants, + configurator.get_constants_str(), call, ] ) @@ -32,11 +34,5 @@ def _create_str(in_modules, in_objects, in_encrypted_puzzle, constants: str) -> def create(in_puzzle, **kwargs) -> str: configurator = configurators.get_configurator(**kwargs) - encrypted_puzzle = encrypt_data(in_puzzle, configurator.get_encrypt()) - needed_modules = ["hashlib", "itertools", "base64", "json", "sys", "typing"] - - needed_objects = configurator.get_needed_objects() - constants: str = configurator.get_constants_str() - - return _create_str(needed_modules, needed_objects, encrypted_puzzle, constants) + return _create_str(encrypted_puzzle, configurator) diff --git a/pyproject.toml b/pyproject.toml index 1e45371..6ab04dc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "puzzle-generator" -version = "0.5.2" +version = "0.5.3" description = "Generates python code representing a puzzle" authors = ["piotr.idzik "] readme = "./puzzle_generator/README.md"