Add prefix using from-pool to NIPAP

def add_prefix_from_pool(arg, opts):
    """ Add prefix using from-pool to NIPAP
    """

    args = {}

    # sanity checking
    if 'from-pool' in opts:
        res = Pool.list({ 'name': opts['from-pool'] })
        if len(res) == 0:
            print("No pool named '%s' found." % opts['from-pool'], file=sys.stderr)
            sys.exit(1)

        args['from-pool'] = res[0]

    if 'family' not in opts:
        print("ERROR: You have to specify the address family.", file=sys.stderr)
        sys.exit(1)

    if opts['family'] == 'ipv4':
        afis = [4]
    elif opts['family'] == 'ipv6':
        afis = [6]
    elif opts['family'] == 'dual-stack':
        afis = [4, 6]
        if 'prefix_length' in opts:
            print("ERROR: 'prefix_length' can not be specified for 'dual-stack' assignment", file=sys.stderr)
            sys.exit(1)
    else:
        print("ERROR: 'family' must be one of: %s" % " ".join(valid_families), file=sys.stderr)
        sys.exit(1)

    if 'prefix_length' in opts:
        args['prefix_length'] = int(opts['prefix_length'])

    for afi in afis:
        p = _prefix_from_opts(opts)

        if opts.get('vrf_rt') is None:
            # if no VRF is specified use the pools implied VRF
            p.vrf = args['from-pool'].vrf
        else:
            # use the specified VRF
            p.vrf = get_vrf(opts.get('vrf_rt'), abort=True)

        # set type to default type of pool unless already set
        if p.type is None:
            if args['from-pool'].default_type is None:
                print("ERROR: Type not specified and no default-type specified for pool: %s" % opts['from-pool'], file=sys.stderr)
            p.type = args['from-pool'].default_type

        for avp in opts.get('extra-attribute', []):
            try:
                key, value = avp.split('=', 1)
            except ValueError:
                print("ERROR: Incorrect extra-attribute: %s. Accepted form: 'key=value'\n" % avp, file=sys.stderr)
                return
            p.avps[key] = value


        args['family'] = afi

        try:
            p.save(args)
        except NipapError as exc:
            print("Could not add prefix to NIPAP: %s" % str(exc), file=sys.stderr)
            sys.exit(1)

        if p.type == 'host':
            print("Host %s added to %s: %s" % (p.display_prefix,
                    vrf_format(p.vrf), p.node or p.description))
        else:
            print("Network %s added to %s: %s" % (p.display_prefix,
                    vrf_format(p.vrf), p.description))

        if opts.get('add-hosts') is not None:
            if p.type != 'assignment':
                print("ERROR: Not possible to add hosts to non-assignment", file=sys.stderr)
                sys.exit(1)

            for host in opts.get('add-hosts').split(','):
                h_opts = {
                        'from-prefix': p.prefix,
                        'vrf_rt': p.vrf.rt,
                        'type': 'host',
                        'node': host
                        }
                add_prefix({}, h_opts, {})