Skip to content

Commit

Permalink
Replace square brackets by #
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Dec 16, 2021
1 parent dae085c commit c65dc6e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 48 deletions.
58 changes: 21 additions & 37 deletions src/tribler-common/tribler_common/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@
from unittest.mock import MagicMock, patch

from tribler_common.patch_import import patch_import
from tribler_common.utilities import (
Query,
extract_plain_fts_query_text,
extract_tags,
parse_query,
show_system_popup,
to_fts_query,
uri_to_path,
)
from tribler_common.utilities import Query, extract_tags, parse_query, show_system_popup, to_fts_query, uri_to_path

# pylint: disable=import-outside-toplevel, import-error
# fmt: off
Expand All @@ -32,45 +24,37 @@ def test_to_fts_query():

def test_extract_tags():
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('[ta]') == (set(), '')
assert extract_tags('[' + 't' * 51 + ']') == (set(), '')
assert extract_tags('[tag1[tag2]text]') == (set(), '')
assert extract_tags('[not a tag]') == (set(), '')
assert extract_tags('text') == (set(), 'text')
assert extract_tags('#') == (set(), '#')
assert extract_tags('# ') == (set(), '# ')
assert extract_tags('#t ') == (set(), '#t ')
assert extract_tags('#' + 't' * 51) == (set(), '#' + 't' * 51)
assert extract_tags('####') == (set(), '####')

assert extract_tags('[tag]') == ({'tag'}, '[tag]')
assert extract_tags('[tag1][tag2]') == ({'tag1', 'tag2'}, '[tag1][tag2]')
assert extract_tags('[tag_with_underscore][tag-with-dash]') == ({'tag_with_underscore', 'tag-with-dash'},
'[tag_with_underscore][tag-with-dash]')

assert extract_tags(' [tag][not tag]for complex query with [not tag at the end]') == ({'tag'}, ' [tag]')


def test_extract_plain_fts_query_text():
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'
assert extract_tags('#tag') == ({'tag'}, '')
assert extract_tags('a #tag in the middle') == ({'tag'}, 'a in the middle')
assert extract_tags('at the end of the query #tag') == ({'tag'}, 'at the end of the query ')
assert extract_tags('multiple tags: #tag1 #tag2#tag3') == ({'tag1', 'tag2', 'tag3'}, 'multiple tags: ')
assert extract_tags('#tag_with_underscores #tag-with-dashes') == ({'tag_with_underscores', 'tag-with-dashes'}, ' ')


def test_parse_query():
assert parse_query('') == Query(original_query='')

actual = parse_query('[tag1][tag2]')
expected = Query(original_query='[tag1][tag2]', tags={'tag1', 'tag2'})
actual = parse_query('#tag1 #tag2')
expected = Query(original_query='#tag1 #tag2', tags={'tag1', 'tag2'}, fts_text='')
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]')
actual = parse_query('query without tags')
expected = Query(original_query='query without tags',
tags=set(),
fts_text='query without tags')
assert actual == expected

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


Expand Down
24 changes: 13 additions & 11 deletions src/tribler-common/tribler_common/utilities.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import itertools
import os
import platform
import re
Expand Down Expand Up @@ -29,7 +30,7 @@ def uri_to_path(uri):


fts_query_re = re.compile(r'\w+', re.UNICODE)
tags_re = re.compile(r'^\s*(?:\[[^\s\[\]]{3,50}\]\s*)+')
tags_re = re.compile(r'#[^\s^#]{3,50}(?=[#\s]|$)')


@dataclass
Expand All @@ -49,23 +50,24 @@ def parse_query(query: str) -> Query:
if not query:
return Query(original_query=query)

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)
tags, remaining_text = extract_tags(query)
return Query(original_query=query, tags=tags, fts_text=remaining_text.strip())


def extract_tags(text: str) -> Tuple[Set[str], str]:
if not 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(), ''

tags = set()
positions = [0]

for m in tags_re.finditer(text):
tags.add(m.group(0)[1:])
positions.extend(itertools.chain.from_iterable(m.regs))
positions.append(len(text))

def extract_plain_fts_query_text(query: str, tags_string: str) -> str:
return query[len(tags_string) :].strip()
remaining_text = ''.join(text[positions[i] : positions[i + 1]] for i in range(0, len(positions) - 1, 2))
return tags, remaining_text


def to_fts_query(text):
Expand Down

0 comments on commit c65dc6e

Please sign in to comment.