-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathurl.py
More file actions
87 lines (69 loc) · 2.23 KB
/
url.py
File metadata and controls
87 lines (69 loc) · 2.23 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
URL Library
===========
Module contains classes for easier handling of URLs.
"""
try:
from typing import Self
except ImportError:
from typing_extensions import Self
import urllib.parse
import copy
import pydantic
class URL:
"""URL class for ease of construction and use of server endpoints."""
@pydantic.validate_call
def __init__(self, url: str) -> None:
"""Initialise a url from string form"""
url = url[:-1] if url.endswith("/") else url
_url = urllib.parse.urlparse(url)
self._scheme: str = _url.scheme
self._path: str = _url.path
self._host: str | None = _url.hostname
self._port: int | None = _url.port
self._fragment: str = _url.fragment
def __truediv__(self, other: str) -> Self:
"""Define URL extension through use of '/'"""
_new = copy.deepcopy(self)
_new /= other
return _new
def __repr__(self) -> str:
"""Representation of URL"""
_out_str = f"{self.__class__.__module__}.{self.__class__.__qualname__}"
return f"{_out_str}(url={self.__str__()!r})"
@pydantic.validate_call
def __itruediv__(self, other: str) -> Self:
"""Define URL extension through use of '/'"""
other = other[1:] if other.startswith("/") else other
other = other[:-1] if other.endswith("/") else other
self._path = f"{self._path}/{other}" if other else self._path
return self
@property
def scheme(self) -> str:
return self._scheme
@property
def path(self) -> str:
return self._path
@property
def hostname(self) -> str | None:
return self._host
@property
def fragment(self) -> str:
return self._fragment
@property
def port(self) -> int | None:
return self._port
def __str__(self) -> str:
"""Construct string form of the URL"""
_out_str: str = ""
if self.scheme:
_out_str += f"{self.scheme}://"
if self.hostname:
_out_str += self.hostname
if self.port:
_out_str += f":{self.port}"
if self.path:
_out_str += self.path
if self.fragment:
_out_str += self.fragment
return _out_str