Refactor pass generation into a closure
authorFred Cox <redacted>
Thu, 30 Nov 2017 09:18:45 +0000 (11:18 +0200)
committerFred Cox <redacted>
Wed, 6 Dec 2017 09:53:20 +0000 (11:53 +0200)
xkcdpass/xkcd_password.py

index 54024b2b8befab331f3fc589563ecd287ebb9f5b..0b74798a245d4d8b931dc7d167ee422ea6a4fb4c 100755 (executable)
@@ -235,14 +235,17 @@ def generate_xkcdpassword(wordlist,
     if acrostic:
         worddict = wordlist_to_worddict(wordlist)
 
-    # useful if driving the logic from other code
-    if not interactive:
+    def gen_passwd():
         if not acrostic:
-            passwd = delimiter.join(choose_words(wordlist, numwords))
+            words = choose_words(wordlist, numwords)
         else:
-            passwd = delimiter.join(find_acrostic(acrostic, worddict))
+            words = find_acrostic(acrostic, worddict)
+        
+        return delimiter.join(words)
 
-        return passwd
+    # useful if driving the logic from other code
+    if not interactive:
+        return gen_passwd()
 
     # else, interactive session
     # define input validators
@@ -277,10 +280,7 @@ def generate_xkcdpassword(wordlist,
     accepted = False
 
     while not accepted:
-        if not acrostic:
-            passwd = delimiter.join(choose_words(wordlist, numwords))
-        else:
-            passwd = delimiter.join(find_acrostic(acrostic, worddict))
+        passwd = gen_passwd()
         print("Generated: " + passwd)
         accepted = try_input("Accept? [yN] ", accepted_validator)
 
git clone https://git.99rst.org/PROJECT