forked from tweecode/twee
-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
33 lines (28 loc) · 757 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from fnmatch import fnmatch
def decodePrettyLink(str):
"""
separate PrettyLinks' label and URI
@param str (str): PrettyLink
@return (dict): label and URI
@raise ValueError: invalid PrettyLink
"""
if str.startswith("[[") and str.endswith("]]"):
link = {}
label, uri = str[2:-2].split("|")
return { "label": label, "uri": uri }
else:
raise ValueError("invalid PrettyLink")
def trimURI(uri): # TODO: properly normalize URI
"""
strip non-essential trailing characters from URI
@param uri (str): URI
@return (str): uniform URI
"""
uri = uri.split("#", 1)[0]
uri = uri.rstrip("/").lower()
return uri
def matchPatterns(term, patterns): # TODO: rename
for pattern in patterns:
if fnmatch(term, pattern):
return True
return False