Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move glob_to_regex to matrix-python-common #281

Merged
merged 2 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember if we had a conversation, but 👍 on pinning this. I think we want exact versions here since we're just trying to avoid vendoring this code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I seem to remember someone telling me to pin the matrix-common import, but can't remember where that was.

]

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