| 1 | from urllib.parse import urlparse, quote_plus |
| 2 | |
| 3 | |
| 4 | def 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 | |
| 14 | def 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 | |
| 21 | print(from_string("repository://fuu/bar@example.org")) |
| 22 | print(from_string("repository://fuu/bar/baz/qux@example.org")) |
| 23 | print(from_string("repository://fuu/bar/baz/qux")) |
| 24 | print(from_string("repository://~hello/world")) |
| 25 | print(from_string("repository://~hello/world@example.org")) |
| 26 | print(from_string("repository://~hello/world@example.org@aaaa")) |
| 27 | print(to_string("fuu/bar/baz", None)) |
| 28 | print(to_string("fuu/bar/baz", "example.org")) |