# coding: utf-8

import sys
import argparse

import netaddr

import lglass.generators.dns
import lglass.database.file

def build_argparser():
	argparser = argparse.ArgumentParser(description="Generator for delegating zones")
	argparser.add_argument("--database", "--db", "-d", help="Whois database",
			type=str, default=".")
	argparser.add_argument("--nameserver", "-n", dest="nameservers", action="append",
			help="Nameserver")
	argparser.add_argument("--master", "-m", help="Master nameserver")
	argparser.add_argument("--email", "-e", help="Email address for SOA",
			required=True)
	argparser.add_argument("--ttl", "-t", help="Time to live for generated zone",
			type=int, default=3600)

	subparsers = argparser.add_subparsers(dest='type')

	argparser_dns = subparsers.add_parser('dns')
	argparser_rdns4 = subparsers.add_parser('rdns4')
	argparser_rdns6 = subparsers.add_parser('rdns6')

	argparser_dns.add_argument('--zone', '-z', help="DNS Zone", required=True)
	argparser_rdns4.add_argument("--network", "-N", help="IPv4 Network")
	argparser_rdns6.add_argument("--network", "-N", help="IPv6 Network")

	return argparser

def main(argv=sys.argv[1:]):
	argparser = build_argparser()
	args = argparser.parse_args(argv)

	db = lglass.database.file.FileDatabase(args.database)
	master_nameserver = args.master
	if master_nameserver is None:
		if args.nameservers:
			master_nameserver = args.nameservers[0]
		else:
			master_nameserver = args.zone
	args.master = master_nameserver

	if args.type == "dns":
		return main_dns(args, db)
	elif args.type == "rdns4":
		return main_rdns4(args, db)
	elif args.type == "rdns6":
		return main_rdns6(args, db)

def main_dns(args, db):
	domains = (db.get(*spec) for spec in db.list()
		if (spec[0] == "dns" or spec[0] == "domain")
		and spec[1].endswith("." + args.zone))

	soa = lglass.generators.dns.generate_soa(args.zone,
		args.master,
		args.email)

	zone = ["$TTL {time}".format(time=args.ttl)]

	zone.extend(lglass.generators.dns.generate_zone(
		args.zone,
		domains,
		soa=soa,
		nameservers=args.nameservers
	))

	print("\n".join(map(str, zone)))

def main_rdns4(args, db):
	inetnums = (db.get(*spec) for spec in db.list() if spec[0] == "inetnum")

	network = netaddr.IPNetwork(args.network)
	zone = lglass.generators.dns.rdns_domain(network)

	soa = lglass.generators.dns.generate_soa(zone, args.master, args.email)

	zone = ["$TTL {time}".format(time=args.ttl)]

	zone.extend(lglass.generators.dns.generate_rdns4_zone(
		network=network,
		inetnums=inetnums,
		soa=soa,
		nameservers=args.nameservers
	))

	print("\n".join(map(str, zone)))

def main_rdns6(args, db):
	inet6nums = (db.get(*spec) for spec in db.list() if spec[0] == "inet6num")

	network = netaddr.IPNetwork(args.network)
	zone = lglass.generators.dns.rdns_domain(network)

	soa = lglass.generators.dns.generate_soa(zone, args.master, args.email)

	zone = ["$TTL {time}".format(time=args.ttl)]

	zone.extend(lglass.generators.dns.generate_rdns6_zone(
		network,
		inet6nums,
		soa=soa,
		nameservers=args.nameservers
	))

	print("\n".join(map(str, zone)))

if __name__ == "__main__":
	main()


@@ -83,16 +83,39 @@
 ors.dns%0A
+import lglass.registry%0A
 import l
@@ -272,30 +272,22 @@
 (%22--
-database%22, %22--db
+registry
 %22, %22-
-d
+r
 %22, h
@@ -295,28 +295,24 @@
 lp=%22
-Whois database%22,%0A%09%09%09
+Registry path%22, 
 type
@@ -316,17 +316,20 @@
 ype=str,
- 
+%0A%09%09%09
 default=
@@ -1235,18 +1235,19 @@
 argv)%0A%0A%09
-db
+reg
  = lglas
@@ -1252,48 +1252,43 @@
 ass.
-database.file.FileDatabase(args.database
+registry.FileRegistry(args.registry
 )%0A%09m
@@ -1534,26 +1534,27 @@
 n_dns(args, 
-db
+reg
 )%0A%09elif args
@@ -1597,18 +1597,19 @@
 4(args, 
-db
+reg
 )%0A%09elif 
@@ -1656,18 +1656,19 @@
 6(args, 
-db
+reg
 )%0A%0Adef m
@@ -1677,26 +1677,27 @@
 n_dns(args, 
-db
+reg
 ):%0A%09domains 
@@ -1695,26 +1695,27 @@
 %09domains = (
-db
+reg
 .get(*spec) 
@@ -1722,26 +1722,27 @@
 for spec in 
-db
+reg
 .list()%0A%09%09if
@@ -2129,26 +2129,27 @@
 rdns4(args, 
-db
+reg
 ):%0A%09inetnums
@@ -2144,34 +2144,35 @@
 ):%0A%09inetnums = (
-db
+reg
 .get(*spec) for 
@@ -2171,34 +2171,35 @@
 ec) for spec in 
-db
+reg
 .list() if spec%5B
@@ -2631,18 +2631,19 @@
 6(args, 
-db
+reg
 ):%0A%09inet
@@ -2651,18 +2651,19 @@
 nums = (
-db
+reg
 .get(*sp
@@ -2682,10 +2682,11 @@
  in 
-db
+reg
 .lis

Use new registry structure for database access