Skip to content

Commit 331d350

Browse files
committed
2 parents 071bda4 + f4e78f2 commit 331d350

File tree

4 files changed

+61
-70
lines changed

4 files changed

+61
-70
lines changed

docs/classdiagram/enigmapython.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,8 @@ classDiagram
278278
}
279279
280280
class Utils {
281-
+ @staticmethod render_enigma_diagram(enigma)
282281
+ @staticmethod find_divergence(str1, str2)
283282
+ @staticmethod find_all_subclasses(cls)
284-
+ @staticmethod get_class_instance(cls)
285283
+ @staticmethod swap_chars(string, ch1, ch2)
286284
}
287285
@@ -363,6 +361,10 @@ classDiagram
363361
- __init__(self, rotor1, rotor2, rotor3, reflector, etw, auto_increment_rotors) None
364362
}
365363
364+
class XRay {
365+
+ @staticmethod render_enigma_xray(enigma)
366+
}
367+
366368
class ReflectorUKWBThin {
367369
+ str wiring
368370
+ str tag

src/enigma/enigmapython/SwappablePlugboard.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from .Plugboard import Plugboard
2-
from .Utils import Utils
32
from .Alphabets import Alphabets
43
from .Swappable import Swappable
54

src/enigma/enigmapython/Utils.py

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,6 @@
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-
61

72
class Utils:
83

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")
624

635
@staticmethod
646
def find_divergence(str1, str2):
@@ -94,15 +36,6 @@ def find_all_subclasses(cls):
9436
return set(cls.__subclasses__()).union(
9537
[s for c in cls.__subclasses__() for s in Utils.find_all_subclasses(c)])
9638

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-
10639
@staticmethod
10740
def swap_chars(string, ch1, ch2):
10841
if ch1 == ch2: return string

src/enigma/enigmapython/XRay.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from rich.console import Console
2+
from rich.text import Text
3+
4+
class XRay():
5+
@staticmethod
6+
def render_enigma_xray(enigma):
7+
"""
8+
Render the Enigma Z ASCII diagram with a variable number of rotors.
9+
UKW, ETW and Plugboard are always visible. The rotor numbers start from 0.
10+
11+
Parameters:
12+
- enigma (Enigma): The Enigma machine to render (minimum 1).
13+
"""
14+
console = Console()
15+
16+
rotor_count = len(enigma.rotors)
17+
18+
rotor_labels = " ".join([f"Rotor" for i in range(rotor_count - 1, -1, -1)])
19+
rotor_numbers = " ".join([f"{i}" for i in range(rotor_count - 1, -1, -1)])
20+
21+
rotor_walls_top = " ".join(["+-----+"] * rotor_count)
22+
23+
rotors_positions = " ".join(["{:02}".format(enigma.rotors[i].position) for i in range(rotor_count - 1, -1, -1)])
24+
25+
rotors_rings = " ".join(["{:02}".format(enigma.rotors[i].ring) for i in range(rotor_count - 1, -1, -1)])
26+
27+
rotor_walls = " ".join(["| |"] * rotor_count)
28+
29+
rotor_walls_bottom = " ".join(["+-----+"] * rotor_count)
30+
31+
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)])
32+
33+
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)])
34+
35+
rotor_wiring_top = "".join(["|-----|--<--"] * rotor_count)
36+
37+
rotor_wiring_bottom = "".join(["|-----|-->--"] * rotor_count)
38+
39+
diagram = f"""
40+
UKW {rotor_labels} ETW PLUGBOARD
41+
{rotor_numbers}
42+
+-----+ {rotor_walls_top} +-----+ +-----+
43+
| | {rotor_walls} | | | |
44+
| +--|--<--{rotor_wiring_top}|-----|--<--|-----|----< {enigma.journal[-1]['input_char'] if len(enigma.journal) >= 1 else ' '} <-- Key
45+
| | {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 ' '} | |
46+
| | | {rotor_walls} | | | |
47+
| | | {rotor_walls} | | | |
48+
| | | {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 ' '} | |
49+
| +--|-->--{rotor_wiring_bottom}|-----|-->--|-----|----> {enigma.journal[-1]['output_char'] if len(enigma.journal) >= 1 else ' '} --> Lamp
50+
| | {rotor_walls} | | | |
51+
+-----+ {rotor_walls_bottom} +-----+ +-----+
52+
53+
Pos.: {"{:02}".format(enigma.reflector.position) if isinstance(enigma.reflector, RotatingReflector) else ' '} {rotors_positions}
54+
Ring: {rotors_rings}
55+
"""
56+
console.print(Text(diagram, style="bold"))
57+
return Text(diagram, style="bold")

0 commit comments

Comments
 (0)