# 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/cli.py
# def parse_user_spec(ctx, ustr):
#     """Get details of the user from ustr (username, aor or sip uri)"""
#     udata = {}
#     if ":" in ustr:
#         uaor = ustr.split(":")[1]
#     else:
#         uaor = ustr
#     if "@" in uaor:
#         udata["username"] = uaor.split("@")[0]
#         udata["domain"] = uaor.split("@")[1]
#     else:
#         udata["username"] = uaor.split("@")[0]
#         try:
#             udata["domain"] = ctx.gconfig.get("main", "domain")
#         except configparser.NoOptionError:
#             ctx.log("Default domain not set in config file")
#             sys.exit()
#     if udata["username"] is None:
#         ctx.log("Failed to get username")
#         sys.exit()
#     if udata["domain"] is None:
#         ctx.log("Failed to get domain")
#         sys.exit()
#     udata["username"] = udata["username"].encode("ascii", "ignore").decode()
#     udata["domain"] = udata["domain"].encode("ascii", "ignore").decode()
#     return udata


import click
from sqlalchemy import create_engine
from kamcli.ioutils import ioutils_dbres_print
from kamcli.cli import pass_context
from kamcli.cli import parse_user_spec
    default=None,
    help="Style of the output (tabulate table format)",
)
@click.option(
    "table",
    "--table",
    default="dbaliases",
    help="Name of database table (default: dbaliases)",
)
@click.option(
    "matchalias",
    "--match-alias",
    is_flag=True,
    help="Match userid value as alias",
)
@click.argument("userid", nargs=-1, metavar="[<userid>]")
@pass_context
def aliasdb_show(ctx, oformat, ostyle, table, matchalias, userid):
    """Show details for user aliases

    \b
    Parameters:
        [<userid>] - username, AoR or SIP URI for user or alias
                   - it can be a list of userids
                   - if not provided then all aliases are shown
    """
    if not userid:
        ctx.vlog("Showing all records")
        e = create_engine(ctx.gconfig.get("db", "rwurl"))
        res = e.execute("select * from {0}".format(table))

# 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/cli.py
# def parse_user_spec(ctx, ustr):
#     """Get details of the user from ustr (username, aor or sip uri)"""
#     udata = {}
#     if ":" in ustr:
#         uaor = ustr.split(":")[1]
#     else:
#         uaor = ustr
#     if "@" in uaor:
#         udata["username"] = uaor.split("@")[0]
#         udata["domain"] = uaor.split("@")[1]
#     else:
#         udata["username"] = uaor.split("@")[0]
#         try:
#             udata["domain"] = ctx.gconfig.get("main", "domain")
#         except configparser.NoOptionError:
#             ctx.log("Default domain not set in config file")
#             sys.exit()
#     if udata["username"] is None:
#         ctx.log("Failed to get username")
#         sys.exit()
#     if udata["domain"] is None:
#         ctx.log("Failed to get domain")
#         sys.exit()
#     udata["username"] = udata["username"].encode("ascii", "ignore").decode()
#     udata["domain"] = udata["domain"].encode("ascii", "ignore").decode()
#     return udata


# Path: kamcli/commands/cmd_aliasdb.py
import click
from sqlalchemy import create_engine
from kamcli.ioutils import ioutils_dbres_print
from kamcli.cli import pass_context
from kamcli.cli import parse_user_spec

    default=None,
    help="Style of the output (tabulate table format)",
)
@click.option(
    "table",
    "--table",
    default="dbaliases",
    help="Name of database table (default: dbaliases)",
)
@click.option(
    "matchalias",
    "--match-alias",
    is_flag=True,
    help="Match userid value as alias",
)
@click.argument("userid", nargs=-1, metavar="[<userid>]")
@pass_context
def aliasdb_show(ctx, oformat, ostyle, table, matchalias, userid):
    """Show details for user aliases

    \b
    Parameters:
        [<userid>] - username, AoR or SIP URI for user or alias
                   - it can be a list of userids
                   - if not provided then all aliases are shown
    """
    if not userid:
        ctx.vlog("Showing all records")
        e = create_engine(ctx.gconfig.get("db", "rwurl"))
        res = e.execute("select * from {0}".format(table))

        ioutils_dbres_print(ctx, oformat, ostyle, res)