Make word file an optional kwarg for generate_wordlist()
authorSteven Tobin <tobin2402 (at) gmail.com>
Mon, 26 Sep 2011 17:46:31 +0000 (18:46 +0100)
committerSteven Tobin <tobin2402 (at) gmail.com>
Mon, 26 Sep 2011 17:48:19 +0000 (18:48 +0100)
Otherwise uses sys.platform to use system dictionary (Linux, OS X)

xkcd-password.py

index 870148fd13298867884717d4dd92d97218d5d6b4..6f033310b4c979f3814201a6b56a87239d2b55c1 100755 (executable)
@@ -30,19 +30,26 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 import random
 import os
+import sys
 
-def generate_wordlist(min_length=5, max_length=9):
-    ## OS X
-    # word_list = "/usr/share/dict/words"
-    ## Linux
-    # word_list = "/usr/dict/words"
-    ## Downloaded || custom
-    word_list = os.path.expanduser("~/local/share/dict/common")
+def generate_wordlist(word_file="", min_length=5, max_length=9):
+    """
+    generate a word list from either a kwarg word_file, or a system default
+    """
+    if not word_file:
+        if "darwin" in sys.platform:
+            ## OS X
+            word_file = "/usr/share/dict/words"
+        elif "linux" in sys.platform:
+            ## Linux
+            word_file = "/usr/dict/words"
+
+    word_file = os.path.expanduser(word_file) # just to be sure
 
     words = []
 
     try:
-        with open(word_list) as wlf:
+        with open(word_file) as wlf:
             for line in wlf:
                 if min_length <= len(line.strip()) <= max_length:
                     words.append(line.strip())
@@ -57,6 +64,7 @@ if __name__ == '__main__':
     else: n_words = int(n_words)
     
     accepted = "n"
+    custom_wordfile = "~/local/share/dict/common"
     wordlist = generate_wordlist()
 
     while accepted.lower() not in [ "y", "yes" ]:
git clone https://git.99rst.org/PROJECT