# Path: courseraresearchexports/containers/utils.py
# def extract_zip_archive(archive, dest, delete_archive=True):
# def create_tar_archive(str, name='init-user-db.sh'):
# def get_next_available_port(containers_info):
# def is_container_running(container_name, docker_client):
# def docker_client_arg_parser():
# def docker_client(docker_url=None, timeout=60):


import logging
import courseraresearchexports.db.db as db
from tabulate import tabulate
from courseraresearchexports.containers import utils

def list_views(args):
    """
    List all of the views present in a dockerized database.
    """
    d = utils.docker_client(args.docker_url, args.timeout)
    tables = db.get_view_names(args.container_name, docker_client=d)
    print(tabulate([[table] for table in tables]))


def create_view(args):
    """
    Create a view from a sql query.
    """
    d = utils.docker_client(args.docker_url, args.timeout)

    if args.view_name:
        created_view = db.create_registered_view(
            args.container_name, args.view_name, d)
    elif args.sql_file:
        created_view = db.create_view_from_file(
            args.container_name, args.sql_file, d)

    logging.info('Created view {}'.format(created_view))


def unload_relation(args):
    """
    Unload a table or view to a CSV file.
    """

# Path: courseraresearchexports/containers/utils.py
# def extract_zip_archive(archive, dest, delete_archive=True):
# def create_tar_archive(str, name='init-user-db.sh'):
# def get_next_available_port(containers_info):
# def is_container_running(container_name, docker_client):
# def docker_client_arg_parser():
# def docker_client(docker_url=None, timeout=60):


# Path: courseraresearchexports/commands/db.py
import logging
import courseraresearchexports.db.db as db
from tabulate import tabulate
from courseraresearchexports.containers import utils


def list_views(args):
    """
    List all of the views present in a dockerized database.
    """
    d = utils.docker_client(args.docker_url, args.timeout)
    tables = db.get_view_names(args.container_name, docker_client=d)
    print(tabulate([[table] for table in tables]))


def create_view(args):
    """
    Create a view from a sql query.
    """
    d = utils.docker_client(args.docker_url, args.timeout)

    if args.view_name:
        created_view = db.create_registered_view(
            args.container_name, args.view_name, d)
    elif args.sql_file:
        created_view = db.create_view_from_file(
            args.container_name, args.sql_file, d)

    logging.info('Created view {}'.format(created_view))


def unload_relation(args):
    """
    Unload a table or view to a CSV file.
    """

    d = utils.docker_client(args.docker_url, args.timeout)