<commit_before># coding: utf-8

# XXX This code is so horrible, don't read it

import lglass.rpsl
import netaddr
import time
import traceback

def rdns_domain(network):
	if network.version == 4:
		return ".".join(map(str, reversed(network.ip.words[:network.prefixlen // 8]))) + ".in-addr.arpa"
	elif network.version == 6:
		return ".".join(map(str, reversed(list("".join(hex(n)[2:].rjust(4, "0") for n in network.ip.words))[:network.prefixlen // 4]))) + ".ip6.arpa"

def comment(txt):
	return "; {}".format(str(txt))

def delegation(domain, nameserver):
	nameserver, *trash = nameserver.split()
	return "{domain}. IN NS {nserver}.".format(
		domain=domain,
		nserver=nameserver)

def rdns_delegation(network, nameserver):
	return delegation(rdns_domain(network), nameserver)

rdns4_delegation = rdns_delegation
rdns6_delegation = rdns_delegation

def glue(domain, addr):
	if addr.version == 4:
		return "{ns}. IN A {glue}".format(ns=domain, glue=str(addr))
	elif addr.version == 6:
		return "{ns}. IN AAAA {glue}".format(ns=domain, glue=str(addr))

def generate_delegation(dns, with_glue=True):
	""" Generate a valid DNS delegation to the given dns object. This function
	may generate glue records if with_glue == True. """
	for _, nserver in dns.get("nserver"):
		ns, *glues = nserver.split()
		yield delegation(dns.primary_key, ns)
		if with_glue is False:
			glues = []
		for _glue in glues:
			if not ns.endswith(dns.primary_key):
				continue
			try:
				_glue = netaddr.IPAddress(_glue)
				yield glue(ns, _glue)
			except:
				pass<commit_msg>Also support format used by old zonefile generators for nserver entries<commit_after># coding: utf-8

# XXX This code is so horrible, don't read it

import lglass.rpsl
import netaddr
import time
import traceback

def rdns_domain(network):
	if network.version == 4:
		return ".".join(map(str, reversed(network.ip.words[:network.prefixlen // 8]))) + ".in-addr.arpa"
	elif network.version == 6:
		return ".".join(map(str, reversed(list("".join(hex(n)[2:].rjust(4, "0") for n in network.ip.words))[:network.prefixlen // 4]))) + ".ip6.arpa"

def comment(txt):
	return "; {}".format(str(txt))

def delegation(domain, nameserver):
	nameserver, *trash = nameserver.split()
	return "{domain}. IN NS {nserver}.".format(
		domain=domain,
		nserver=nameserver)

def rdns_delegation(network, nameserver):
	return delegation(rdns_domain(network), nameserver)

rdns4_delegation = rdns_delegation
rdns6_delegation = rdns_delegation

def glue(domain, addr):
	if addr.version == 4:
		return "{ns}. IN A {glue}".format(ns=domain, glue=str(addr))
	elif addr.version == 6:
		return "{ns}. IN AAAA {glue}".format(ns=domain, glue=str(addr))

def generate_delegation(dns, with_glue=True):
	""" Generate a valid DNS delegation to the given dns object. This function
	may generate glue records if with_glue == True. """
	seen_servers = set()
	for _, nserver in dns.get("nserver"):
		ns, *glues = nserver.split()
		if ns not in seen_servers:
			seen_servers.add(ns)
			yield delegation(dns.primary_key, ns)
		if with_glue is False:
			glues = []
		for _glue in glues:
			if not ns.endswith(dns.primary_key):
				continue
			try:
				_glue = netaddr.IPAddress(_glue)
				yield glue(ns, _glue)
			except:
				pass
	if with_glue is True:
		for _, glueval in dns.get("glue"):
			domain, *glues = glueval.split()
			if not domain.endswith(dns.primary_key):
				continue
			for _glue in glues:
				try:
					_glue = netaddr.IPAddress(_glue)
					yield glue(ns, _glue)
				except:
					pass
{'fraction_non_alphanumeric': 0.14090131203650885, 'fraction_numerical': 0.007415858528237307, 'mean_word_length': 4.362385321100917, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6684722', 'n_tokens_mistral': 1418, 'n_tokens_neox': 1266, 'n_words': 332}