# Path: kamcli/ioutils.py
# def ioutils_dbres_print(ctx, oformat, ostyle, res):
#     """ print a database result using different formats and styles """
#     if oformat is None:
#         oformat = ctx.gconfig.get("db", "outformat", fallback=None)
#         if oformat is None:
#             if ioutils_tabulate_format is True:
#                 oformat = "table"
#             else:
#                 oformat = "json"
# 
#     if oformat == "table":
#         if ioutils_tabulate_format is False:
#             ctx.log("Package tabulate is not installed")
#             sys.exit()
# 
#     if oformat == "yaml":
#         if ioutils_yaml_format is False:
#             ctx.log("Package yaml is not installed")
#             sys.exit()
# 
#     if ostyle is None:
#         ostyle = ctx.gconfig.get("db", "outstyle", fallback="grid")
#         if ostyle is None:
#             ostyle = "grid"
# 
#     if oformat == "json":
#         jdata = []
#         for row in res:
#             jdata.append(dict(row))
#         print(json.dumps(jdata, indent=4))
#         print()
#     elif oformat == "yaml":
#         ydata = []
#         for row in res:
#             ydata.append(dict(row))
#         print(yaml.dump(ydata, indent=4))
#         print()
#     elif oformat == "dict":
#         for row in res:
#             print(dict(row))
#             # pprint.pprint(dict(row), indent=4)
#             print()
#     elif oformat == "table":
#         arows = res.fetchall()
#         dcols = dict((k, k) for k in res.keys())
#         drows = [dict(r) for r in arows]
#         gstring = tabulate(drows, headers=dcols, tablefmt=ostyle)
#         print(gstring)
#     else:
#         allrows = res.fetchall()
#         print(allrows)
# 
# Path: kamcli/cli.py
# COMMAND_ALIASES = {
#     "subs": "subscriber",
#     "rpc": "jsonrpc",
#     "sht": "htable",
# }
# CONTEXT_SETTINGS = dict(
#     auto_envvar_prefix="KAMCLI", help_option_names=["-h", "--help"]
# )
# def read_global_config(config_paths):
# def parse_user_spec(ctx, ustr):
#     def __init__(self):
#     def log(self, msg, *args):
#     def vlog(self, msg, *args):
#     def printf(self, msg, *args):
#     def printnlf(self, msg, *args):
#     def read_config(self):
#     def gconfig(self):
#     def list_commands(self, ctx):
#     def get_command(self, ctx, name):
# def cli(ctx, debug, config, wdir, nodefaultconfigs, oformat):
# class Context(object):
# class KamCLI(click.MultiCommand):
# 
# Path: kamcli/iorpc.py
# def command_ctl(ctx, cmd, params=[], cbexec={}):
#     """Execute a rpc control command
# 
#     \b
#     Parameters:
#       - ctx: kamcli execution context
#       - cmd: the string with rpc control command
#       - params: an array with the parameters for the rpc control command
#       - cbexec: dictionary with callaback function and its parameters
#                 to handle the response of the rpc control commands. If not
#                 provided, the response will be printed with the function
#                 command_ctl_response_print().
#                 The callback function has to be provided by 'func' key in
#                 the dictionary and its parameters by 'params' key.
#     """
# 
#     if ctx.gconfig.get("jsonrpc", "transport") == "socket":
#         command_jsonrpc_socket(
#             ctx,
#             False,
#             ctx.gconfig.get("jsonrpc", "srvaddr"),
#             ctx.gconfig.get("jsonrpc", "rcvaddr"),
#             ctx.gconfig.get("jsonrpc", "outformat"),
#             command_ctl_name(cmd, "rpc"),
#             params,
#             cbexec,
#         )
#     else:
#         command_jsonrpc_fifo(
#             ctx,
#             False,
#             ctx.gconfig.get("jsonrpc", "path"),
#             ctx.gconfig.get("jsonrpc", "rplnamebase"),
#             ctx.gconfig.get("jsonrpc", "outformat"),
#             command_ctl_name(cmd, "rpc"),
#             params,
#             cbexec,
#         )


import click
from sqlalchemy import create_engine
from kamcli.ioutils import ioutils_dbres_print
from kamcli.cli import pass_context
from kamcli.iorpc import command_ctl
    "oformat",
    "--output-format",
    "-F",
    type=click.Choice(["raw", "json", "table", "dict"]),
    default=None,
    help="Format the output",
)
@click.option(
    "ostyle",
    "--output-style",
    "-S",
    default=None,
    help="Style of the output (tabulate table format)",
)
@click.argument("group", nargs=-1, metavar="[<group>]", type=int)
@pass_context
def address_showdb(ctx, oformat, ostyle, group):
    """Show details for records in address db table

    \b
    Parameters:
        <group> - address group
    """
    e = create_engine(ctx.gconfig.get("db", "rwurl"))
    if not group:
        ctx.vlog("Showing all address records")
        res = e.execute("select * from address")
    else:
        ctx.vlog("Showing address records for group")
        res = e.execute("select * from address where group={0}".format(group))

# Path: kamcli/ioutils.py
# def ioutils_dbres_print(ctx, oformat, ostyle, res):
#     """ print a database result using different formats and styles """
#     if oformat is None:
#         oformat = ctx.gconfig.get("db", "outformat", fallback=None)
#         if oformat is None:
#             if ioutils_tabulate_format is True:
#                 oformat = "table"
#             else:
#                 oformat = "json"
# 
#     if oformat == "table":
#         if ioutils_tabulate_format is False:
#             ctx.log("Package tabulate is not installed")
#             sys.exit()
# 
#     if oformat == "yaml":
#         if ioutils_yaml_format is False:
#             ctx.log("Package yaml is not installed")
#             sys.exit()
# 
#     if ostyle is None:
#         ostyle = ctx.gconfig.get("db", "outstyle", fallback="grid")
#         if ostyle is None:
#             ostyle = "grid"
# 
#     if oformat == "json":
#         jdata = []
#         for row in res:
#             jdata.append(dict(row))
#         print(json.dumps(jdata, indent=4))
#         print()
#     elif oformat == "yaml":
#         ydata = []
#         for row in res:
#             ydata.append(dict(row))
#         print(yaml.dump(ydata, indent=4))
#         print()
#     elif oformat == "dict":
#         for row in res:
#             print(dict(row))
#             # pprint.pprint(dict(row), indent=4)
#             print()
#     elif oformat == "table":
#         arows = res.fetchall()
#         dcols = dict((k, k) for k in res.keys())
#         drows = [dict(r) for r in arows]
#         gstring = tabulate(drows, headers=dcols, tablefmt=ostyle)
#         print(gstring)
#     else:
#         allrows = res.fetchall()
#         print(allrows)
# 
# Path: kamcli/cli.py
# COMMAND_ALIASES = {
#     "subs": "subscriber",
#     "rpc": "jsonrpc",
#     "sht": "htable",
# }
# CONTEXT_SETTINGS = dict(
#     auto_envvar_prefix="KAMCLI", help_option_names=["-h", "--help"]
# )
# def read_global_config(config_paths):
# def parse_user_spec(ctx, ustr):
#     def __init__(self):
#     def log(self, msg, *args):
#     def vlog(self, msg, *args):
#     def printf(self, msg, *args):
#     def printnlf(self, msg, *args):
#     def read_config(self):
#     def gconfig(self):
#     def list_commands(self, ctx):
#     def get_command(self, ctx, name):
# def cli(ctx, debug, config, wdir, nodefaultconfigs, oformat):
# class Context(object):
# class KamCLI(click.MultiCommand):
# 
# Path: kamcli/iorpc.py
# def command_ctl(ctx, cmd, params=[], cbexec={}):
#     """Execute a rpc control command
# 
#     \b
#     Parameters:
#       - ctx: kamcli execution context
#       - cmd: the string with rpc control command
#       - params: an array with the parameters for the rpc control command
#       - cbexec: dictionary with callaback function and its parameters
#                 to handle the response of the rpc control commands. If not
#                 provided, the response will be printed with the function
#                 command_ctl_response_print().
#                 The callback function has to be provided by 'func' key in
#                 the dictionary and its parameters by 'params' key.
#     """
# 
#     if ctx.gconfig.get("jsonrpc", "transport") == "socket":
#         command_jsonrpc_socket(
#             ctx,
#             False,
#             ctx.gconfig.get("jsonrpc", "srvaddr"),
#             ctx.gconfig.get("jsonrpc", "rcvaddr"),
#             ctx.gconfig.get("jsonrpc", "outformat"),
#             command_ctl_name(cmd, "rpc"),
#             params,
#             cbexec,
#         )
#     else:
#         command_jsonrpc_fifo(
#             ctx,
#             False,
#             ctx.gconfig.get("jsonrpc", "path"),
#             ctx.gconfig.get("jsonrpc", "rplnamebase"),
#             ctx.gconfig.get("jsonrpc", "outformat"),
#             command_ctl_name(cmd, "rpc"),
#             params,
#             cbexec,
#         )


# Path: kamcli/commands/cmd_address.py
import click
from sqlalchemy import create_engine
from kamcli.ioutils import ioutils_dbres_print
from kamcli.cli import pass_context
from kamcli.iorpc import command_ctl

    "oformat",
    "--output-format",
    "-F",
    type=click.Choice(["raw", "json", "table", "dict"]),
    default=None,
    help="Format the output",
)
@click.option(
    "ostyle",
    "--output-style",
    "-S",
    default=None,
    help="Style of the output (tabulate table format)",
)
@click.argument("group", nargs=-1, metavar="[<group>]", type=int)
@pass_context
def address_showdb(ctx, oformat, ostyle, group):
    """Show details for records in address db table

    \b
    Parameters:
        <group> - address group
    """
    e = create_engine(ctx.gconfig.get("db", "rwurl"))
    if not group:
        ctx.vlog("Showing all address records")
        res = e.execute("select * from address")
    else:
        ctx.vlog("Showing address records for group")
        res = e.execute("select * from address where group={0}".format(group))

    ioutils_dbres_print(ctx, oformat, ostyle, res)