Skip to content
Snippets Groups Projects
Commit 473cb840 authored by ulif's avatar ulif
Browse files

Make refine_wordlist_entry a class member.

We want to have such functionality be aggregated in a class.
parent d54017ce
No related branches found
No related tags found
No related merge requests found
......@@ -49,21 +49,6 @@ def get_wordlist_names():
return sorted(result)
def refine_wordlist_entry(entry, signed=False):
"""Apply modifications to form a proper wordlist entry.
Set `signed` to `True` if the entry is part of a cryptographically
signed wordlist.
"""
if signed and entry.startswith('- '):
entry = entry[2:]
entry = entry.strip()
match = RE_NUMBERED_WORDLIST_ENTRY.match(entry)
if match:
entry = match.groups()[0]
return entry
def get_wordlist_path(name):
"""Get path to a wordlist file for a wordlist named `name`.
......@@ -119,7 +104,7 @@ class WordList(object):
# wait for first empty line
pass
for line in self.fd:
line = refine_wordlist_entry(line, signed=self.signed)
line = self.refine_entry(line)
if not line:
continue
elif self.signed and line == '-----BEGIN PGP SIGNATURE-----':
......@@ -138,3 +123,18 @@ class WordList(object):
if line1.rstrip() == "-----BEGIN PGP SIGNED MESSAGE-----":
return True
return False
def refine_entry(self, entry):
"""Apply modifications to form a proper wordlist entry.
Refining means: strip() `entry` remove escape-dashes (if this is
a signed wordlist) and extract the term if it is preceded by
numbers.
"""
if self.signed and entry.startswith('- '):
entry = entry[2:]
entry = entry.strip()
match = RE_NUMBERED_WORDLIST_ENTRY.match(entry)
if match:
entry = match.groups()[0]
return entry
......@@ -3,7 +3,7 @@ import pytest
from diceware.wordlist import (
WORDLISTS_DIR, RE_WORDLIST_NAME, RE_NUMBERED_WORDLIST_ENTRY,
RE_VALID_WORDLIST_FILENAME, get_wordlist_path, get_wordlist_names,
refine_wordlist_entry, WordList,
WordList,
)
......@@ -115,29 +115,6 @@ class TestWordlistModule(object):
wordlists_dir.join("file_without_dot-in-name").write("a\nb\n")
assert get_wordlist_names() == []
def test_refine_wordlist_entry_strips(self):
# we strip() entries
assert refine_wordlist_entry("foo") == "foo"
assert refine_wordlist_entry(" foo \n") == "foo"
assert refine_wordlist_entry(" foo bar \n") == "foo bar"
def test_refine_wordlist_entry_handles_numbered(self):
# we transform numbered lines
assert refine_wordlist_entry("11111\tfoo") == "foo"
def test_refine_wordlist_entry_handles_dash_quotes_when_signed(self):
# we handle dash-escaped lines correctly when in signed mode
assert refine_wordlist_entry("- foo") == "- foo"
assert refine_wordlist_entry("- foo", signed=True) == "foo"
def test_refine_wordlist_strips_also_dash_quoted(self):
# also dash-escaped lines in signed wordlistgs are stripped.
assert refine_wordlist_entry("- \tfoo\n", signed=True) == "foo"
def test_refine_wordlist_strips_also_numbered(self):
# also numbered entries are stripped
assert refine_wordlist_entry("11111 \t foo\n") == "foo"
class TestWordList(object):
......@@ -299,3 +276,34 @@ class TestWordList(object):
w_list.fd = fd
result = w_list.is_signed()
assert result is False
def test_refine_entry_strips(self, wordlist):
# we strip() entries
assert wordlist.refine_entry("foo") == "foo"
assert wordlist.refine_entry(" foo \n") == "foo"
assert wordlist.refine_entry(" foo bar \n") == "foo bar"
def test_refine_entry_handles_numbered(self, wordlist):
# we transform numbered lines
assert wordlist.refine_entry("11111\tfoo") == "foo"
def test_refine_entry_handles_dash_quotes_when_signed(
self, wordlist):
# we handle dash-escaped lines correctly when in signed mode
assert wordlist.refine_entry("- foo") == "- foo"
wordlist.signed = True
assert wordlist.refine_entry("- foo") == "foo"
def test_refine_entry_strips_also_dash_quoted(self, wordlist):
# also dash-escaped lines in signed wordlistgs are stripped.
wordlist.signed = True
assert wordlist.refine_entry("- \tfoo\n") == "foo"
def test_refine_entry_strips_also_numbered(self, wordlist):
# also numbered entries are stripped
assert wordlist.refine_entry("11111 \t foo\n") == "foo"
def test_refine_entry_can_handle_all_at_once(self, wordlist):
# we can do all the things above at once and in right order.
wordlist.signed = True
assert wordlist.refine_entry("- 11111 foo \n") == "foo"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment