changeset 8:b5e6552394a2

Added a M209.set_all_pins() method.
author Brian Neal <bgneal@gmail.com>
date Sun, 02 Jun 2013 19:44:21 -0500
parents 65f72a842650
children a29fa17cefc4
files m209/converter.py m209/tests/test_converter.py
diffstat 2 files changed, 53 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/m209/converter.py	Sun Jun 02 19:21:11 2013 -0500
+++ b/m209/converter.py	Sun Jun 02 19:44:21 2013 -0500
@@ -47,6 +47,25 @@
             raise M209Error("set_pins(): invalid key wheel index {}".format(n))
         self.key_wheels[n].set_pins(effective_pins)
 
+    def set_all_pins(self, pin_list):
+        """Sets all key wheel pins according to the supplied pin list.
+
+        The pin_list parameter must either be None or a 6-element list
+        where each element is as described by the effective_pins parameter of
+        the set_key_wheel_pins() method. If None, all pins in all key wheels are
+        moved to the ineffective position.
+
+        """
+        if pin_list is None:
+            for kw in self.key_wheels:
+                kw.set_pins(None)
+        else:
+            if len(pin_list) != len(self.key_wheels):
+                raise M209Error("set_all_pins(): invalid pin_list length")
+
+            for kw, pins in zip(self.key_wheels, pin_list):
+                kw.set_pins(pins)
+
     def set_drum_lugs(self, lug_list):
         """Sets the drum lugs according to the given lug_list parameter.
 
--- a/m209/tests/test_converter.py	Sun Jun 02 19:21:11 2013 -0500
+++ b/m209/tests/test_converter.py	Sun Jun 02 19:44:21 2013 -0500
@@ -37,6 +37,15 @@
         self.assertRaises(M209Error, m.set_pins, 7, pins)
         self.assertRaises(M209Error, m.set_pins, 100, pins)
 
+    def test_invald_set_all_pins(self):
+        m = M209()
+        self.assertRaises(M209Error, m.set_all_pins, 'A')
+
+        bad_pins1 = AA_PIN_LIST * 2
+        self.assertRaises(M209Error, m.set_all_pins, bad_pins1)
+        bad_pins2 = ['ABCD', 'EFGH', 'XYZ']
+        self.assertRaises(M209Error, m.set_all_pins, bad_pins2)
+
     def letter_check(self, lugs, pin_list, check):
         """Generic letter check routine"""
 
@@ -46,8 +55,7 @@
         m = M209()
         m.set_drum_lugs(lugs)
 
-        for n, pins in enumerate(pin_list):
-            m.set_pins(n, pins)
+        m.set_all_pins(pin_list)
 
         result = m.encrypt(pt)
 
@@ -87,8 +95,7 @@
 
         m = M209()
         m.set_drum_lugs(AA_LUGS)
-        for n, pins in enumerate(AA_PIN_LIST):
-            m.set_pins(n, pins)
+        m.set_all_pins(AA_PIN_LIST)
 
         result = m.encrypt('A' * 26, group=False)
         expected = AA_CHECK.replace(' ', '')
@@ -103,8 +110,7 @@
 
         m = M209()
         m.set_drum_lugs(AA_LUGS)
-        for n, pins in enumerate(AA_PIN_LIST):
-            m.set_pins(n, pins)
+        m.set_all_pins(AA_PIN_LIST)
 
         wheels = 'YGXREL'
         m.set_key_wheels(wheels)
@@ -127,8 +133,7 @@
 
         m = M209()
         m.set_drum_lugs(AA_LUGS)
-        for n, pins in enumerate(AA_PIN_LIST):
-            m.set_pins(n, pins)
+        m.set_all_pins(AA_PIN_LIST)
 
         pt = 'ATTACK AT DAWN'
         wheels = 'YGXREL'
@@ -139,3 +144,24 @@
         result = m.decrypt(ct, z_sub=False)
 
         self.assertEqual(pt.replace(' ', 'Z'), result)
+
+    def test_set_pins_vs_all_pins(self):
+
+        m1 = M209()
+        m1.set_drum_lugs(AA_LUGS)
+        m1.set_all_pins(AA_PIN_LIST)
+
+        pt = 'ATTACK AT DAWN'
+        wheels = 'YGXREL'
+        m1.set_key_wheels(wheels)
+        ct1 = m1.encrypt(pt)
+
+        m2 = M209()
+        m2.set_drum_lugs(AA_LUGS)
+        for n, pins in enumerate(AA_PIN_LIST):
+            m2.set_pins(n, pins)
+
+        m2.set_key_wheels(wheels)
+        ct2 = m2.encrypt(pt)
+
+        self.assertEqual(ct1, ct2)