changeset 13:3fbdc7005075

Integrate Plugboard with EnigmaMachine class and tests.
author Brian Neal <bgneal@gmail.com>
date Sun, 27 May 2012 13:57:13 -0500
parents 42858648f8b5
children 0fe1c4a11bad
files enigma/machine.py enigma/tests/test_enigma.py
diffstat 2 files changed, 10 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/enigma/machine.py	Sun May 27 13:40:58 2012 -0500
+++ b/enigma/machine.py	Sun May 27 13:57:13 2012 -0500
@@ -19,7 +19,7 @@
 class EnigmaMachine:
     """Top-level class for the Enigma Machine simulation."""
 
-    def __init__(self, rotors, reflector):
+    def __init__(self, rotors, reflector, plugboard):
         """Configures the Enigma Machine. Parameters are as follows:
 
         rotors - a list containing 3 or 4 (for the Kriegsmarine M4 version)
@@ -29,6 +29,8 @@
 
         reflector - a rotor object to represent the reflector
 
+        plugboard - a plugboard object to represent the state of the plugboard
+
         Note that on the military Enigma machines we are simulating, the entry
         wheel is a simple straight-pass through and is not simulated here. It
         would not be too hard to add a parameter for the entry wheel and pass a
@@ -42,6 +44,7 @@
         self.rotors = rotors
         self.rotor_count = len(rotors)
         self.reflector = reflector
+        self.plugboard = plugboard
 
     def set_display(self, val):
         """Sets the rotor operator windows to 'val'.
@@ -82,7 +85,6 @@
         self._step_rotors()
 
         # simulate the electrical operations:
-        # TODO: plugboard
         signal_num = ord(key) - ord('A')
         lamp_num = self._electric_signal(signal_num)
         return KEYBOARD_CHARS[lamp_num]
@@ -128,9 +130,8 @@
         Returns a lamp number to light (an integer 0-25).
 
         """
-        # TODO Plugboard
+        pos = self.plugboard.signal(signal_num)
 
-        pos = signal_num
         for rotor in reversed(self.rotors):
             pos = rotor.signal_in(pos)
 
@@ -139,7 +140,7 @@
         for rotor in self.rotors:
             pos = rotor.signal_out(pos)
 
-        return pos
+        return self.plugboard.signal(pos)
 
     def process_text(self, text):
         """Run the text through the machine, simulating a key press for each
--- a/enigma/tests/test_enigma.py	Sun May 27 13:40:58 2012 -0500
+++ b/enigma/tests/test_enigma.py	Sun May 27 13:57:13 2012 -0500
@@ -8,6 +8,7 @@
 
 from ..rotors.factory import create_rotor, create_reflector
 from ..machine import EnigmaMachine
+from ..plugboard import Plugboard
 
 
 class SteppingTestCase(unittest.TestCase):
@@ -25,7 +26,7 @@
         rotors.append(create_rotor("II"))
         rotors.append(create_rotor("I"))
 
-        m = EnigmaMachine(rotors, create_reflector('B'))
+        m = EnigmaMachine(rotors, create_reflector('B'), Plugboard())
 
         m.set_display('KDO')
 
@@ -49,7 +50,8 @@
 
         reflector = create_reflector('B')
 
-        self.machine = EnigmaMachine(rotors=rotors, reflector=reflector)
+        self.machine = EnigmaMachine(rotors=rotors, reflector=reflector,
+                                     plugboard=Plugboard())
         self.machine.set_display('AAA')
 
     def test_simple_encrypt(self):