2026-04-27 12:40:58 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
from functools import cached_property
|
2026-05-06 15:26:14 +02:00
|
|
|
import copy
|
2026-04-27 12:40:58 +02:00
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from typing import Self
|
|
|
|
|
import urllib
|
|
|
|
|
|
|
|
|
|
# Make sure URIs are interpreted indentically everywhere
|
|
|
|
|
|
|
|
|
|
class Uri:
|
|
|
|
|
|
2026-05-06 15:26:14 +02:00
|
|
|
def __assemble(self, scheme: bool, credentials: bool, secure: bool, path: bool) -> str:
|
2026-04-27 12:40:58 +02:00
|
|
|
ret = ''
|
|
|
|
|
if scheme:
|
|
|
|
|
ret += f'{self.protocol}://'
|
|
|
|
|
if credentials and self.username:
|
|
|
|
|
ret += self.username
|
|
|
|
|
if self.password:
|
|
|
|
|
ret += ':' + '<hidden>' if secure else self.password
|
|
|
|
|
ret += '@'
|
|
|
|
|
if self.hostname:
|
|
|
|
|
ret += self.hostname
|
|
|
|
|
if self.port_str:
|
|
|
|
|
ret += ':' + self.port_str
|
2026-05-06 15:26:14 +02:00
|
|
|
if path:
|
|
|
|
|
ret += self.path
|
2026-04-27 12:40:58 +02:00
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
def __init__(self, string: str) -> None:
|
|
|
|
|
self.__string = string
|
|
|
|
|
self.__username: str|None = None
|
|
|
|
|
self.__password: str|None = None
|
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return self.full
|
|
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
2026-05-06 15:26:14 +02:00
|
|
|
return self.safe_full_with_username
|
2026-04-27 12:40:58 +02:00
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
def __p(self) -> urllib.parse.ParseResult:
|
|
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
return urlparse(self.__string)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def pimp(cls, url: str|Self) -> Uri:
|
|
|
|
|
if isinstance(url, Uri):
|
|
|
|
|
return url
|
|
|
|
|
return Uri(url)
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def to_string(self) -> str:
|
|
|
|
|
return self.__string
|
|
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
def scheme(self) -> str:
|
|
|
|
|
ret = self.__p.scheme
|
|
|
|
|
if not ret:
|
|
|
|
|
return 'file://'
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
def protocol(self) -> str:
|
|
|
|
|
return self.scheme.replace('://', '')
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def username(self) -> str|None:
|
|
|
|
|
if self.__username is None:
|
|
|
|
|
return self.__p.username
|
|
|
|
|
return self.__username
|
|
|
|
|
|
|
|
|
|
def set_username(self, username: str) -> None:
|
|
|
|
|
self.__username = username
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def password(self) -> str|None:
|
|
|
|
|
if self.__password is None:
|
|
|
|
|
return self.__p.password
|
|
|
|
|
return self.__password
|
|
|
|
|
|
|
|
|
|
def set_password(self, password: str) -> None:
|
|
|
|
|
self.__password = password
|
|
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
def hostname(self) -> str|None:
|
|
|
|
|
return self.__p.hostname
|
|
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
def port(self) -> int|None:
|
|
|
|
|
return self.__p.port
|
|
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
def port_str(self) -> str|None:
|
|
|
|
|
if self.port is None:
|
|
|
|
|
return None
|
|
|
|
|
return str(self.port)
|
|
|
|
|
|
2026-05-06 15:26:14 +02:00
|
|
|
@cached_property
|
|
|
|
|
def path(self) -> str:
|
|
|
|
|
return self.__p.path
|
|
|
|
|
|
2026-04-27 12:40:58 +02:00
|
|
|
@cached_property
|
|
|
|
|
def authority(self) -> str:
|
2026-05-06 15:26:14 +02:00
|
|
|
return self.__assemble(scheme=False, credentials=True, secure=False, path=False)
|
2026-04-27 12:40:58 +02:00
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
def origin(self) -> str:
|
2026-05-06 15:26:14 +02:00
|
|
|
return self.__assemble(scheme=False, credentials=False, secure=True, path=False)
|
2026-04-27 12:40:58 +02:00
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
def scheme_plus_authority(self) -> str:
|
|
|
|
|
return self.scheme + '://' + self.authority
|
|
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
def id(self) -> str:
|
2026-05-06 15:26:14 +02:00
|
|
|
return self.__assemble(scheme=True, credentials=True, secure=True, path=False)
|
2026-04-27 12:40:58 +02:00
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
def full(self) -> str:
|
2026-05-06 15:26:14 +02:00
|
|
|
return self.__assemble(scheme=True, credentials=True, secure=False, path=True)
|
2026-04-27 12:40:58 +02:00
|
|
|
|
|
|
|
|
@cached_property
|
2026-05-06 15:26:14 +02:00
|
|
|
def safe_full_with_username(self) -> str:
|
|
|
|
|
return self.__assemble(scheme=True, credentials=True, secure=True, path=True)
|
|
|
|
|
|
|
|
|
|
def __new_with_path(self, base: str, path: str) -> Self:
|
|
|
|
|
ret = copy.deepcopy(self)
|
|
|
|
|
ret.__string = base
|
|
|
|
|
ret.__username = None
|
|
|
|
|
ret.__password = None
|
|
|
|
|
if not path:
|
|
|
|
|
return ret
|
|
|
|
|
if ret.__string[-1] == '/':
|
|
|
|
|
if path[0] == '/':
|
|
|
|
|
ret.__string += path[1:]
|
|
|
|
|
else:
|
|
|
|
|
ret.__string += path
|
|
|
|
|
else:
|
|
|
|
|
if path[0] == '/':
|
|
|
|
|
ret.__string += path
|
|
|
|
|
else:
|
|
|
|
|
ret.__string += '/' + path
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
def new_add_path(self, path: str) -> Self:
|
|
|
|
|
return self.__new_with_path(self.__string, path)
|
|
|
|
|
|
|
|
|
|
def new_replace_path(self, path: str) -> Self:
|
|
|
|
|
return self.__new_with_path(self.schema_plus_authority, path)
|