summary refs log tree commit diff
path: root/synapse/util/__init__.py
diff options
context:
space:
mode:
authorAmber Brown <hawkowl@atleastfornow.net>2018-11-01 21:31:46 +1100
committerAmber Brown <hawkowl@atleastfornow.net>2018-11-01 21:31:46 +1100
commit907e6da5be20523bd1d0a14c196289a73182aa65 (patch)
tree30e700dadabd415c169c3850aaf73aef817ef7e2 /synapse/util/__init__.py
parentMerge pull request #4072 from steamp0rt/patch-1 (diff)
parentchangelog (diff)
downloadsynapse-907e6da5be20523bd1d0a14c196289a73182aa65.tar.xz
Merge branch 'release-v0.33.8'
Diffstat (limited to 'synapse/util/__init__.py')
-rw-r--r--synapse/util/__init__.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/synapse/util/__init__.py b/synapse/util/__init__.py
index 9a8fae0497..0ae7e2ef3b 100644
--- a/synapse/util/__init__.py
+++ b/synapse/util/__init__.py
@@ -14,6 +14,7 @@
 # limitations under the License.
 
 import logging
+import re
 from itertools import islice
 
 import attr
@@ -138,3 +139,27 @@ def log_failure(failure, msg, consumeErrors=True):
 
     if not consumeErrors:
         return failure
+
+
+def glob_to_regex(glob):
+    """Converts a glob to a compiled regex object.
+
+    The regex is anchored at the beginning and end of the string.
+
+    Args:
+        glob (str)
+
+    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)