# HG changeset patch # User Brian Neal # Date 1338063588 18000 # Node ID 7a90beffd8f2a20bed354374b1e05f29bf9bf234 # Parent 6f13bc4393593ba6cc5e89be5f38a69410abbdd8 Created a data file and factory functions for rotors & reflectors. diff -r 6f13bc439359 -r 7a90beffd8f2 enigma/rotors/__init__.py --- a/enigma/rotors/__init__.py Sat May 26 14:29:03 2012 -0500 +++ b/enigma/rotors/__init__.py Sat May 26 15:19:48 2012 -0500 @@ -0,0 +1,8 @@ +# Copyright (C) 2012 by Brian Neal. +# This file is part of Py-Enigma, the Enigma Machine simulation. +# Py-Enigma is released under the MIT License (see License.txt). + +"""The rotors package simulates the Enigma rotors & reflectors.""" + +class RotorError(Exception): + pass diff -r 6f13bc439359 -r 7a90beffd8f2 enigma/rotors/data.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/enigma/rotors/data.py Sat May 26 15:19:48 2012 -0500 @@ -0,0 +1,71 @@ +# Copyright (C) 2012 by Brian Neal. +# This file is part of Py-Enigma, the Enigma Machine simulation. +# Py-Enigma is released under the MIT License (see License.txt). + +"""data.py - This file contains rotor & reflector data for all the types +we simulate. + +""" + +# This data is taken from Dirk Rijmenants very informative and useful Enigma +# website: "Techical Details of the Engigma Machine" +# http://users.telenet.be/d.rijmenants/en/enigmatech.htm +# +# Rotors I-V were used by the Wehrmacht, Luftwaffe, and Kriegsmarine. The +# Kriegsmarine added rotors VI-VIII to the M3 model, and added Beta & Gamma to +# the M4 model (used with thin reflectors only). Note that Beta & Gamma rotors +# did not rotate. +# +# The Wehrmacht, Luftwaffe, & Kriegsmarine M3 machines used reflectors B & C, +# while the Kriegsmarine M4 used thin reflectors B & C. +# + +ROTORS = { + 'I': { + 'wiring': 'EKMFLGDQVZNTOWYHXUSPAIBRCJ', + 'stepping': 'Q', + }, + 'II': { + 'wiring': 'AJDKSIRUXBLHWTMCQGZNPYFVOE', + 'stepping': 'E', + }, + 'III': { + 'wiring': 'BDFHJLCPRTXVZNYEIWGAKMUSQO', + 'stepping': 'V', + }, + 'IV': { + 'wiring': 'ESOVPZJAYQUIRHXLNFTGKDCMWB', + 'stepping': 'J', + }, + 'V': { + 'wiring': 'VZBRGITYUPSDNHLXAWMJQOFECK', + 'stepping': 'Z', + }, + 'VI': { + 'wiring': 'JPGVOUMFYQBENHZRDKASXLICTW', + 'stepping': 'ZM', + }, + 'VII': { + 'wiring': 'NZJHGRCXMYSWBOUFAIVLPEKQDT', + 'stepping': 'ZM', + }, + 'VIII': { + 'wiring': 'FKQHTLXOCBJSPDZRAMEWNIUYGV', + 'stepping': 'ZM', + }, + 'Beta': { + 'wiring': 'LEYJVCNIXWPBQMDRTAKZGFUHOS', + 'stepping': None, + }, + 'Gamma': { + 'wiring': 'FSOKANUERHMBTIYCWLQPZXVGJD', + 'stepping': None, + }, +} + +REFLECTORS = { + 'B': 'YRUHQSLDPXNGOKMIEBFZCWVJAT', + 'C': 'FVPJIAOYEDRZXWGCTKUQSBNMHL', + 'B-Thin': 'ENKQAUYWJICOPBLMDXZVFTHRGS', + 'C-Thin': 'RDOBJNTKVEHMLFCWZAXGYIPSUQ', +} diff -r 6f13bc439359 -r 7a90beffd8f2 enigma/rotors/factory.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/enigma/rotors/factory.py Sat May 26 15:19:48 2012 -0500 @@ -0,0 +1,31 @@ +# Copyright (C) 2012 by Brian Neal. +# This file is part of Py-Enigma, the Enigma Machine simulation. +# Py-Enigma is released under the MIT License (see License.txt). + +"""Contains factory functions for creating rotors and reflectors.""" + +from enigma.rotors import RotorError +from enigma.rotors.rotor import Rotor +from enigma.rotors.data import ROTORS, REFLECTORS + + +def create_rotor(model, ring_setting, alpha_labels=True): + """Factory function to create and return a rotor of the given model name.""" + + if model in ROTORS: + data = ROTORS[model] + return Rotor(model, data['wiring'], ring_setting, data['stepping'], + alpha_labels) + + raise RotorError("Unknown rotor type: %s" % model) + + +def create_reflector(model): + """Factory function to create and return a reflector of the given model + name. + + """ + if model in REFLECTORS: + return Rotor(model, wiring=REFLECTORS[model]) + + raise RotorError("Unknown reflector type: %s" % model) diff -r 6f13bc439359 -r 7a90beffd8f2 enigma/rotors/rotor.py --- a/enigma/rotors/rotor.py Sat May 26 14:29:03 2012 -0500 +++ b/enigma/rotors/rotor.py Sat May 26 15:19:48 2012 -0500 @@ -7,9 +7,8 @@ import string import collections +from . import RotorError -class RotorError(Exception): - pass ALPHA_LABELS = string.ascii_uppercase NUMERIC_LABELS = ['{:02d}'.format(n) for n in range(1, 27)] diff -r 6f13bc439359 -r 7a90beffd8f2 enigma/tests/test_rotor.py --- a/enigma/tests/test_rotor.py Sat May 26 14:29:03 2012 -0500 +++ b/enigma/tests/test_rotor.py Sat May 26 15:19:48 2012 -0500 @@ -8,7 +8,8 @@ import collections import string -from rotors.rotor import Rotor, RotorError, ALPHA_LABELS, NUMERIC_LABELS +from ..rotors.rotor import Rotor, ALPHA_LABELS, NUMERIC_LABELS +from ..rotors import RotorError WIRING = 'EKMFLGDQVZNTOWYHXUSPAIBRCJ'