Mercurial > public > enigma
changeset 6:7a90beffd8f2
Created a data file and factory functions for rotors & reflectors.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 26 May 2012 15:19:48 -0500 |
parents | 6f13bc439359 |
children | 39297f695cff |
files | enigma/rotors/__init__.py enigma/rotors/data.py enigma/rotors/factory.py enigma/rotors/rotor.py enigma/tests/test_rotor.py |
diffstat | 5 files changed, 113 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- 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
--- /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', +}
--- /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)
--- 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)]
--- 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'