From 310cc7c24669c440070184431a8725205416d721 Mon Sep 17 00:00:00 2001
From: ulif <uli@gnufix.de>
Date: Thu, 29 Jan 2015 11:57:27 +0100
Subject: [PATCH] Make special chars configurable.

---
 diceware/diceware.py   | 16 ++++++++++++----
 tests/test_diceware.py | 21 ++++++++++++---------
 2 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/diceware/diceware.py b/diceware/diceware.py
index 4978d92..3598584 100644
--- a/diceware/diceware.py
+++ b/diceware/diceware.py
@@ -1,5 +1,6 @@
 import argparse
 import os
+import pkg_resources
 import re
 import sys
 from random import SystemRandom
@@ -30,6 +31,9 @@ def handle_options(args):
     cap_group.add_argument(
         '--no-caps', action='store_false', dest='capitalize',
         help='Turn off capitalization.')
+    parser.add_argument(
+        '-s', '--specials', default=1, type=int, metavar='NUM',
+        help="Insert NUM special chars into generated word.")
     parser.set_defaults(capitalize=True)
     args = parser.parse_args(args)
     return args
@@ -76,7 +80,7 @@ def insert_special_char(word, specials=SPECIAL_CHARS, rnd=None):
     return ''.join(char_list)
 
 
-def get_passphrase(wordnum=6, specials=True, separator='', lang='en',
+def get_passphrase(wordnum=6, specialsnum=1, separator='', lang='en',
                    capitalized=True):
     """Get a diceware passphrase.
     """
@@ -86,7 +90,7 @@ def get_passphrase(wordnum=6, specials=True, separator='', lang='en',
     if capitalized:
         words = [x.capitalize() for x in words]
     result = separator.join(words)
-    if specials:
+    for x in range(specialsnum):
         result = insert_special_char(result, rnd=rnd)
     return result
 
@@ -95,5 +99,9 @@ def main(args=1):
     if args is 1:
         args = sys.argv[1:]
     options = handle_options(args)
-    print(get_passphrase(wordnum=options.num,
-                         capitalized=options.capitalize))
+    print(get_passphrase(
+        wordnum=options.num,
+        specialsnum=options.specials,
+        capitalized=options.capitalize
+        )
+    )
diff --git a/tests/test_diceware.py b/tests/test_diceware.py
index d44dd93..15d9232 100644
--- a/tests/test_diceware.py
+++ b/tests/test_diceware.py
@@ -137,6 +137,7 @@ class TestDicewareModule(object):
         options = handle_options([])
         assert options.num == 6
         assert options.capitalize is True
+        assert options.specials == 1
 
     def test_main(self, capsys):
         # we can get a passphrase
@@ -156,15 +157,17 @@ class TestDicewareModule(object):
         out = out.replace(
             os.path.basename(sys.argv[0]), 'diceware')
         assert out == (
-            'usage: diceware [-h] [-n NUM] [-c | --no-caps]\n'
-            '\n'
-            'Create a passphrase\n'
-            '\n'
-            'optional arguments:\n'
-            '  -h, --help         show this help message and exit\n'
-            '  -n NUM, --num NUM  number of words to concatenate. Default: 6\n'
-            '  -c, --caps         Capitalize words. This is the default.\n'
-            '  --no-caps          Turn off capitalization.\n'
+     'usage: diceware [-h] [-n NUM] [-c | --no-caps] [-s NUM]\n'
+     '\n'
+     'Create a passphrase\n'
+     '\n'
+     'optional arguments:\n'
+     '  -h, --help            show this help message and exit\n'
+     '  -n NUM, --num NUM     number of words to concatenate. Default: 6\n'
+     '  -c, --caps            Capitalize words. This is the default.\n'
+     '  --no-caps             Turn off capitalization.\n'
+     '  -s NUM, --specials NUM\n'
+     '                        Insert NUM special chars into generated word.\n'
             )
 
     def test_main_argv(self, argv_handler):
-- 
GitLab