Line | |
---|
1 | from __future__ import annotations
|
---|
2 | from rfc3986.api import uri_reference
|
---|
3 |
|
---|
4 | class URI:
|
---|
5 | '''
|
---|
6 | Immutable object containing an Uniform Resource Identifier (URI).
|
---|
7 | An URI is a string of characters identifying a resource on the web.
|
---|
8 | It generally looks like scheme://user@host:port/bla/bla?query#fragment.
|
---|
9 | Many of these parts are optional, check the specs of rfc3986
|
---|
10 | '''
|
---|
11 | def __init__(self, uri:str):
|
---|
12 | '''
|
---|
13 | Constructor checks that the provided URI meets the specs.
|
---|
14 | '''
|
---|
15 | self._uri=uri
|
---|
16 | self._parse= uri_reference(uri)
|
---|
17 | assert self._parse.scheme , "missing scheme"
|
---|
18 | self._normal=self._parse.normalize()
|
---|
19 |
|
---|
20 | def getUri(self):
|
---|
21 | '''
|
---|
22 | @return the original URI provided in the constructor
|
---|
23 | '''
|
---|
24 | return self._uri
|
---|
25 |
|
---|
26 | def getScheme(self):
|
---|
27 | '''
|
---|
28 | The scheme of the URI. Scheme specifies the format of the data
|
---|
29 | and the communication protocols needed. Eg "https" or "ws"
|
---|
30 | '''
|
---|
31 | return self._parse.scheme
|
---|
32 |
|
---|
33 |
|
---|
34 | def getHost(self):
|
---|
35 | '''
|
---|
36 | @return the Name or IP address of the machine containing the resource
|
---|
37 | '''
|
---|
38 | return self._parse.host
|
---|
39 |
|
---|
40 | def getPath(self):
|
---|
41 | '''
|
---|
42 | @return the Path part of the URI. The pat to the data on the machine.
|
---|
43 | '''
|
---|
44 | return self._parse.path
|
---|
45 |
|
---|
46 | def getQuery(self):
|
---|
47 | '''
|
---|
48 | @return the query part of the URI
|
---|
49 | '''
|
---|
50 | return self._parse.query
|
---|
51 |
|
---|
52 | def getFragment(self):
|
---|
53 | '''
|
---|
54 | @return the fragment contained in the URI.
|
---|
55 | '''
|
---|
56 | return self._parse.fragment
|
---|
57 |
|
---|
58 | def __repr__(self)->str:
|
---|
59 | return self._uri
|
---|
60 |
|
---|
61 | def __eq__(self, other):
|
---|
62 | return isinstance(other, self.__class__) and \
|
---|
63 | self._normal==other._normal
|
---|
64 |
|
---|
65 | def __hash__(self):
|
---|
66 | return hash(self._normal)
|
---|
67 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.