# Path: src/myaas/settings.py
# HOSTNAME = config('MYAAS_HOSTNAME', default='localhost')
# 
# DEBUG = config('MYAAS_DEBUG', default=False, cast=bool)
# 
# CONTAINER_TTL = config('MYAAS_CONTAINER_TTL', cast=int, default=86400)
# 
# Path: src/myaas/utils/container.py
# def find_container(name):
# def list_containers(all=True):
# def translate_host_basedir(path):
# def get_random_cpuset(cores_to_assign):
# def get_mapped_cpuset():
# 
# Path: src/myaas/utils/database.py
# def get_myaas_containers():
#     return filter(_is_database_container, list_containers())
# 
# def get_enabled_backend():
#     return importlib.import_module(settings.BACKEND)
# 
# def list_databases():
#     containers = filter(_is_database_container, list_containers())
#     return [_get_database_name(c) for c in containers]
# 
# def list_database_templates():
#     containers = filter(_is_template_container, list_containers())
#     return [_get_database_name(c) for c in containers]
# 
# Path: src/myaas/backends/exceptions.py
# class NonExistentDatabase(Exception):
#     pass
# 
# class NonExistentTemplate(NonExistentDatabase):
#     pass
# 
# class ImportInProgress(Exception):
#     pass


import logging
import os
from flask import Flask, Response, request, jsonify, abort
from .settings import HOSTNAME, DEBUG, CONTAINER_TTL
from .utils.container import client
from .utils.database import (get_myaas_containers, get_enabled_backend,
                             list_databases, list_database_templates)
from .backends.exceptions import (NonExistentDatabase, NonExistentTemplate,
                                  ImportInProgress)
        databases=list_databases())


@app.route('/db', methods=['get'])
def show_databases():
    databases = []
    for c in get_myaas_containers():
        db = {
            'template': c['Labels']['com.myaas.template'],
            'name': c['Labels']['com.myaas.instance'],
            'state': c['Status'],
            'created': c['Created'],
        }
        if 'com.myaas.expiresAt' in c['Labels']:
            db.update({'expires_at': c['Labels']['com.myaas.expiresAt']})
        databases.append(db)

    return jsonify(databases=databases)


@app.route('/templates', methods=['get'])
def show_templates():
    return jsonify(templates=list_database_templates())


@app.route('/db/<template>/<name>', methods=['get'])
def inspect_database(template, name):
    logger.debug(f'requested inspect DB for: "{template}" => "{name}"')
    database = get_enabled_backend().Database
    try:

# Path: src/myaas/settings.py
# HOSTNAME = config('MYAAS_HOSTNAME', default='localhost')
# 
# DEBUG = config('MYAAS_DEBUG', default=False, cast=bool)
# 
# CONTAINER_TTL = config('MYAAS_CONTAINER_TTL', cast=int, default=86400)
# 
# Path: src/myaas/utils/container.py
# def find_container(name):
# def list_containers(all=True):
# def translate_host_basedir(path):
# def get_random_cpuset(cores_to_assign):
# def get_mapped_cpuset():
# 
# Path: src/myaas/utils/database.py
# def get_myaas_containers():
#     return filter(_is_database_container, list_containers())
# 
# def get_enabled_backend():
#     return importlib.import_module(settings.BACKEND)
# 
# def list_databases():
#     containers = filter(_is_database_container, list_containers())
#     return [_get_database_name(c) for c in containers]
# 
# def list_database_templates():
#     containers = filter(_is_template_container, list_containers())
#     return [_get_database_name(c) for c in containers]
# 
# Path: src/myaas/backends/exceptions.py
# class NonExistentDatabase(Exception):
#     pass
# 
# class NonExistentTemplate(NonExistentDatabase):
#     pass
# 
# class ImportInProgress(Exception):
#     pass


# Path: src/myaas/server.py
import logging
import os
from flask import Flask, Response, request, jsonify, abort
from .settings import HOSTNAME, DEBUG, CONTAINER_TTL
from .utils.container import client
from .utils.database import (get_myaas_containers, get_enabled_backend,
                             list_databases, list_database_templates)
from .backends.exceptions import (NonExistentDatabase, NonExistentTemplate,
                                  ImportInProgress)

        databases=list_databases())


@app.route('/db', methods=['get'])
def show_databases():
    databases = []
    for c in get_myaas_containers():
        db = {
            'template': c['Labels']['com.myaas.template'],
            'name': c['Labels']['com.myaas.instance'],
            'state': c['Status'],
            'created': c['Created'],
        }
        if 'com.myaas.expiresAt' in c['Labels']:
            db.update({'expires_at': c['Labels']['com.myaas.expiresAt']})
        databases.append(db)

    return jsonify(databases=databases)


@app.route('/templates', methods=['get'])
def show_templates():
    return jsonify(templates=list_database_templates())


@app.route('/db/<template>/<name>', methods=['get'])
def inspect_database(template, name):
    logger.debug(f'requested inspect DB for: "{template}" => "{name}"')
    database = get_enabled_backend().Database
    try:

        db = database(client, template, name)