From 02ad00b9c1720a4cb9a1827e2d162af3482bd533 Mon Sep 17 00:00:00 2001 From: ulif <uli@gnufix.de> Date: Tue, 27 Jan 2015 09:38:53 +0100 Subject: [PATCH] Add wordlist parser. --- diceware/diceware.py | 17 +++++++++++++++++ tests/test_diceware.py | 12 ++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/diceware/diceware.py b/diceware/diceware.py index 67a4e9a..4c469af 100644 --- a/diceware/diceware.py +++ b/diceware/diceware.py @@ -1,3 +1,20 @@ +import os + +#: The directory in which wordlists are stored +SRC_DIR = os.path.dirname(__file__) + +def get_wordlist(path): + """Parse file at `path` and build a word list of it. + """ + result = [] + with open(path, 'r') as fd: + for line in fd.readlines(): + if not '\t' in line: + continue + term = line.split('\t')[1].strip() + result.append(term) + return result + def main(): pass diff --git a/tests/test_diceware.py b/tests/test_diceware.py index bc5723d..e138b27 100644 --- a/tests/test_diceware.py +++ b/tests/test_diceware.py @@ -1,3 +1,11 @@ +import os +from diceware.diceware import SRC_DIR, get_wordlist -def test_foo(): - assert 1 == 1 + +def test_get_wordlist_en(): + # we can get a list of words out of english wordlist. + en_src = os.path.join(SRC_DIR, 'wordlist_en.asc') + en_result = get_wordlist(en_src) + assert en_result[0] == 'a' + assert en_result[-1] == '@' + assert len(en_result) == 7776 -- GitLab