Skip to content

Commit

Permalink
Move glob_to_regex to matrix-python-common (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
squahtx committed Dec 3, 2021
1 parent 4a7367e commit aa055e1
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 27 deletions.
1 change: 1 addition & 0 deletions changelog.d/281.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Move `glob_to_regex` to `matrix-python-common`.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"importlib_metadata",
"pywebpush>=1.13.0",
"py-vapid>=1.7.0",
"matrix-common==1.0.0",
]

EXTRAS_REQUIRE = {
Expand Down
27 changes: 1 addition & 26 deletions sygnal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import re
from logging import LoggerAdapter
from typing import TYPE_CHECKING, Any, MutableMapping, Pattern, Tuple
from typing import TYPE_CHECKING, Any, MutableMapping, Tuple

from twisted.internet.defer import Deferred

Expand Down Expand Up @@ -47,30 +46,6 @@ def process(
return f"[{self.extra['request_id']}] {msg}", kwargs


def glob_to_regex(glob: str) -> Pattern:
"""Converts a glob to a compiled regex object.
The regex is anchored at the beginning and end of the string.
Args:
glob
Returns:
re.RegexObject
"""
res = ""
for c in glob:
if c == "*":
res = res + ".*"
elif c == "?":
res = res + "."
else:
res = res + re.escape(c)

# \A anchors at start of string, \Z at end of string
return re.compile(r"\A" + res + r"\Z", re.IGNORECASE)


def _reject_invalid_json(val: Any) -> None:
"""Do not allow Infinity, -Infinity, or NaN values in JSON."""
raise ValueError(f"Invalid JSON value: {val!r}")
Expand Down
2 changes: 1 addition & 1 deletion sygnal/webpushpushkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Pattern
from urllib.parse import urlparse

from matrix_common.regex import glob_to_regex
from prometheus_client import Gauge, Histogram
from py_vapid import Vapid, VapidException
from pywebpush import CaseInsensitiveDict, webpush
Expand All @@ -38,7 +39,6 @@
Notification,
NotificationContext,
)
from sygnal.utils import glob_to_regex

if TYPE_CHECKING:
from sygnal.sygnal import Sygnal
Expand Down

0 comments on commit aa055e1

Please sign in to comment.