From: Tomas Krizek Date: Mon, 1 May 2017 20:54:36 +0000 (+0200) Subject: xkcdpass: locate_wordfile - allow search in static dir X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=6e8d63b451165b89d922619ad9f8b5fc1a9d6022;p=redacted-XKCD-password-generator.git xkcdpass: locate_wordfile - allow search in static dir 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 --- diff --git a/xkcdpass/xkcd_password.py b/xkcdpass/xkcd_password.py index 0acc1d1..724f4e7 100755 --- a/xkcdpass/xkcd_password.py +++ b/xkcdpass/xkcd_password.py @@ -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: