Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
diceware-debian
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
drebs
diceware-debian
Commits
473cb840
Commit
473cb840
authored
9 years ago
by
ulif
Browse files
Options
Downloads
Patches
Plain Diff
Make refine_wordlist_entry a class member.
We want to have such functionality be aggregated in a class.
parent
d54017ce
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
diceware/wordlist.py
+16
-16
16 additions, 16 deletions
diceware/wordlist.py
tests/test_wordlist.py
+32
-24
32 additions, 24 deletions
tests/test_wordlist.py
with
48 additions
and
40 deletions
diceware/wordlist.py
+
16
−
16
View file @
473cb840
...
...
@@ -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
This diff is collapsed.
Click to expand it.
tests/test_wordlist.py
+
32
−
24
View file @
473cb840
...
...
@@ -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
\n
b
\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
\t
foo
"
)
==
"
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
(
"
-
\t
foo
\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
\t
foo
"
)
==
"
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
(
"
-
\t
foo
\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
"
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment