|
1 | | -from importlib import import_module |
2 | | -from rich.console import Console |
3 | | -from rich.text import Text |
4 | | -from enigmapython.RotatingReflector import RotatingReflector |
5 | | - |
6 | 1 |
|
7 | 2 | class Utils: |
8 | 3 |
|
9 | | - @staticmethod |
10 | | - def render_enigma_diagram(enigma): |
11 | | - """ |
12 | | - Render the Enigma Z ASCII diagram with a variable number of rotors. |
13 | | - UKW, ETW and Plugboard are always visible. The rotor numbers start from 0. |
14 | | -
|
15 | | - Parameters: |
16 | | - - enigma (Enigma): The Enigma machine to render (minimum 1). |
17 | | - """ |
18 | | - console = Console() |
19 | | - |
20 | | - rotor_count = len(enigma.rotors) |
21 | | - |
22 | | - rotor_labels = " ".join([f"Rotor" for i in range(rotor_count - 1, -1, -1)]) |
23 | | - rotor_numbers = " ".join([f"{i}" for i in range(rotor_count - 1, -1, -1)]) |
24 | | - |
25 | | - rotor_walls_top = " ".join(["+-----+"] * rotor_count) |
26 | | - |
27 | | - rotors_positions = " ".join(["{:02}".format(enigma.rotors[i].position) for i in range(rotor_count - 1, -1, -1)]) |
28 | | - |
29 | | - rotors_rings = " ".join(["{:02}".format(enigma.rotors[i].ring) for i in range(rotor_count - 1, -1, -1)]) |
30 | | - |
31 | | - rotor_walls = " ".join(["| |"] * rotor_count) |
32 | | - |
33 | | - rotor_walls_bottom = " ".join(["+-----+"] * rotor_count) |
34 | | - |
35 | | - rotor_walls_forward = " ".join(["| {} |".format(enigma.rotors[i].journal[-2]['output_char'] if len(enigma.rotors[i].journal) >= 2 else ' ') for i in range(rotor_count - 1, -1, -1)]) |
36 | | - |
37 | | - rotor_walls_backward = " ".join(["| {} |".format(enigma.rotors[i].journal[-1]['output_char'] if len(enigma.rotors[i].journal) >= 2 else ' ') for i in range(rotor_count - 1, -1, -1)]) |
38 | | - |
39 | | - rotor_wiring_top = "".join(["|-----|--<--"] * rotor_count) |
40 | | - |
41 | | - rotor_wiring_bottom = "".join(["|-----|-->--"] * rotor_count) |
42 | | - |
43 | | - diagram = f""" |
44 | | - UKW {rotor_labels} ETW PLUGBOARD |
45 | | - {rotor_numbers} |
46 | | - +-----+ {rotor_walls_top} +-----+ +-----+ |
47 | | - | | {rotor_walls} | | | | |
48 | | - | +--|--<--{rotor_wiring_top}|-----|--<--|-----|----< {enigma.journal[-1]['input_char'] if len(enigma.journal) >= 1 else ' '} <-- Key |
49 | | - | | {rotor_walls_forward} | {enigma.etw.journal[-2]['output_char'] if len(enigma.etw.journal) >= 2 else ' '} | | {enigma.plugboard.journal[-2]['output_char'] if len(enigma.plugboard.journal) >= 2 else ' '} | | |
50 | | - | | | {rotor_walls} | | | | |
51 | | - | | | {rotor_walls} | | | | |
52 | | - | | | {enigma.reflector.journal[-1]['output_char'] if len(enigma.reflector.journal) >= 1 else ' '} | {rotor_walls_backward} | {enigma.etw.journal[-1]['output_char'] if len(enigma.etw.journal) >= 2 else ' '} | | |
53 | | - | +--|-->--{rotor_wiring_bottom}|-----|-->--|-----|----> {enigma.journal[-1]['output_char'] if len(enigma.journal) >= 1 else ' '} --> Lamp |
54 | | - | | {rotor_walls} | | | | |
55 | | - +-----+ {rotor_walls_bottom} +-----+ +-----+ |
56 | | -
|
57 | | - Pos.: {"{:02}".format(enigma.reflector.position) if isinstance(enigma.reflector, RotatingReflector) else ' '} {rotors_positions} |
58 | | - Ring: {rotors_rings} |
59 | | - """ |
60 | | - console.print(Text(diagram, style="bold")) |
61 | | - return Text(diagram, style="bold") |
62 | 4 |
|
63 | 5 | @staticmethod |
64 | 6 | def find_divergence(str1, str2): |
@@ -94,15 +36,6 @@ def find_all_subclasses(cls): |
94 | 36 | return set(cls.__subclasses__()).union( |
95 | 37 | [s for c in cls.__subclasses__() for s in Utils.find_all_subclasses(c)]) |
96 | 38 |
|
97 | | - @staticmethod |
98 | | - def get_class_instance(cls): |
99 | | - try: |
100 | | - module_path, class_name = cls.rsplit('.', 1) |
101 | | - module = import_module(module_path) |
102 | | - return getattr(module, class_name) |
103 | | - except (ImportError, AttributeError) as e: |
104 | | - raise ImportError(cls) |
105 | | - |
106 | 39 | @staticmethod |
107 | 40 | def swap_chars(string, ch1, ch2): |
108 | 41 | if ch1 == ch2: return string |
|
0 commit comments