Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
After upgrading, update your cache file by deleting it or via `tldextract
--update`.

## unreleased

* Misc.
* Add `reverse_domain_name` property, for Reverse Domain Name Notation.
See:
* https://en.wikipedia.org/wiki/Reverse_domain_name_notation
* https://github.com/john-kurkowski/tldextract/issues/342

## 5.1.3 (2024-11-04)

* Bugfixes
Expand Down
32 changes: 32 additions & 0 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,38 @@ def test_ipv4_lookalike() -> None:
)


def test_reverse_domain_name_notation() -> None:
"""Test property `reverse_domain_name`."""
assert (
tldextract.extract("www.example.com").reverse_domain_name == "com.example.www"
)
assert (
tldextract.extract("www.theregister.co.uk").reverse_domain_name
== "co.uk.theregister.www"
)
assert tldextract.extract("example.com").reverse_domain_name == "com.example"
assert (
tldextract.extract("theregister.co.uk").reverse_domain_name
== "co.uk.theregister"
)
assert (
tldextract.extract("media.forums.theregister.co.uk").reverse_domain_name
== "co.uk.theregister.forums.media"
)
assert (
tldextract.extract(
"foo.uk.com", include_psl_private_domains=False
).reverse_domain_name
== "com.uk.foo"
)
assert (
tldextract.extract(
"foo.uk.com", include_psl_private_domains=True
).reverse_domain_name
== "uk.com.foo"
)


def test_bad_kwargs_no_way_to_fetch() -> None:
"""Test an impossible combination of kwargs that disable all ways to fetch data."""
with pytest.raises(ValueError, match="disable all ways"):
Expand Down
19 changes: 19 additions & 0 deletions tldextract/tldextract.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,25 @@ def ipv6(self) -> str:
return debracketed
return ""

@property
def reverse_domain_name(self) -> str:
"""Return the Reverse Domain Name Notation.

Applies a reverse domain name notation to the submitted domain, in which the
registered domain is used as the leftmost component. Reverse Domain Name
Notation is typically used to organize namespaces for packages and plugins.

>>> extract("login.example.com").reverse_domain_name
'com.example.login'

>>> extract("login.example.co.uk").reverse_domain_name
'co.uk.example.login'
"""
stack = [self.suffix, self.domain]
if self.subdomain:
stack.extend(reversed(self.subdomain.split(".")))
return ".".join(stack)


class TLDExtract:
"""A callable for extracting, subdomain, domain, and suffix components from a URL."""
Expand Down