Skip to content

Commit fd7cd4d

Browse files
author
Scott Kitterman
committed
Add option to exclude private PSL names and associated test
Signed-off-by: Scott Kitterman <[email protected]>
1 parent 4b2a771 commit fd7cd4d

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/publicsuffix2/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262

6363
class PublicSuffixList(object):
6464

65-
def __init__(self, psl_file=None, idna=True):
65+
def __init__(self, psl_file=None, idna=True, private=True):
6666
"""
6767
Read and parse a public suffix list. `psl_file` is either a file
6868
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):
8080
8181
:param psl_file: string or None
8282
:param idna: boolean, whether to convert file to IDNA-encoded strings
83+
:param private: boolean, include non-ICANN private names, default=True
8384
"""
8485
# Note: we test for None as we accept empty lists as inputs
8586
if psl_file is None or isinstance(psl_file, str):
@@ -91,7 +92,7 @@ def __init__(self, psl_file=None, idna=True):
9192

9293
# a list of eTLDs with their modifiers, e.g., *
9394
self.tlds = []
94-
root = self._build_structure(psl, idna)
95+
root = self._build_structure(psl, idna, private)
9596
self.root = self._simplify(root)
9697

9798
def _find_node(self, parent, parts):
@@ -161,7 +162,7 @@ def _simplify(self, node):
161162

162163
return (node[0], dict((k, self._simplify(v)) for (k, v) in node[1].items()))
163164

164-
def _build_structure(self, fp, idna):
165+
def _build_structure(self, fp, idna, private):
165166
"""
166167
Build a Trie from the public suffix list. If idna==True, idna-encode
167168
each line before building.
@@ -180,6 +181,7 @@ def _build_structure(self, fp, idna):
180181
181182
:param fp: pointer for the public suffix list
182183
:param idna: boolean, convert lines to idna-encoded strings
184+
:param private: boolean, include non-ICANN private names, default=True
183185
:return: Trie
184186
"""
185187
root = [0]
@@ -188,6 +190,8 @@ def _build_structure(self, fp, idna):
188190

189191
for line in fp:
190192
line = line.strip()
193+
if not private and line.startswith('// ===BEGIN PRIVATE'):
194+
break
191195
if not line or line.startswith('//'):
192196
continue
193197
if idna:

tests.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ def test_PublicSuffixList_tlds_is_loaded_correctly(self):
256256
psl = publicsuffix.PublicSuffixList()
257257
assert psl.tlds
258258

259+
def test_get_tld_no_private(self):
260+
psl = publicsuffix.PublicSuffixList(private=False)
261+
# Without private, ap-northeast-1.elasticbeanstalk.com is com
262+
assert 'com' == psl.get_tld('ap-northeast-1.elasticbeanstalk.com')
259263

260264
class TestPublicSuffixGetSld(unittest.TestCase):
261265

0 commit comments

Comments
 (0)