Skip to content

Commit

Permalink
style: add some type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
vil02 committed Jul 27, 2024
1 parent 8d39058 commit 04d6825
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
36 changes: 18 additions & 18 deletions puzzle_generator/create_puzzle.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,26 @@ def run_puzzle(in_puzzle, in_decrypt, get_answer):
run_puzzle(new_puzzle, in_decrypt, get_answer)


def _create_str(in_modules, in_objects, in_encrypted_puzzle, constants_str: str) -> str:
def _create_str(in_modules, in_objects, in_encrypted_puzzle, constants: str) -> 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)
puzzle_data_str = f"_PUZZLE = {in_encrypted_puzzle}"
call_str = "run_puzzle(_PUZZLE, _DECRYPT, input)"
modules: str = "\n".join("import " + _ for _ in in_modules) + "\n"
objects: str = "\n".join(inspect.getsource(_) for _ in in_objects)
puzzle_data: str = f"_PUZZLE = {in_encrypted_puzzle}"
call: str = "run_puzzle(_PUZZLE, _DECRYPT, input)"

return (
"\n".join(
[
advertisement,
modules_str,
objects_str,
puzzle_data_str,
constants_str,
call_str,
modules,
objects,
puzzle_data,
constants,
call,
]
)
+ "\n"
Expand Down Expand Up @@ -95,8 +95,8 @@ def _list_of_bytes_to_str(in_list: typing.List[bytes]) -> str:


def _list_of_bytes_to_codestr(in_list: typing.List[bytes]) -> str:
raw_str = _list_of_bytes_to_str(in_list)
return f"[bytestr_to_bytes(_) for _ in {raw_str}]"
raw: str = _list_of_bytes_to_str(in_list)
return f"[bytestr_to_bytes(_) for _ in {raw}]"


class SpicedSimpleEncryptionConfigurator:
Expand Down Expand Up @@ -129,20 +129,20 @@ def get_needed_objects(self):
def get_constants_str(
self,
) -> str:
proc_spices_str = (
proc_spices: str = (
f"_PROC_SPICES = {_list_of_bytes_to_codestr(self._proc_spices)}"
)
signature_spices_str = (
signature_spices: str = (
f"_SIGNATURE_SPICES = {_list_of_bytes_to_codestr(self._signature_spices)}"
)
decrypt_str = (
decrypt: str = (
"_DECRYPT = get_decrypt("
f"{_get_hasher_name(self._proc_hasher)}, "
f"{_get_hasher_name(self._signature_hasher)}, "
"_PROC_SPICES, "
"_SIGNATURE_SPICES)"
)
return "\n".join([proc_spices_str, signature_spices_str, decrypt_str])
return "\n".join([proc_spices, signature_spices, decrypt])


def _get_configurator(**kwargs):
Expand All @@ -161,6 +161,6 @@ def create(in_puzzle, **kwargs) -> str:
needed_modules = ["hashlib", "itertools", "base64", "json", "sys", "typing"]

needed_objects = configurator.get_needed_objects()
constants_str = configurator.get_constants_str()
constants: str = configurator.get_constants_str()

return _create_str(needed_modules, needed_objects, encrypted_puzzle, constants_str)
return _create_str(needed_modules, needed_objects, encrypted_puzzle, constants)
13 changes: 8 additions & 5 deletions puzzle_generator/puzzle_data_encryption.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import json
import typing


def encrypt_data(in_data, in_encrypt):
def encrypt_data(in_data, in_encrypt: typing.Callable[[str, str], str]):
if list(in_data.keys()) == ["str"]:
return {"str": in_data["str"]}
rest_str = json.dumps(encrypt_data(in_data["rest"], in_encrypt))
encrypted_str = in_encrypt(rest_str, in_data["pass"])
return {"str": in_data["str"], "rest": encrypted_str}
rest: str = json.dumps(encrypt_data(in_data["rest"], in_encrypt))
encrypted: str = in_encrypt(rest, in_data["pass"])
return {"str": in_data["str"], "rest": encrypted}


def decrypt_data(in_rest: str, in_pass: str, in_decrypt):
def decrypt_data(
in_rest: str, in_pass: str, in_decrypt: typing.Callable[[str, str], str | None]
):
rest_str = in_decrypt(in_rest, in_pass)
if rest_str is None:
return None
Expand Down
12 changes: 6 additions & 6 deletions tests/test_create_puzzle.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ def _run_puzzle_file(


def _run_puzzle_str(
in_puzzle_str: str, answers: list[str], in_puzzle_path: pathlib.Path
in_puzzle: str, answers: list[str], in_puzzle_path: pathlib.Path
) -> subprocess.CompletedProcess[str]:
with open(in_puzzle_path, "w", encoding="utf-8") as puzzle_file:
puzzle_file.write(in_puzzle_str)
puzzle_file.write(in_puzzle)
return _run_puzzle_file(in_puzzle_path, answers)


Expand Down Expand Up @@ -109,8 +109,8 @@ def test_all_good_answers(
puzzle_path: pathlib.Path,
configuration,
) -> None:
puzzle_str = cp.create(puzzle_tc.puzzle, **configuration)
res = _run_puzzle_str(puzzle_str, puzzle_tc.correct.input, puzzle_path)
puzzle: str = cp.create(puzzle_tc.puzzle, **configuration)
res = _run_puzzle_str(puzzle, puzzle_tc.correct.input, puzzle_path)

assert res.returncode == 0
assert res.stdout == puzzle_tc.correct.output
Expand All @@ -124,8 +124,8 @@ def test_wrong_answers(
configuration,
) -> None:
for cur_wrong in puzzle_tc.wrong:
puzzle_str = cp.create(puzzle_tc.puzzle, **configuration)
res = _run_puzzle_str(puzzle_str, cur_wrong.input, puzzle_path)
puzzle: str = cp.create(puzzle_tc.puzzle, **configuration)
res = _run_puzzle_str(puzzle, cur_wrong.input, puzzle_path)
assert res.returncode == 1
assert res.stdout == cur_wrong.output
assert not res.stderr
Expand Down

0 comments on commit 04d6825

Please sign in to comment.