Skip to content

Url

Base classes

Name Children Inherits
SerializeMixin
prettyqt.utils.serializemixin
QUrl
PySide6.QtCore
QUrl(self, /) -> None

⋔ Inheritance diagram

graph TD
  2833587809["core.Url"]
  69336483["utils.SerializeMixin"]
  2751680898["builtins.object"]
  1804377002["QtCore.QUrl"]
  37984305["Shiboken.Object"]
  69336483 --> 2833587809
  2751680898 --> 69336483
  1804377002 --> 2833587809
  37984305 --> 1804377002
  2751680898 --> 37984305

🛈 DocStrings

Bases: SerializeMixin, QUrl

Convenient interface for working with URLs.

Source code in prettyqt/core/url.py
class Url(serializemixin.SerializeMixin, QtCore.QUrl):
    """Convenient interface for working with URLs."""

    def __init__(self, *args, **kwargs):
        match args:
            case (os.PathLike(), *rest):
                path = os.fspath(args[0])
                if pathlib.Path(path).exists():
                    path = self.fromLocalFile(path)
                super().__init__(path, *rest, **kwargs)
            case _:
                super().__init__(*args, **kwargs)

    @property
    def _toString(self) -> str:
        return self.toString()

    __match_args__ = ("_toString",)

    # def __str__(self):
    #     return self.absolutePath()

    def __repr__(self):
        return get_repr(self, self.toString())

    def __str__(self):
        return self.toString()

    def __vfspath__(self) -> str:
        return self.toString()

    def serialize_fields(self):
        return dict(path=self.toString())

    def serialize(self) -> dict[str, Any]:
        return self.serialize_fields()

    def to_string(self) -> str:
        return self.toString()

    def to_path(self) -> pathlib.Path:
        """Get pathlib object from the URL.

        Returns:
            Path
        """
        return pathlib.Path(str(self))

    def is_local_file(self) -> bool:
        return self.isLocalFile()

    @classmethod
    def from_user_input(cls, url: str, working_dir: str | None = None) -> Self:
        if working_dir is None:
            working_dir = ""
        return cls(cls.fromUserInput(url, working_dir))

    @classmethod
    def from_local_file(cls, path: datatypes.PathType) -> Self:
        url = cls.fromLocalFile(os.fspath(path))
        return cls(url)

    def _has_explicit_scheme(self) -> bool:
        """Check if a url has an explicit scheme given."""
        return bool(
            self.isValid()
            and self.scheme()
            and (self.host() or self.path())
            and not self.path().startswith(":")
        )

    def is_special_url(self) -> bool:
        """Return True if url is an about:... or other special URL."""
        return self.scheme() in ("about", "file") if self.isValid() else False

is_special_url() -> bool

Return True if url is an about:... or other special URL.

Source code in prettyqt/core/url.py
def is_special_url(self) -> bool:
    """Return True if url is an about:... or other special URL."""
    return self.scheme() in ("about", "file") if self.isValid() else False

to_path() -> pathlib.Path

Get pathlib object from the URL.

Source code in prettyqt/core/url.py
def to_path(self) -> pathlib.Path:
    """Get pathlib object from the URL.

    Returns:
        Path
    """
    return pathlib.Path(str(self))

:fa-brands-github: Show source on GitHub