62
62
63
63
class PublicSuffixList (object ):
64
64
65
- def __init__ (self , psl_file = None , idna = True ):
65
+ def __init__ (self , psl_file = None , idna = True , private = True ):
66
66
"""
67
67
Read and parse a public suffix list. `psl_file` is either a file
68
68
location string, or a file-like object, or an iterable of lines from a
@@ -80,6 +80,7 @@ def __init__(self, psl_file=None, idna=True):
80
80
81
81
:param psl_file: string or None
82
82
:param idna: boolean, whether to convert file to IDNA-encoded strings
83
+ :param private: boolean, include non-ICANN private names, default=True
83
84
"""
84
85
# Note: we test for None as we accept empty lists as inputs
85
86
if psl_file is None or isinstance (psl_file , str ):
@@ -91,7 +92,7 @@ def __init__(self, psl_file=None, idna=True):
91
92
92
93
# a list of eTLDs with their modifiers, e.g., *
93
94
self .tlds = []
94
- root = self ._build_structure (psl , idna )
95
+ root = self ._build_structure (psl , idna , private )
95
96
self .root = self ._simplify (root )
96
97
97
98
def _find_node (self , parent , parts ):
@@ -161,7 +162,7 @@ def _simplify(self, node):
161
162
162
163
return (node [0 ], dict ((k , self ._simplify (v )) for (k , v ) in node [1 ].items ()))
163
164
164
- def _build_structure (self , fp , idna ):
165
+ def _build_structure (self , fp , idna , private ):
165
166
"""
166
167
Build a Trie from the public suffix list. If idna==True, idna-encode
167
168
each line before building.
@@ -180,6 +181,7 @@ def _build_structure(self, fp, idna):
180
181
181
182
:param fp: pointer for the public suffix list
182
183
:param idna: boolean, convert lines to idna-encoded strings
184
+ :param private: boolean, include non-ICANN private names, default=True
183
185
:return: Trie
184
186
"""
185
187
root = [0 ]
@@ -188,6 +190,8 @@ def _build_structure(self, fp, idna):
188
190
189
191
for line in fp :
190
192
line = line .strip ()
193
+ if not private and line .startswith ('// ===BEGIN PRIVATE' ):
194
+ break
191
195
if not line or line .startswith ('//' ):
192
196
continue
193
197
if idna :
0 commit comments