1212 UnknownTld ,
1313)
1414
15+ # from .parameterContext import ParameterContext # not needed yet here
16+
1517Verbose : bool = False
1618TLD_RE : Dict [str , Dict [str , Any ]] = {}
1719REG_COLLECTION_BY_KEY : Dict [str , Any ] = {}
1820
1921
20- def validTlds () -> List [str ]:
21- return sorted (TLD_RE .keys ())
22-
23-
24- def filterTldToSupportedPattern (
25- domain : str ,
26- d : List [str ],
27- verbose : bool = False ,
28- ) -> str :
29- # we have max 2 levels so first check if the last 2 are in our list
30- tld = f"{ d [- 2 ]} .{ d [- 1 ]} "
31- if tld in ZZ :
32- if verbose :
33- print (f"we have { tld } " , file = sys .stderr )
34- return tld
35-
36- # if not check if the last item we have
37- tld = f"{ d [- 1 ]} "
38- if tld in ZZ :
39- if verbose :
40- print (f"we have { tld } " , file = sys .stderr )
41- return tld
42-
43- if verbose :
44- print (f"we DONT have { tld } " , file = sys .stderr )
45-
46- # if not fail
47- a = f"The TLD { tld } is currently not supported by this package."
48- b = "Use validTlds() to see what toplevel domains are supported."
49- msg = f"{ a } { b } "
50- raise UnknownTld (msg )
51-
52-
53- def get_tld_re (tld : str , override : bool = False ) -> Dict [str , Any ]:
22+ def _get_tld_re (
23+ tld : str ,
24+ override : bool = False ,
25+ ) -> Dict [str , Any ]:
5426 if override is False :
5527 if tld in TLD_RE :
5628 return TLD_RE [tld ]
@@ -59,7 +31,7 @@ def get_tld_re(tld: str, override: bool = False) -> Dict[str, Any]:
5931
6032 extend = v .get ("extend" )
6133 if extend :
62- e = get_tld_re (extend ) # call recursive
34+ e = _get_tld_re (extend ) # call recursive
6335 tmp = e .copy ()
6436 tmp .update (v ) # and merge results in tmp with caller data in v
6537 # The update() method updates the dictionary with the elements
@@ -87,22 +59,14 @@ def get_tld_re(tld: str, override: bool = False) -> Dict[str, Any]:
8759 return tld_re
8860
8961
90- def mergeExternalDictWithRegex (aDict : Dict [str , Any ] = {}) -> None :
91- # merge in ZZ, this extends ZZ with new tld's and overrides existing tld's
92- for tld in aDict .keys ():
93- ZZ [tld ] = aDict [tld ]
94-
95- # reprocess te regexes we newly defined or overrode
96- override = True
97- for tld in aDict .keys ():
98- initOne (tld , override )
99-
100-
101- def initOne (tld : str , override : bool = False ) -> None :
62+ def _initOne (
63+ tld : str ,
64+ override : bool = False ,
65+ ) -> None :
10266 if tld [0 ] == "_" : # skip meta domain patterns , these are not domains just handles we reuse
10367 return
10468
105- what = get_tld_re (tld , override )
69+ what = _get_tld_re (tld , override )
10670
10771 # test if the string is identical after idna conversion
10872 d = tld .split ("." )
@@ -117,7 +81,9 @@ def initOne(tld: str, override: bool = False) -> None:
11781 print (f"{ tld } -> { tld2 } " , file = sys .stderr )
11882
11983
120- def buildRegCollection (zz : Dict [str , Any ]) -> Dict [str , Any ]:
84+ def _buildRegCollection (
85+ zz : Dict [str , Any ],
86+ ) -> Dict [str , Any ]:
12187 regCollection : Dict [str , Any ] = {}
12288
12389 # get all regexes
@@ -152,15 +118,65 @@ def buildRegCollection(zz: Dict[str, Any]) -> Dict[str, Any]:
152118 return regCollection
153119
154120
155- def initOnImport () -> None :
121+ def _initOnImport () -> None :
156122 global REG_COLLECTION_BY_KEY
157123 # here we run the import processing
158124 # we load all tld's on import so we dont lose time later
159125 # we keep ZZ so we can later reuse it if we want to aoverrid or update tld's
160- REG_COLLECTION_BY_KEY = buildRegCollection (ZZ )
126+ REG_COLLECTION_BY_KEY = _buildRegCollection (ZZ )
161127 override = False
162128 for tld in ZZ .keys ():
163- initOne (tld , override )
129+ _initOne (tld , override )
130+
131+
132+ # ========================================
133+ # external use
134+
135+
136+ def filterTldToSupportedPattern (
137+ domain : str ,
138+ d : List [str ],
139+ verbose : bool = False ,
140+ ) -> str :
141+ # we have max 2 levels so first check if the last 2 are in our list
142+ tld = f"{ d [- 2 ]} .{ d [- 1 ]} "
143+ if tld in ZZ :
144+ if verbose :
145+ print (f"we have { tld } " , file = sys .stderr )
146+ return tld
147+
148+ # if not check if the last item we have
149+ tld = f"{ d [- 1 ]} "
150+ if tld in ZZ :
151+ if verbose :
152+ print (f"we have { tld } " , file = sys .stderr )
153+ return tld
154+
155+ if verbose :
156+ print (f"we DONT have { tld } " , file = sys .stderr )
157+
158+ # if not fail
159+ a = f"The TLD { tld } is currently not supported by this package."
160+ b = "Use validTlds() to see what toplevel domains are supported."
161+ msg = f"{ a } { b } "
162+ raise UnknownTld (msg )
163+
164+
165+ def mergeExternalDictWithRegex (
166+ aDict : Dict [str , Any ] = {},
167+ ) -> None :
168+ # merge in ZZ, this extends ZZ with new tld's and overrides existing tld's
169+ for tld in aDict .keys ():
170+ ZZ [tld ] = aDict [tld ]
171+
172+ # reprocess te regexes we newly defined or overrode
173+ override = True
174+ for tld in aDict .keys ():
175+ _initOne (tld , override )
176+
177+
178+ def validTlds () -> List [str ]:
179+ return sorted (TLD_RE .keys ())
164180
165181
166- initOnImport ()
182+ _initOnImport ()
0 commit comments