Skip to content

Commit

Permalink
Pin tags to the start of the query
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Dec 9, 2021
1 parent 1e78e24 commit c9ef336
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
50 changes: 29 additions & 21 deletions src/tribler-common/tribler_common/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,44 @@ def test_to_fts_query():


def test_extract_tags():
assert extract_tags(None) == set()
assert extract_tags('') == set()
assert extract_tags('text') == set()
assert extract_tags('[text') == set()
assert extract_tags('text]') == set()
assert extract_tags('[]') == set()
assert extract_tags(None) == (set(), '')
assert extract_tags('') == (set(), '')
assert extract_tags('text') == (set(), '')
assert extract_tags('[text') == (set(), '')
assert extract_tags('text]') == (set(), '')
assert extract_tags('[]') == (set(), '')
assert extract_tags('[tag1[tag2]text]') == (set(), '')

assert extract_tags('[tag]text') == {'tag'}
assert extract_tags('[tag1][tag2]text') == {'tag1', 'tag2'}
assert extract_tags('[tag1[tag2]text]') == {'tag2'}
assert extract_tags('[tag]text') == ({'tag'}, '[tag]')
assert extract_tags('[tag1][tag2]text') == ({'tag1', 'tag2'}, '[tag1][tag2]')
assert extract_tags(' [tag]text[not_tag]') == ({'tag'}, ' [tag]')


def test_extract_plain_fts_query_text():
assert not extract_plain_fts_query_text(None, set())
assert not extract_plain_fts_query_text('', set())
assert extract_plain_fts_query_text('query', set()) == 'query'
assert extract_plain_fts_query_text('query', {'tag'}) == 'query'
assert extract_plain_fts_query_text('[tag] query', {'tag'}) == 'query'
assert not extract_plain_fts_query_text(None, '')
assert not extract_plain_fts_query_text('', '')
assert extract_plain_fts_query_text('query', '') == 'query'
assert extract_plain_fts_query_text('[tag] query', '[tag]') == 'query'


def test_parse_query():
assert parse_query(None) == Query(original_query=None)
assert parse_query('') == Query(original_query='')
assert parse_query('[tag1][tag2]') == Query(original_query='[tag1][tag2]',
tags={'tag1', 'tag2'})
assert parse_query('fts query') == Query(original_query='fts query',
fts_text='fts query')
assert parse_query('[tag1][tag2] fts query') == Query(original_query='[tag1][tag2] fts query',
tags={'tag1', 'tag2'},
fts_text='fts query')

actual = parse_query('[tag1][tag2]')
expected = Query(original_query='[tag1][tag2]', tags={'tag1', 'tag2'})
assert actual == expected

actual = parse_query('fts query with potential [brackets]')
expected = Query(original_query='fts query with potential [brackets]',
fts_text='fts query with potential [brackets]')
assert actual == expected

actual = parse_query('[tag1][tag2] fts query with potential [brackets]')
expected = Query(original_query='[tag1][tag2] fts query with potential [brackets]',
tags={'tag1', 'tag2'},
fts_text='fts query with potential [brackets]',)
assert actual == expected


@patch_import(modules=['win32api'], MessageBox=MagicMock())
Expand Down
24 changes: 12 additions & 12 deletions src/tribler-common/tribler_common/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
import sys
from dataclasses import dataclass, field
from typing import Optional, Set
from typing import Optional, Set, Tuple
from urllib.parse import urlparse
from urllib.request import url2pathname

Expand All @@ -29,7 +29,7 @@ def uri_to_path(uri):


fts_query_re = re.compile(r'\w+', re.UNICODE)
extract_tags_re = re.compile(r'\[\w+\]')
tags_re = re.compile(r'^\s*(?:\[\w+\]\s*)+')


@dataclass
Expand All @@ -49,26 +49,26 @@ def parse_query(query: Optional[str]) -> Query:
if not query:
return Query(original_query=query)

tags = extract_tags(query)
fts_text = extract_plain_fts_query_text(query, tags)
tags, tags_string = extract_tags(query)
fts_text = extract_plain_fts_query_text(query, tags_string)

return Query(original_query=query, tags=tags, fts_text=fts_text)


def extract_tags(text: Optional[str]) -> Set[str]:
def extract_tags(text: Optional[str]) -> Tuple[Set[str], str]:
if not text:
return set()
return {tag[1:-1] for tag in extract_tags_re.findall(text)}
return set(), ''
if (m := tags_re.match(text)) is not None:
tags = m.group(0).strip()
return {tag[1:] for tag in tags.split(']') if tag}, m.group(0)
return set(), ''


def extract_plain_fts_query_text(query: Optional[str], tags: Set[str]) -> str:
def extract_plain_fts_query_text(query: Optional[str], tags_string: str) -> str:
if query is None:
return ''

for tag in tags:
query = query.replace(f'[{tag}]', '')

return query.strip()
return query[len(tags_string):].strip()


def to_fts_query(text):
Expand Down
5 changes: 0 additions & 5 deletions src/tribler-gui/tribler_gui/tribler_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,11 +1033,6 @@ def on_top_search_bar_return_pressed(self):

query = parse_query(self.top_search_bar.text())

if query.tags and query.fts_text:
# in this case we will do normal search. To show this behaviour to
# the user, we need to remove "tags" from the top_search_bar
self.top_search_bar.setText(query.fts_text)

self._logger.info(f'Do search for query: {query}')
self.deselect_all_menu_buttons()

Expand Down

0 comments on commit c9ef336

Please sign in to comment.