xkcdpass: locate_wordfile - allow search in static dir
authorTomas Krizek <redacted>
Mon, 1 May 2017 20:54:36 +0000 (22:54 +0200)
committerTomas Krizek <redacted>
Mon, 1 May 2017 22:14:39 +0000 (00:14 +0200)
Change locate_wordfile to locate wordfiles in the following order:
  - inside static directory by their filename
  - any valid path provided as an argument
  - previous defaults if no argument is provided

Signed-off-by: Tomas Krizek <redacted>
xkcdpass/xkcd_password.py

index 0acc1d1ff0897b67d3be5d7315847ce05251be10..724f4e7b6a02fa3f51304135071599cb94281008 100755 (executable)
@@ -57,6 +57,9 @@ if sys.version_info[0] >= 3:
     xrange = range
 
 
+DEFAULT_WORDFILE = "default.txt"
+
+
 def validate_options(parser, options):
     """
     Given a parsed collection of options, performs various validation checks.
@@ -68,29 +71,34 @@ def validate_options(parser, options):
                          "Check the specified settings.\n")
         sys.exit(1)
 
-    if options.wordfile is not None:
-        if not os.path.isfile(os.path.abspath(options.wordfile)):
-            sys.stderr.write("Could not open the specified word file.\n")
-            sys.exit(1)
-    else:
-        options.wordfile = locate_wordfile()
-
-        if not options.wordfile:
-            sys.stderr.write("Could not find a word file, or word file does "
-                             "not exist.\n")
-            sys.exit(1)
+    wordfile = locate_wordfile(options.wordfile)
+    if not wordfile:
+        sys.stderr.write("Could not find a word file, or word file does "
+                         "not exist.\n")
+        sys.exit(1)
 
 
-def locate_wordfile():
-    static_default = os.path.join(
+def locate_wordfile(wordfile=None):
+    """
+    Locate a wordfile from provided name/path. Return a path to wordfile
+    either from static directory, the provided path or use a default.
+    """
+    common_word_files = []
+    static_dir = os.path.join(
         os.path.dirname(os.path.abspath(__file__)),
-        'static',
-        'default.txt')
-    common_word_files = ["/usr/share/cracklib/cracklib-small",
-                         "/usr/share/dict/cracklib-small",
-                         static_default,
-                         "/usr/dict/words",
-                         "/usr/share/dict/words"]
+        'static')
+
+    if wordfile is not None:
+        # wordfile can be in static dir or provided as a complete path
+        common_word_files.append(os.path.join(static_dir, wordfile))
+        common_word_files.append(os.path.expanduser(wordfile))
+
+    common_word_files.extend([
+        "/usr/share/cracklib/cracklib-small",
+        "/usr/share/dict/cracklib-small",
+        os.path.join(static_dir, DEFAULT_WORDFILE),
+        "/usr/dict/words",
+        "/usr/share/dict/words"])
 
     for wfile in common_word_files:
         if os.path.isfile(wfile):
@@ -106,8 +114,7 @@ def generate_wordlist(wordfile=None,
     valid_chars is a regular expression match condition (default - all chars)
     """
 
-    if wordfile is None:
-        wordfile = locate_wordfile()
+    wordfile = locate_wordfile(wordfile)
 
     words = []
 
@@ -115,9 +122,6 @@ def generate_wordlist(wordfile=None,
                                                   min_length,
                                                   max_length))
 
-    # At this point wordfile is set
-    wordfile = os.path.expanduser(wordfile)  # just to be sure
-
     # read words from file into wordlist
     with open(wordfile) as wlf:
         for line in wlf:
git clone https://git.99rst.org/PROJECT