-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdomain_model.py
51 lines (37 loc) · 1.32 KB
/
domain_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import logging
from typing import NamedTuple
from data_platform_catalogue.search_types import SearchFacets
logger = logging.getLogger(__name__)
class Domain(NamedTuple):
urn: str
label: str
class DomainModel:
"""
Store information about domains and subdomains
"""
def __init__(self, search_facets: SearchFacets):
self.labels = {}
self.top_level_domains = [
Domain(option.value, option.label)
for option in search_facets.options("domains")
]
self.top_level_domains.sort(key=lambda d: d.label)
logger.info(f"{self.top_level_domains=}")
self.subdomains = {}
for urn, label in self.top_level_domains:
self.labels[urn] = label
def all_subdomains(self) -> list[Domain]: # -> list[Any]
"""
A flat list of all subdomains
"""
subdomains = []
for domain_choices in self.subdomains.values():
subdomains.extend(domain_choices)
return subdomains
def get_parent_urn(self, child_subdomain_urn) -> str | None:
for domain, subdomains in self.subdomains.items():
for subdomain in subdomains:
if child_subdomain_urn == subdomain.urn:
return domain
def get_label(self, urn):
return self.labels.get(urn, urn)