Rev | Line | |
---|
[231] | 1 | from __future__ import annotations
|
---|
| 2 | from rfc3986.api import uri_reference
|
---|
| 3 |
|
---|
| 4 | class URI:
|
---|
[262] | 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 | '''
|
---|
[231] | 11 | def __init__(self, uri:str):
|
---|
[262] | 12 | '''
|
---|
| 13 | Constructor checks that the provided URI meets the specs.
|
---|
| 14 | '''
|
---|
[231] | 15 | self._uri=uri
|
---|
| 16 | self._parse= uri_reference(uri)
|
---|
[264] | 17 | assert self._parse.scheme , "missing scheme"
|
---|
[231] | 18 | self._normal=self._parse.normalize()
|
---|
| 19 |
|
---|
| 20 | def getUri(self):
|
---|
[262] | 21 | '''
|
---|
| 22 | @return the original URI provided in the constructor
|
---|
| 23 | '''
|
---|
[231] | 24 | return self._uri
|
---|
| 25 |
|
---|
| 26 | def getScheme(self):
|
---|
[262] | 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 | '''
|
---|
[231] | 31 | return self._parse.scheme
|
---|
| 32 |
|
---|
| 33 |
|
---|
| 34 | def getHost(self):
|
---|
[262] | 35 | '''
|
---|
| 36 | @return the Name or IP address of the machine containing the resource
|
---|
| 37 | '''
|
---|
[231] | 38 | return self._parse.host
|
---|
| 39 |
|
---|
| 40 | def getPath(self):
|
---|
[262] | 41 | '''
|
---|
| 42 | @return the Path part of the URI. The pat to the data on the machine.
|
---|
| 43 | '''
|
---|
[231] | 44 | return self._parse.path
|
---|
| 45 |
|
---|
| 46 | def getQuery(self):
|
---|
[262] | 47 | '''
|
---|
| 48 | @return the query part of the URI
|
---|
| 49 | '''
|
---|
[231] | 50 | return self._parse.query
|
---|
| 51 |
|
---|
| 52 | def getFragment(self):
|
---|
[262] | 53 | '''
|
---|
| 54 | @return the fragment contained in the URI.
|
---|
| 55 | '''
|
---|
[231] | 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):
|
---|
[232] | 66 | return hash(self._normal)
|
---|
[231] | 67 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.