add tree option on domain_list()

This commit is contained in:
axolotle 2022-02-01 18:38:01 +01:00
parent 6001b0f7af
commit 77471c4140
2 changed files with 47 additions and 2 deletions

View file

@ -443,6 +443,9 @@ domain:
--exclude-subdomains:
help: Filter out domains that are obviously subdomains of other declared domains
action: store_true
--tree:
help: Display domains as a tree
action: store_true
### domain_add()
add:

View file

@ -90,15 +90,42 @@ def _get_domains(exclude_subdomains=False, no_cache=False):
return domain_list_cache
def domain_list(exclude_subdomains=False):
def domain_list(exclude_subdomains=False, tree=False):
"""
List domains
Keyword argument:
exclude_subdomains -- Filter out domains that are subdomains of other declared domains
tree -- Display domains as a hierarchy tree
"""
return {"domains": _get_domains(exclude_subdomains), "main": _get_maindomain()}
from collections import OrderedDict
domains = _get_domains(exclude_subdomains)
main = _get_maindomain()
if not tree:
return {"domains": domains, "main": main}
if tree and exclude_subdomains:
return {
"domains": OrderedDict({domain: {} for domain in domains}),
"main": main,
}
def get_parent_dict(tree, child):
# If parent exists it should be the last added (see `_get_domains` ordering)
possible_parent = next(reversed(tree)) if tree else None
if possible_parent and child.endswith(f".{possible_parent}"):
return get_parent_dict(tree[possible_parent], child)
return tree
result = OrderedDict()
for domain in domains:
parent = get_parent_dict(result, domain)
parent[domain] = OrderedDict()
return {"domains": result, "main": main}
def _assert_domain_exists(domain):
@ -118,6 +145,21 @@ def _list_subdomains_of(parent_domain):
return out
# def _get_parent_domain_of(domain):
#
# _assert_domain_exists(domain)
#
# if "." not in domain:
# return domain
#
# parent_domain = domain.split(".", 1)[-1]
# if parent_domain not in _get_domains():
# return domain # Domain is its own parent
#
# else:
# return _get_parent_domain_of(parent_domain)
def _get_parent_domain_of(domain, return_self=True):
_assert_domain_exists(domain)