Pythonrepository-uri.py
-rw-r--r-- 851 B
1from urllib.parse import urlparse, quote_plus
2
3
4def from_string(text):
5 url = urlparse(text)
6 if not url.path:
7 return (None, None)
8 split = url.path.split("@", 1)
9 if len(split) == 2:
10 return (split[0], split[1])
11 return (split[0], None)
12
13
14def to_string(slug, domain=None):
15 if domain:
16 return quote_plus(f"repository:{slug}@{domain}")
17 else:
18 return quote_plus(f"repository:{slug}")
19
20
21print(from_string("repository:fuu/bar@example.org"))
22print(from_string("repository:fuu/bar/baz/qux@example.org"))
23print(from_string("repository:fuu/bar/baz/qux"))
24print(from_string("repository:~hello/world"))
25print(from_string("repository:~hello/world@example.org"))
26print(from_string("repository:~hello/world@example.org@aaaa"))
27print(to_string("fuu/bar/baz", None))
28print(to_string("fuu/bar/baz", "example.org"))