Skip to content

Commit

Permalink
Support qkeep=True to keep all query parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
sopoforic authored and karlicoss committed Mar 5, 2022
1 parent f8b8bd7 commit ba333c7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/promnesia/cannon.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@ def canonify_domain(dom: str) -> str:

# TODO perhaps, decide if fragment is meaningful (e.g. wiki) or random sequence of letters?
class Spec(NamedTuple):
qkeep : Optional[Collection[str]] = None
qkeep : Optional[Union[Collection[str], bool]] = None
qremove: Optional[Set[str]] = None
fkeep : bool = False

def keep_query(self, q: str) -> Optional[int]: # returns order
if self.qkeep is True:
return 1
qkeep = {
q: i for i, q in enumerate(chain(default_qkeep, self.qkeep or []))
}
Expand Down Expand Up @@ -183,6 +185,7 @@ def make(cls, **kwargs) -> 'Spec':
'ycombinator.com' : S(qkeep={'id'}), # todo just keep id by default?
'play.google.com' : S(qkeep={'id'}),
'answers.yahoo.com' : S(qkeep={'qid'}),
'isfdb.org': S(qkeep=True),
}

_def_spec = S()
Expand Down
8 changes: 8 additions & 0 deletions tests/cannon.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,11 @@ def test_error():
])
def test_empty_query_parameter(url, expected):
assert canonify(url) == expected

@pytest.mark.parametrize("url,expected", [
('http://www.isfdb.org/cgi-bin/title.cgi?2172', 'isfdb.org/cgi-bin/title.cgi?2172='),
('http://www.isfdb.org/cgi-bin/title.cgi?2172+1', 'isfdb.org/cgi-bin/title.cgi?2172%201='),
('http://www.isfdb.org/cgi-bin/title.cgi?2172&foo=bar&baz&quux', 'isfdb.org/cgi-bin/title.cgi?2172=&baz=&foo=bar&quux='),
])
def test_qkeep_true(url, expected):
assert canonify(url) == expected

0 comments on commit ba333c7

Please sign in to comment.