#!/usr/bin/env python

""" This is the entry point for TACA.
"""
import os

from cement.core import foundation, handler
from cement.ext.ext_yaml import YamlConfigHandler
from cement.utils.misc import init_defaults
from taca.controllers import BaseController
from taca.controllers import storage

CONFIG_FLIE = os.path.join(os.getenv('HOME'), '.taca', 'taca.yaml')
LOG_FILE = os.path.join(os.getenv('HOME'), '.taca', 'taca.log')

class TACAApp(foundation.CementApp):
    class Meta:
        label = 'TACA'
        base_controller = BaseController
        config_handler = YamlConfigHandler

# Create the main application
defaults = init_defaults('TACA', 'log.logging')
defaults['log.logging']['file'] = LOG_FILE
app = TACAApp(config_defaults=defaults)

try:
    # Register handlers
    handler.register(storage.StorageController)

    # Setup the application
    app.setup()

    # Run the application
    if not app.config.parse_file(CONFIG_FLIE):
        app.log.error('No config file {}; please create and set relevant config sections'.format(CONFIG_FLIE))
    else:
        app.run()
finally:
    # close the app
    app.close()
