import os.path
from configparser import ConfigParser
import yaml


APP_NAME = 'pos'

CONFIG_PATHS = ['conf/', '~/.config/pos', '/usr/local/etc/pos', '/etc/pos']
CONFIG_FILES = {
    'core': 'core.ini',
    'logging': 'logging.yaml'
}


def get_default_path():
    for p in CONFIG_PATHS:
        if all([
            os.path.isfile(os.path.join(p, f))
            for f in CONFIG_FILES.values()
        ]):
            return p
        else:
            raise Exception('Unable to find a configuration folder.')


class Config:
    def __init__(self):
        self.basedir = get_default_path()
        self.path_core = os.path.join(self.basedir, CONFIG_FILES['core'])
        self.path_logging = os.path.join(self.basedir, CONFIG_FILES['logging'])

        self.core = ConfigParser()
        self.logging = None

        self.read()

    def read(self):
        self.core.read(self.path_core)
        self.logging = yaml.load(open(self.path_logging, 'r'))