#!/usr/bin/env python
# vim: et :

import logging
import re
import sys
import time

sys.path.append('../../nipap/')
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

log_format = "%(levelname)-8s %(message)s"
log_stream = logging.StreamHandler()
log_stream.setFormatter(logging.Formatter("%(asctime)s: " + log_format))
log_stream.setLevel(logging.WARNING)
logger.addHandler(log_stream)


import nipap.backend

class bonk:
    def __init__(self):
        self.n = nipap.backend.Nipap()

        #self.n.remove_prefix({ 'name': 'test-schema' }, { 'prefix': '2.0.0.0/8' })
        #self.n.add_prefix({'name': 'test-schema' }, { 'prefix': '2.0.0.0/8', 'description': 'test' })



    def clear_db(self):
        """ Clear out everything in the database
        """
        self.n._execute("TRUNCATE ip_net_plan CASCADE")



    def init_db(self):
        """ Initialise a few things we need in the db, a schema, a pool, a truck
        """

    def find_prefix(self, argp):
        """
        """
        arg_prefix = argp.split("/")[0]
        arg_pl = argp.split("/")[1]
        if self.n._is_ipv4(arg_prefix):
            m = re.match("([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)", arg_prefix)
            os1 = int(m.group(1))
            os2 = int(m.group(2))
            os3 = int(m.group(3))
            os4 = int(m.group(4))
            count = 2**(32-int(arg_pl))
            i = 0
            t0 = time.time()
            try:
                for o1 in xrange(os1, 255):
                    for o2 in xrange(os2, 255):
                        for o3 in xrange(os3, 255):
                            t2 = time.time()