~cedric/pyenigma

f557d77ef49be50216a665da3290ad9d4be0b6df — C├ędric Bonhomme 10 days ago 1e682f9
new: [cli] added -v/--verbose argument in order to display the state of the machine in stderr
1 files changed, 45 insertions(+), 14 deletions(-)

M bin/enigma.py
M bin/enigma.py => bin/enigma.py +45 -14
@@ 1,5 1,5 @@
#!/usr/bin/env python
#-*- coding: utf-8 -*-
# -*- coding: utf-8 -*-

import sys



@@ 9,11 9,14 @@ from pyenigma.enigma import *
"""A trivial and minimaliste CLI.
"""


def usage():
    print("Usage:")
    print('\techo "Hello World" | enigma ABC ref rotor1 rotor2 rotor3 plugboard')
    print("\nExample:")
    print('\t$ echo "Hello World" | enigma ABC A  I II III "AV BS CG DL FU HZ IN KM OW RX"')
    print(
        '\t$ echo "Hello World" | enigma ABC A  I II III "AV BS CG DL FU HZ IN KM OW RX"'
    )
    print("\tQgqop Vyzxp")




@@ 26,24 29,52 @@ def main():
        r2 = sys.argv[4].upper()
        r3 = sys.argv[5].upper()
        plugs = sys.argv[6].upper()
    except:
        verbose = (sys.argv[7] if 7 < len(sys.argv) else '') in ['-v', '--verbose']
    except Exception as e:
        print(e)
        usage()
        exit()
    raw = sys.stdin.read(-1)

    rotors = { \
          "I":ROTOR_I,"II":ROTOR_II,"III":ROTOR_III,"IV":ROTOR_IV, \
          "V":ROTOR_V,"VI":ROTOR_VI,"VII":ROTOR_VII \
          }
    reflectors = { \
          "A":ROTOR_Reflector_A,"B":ROTOR_Reflector_B, \
          "C":ROTOR_Reflector_C \
          }
    rotors = {
        "I": ROTOR_I,
        "II": ROTOR_II,
        "III": ROTOR_III,
        "IV": ROTOR_IV,
        "V": ROTOR_V,
        "VI": ROTOR_VI,
        "VII": ROTOR_VII,
    }
    reflectors = {
        "A": ROTOR_Reflector_A,
        "B": ROTOR_Reflector_B,
        "C": ROTOR_Reflector_C,
    }

    if len(key) == 3:  # add the default ringstellung
        key += "-AAA"

    engr = Enigma(reflectors[ref], rotors[r1], rotors[r2],
                  rotors[r3], key=key[:3], plugs=plugs, ring=key[4:7])
    res = engr.encipher(raw)
    engr = Enigma(
        reflectors[ref],
        rotors[r1],
        rotors[r2],
        rotors[r3],
        key=key[:3],
        plugs=plugs,
        ring=key[4:7],
    )
    res = engr.encipher(raw).strip()
    print(res)
    if verbose:
        print(
            '{}{}{} {} {} {} {} "{}"'.format(
                engr.rotor1.state,
                engr.rotor2.state,
                engr.rotor3.state,
                ref,
                r1,
                r2,
                r3,
                plugs,
            ), file=sys.stderr
        )