# Path: invtool/main.py
# def do_dispatch(args, IN=sys.stdin):
#     inv_parser = argparse.ArgumentParser(prog='invtool')
#     format_group = inv_parser.add_mutually_exclusive_group()
#     format_group.add_argument(
#         '--json', default=False, dest='p_json',  action='store_true',
#         help="Format the output as JSON"
#     )
#     format_group.add_argument(
#         '--silent', default=False, dest='p_silent', action='store_true',
#         help="Silence all stdout and stderr"
#     )
#     format_group.add_argument(
#         '--debug', default=False, dest='DEBUG', action='store_true',
#         help="Print stuff"
#     )
#     format_group.add_argument(
#         '--pk-only', default=False, dest='p_pk_only', action='store_true',
#         help="If an object was just update/created print the primary key"
#         "of that object otherwise print nothing. No new line is printed."
#     )
#     base_parser = inv_parser.add_subparsers(dest='dtype')
# 
#     # Build parsers. Parses should register arguments.
#     for d in registrar.dispatches:
#         d.build_parser(base_parser)
# 
#     nas = inv_parser.parse_args(args)
#     nas.IN = IN  # Where invtool reads its input from
#     if nas.p_pk_only:
#         nas.p_json = True
#     return nas, dispatch(nas)
# 
# Path: invtool/lib/config.py
# REMOTE = "http{0}://{1}{2}".format(
#     's' if dev != 'True' else '',
#     host,
#     '' if port == '80' else ':' + port
# )
# 
# def auth():
#     global authcache
#     if authcache is False:
#         authcache = _realauth()
# 
#     return authcache


import simplejson as json
import shlex
import io
import requests
from invtool.main import do_dispatch
from invtool.lib.config import REMOTE, auth
    """
    tmp_url = "/en-US/bulk_action/gather_vlan_pools/"
    url = "{0}{1}".format(REMOTE, tmp_url)
    headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
    data = {
        'vlan_name': vlan_name,
        'vlan_number': vlan_number,
        'site_name': site,
        'ip_type': ip_type
    }

    resp = requests.get(url, params=data, headers=headers, auth=auth())
    json_resp = json.loads(resp.content)
    if 'errors' in json_resp:
        return None, json_resp['errors']
    assert 'free_ranges' in json_resp, (
        "Something is broken. 'free_ranges' was not returen by Inventory"
    )
    return json_resp['free_ranges'], None


def ba_gather_range_pool(ip_range):
    resp, errors = ba_gather_ip_pool(ip_range)
    if errors:
        return resp, errors
    return resp['free_ranges'], None


def ba_gather_ip_pool(ip_range):
    command = '--json search --display-integers --range {0}'.format(ip_range)

# Path: invtool/main.py
# def do_dispatch(args, IN=sys.stdin):
#     inv_parser = argparse.ArgumentParser(prog='invtool')
#     format_group = inv_parser.add_mutually_exclusive_group()
#     format_group.add_argument(
#         '--json', default=False, dest='p_json',  action='store_true',
#         help="Format the output as JSON"
#     )
#     format_group.add_argument(
#         '--silent', default=False, dest='p_silent', action='store_true',
#         help="Silence all stdout and stderr"
#     )
#     format_group.add_argument(
#         '--debug', default=False, dest='DEBUG', action='store_true',
#         help="Print stuff"
#     )
#     format_group.add_argument(
#         '--pk-only', default=False, dest='p_pk_only', action='store_true',
#         help="If an object was just update/created print the primary key"
#         "of that object otherwise print nothing. No new line is printed."
#     )
#     base_parser = inv_parser.add_subparsers(dest='dtype')
# 
#     # Build parsers. Parses should register arguments.
#     for d in registrar.dispatches:
#         d.build_parser(base_parser)
# 
#     nas = inv_parser.parse_args(args)
#     nas.IN = IN  # Where invtool reads its input from
#     if nas.p_pk_only:
#         nas.p_json = True
#     return nas, dispatch(nas)
# 
# Path: invtool/lib/config.py
# REMOTE = "http{0}://{1}{2}".format(
#     's' if dev != 'True' else '',
#     host,
#     '' if port == '80' else ':' + port
# )
# 
# def auth():
#     global authcache
#     if authcache is False:
#         authcache = _realauth()
# 
#     return authcache


# Path: invtool/lib/ba.py
import simplejson as json
import shlex
import io
import requests
from invtool.main import do_dispatch
from invtool.lib.config import REMOTE, auth

    """
    tmp_url = "/en-US/bulk_action/gather_vlan_pools/"
    url = "{0}{1}".format(REMOTE, tmp_url)
    headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
    data = {
        'vlan_name': vlan_name,
        'vlan_number': vlan_number,
        'site_name': site,
        'ip_type': ip_type
    }

    resp = requests.get(url, params=data, headers=headers, auth=auth())
    json_resp = json.loads(resp.content)
    if 'errors' in json_resp:
        return None, json_resp['errors']
    assert 'free_ranges' in json_resp, (
        "Something is broken. 'free_ranges' was not returen by Inventory"
    )
    return json_resp['free_ranges'], None


def ba_gather_range_pool(ip_range):
    resp, errors = ba_gather_ip_pool(ip_range)
    if errors:
        return resp, errors
    return resp['free_ranges'], None


def ba_gather_ip_pool(ip_range):
    command = '--json search --display-integers --range {0}'.format(ip_range)

    nas, (resp_code, resp_list) = do_dispatch(shlex.split(command))