From 7c11c4d67ebe275982f238101022502f76a1e6df Mon Sep 17 00:00:00 2001 From: ulif <uli@gnufix.de> Date: Fri, 17 Jul 2015 15:47:22 +0200 Subject: [PATCH] Add func to get all local wordlists names. --- diceware/__init__.py | 18 ++++++++++++++++++ tests/test_diceware.py | 7 +++++++ 2 files changed, 25 insertions(+) diff --git a/diceware/__init__.py b/diceware/__init__.py index 18ae20b..8a87c7a 100644 --- a/diceware/__init__.py +++ b/diceware/__init__.py @@ -124,6 +124,24 @@ def handle_options(args): return args +def get_wordlist_names(): + """Get a all names of wordlists stored locally. + """ + result = [] + filenames = os.listdir(WORDLISTS_DIR) + for filename in filenames: + if not os.path.isfile(os.path.join(WORDLISTS_DIR, filename)): + continue + if not "_" in filename: + continue + if not "." in filename: + continue + basename = filename.split(".")[0] + name = basename.split("_", 1)[1] + result.append(name) + return sorted(result) + + def get_wordlist(file_descriptor): """Parse file in `file_descriptor` and build a word list of it. diff --git a/tests/test_diceware.py b/tests/test_diceware.py index d710ddf..6c19b26 100644 --- a/tests/test_diceware.py +++ b/tests/test_diceware.py @@ -8,6 +8,7 @@ from diceware import ( WORDLISTS_DIR, RE_WORDLIST_NAME, SPECIAL_CHARS, get_wordlist, get_wordlist_path, insert_special_char, get_passphrase, handle_options, main, __version__, print_version, get_random_sources, + get_wordlist_names, ) @@ -120,6 +121,12 @@ class TestDicewareModule(object): assert exc_info.value.args[0].startswith( 'Not a valid wordlist name') + def test_get_wordlist_names(self, wordlists_dir): + # we can get wordlist names also if directory is empty. + wlist_path = wordlists_dir.join('mywordlist_en_8k.txt') + wlist_path.write("some\nirrelevant\nwords") + assert get_wordlist_names() == ['en_8k'] + def test_insert_special_char(self): # we can insert special chars in words. fake_rnd = FakeRandom() -- GitLab