# HG changeset patch # User Brian Neal # Date 1392591280 21600 # Node ID 196c3c4a15aa7d66d381648822670a10c52697a1 # Parent 9c90ca37c023bd28cc7b72d3b3c4c7a40da30632 Added the --group and --width options. diff -r 9c90ca37c023 -r 196c3c4a15aa purple/main.py --- a/purple/main.py Sun Feb 16 14:31:24 2014 -0600 +++ b/purple/main.py Sun Feb 16 16:54:40 2014 -0600 @@ -1,4 +1,4 @@ -# Copyright (C) 2013 by Brian Neal. +# Copyright (C) 2013 - 2014 by Brian Neal. # This file is part of purple, the PURPLE (Cipher Machine 97) simulation. # purple is released under the MIT License (see LICENSE.txt). @@ -8,6 +8,7 @@ import os import string import sys +import textwrap from purple.machine import Purple97, Purple97Error from purple.switch import SteppingSwitchError @@ -77,24 +78,37 @@ yield c +def group_text(text, n=5): + """Groups the given text into n-letter groups separated by spaces.""" + + return ' '.join(text[i:i+n] for i in range(0, len(text), n)) + + def main(argv=None): """Entry point for the command-line purple97 simulation.""" parser = argparse.ArgumentParser(description=DESC, epilog=EPILOG) parser.add_argument('-e', '--encrypt', action='store_true', - help='encrypt text from files or the command-line') + help='perform an encrypt operation') parser.add_argument('-d', '--decrypt', action='store_true', - help='decrypt text from files or the command-line') + help='perform a decrypt operation') parser.add_argument('-f', '--filter', action='store_true', help='filter plaintext and provide useful substitutions') parser.add_argument('-s', '--switches', help='switch settings, e.g. 9-1,24,6-23') parser.add_argument('-a', '--alphabet', - help='plugboard wiring string, 26-letters') + help='plugboard wiring string, 26-letters; e.g. AEIOUYBCDFGHJKLMNPQRSTVWXZ') parser.add_argument('-t', '--text', help='input text to encrypt/decrypt') parser.add_argument('-i', '--input', metavar='FILE', help='file to read input text from, - for stdin') + parser.add_argument('-g', '--group', type=int, default=5, metavar='N', + help=('if non-zero, group output in %(metavar)s-letter groups ' + '[default: %(default)s]')) + parser.add_argument('-w', '--width', type=int, default=70, metavar='N', + help=("wrap output text to %(metavar)s letters; " + "a value of 0 means do not wrap " + "[default: %(default)s]")) args = parser.parse_args(args=argv) @@ -119,6 +133,11 @@ else: alphabet = os.environ.get('PURPLE97_ALPHABET', DEFAULT_ALPHABET) + if args.group < 0: + parser.error("The --group option must be 0 or greater") + if args.width < 0: + parser.error("The --width option must be 0 or greater") + # Create purple cipher machine object try: purple = Purple97.from_key_sheet(switches, alphabet) @@ -148,6 +167,16 @@ except (Purple97Error, SteppingSwitchError) as ex: parser.error(str(ex)) + if args.group != 0: + result = group_text(result, args.group) + + if args.width != 0: + # Never break groups when grouping + break_long_words = args.group == 0 + result = textwrap.fill(result, width=args.width, expand_tabs=False, + replace_whitespace=False, drop_whitespace=False, + break_long_words=break_long_words) + print(result) diff -r 9c90ca37c023 -r 196c3c4a15aa purple/tests/test_machine.py --- a/purple/tests/test_machine.py Sun Feb 16 14:31:24 2014 -0600 +++ b/purple/tests/test_machine.py Sun Feb 16 16:54:40 2014 -0600 @@ -1,4 +1,4 @@ -# Copyright (C) 2013 by Brian Neal. +# Copyright (C) 2013 - 2014 by Brian Neal. # This file is part of purple, the PURPLE (Cipher Machine 97) simulation. # purple is released under the MIT License (see LICENSE.txt).