Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Add support for unnamed records #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 32 additions & 22 deletions blockstack_zones/parse_zone_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,29 +255,39 @@ def remove_class(text):
return "\n".join(ret)


def add_default_name(text):
"""
Go through each line of the text and ensure that
a name is defined. Use '@' if there is none.
"""
global SUPPORTED_RECORDS

lines = text.split("\n")
ret = []
for line in lines:
tokens = tokenize_line(line)
if len(tokens) == 0:
continue

if tokens[0] in SUPPORTED_RECORDS and not tokens[0].startswith("$"):
# add back the name
tokens = ['@'] + tokens

ret.append(serialize(tokens))

def add_record_names(text):
"""
Go through each line of the text and ensure that
a name is defined. Use previous record name if there is none.
"""
global SUPPORTED_RECORDS

lines = text.split("\n")
ret = []
previous_record_name = None

for line in lines:
tokens = tokenize_line(line)

if not tokens[0].startswith("$"):
is_int = False
try:
int(tokens[0])
is_int = True
except ValueError:
pass
has_name = not is_int and tokens[0] not in SUPPORTED_RECORDS

if has_name:
previous_record_name = tokens[0]
else:
# add back the name
tokens = [previous_record_name] + tokens

ret.append(serialize(tokens))

return "\n".join(ret)


def parse_line(parser, record_token, parsed_records):
"""
Given the parser, capitalized list of a line's tokens, and the current set of records
Expand Down Expand Up @@ -372,6 +382,6 @@ def parse_zone_file(text, ignore_invalid=False):
text = remove_comments(text)
text = flatten(text)
text = remove_class(text)
text = add_default_name(text)
text = add_record_names(text)
json_zone_file = parse_lines(text, ignore_invalid=ignore_invalid)
return json_zone_file
28 changes: 27 additions & 1 deletion test_sample_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,33 @@
ftp IN CNAME server1
mail IN CNAME server1
mail2 IN CNAME server2
www IN CNAME server2"""
www IN CNAME server2""",
"sample_4": """$TTL 172800
$ORIGIN myzone.com.

@ 3600 IN SOA dns1.example.com. hostmaster.example.com. (
1
3600
300
2419200
300
)

@ 172800 IN NS ns1-08.example-dns.com.
172800 IN NS ns2-08.example-dns.net.
172800 IN NS ns3-08.example-dns.org.
172800 IN NS ns4-08.example-dns.info.

arec 3600 IN A 10.0.0.10

arec2 0 IN A 10.0.1.0
0 IN A 10.0.1.1

txtrec 3600 IN TXT "mytxtvalue"
3600 IN TXT "mytxtvalue2"

txtrec2 3600 IN TXT "hi"
""",
}

zone_file_objects = {
Expand Down
14 changes: 14 additions & 0 deletions unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ def test_zone_file_parsing_3(self):
self.assertTrue("$ttl" in zone_file)
self.assertTrue("$origin" in zone_file)

def test_zone_file_parsing_4(self):
zone_file = parse_zone_file(zone_files["sample_4"])
#print json.dumps(zone_file, indent=2)
self.assertTrue(isinstance(zone_file, dict))
self.assertTrue("soa" in zone_file)
self.assertTrue("a" in zone_file)
self.assertTrue("$ttl" in zone_file)
self.assertTrue("$origin" in zone_file)

text_records = zone_file['txt']
self.assertTrue(len([r for r in text_records
if r['name'] == 'txtrec']) == 2,
'Unnamed records have correct name')

def test_main():
test_support.run_unittest(
ZoneFileTests
Expand Down