-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotors.py
More file actions
267 lines (210 loc) · 8.78 KB
/
rotors.py
File metadata and controls
267 lines (210 loc) · 8.78 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
from abc import ABC, abstractmethod
from constants import CurrentFlowDirections, RotorDirections
from helpers import *
from rotor_mappings import *
"""
:param label: The label indicating the specs of this rotor
:type label: string
"""
class Rotor(ABC):
def __init__(self, **kwargs):
self.label = ''
# Characters as they map to the 26 letter English alphabet
self.characters = []
# Character on which the notch is hit
self.notch_character = None
self.__set_state(**kwargs)
# Forces an overide of the initial character set for the rotor
def override_characters(self, new_characters):
self.characters = new_characters
# Resets the state|configuration for the rotor
def reset_state(self, **kwargs):
self.__set_state(**kwargs)
# Sets the state|configuration for the rotor
def __set_state(self, *, initial_position = 1, ring_setting = 1):
self.set_slot_position()
self.set_initial_position(initial_position)
self.set_ring_setting(ring_setting)
# Setter for the rotor's position in the machine's slots
def set_slot_position(self, slot_position = 1):
# 1 is the rightmost, 2, 3 , 4 are the left (increasing); Defaults to 1
self.position_in_slots = slot_position
# Setter for the rotor's initial position (and current position)
def set_initial_position(self, initial_position):
# Initial character position (can be character A and Z or number between 1 and 26)
self.initial_position = get_position_for_character(initial_position) if isinstance(initial_position, str) else initial_position
# Current character position
self.current_position = self.initial_position
# Setter for the rotor's ring settings
def set_ring_setting(self, ring_setting):
# Ring Setting number
self.ring_setting = ring_setting
@staticmethod
# Gets the index for a character in the traditional English alphabet
def get_alphabet_index(character):
return ord(character.upper()) - ord('A')
# Gets the index for a character in the rotors alphabet mapping
def get_mapping_index(self, character):
return self.characters.index(character)
@staticmethod
# Positions may grow out of the 1 to 26 boundary; this normalizes them
def normalize_character_position(position):
if position <= 0:
alphabet_diff = 0 - position
position = 26 - alphabet_diff
elif position > 26:
alphabet_diff = position - 26
position = 0 + alphabet_diff
return position
@staticmethod
# Indices may grow out of the 0 to 25 boundary; this normalizes them
def normalize_character_index(index):
if index < 0:
alphabet_diff = 0 - index
index = 26 - alphabet_diff
elif index > 25:
alphabet_diff = index - 26
index = 0 + alphabet_diff
return index
@property
# Character representation for the current position
def current_character(self):
return get_character_for_position(self.current_position)
@property
# Identifies if the rotor is at it's notch point
def is_at_notch(self):
return self.notch_character != None and self.current_character == self.notch_character
@property
# Converting the ring setting to a zero based index
def ring_setting_offset(self):
return self.ring_setting - 1
@property
# Distance between the current position and initial position
def initial_position_offset(self):
return self.current_position - self.initial_position
# Rotor rotation - essentially changing the current position
def rotate(self, direction = RotorDirections.CLOCKWISE):
new_position = self.normalize_character_position(self.current_position + direction.value)
self.current_position = new_position
# Converts a character across a rotor in either direction
def handle_key_encoding(self, character, direction = CurrentFlowDirections.FORWARD):
rotor_offset = self.current_position - get_position_for_character('A')
if (direction == CurrentFlowDirections.FORWARD):
matching_rotor_pin_position = get_position_for_character(character) + rotor_offset - self.ring_setting_offset
matching_rotor_pin = self.normalize_character_position(matching_rotor_pin_position)
matching_rotor_character = get_character_for_position(matching_rotor_pin)
encoded_character = self.encode_right_to_left(matching_rotor_character)
matching_rotor_contact_position = get_position_for_character(encoded_character) - rotor_offset + self.ring_setting_offset
matching_rotor_contact = self.normalize_character_position(matching_rotor_contact_position)
encoded_character = get_character_for_position(matching_rotor_contact)
return encoded_character
else:
matching_rotor_contact_position = get_position_for_character(character) + rotor_offset - self.ring_setting_offset
matching_rotor_contact = self.normalize_character_position(matching_rotor_contact_position)
matching_rotor_character = get_character_for_position(matching_rotor_contact)
encoded_character = self.encode_left_to_right(matching_rotor_character)
matching_rotor_pin_position = get_position_for_character(encoded_character) - rotor_offset + self.ring_setting_offset
matching_rotor_pin = self.normalize_character_position(matching_rotor_pin_position)
encoded_character = get_character_for_position(matching_rotor_pin)
return encoded_character
# Forward direction encoding
def encode_right_to_left(self, character):
character_index = self.get_alphabet_index(character)
return self.characters[character_index]
# Reverse direction encoding
def encode_left_to_right(self, character):
character_index = self.get_mapping_index(character)
return chr(character_index + ord('A'))
class BetaRotor(Rotor):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.label = 'Beta'
self.characters = BetaRotorMapping
class GammaRotor(Rotor):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.label = 'Gamma'
self.characters = GammaRotorMapping
class IRotor(Rotor):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.label = 'I'
self.characters = IRotorMapping
self.notch_character = 'Q'
class IIRotor(Rotor):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.label = 'II'
self.characters = IIRotorMapping
self.notch_character = 'E'
class IIIRotor(Rotor):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.label = 'III'
self.characters = IIIRotorMapping
self.notch_character = 'V'
class IVRotor(Rotor):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.label = 'IV'
self.characters = IVRotorMapping
self.notch_character = 'J'
class VRotor(Rotor):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.label = 'V'
self.characters = VRotorMapping
self.notch_character = 'Z'
class ARotor(Rotor):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.label = 'A'
self.characters = ARotorMapping
class BRotor(Rotor):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.label = 'B'
self.characters = BRotorMapping
class CRotor(Rotor):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.label = 'C'
self.characters = CRotorMapping
def get_all_rotors():
return {
'Beta': BetaRotor,
'Gamma': GammaRotor,
'I': IRotor,
'II': IIRotor,
'III': IIIRotor,
'IV': IVRotor,
'V': VRotor,
'A': ARotor,
'B': BRotor,
'C': CRotor
}
def rotor_from_name(name):
rotors_dict = get_all_rotors()
if name in rotors_dict:
cls = rotors_dict[name]
else:
raise ValueError('There is no Rotor for the specified name')
return cls()
def rotor_cls_from_name(name):
rotors_dict = get_all_rotors()
if name in rotors_dict:
cls = rotors_dict[name]
else:
raise ValueError('There is no Rotor for the specified name')
return cls
def assert_rotors():
rotors_dict = get_all_rotors()
for (_, rotor_cls) in rotors_dict.items():
rotor = rotor_cls()
assert(len(set(rotor.characters)) == 26)
rotor = rotor_from_name("I")
assert(rotor.encode_right_to_left("A") == "E")
assert(rotor.encode_left_to_right("A") == "U")
if __name__ == "__main__":
assert_rotors()
pass