-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
85 lines (70 loc) · 4 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from enigma_machine import EnigmaMachine, InvalidPresetCode
from data.test_paragraphs import paragraphs
import datetime
import random
def main():
machine = EnigmaMachine()
help_text = {
"no-logs": '''Disables the logging of ciphered messages for the entire session of this EnigmaMachine run.''',
"get-date": '''Prints the date for which the EnigmaMachine is set to.''',
"set-date": '''The machine starts out with today's date as the default message-date. To set the message-date for
the machine, input a date in any valid ISO 8601 format, except ordinal dates (e.g. YYYY-DDD).
>> Year-Month-Day format:
>> YYYYMMDD (basic format) or YYYY-MM-DD (extended format)
The date is saved for all future messages ciphered through this machine. To change the date, call this command again.''',
"private": '''For one message-cipher only, set a "8-letter" code as the private key. When the next message is
ciphered, the machine will reset to the latest date set using the 'date' command. Format -> re:"[a-zA-Z]{8}"
To check if the private-key is set, use the 'code' command. When trying to decipher the generated message, re-enter the private-key using this command.''',
"code": '''If a private-key is active for the next message, this command will print the private-key, else it
displays "None"''',
"message": '''Enter your message that needs to ciphered by the EnigmaMachine. Press "RETURN/ENTER" after you
have typed the message to process the cipher through the EnigmaMachine.
EnigmaMachine only ciphers alphabets, all white-spaces and punctuation/symbols will remain as they are in the ciphered message.
For private-messages make sure that the private-key is set using the 'private' command. To check if the private-key is set, use the 'code' command.''',
"random": '''Generates a random paragraph from data and prints it to the console.''',
"exit/quit": '''Close the EnigmaMachine and exit the program.'''
}
while True:
print("-" * 100)
command = input("Enter your command:\n> ").strip().lower()
match command:
case "help":
print("Available commands--")
for key, value in help_text.items():
print(f"{key:10}: {value}")
case "no-logs":
machine.disable_logs()
print(">> Logs are disabled for this session.")
case "get-date":
print(f">> Current machine date: {machine.get_preset_date()}")
case "set-date":
date_s = input("Message-date: ").strip()
try:
n_date = datetime.date.fromisoformat(date_s)
except ValueError:
print(">> Invalid date format. Try 'help' for the accepted date-format")
continue
try:
machine.set_preset_date(n_date)
except FileNotFoundError:
print(">> Presets not yet defined for required month. Try using a date from the current decade.")
continue
case "private":
code = input("Private Code: ").strip()
try:
machine.set_private_code(code)
except InvalidPresetCode:
print(">> Invalid private-code. Try 'help' for the accepted code-format")
case "code":
print(f">> Active code: {machine.get_private_code()}")
case "message":
message = input("Type your message:\n").strip()
print(f">> Ciphered message:\n{machine.cipher_message(message)}")
case "random":
print(f">> {random.choice(paragraphs)}")
case "exit" | "quit":
exit(0)
case _:
print(">> Invalid command. Try 'help' to see the available commands")
if __name__ == '__main__':
main()