# Path: voeventdb/server/database/db_utils.py
# def check_database_exists(db_url):
# def create_empty_database(admin_db_url, new_db_name):
# def delete_database(admin_db_url, drop_db_name):
# def load_q3c(connection):
# def create_tables_and_indexes(connection):


from zope.interface import implementer
from twisted.plugin import IPlugin
from comet.icomet import IHandler
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from voeventdb.server.database import db_utils
import comet.log as log
import os
import voeventparse
import voeventdb.server.database.config as dbconfig
import voeventdb.server.database.convenience as dbconv
# Defines a plugin for the Comet broker.
# If top-level directory 'comet_plugin' is added to $PYTHONPATH then comet
# will detect this module at import-time.



voeventdb_dbname = os.environ.get("VOEVENTDB_DBNAME",
                                  dbconfig.testdb_corpus_url.database)

dburl = dbconfig.make_db_url(dbconfig.default_admin_db_params, voeventdb_dbname)
if not db_utils.check_database_exists(dburl):
    log.warn("voeventdb database not found: {}".format(
        voeventdb_dbname))
dbengine = create_engine(dburl)


@implementer(IPlugin, IHandler)
class VoeventdbInserter(object):
    name = "voeventdb-insert"

    # When the handler is called, it is passed an instance of
    # comet.utility.xml.xml_document.

# Path: voeventdb/server/database/db_utils.py
# def check_database_exists(db_url):
# def create_empty_database(admin_db_url, new_db_name):
# def delete_database(admin_db_url, drop_db_name):
# def load_q3c(connection):
# def create_tables_and_indexes(connection):


# Path: comet_plugin/comet/plugins/voeventdb_insert.py
from zope.interface import implementer
from twisted.plugin import IPlugin
from comet.icomet import IHandler
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from voeventdb.server.database import db_utils
import comet.log as log
import os
import voeventparse
import voeventdb.server.database.config as dbconfig
import voeventdb.server.database.convenience as dbconv

# Defines a plugin for the Comet broker.
# If top-level directory 'comet_plugin' is added to $PYTHONPATH then comet
# will detect this module at import-time.



voeventdb_dbname = os.environ.get("VOEVENTDB_DBNAME",
                                  dbconfig.testdb_corpus_url.database)

dburl = dbconfig.make_db_url(dbconfig.default_admin_db_params, voeventdb_dbname)
if not db_utils.check_database_exists(dburl):
    log.warn("voeventdb database not found: {}".format(
        voeventdb_dbname))
dbengine = create_engine(dburl)


@implementer(IPlugin, IHandler)
class VoeventdbInserter(object):
    name = "voeventdb-insert"

    # When the handler is called, it is passed an instance of
    # comet.utility.xml.xml_document.

    def __call__(self, event):