Skip to content

Commit

Permalink
Merge pull request #50 from vil02/simplify_create_puzzle
Browse files Browse the repository at this point in the history
refactor: simplify `create_puzzle.create`
  • Loading branch information
vil02 authored Jul 28, 2024
2 parents 04b2fd2 + c553ae3 commit fca30b8
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions puzzle_generator/configurators/simple/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MODULES = ["hashlib", "itertools", "base64", "json", "sys", "typing"]
6 changes: 6 additions & 0 deletions puzzle_generator/configurators/simple/simple.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions puzzle_generator/configurators/simple/spiced.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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,
Expand Down
18 changes: 7 additions & 11 deletions puzzle_generator/create_puzzle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)"

Expand All @@ -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,
]
)
Expand All @@ -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)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
readme = "./puzzle_generator/README.md"
Expand Down

0 comments on commit fca30b8

Please sign in to comment.