option validation tests collected in new function
authorSteven Tobin <tobin2402 (at) gmail.com>
Fri, 22 Jun 2012 09:45:26 +0000 (10:45 +0100)
committerSteven Tobin <tobin2402 (at) gmail.com>
Fri, 22 Jun 2012 09:45:26 +0000 (10:45 +0100)
xkcd-password.py

index 21f2bc014324525d35572b0c549440ae27ccb04f..72c953a92b7456108c9dd378006bbe17d2e233fa 100755 (executable)
@@ -54,6 +54,23 @@ if sys.version[0] == "3":
     raw_input = input
 
 
+def validate_options(options):
+    """
+    Given a set of command line options, performs various validation checks
+    """
+
+    if options.max_length < options.min_length:
+        sys.stderr.write("The maximum length of a word can not be "
+                         "lesser then minimum length.\n"
+                         "Check the specified settings.\n")
+        sys.exit(1)  
+
+    if options.wordfile is not None:
+        if not os.path.exists(os.path.abspath(options.wordfile)):
+            sys.stderr.write("Could not open the specified word file.\n")
+            sys.exit(1)     
+
+
 def generate_wordlist(wordfile=None,
         min_length=5,
         max_length=9,
@@ -66,6 +83,7 @@ def generate_wordlist(wordfile=None,
     common_word_files = ["/usr/share/dict/words",
             "/usr/dict/words"]
 
+    # if the user provides no wordfile, check some default locations
     if wordfile is None:
         for wfile in common_word_files:
             if os.path.exists(wfile):
@@ -78,24 +96,13 @@ def generate_wordlist(wordfile=None,
         " not exist.\n")
         sys.exit(1)
 
-    wordfile = os.path.expanduser(wordfile)  # just to be sure
-
     words = []
 
-    # Maybe these kinds of checks should get their own function.
-    if max_length < min_length:
-        sys.stderr.write("The maximum length of a word can not be "
-                         "lesser then minimum length.\n"
-                         "Check the specified settings.\n")
-        sys.exit(1)
-
     regexp = re.compile("^%s{%i,%i}$" % (valid_chars, min_length, max_length))
 
-    if os.path.exists(os.path.abspath(wordfile)):
-        wlf = open(wordfile)
-    else:
-        sys.stderr.write("Could not open the specified word file.\n")
-        sys.exit(1)
+    # At this point wordfile is set
+    wordfile = os.path.expanduser(wordfile)  # just to be sure
+    wlf = open(wordfile)
 
     for line in wlf:
         thisword = line.strip()
@@ -185,6 +192,8 @@ if __name__ == '__main__':
 
     (options, args) = parser.parse_args()
 
+    validate_options(options)
+
     if len(args) > 1:
         parser.error("Too many arguments.")
     if len(args) == 1:
@@ -200,6 +209,7 @@ if __name__ == '__main__':
                                     max_length=options.max_length,
                                     valid_chars=options.valid_chars)
 
+
     if options.entropy:
         report_entropy(length=len(my_wordlist), numwords=options.numwords)
     print(generate_xkcdpassword(my_wordlist, interactive=options.interactive,
git clone https://git.99rst.org/PROJECT