# Path: invtool/lib/registrar.py
# class Registrar():
#     def register(self, dispatch):
# 
# Path: invtool/dispatch.py
# def dispatch(nas):
#     for dispatch in registrar.dispatches:
#         if dispatch.dtype.lower() == nas.dtype.lower():
#             return dispatch.route(nas)


import argparse
import simplejson as json
import sys
from invtool.lib.registrar import registrar
from invtool.dispatch import dispatch
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

# Path: invtool/lib/registrar.py
# class Registrar():
#     def register(self, dispatch):
# 
# Path: invtool/dispatch.py
# def dispatch(nas):
#     for dispatch in registrar.dispatches:
#         if dispatch.dtype.lower() == nas.dtype.lower():
#             return dispatch.route(nas)


# Path: invtool/main.py
import argparse
import simplejson as json
import sys
from invtool.lib.registrar import registrar
from invtool.dispatch import dispatch

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)