Skip to content

Commit

Permalink
Merge pull request #188 from Krukov/fix-templating-2
Browse files Browse the repository at this point in the history
fix tempating for tags
  • Loading branch information
Krukov authored Jan 7, 2024
2 parents aecdb92 + e8ac322 commit 3720979
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
8 changes: 2 additions & 6 deletions cashews/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ._typing import KeyOrTemplate, KeyTemplate

TemplateValue = str
_re_special_chars_map = {i: "\\" + chr(i) for i in b"()[]?*+-|^$\\.&~# \t\n\r\v\f"}


def _decode_bytes(value: bytes):
Expand Down Expand Up @@ -159,18 +160,13 @@ def template_to_pattern(template: KeyTemplate, _formatter=_ReplaceFormatter(), *
return _formatter.format(template, **values)


def _re_legacy(field_name):
return f"(?P<{field_name.replace('.', '_')}>[^:]*)"


def _re_default(field_name):
return f"(?P<{field_name.replace('.', '_')}>.+)?"


_re_formatter = _ReplaceFormatter(default=_re_default)
FuncRegKey = Tuple[str, str]


def template_to_re_pattern(template: KeyTemplate) -> Pattern:
pattern = _re_formatter.format(template)
pattern = _re_formatter.format(template.translate(_re_special_chars_map))
return re.compile("^" + pattern + "$", flags=re.MULTILINE)
5 changes: 3 additions & 2 deletions cashews/wrapper/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from cashews._typing import TTL, Key, KeyOrTemplate, OnRemoveCallback, Tag, Tags, Value
from cashews.backends.interface import Backend
from cashews.exceptions import TagNotRegisteredError
from cashews.formatter import template_to_re_pattern
from cashews.formatter import default_formatter, template_to_pattern, template_to_re_pattern

from .commands import CommandWrapper

Expand All @@ -24,7 +24,8 @@ def get_key_tags(self, key: Key) -> Tags:
match = self._match_patterns(key, patterns)
if match:
group_dict = {k: v if v is not None else "" for k, v in match.groupdict().items()}
tags.append(tag.format(**group_dict))
tag = template_to_pattern(tag, _formatter=default_formatter, **group_dict)
tags.append(tag)
return tags

@staticmethod
Expand Down
11 changes: 6 additions & 5 deletions tests/test_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ def method(self, a, k=None):
return a


TEPLATE_FUNC1 = "func1:{a}"
TEPLATE_FUNC2 = "func2:{k}:user:{user}"
TEPLATE_FUNC3 = "func3:{k:len}"


@default_formatter.type_format(Klass)
def _some(value: Klass):
return "hoho"
Expand Down Expand Up @@ -63,6 +58,12 @@ async def func(user):
"{arg1}:{kwarg1}-{kwarg3}",
"A1:k1-true",
),
(
(),
{"kwarg1": "!@#$%^&*()"},
"!@#$%^&*():{kwarg1}",
"!@#$%^&*():!@#$%^&*()",
),
(
("A1", "a2", "a3"),
{"kwarg1": "k1", "kwarg0": True},
Expand Down
2 changes: 2 additions & 0 deletions tests/test_tags_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def test_register_tags(cache: Cache):
cache.register_tag("tag", "key")
cache.register_tag("tag_template", "key{i}")
cache.register_tag("tag_test:{i}", "key{i}:test")
cache.register_tag("!@#$%^&*():{i}", "!@#$%^&*(){i}:test")

assert cache.get_key_tags("key1:test") == ["tag_template", "tag_test:1"]
assert cache.get_key_tags("keyanytest") == ["tag_template"]
Expand All @@ -22,6 +23,7 @@ def test_register_tags(cache: Cache):
assert cache.get_key_tags("k") == []
assert cache.get_key_tags("prefixkey") == []
assert cache.get_key_tags(":key") == []
assert cache.get_key_tags("!@#$%^&*()$:test") == ["!@#$%^&*():$"]


async def test_tags_no_delete(cache: Cache):
Expand Down

0 comments on commit 3720979

Please sign in to comment.