From aa055e11e02145b80fa0f471372c6bf27faaf6bb Mon Sep 17 00:00:00 2001 From: Sean Quah <8349537+squahtx@users.noreply.github.com> Date: Fri, 3 Dec 2021 18:15:59 +0000 Subject: [PATCH] Move `glob_to_regex` to `matrix-python-common` (#281) --- changelog.d/281.misc | 1 + setup.py | 1 + sygnal/utils.py | 27 +-------------------------- sygnal/webpushpushkin.py | 2 +- 4 files changed, 4 insertions(+), 27 deletions(-) create mode 100644 changelog.d/281.misc diff --git a/changelog.d/281.misc b/changelog.d/281.misc new file mode 100644 index 00000000..f09969e4 --- /dev/null +++ b/changelog.d/281.misc @@ -0,0 +1 @@ +Move `glob_to_regex` to `matrix-python-common`. diff --git a/setup.py b/setup.py index 8225666b..f6016eab 100755 --- a/setup.py +++ b/setup.py @@ -36,6 +36,7 @@ "importlib_metadata", "pywebpush>=1.13.0", "py-vapid>=1.7.0", + "matrix-common==1.0.0", ] EXTRAS_REQUIRE = { diff --git a/sygnal/utils.py b/sygnal/utils.py index d77cdd06..5d82393d 100644 --- a/sygnal/utils.py +++ b/sygnal/utils.py @@ -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 @@ -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}") diff --git a/sygnal/webpushpushkin.py b/sygnal/webpushpushkin.py index 5def632b..fea3c494 100644 --- a/sygnal/webpushpushkin.py +++ b/sygnal/webpushpushkin.py @@ -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 @@ -38,7 +39,6 @@ Notification, NotificationContext, ) -from sygnal.utils import glob_to_regex if TYPE_CHECKING: from sygnal.sygnal import Sygnal