Skip to content

Commit

Permalink
Merge pull request #23 from CSCfi/bugfix/better-windows-noconsole
Browse files Browse the repository at this point in the history
Better fix for noconsole bug
  • Loading branch information
gimppa authored May 8, 2023
2 parents 23370fe + ddd34d3 commit 1c03ea1
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions crypt4gh_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from platform import system
from typing import Optional, Union
from io import BufferedWriter, BufferedReader
from unittest.mock import MagicMock

from nacl.public import PrivateKey
from crypt4gh.keys import c4gh, get_private_key, get_public_key
Expand Down Expand Up @@ -42,8 +41,12 @@ def __init__(self, window: tk.Tk) -> None:
self.window = window
self.window.resizable(False, False)
self.window.title("Crypt4GH")
# This prevents pyinstaller --noconsole from referencing a nonexistent sys.stdout.write
if system() == "Windows":
self.old_stdout = sys.stdout
self.tmp_stdout = open(os.devnull, "w")
sys.stdout = self.tmp_stdout
# print to activity log instead of console
sys.stdout = MagicMock()
sys.stdout.write = self.print_redirect # type: ignore

# 1st column FIELDS AND LABELS
Expand Down Expand Up @@ -301,11 +304,17 @@ def mock_callback(self, password: str) -> str:
"""Mock callback to return password."""
return password

def cleanup(self) -> None:
"""Restore the sys.stdout on Windows."""
if system() == "Windows":
sys.stdout = self.old_stdout
self.tmp_stdout.close()


def main() -> None:
"""Run Program."""
root = tk.Tk()
GUI(root)
gui = GUI(root)
print("To begin file encryption:\n")
print("1. Generate keys (optional)")
print("2. Load your private key (optional)")
Expand All @@ -318,6 +327,7 @@ def main() -> None:
print("3. Select file for decryption")
print("4. Click [Decrypt File]\n")
root.mainloop()
gui.cleanup()


if __name__ == "__main__":
Expand Down

0 comments on commit 1c03ea1

Please sign in to comment.