"""
Cheesecake configuration file
"""

import os, sys

# Set defaults
_config = dict(
    INDEX_PYPI_DOWNLOAD = 50,
    INDEX_PYPI_DISTANCE = 5,
    INDEX_URL_DOWNLOAD  = 25,
    INDEX_UNPACK        = 25,
    INDEX_UNPACK_DIR    = 15,
    INDEX_INSTALL       = 50,
    INDEX_FILE_CRITICAL = 15,
    INDEX_FILE          = 10,
    INDEX_REQUIRED_FILES = 100,
    INDEX_FILE_PYC      = -20,
    INDEX_DIR_CRITICAL  = 25,
    INDEX_DIR           = 20,
    INDEX_DIR_EMPTY     = 5,
    MAX_INDEX_DOCSTRINGS = 100, # max. percentage of modules/classes/methods/functions with docstrings
    MAX_INDEX_UNITTESTS  = 100, # max. percentage of methods/functions that are unit tested
    MAX_INDEX_PYLINT     = 100, # max. pylint score
    cheese_files = ["readme", "install", "changelog",
                    "news", "faq", "todo", "thanks",
                    "license", "announce", "setup.py",
                    ],
    critical_cheese_files = ["readme", "license", "setup.py"],
    cheese_dirs = ["doc", "test", "example", "demo"],
    critical_cheese_dirs = ["doc", "test"],
    exclude_files = [],
    exclude_dirs = [],
)

def get_pkg_config(package):
    # try getting the user's home directory
    homedir = "~"
    homedir = os.path.expanduser(homedir)
    if homedir is "~":
        # can't get it...fall back to defaults
        print "Couldn't expand ~ (home directory)"
        pass
    else:
        # check for .cheesecake dir and create it if it's not there
        cheesecake_dir = os.path.join(homedir, ".cheesecake")
        if not os.path.isdir(cheesecake_dir):
            os.mkdir(cheesecake_dir)
        # check for config. file and create it if it's not there
        cfile = os.path.join(cheesecake_dir, "%s_config.py" % package)
        if not os.path.isfile(cfile):
            try:
                c = open(cfile, 'w')
                c.write("my_config = {\n")
                keys = _config.keys()
                keys.sort()
                for key in keys:
                    c.write("\t'%s': %s,\n" % (key, _config[key]))
                c.write("}\n")
                c.close()
            except OSError, e:
                pass

        else:
            # if we find the file, update _config with contents of the file
            sys.path.insert(0, cheesecake_dir)
            try:
                from my_config import my_config
                _config.update(my_config)
            except ImportError, e:
                pass
    return _config

def get(key, default=None):
    """
    Get the configuration value for the given key.
    """
    return _config.get(key, default)

def set(key, value):
    """
    Set the configuration value for the given key.
    """
    _config[key] = value
