From 43baf031c283ac646c5edc9c8c8cce6e3dc4015e Mon Sep 17 00:00:00 2001 From: Sean Quah Date: Fri, 3 Dec 2021 17:54:19 +0000 Subject: [PATCH 1/2] Move `glob_to_regex` to `matrix-python-common` --- setup.py | 1 + sygnal/utils.py | 27 +-------------------------- sygnal/webpushpushkin.py | 2 +- 3 files changed, 3 insertions(+), 27 deletions(-) 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 From c8f0ea018bfe8c74861fa0a9e073b600ac175d78 Mon Sep 17 00:00:00 2001 From: Sean Quah Date: Fri, 3 Dec 2021 17:55:22 +0000 Subject: [PATCH 2/2] Add newsfile --- changelog.d/281.misc | 1 + 1 file changed, 1 insertion(+) 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`.