#!/usr/bin/python

import os
import sys
from traceback import print_exc

home = os.environ.get("CUMIN_HOME", os.path.normpath("/usr/share/cumin"))
sys.path.append(os.path.join(home, "python"))

from cumin.config import PlumageOptionParser, CuminReportConfig
from mint import data_app

def make_ctrl_c(sig, frame):
    log.debug("Signal received, raising KeyboardInterrupt")
    raise KeyboardInterrupt

class ReportTemplate(data_app.DataAppTemplate):
    def __init__(self):

        self.default_data_section = "report"
        super(ReportTemplate, self).__init__()

    def get_parser(self):
        return PlumageOptionParser()

    def get_config(self, section):
        # --section controls which section is read from the config file
        # If a section other than the default is specified, require it to exist
        return CuminReportConfig(section, 
                   strict_section = section != self.default_data_section)

    def get_app(self, home, values, opts):
        from mint.plumage.main import Plumage

        mint = Plumage(home,
                       ("model/plumage", "model/admin"),
                       values.plumage_host, 
                       values.plumage_port, 
                       opts.database)
        return mint

if __name__ == "__main__":
    try:
        import signal
        signal.signal(signal.SIGTERM, make_ctrl_c)
        sys.exit(data_app.main(ReportTemplate()))
    except KeyboardInterrupt:
        sys.exit(0)
