#!/usr/bin/env python
import ConfigParser, os
import sys
import inspect

class Configuration(object):



    def __WriteDefaultValues( self,configfile):
        ## When adding sections or items, add them in the reverse order of
        # how you want them to be displayed in the actual file.
        # In addition, please note that using RawConfigParser's and the raw
        # mode of ConfigParser's respective set functions, you can assign
        # non-string values to keys internally, but will receive an error
        # when attempting to write to a file or when you get it in non-raw
        # mode. SafeConfigParser does not allow such assignments to take place.
        self.config.add_section('General')
        self.config.set('General', 'cast_location', '~/cast')
        self.config.set('General', 'scan_recursive', 'false')
        self.config.set('General', 'include_sifz', 'false')

        # Writing our configuration file to 'example.cfg'
        with open(configfile, 'wb') as configfile:
            self.config.write(configfile)

    def __init__(self, configfile=None):
        try:
            self.config = ConfigParser.ConfigParser()
            configfile = os.path.expanduser(configfile)
            if os.path.isfile(configfile) is False:
                open(configfile, 'w+')
                self.__WriteDefaultValues(configfile)
            self.config.readfp(open(configfile))
        except Exception as e:
            print "Unexpected error:", sys.exc_info()[0]
            print str(e)
        pass
 
    def Cast_Location(self):
        castLoc = "~/cast"
        if self.config.get('General', 'Cast_Location'):
            castLoc = self.config.get('General', 'Cast_Location')
        return castLoc

    def Scan_Recursive(self):
        try:
            scanRecurse = self.config.getboolean('General', 'Scan_Recursive')
        except:
            scanRecurse = False
        finally:
            return scanRecurse

    def Include_SIFZ(self):
        try:
            includeSIFZ = self.config.getboolean('General', 'Include_SIFZ')
        except:
            includeSIFZ = False
        finally:
            return False  # SIFZ Scanning is not currently supported

    def GetDictionary(self):
        Methodname = inspect.stack()[0][3]
        pr = {}
        print pr

        for name in dir(self):

            if (not name == Methodname) and (not name =='_Configuration__WriteDefaultValues'):
                value = getattr(self, name)
                if not name.startswith('__')   and inspect.ismethod(value):
                    print "ADDING name = " + name
                    pr[name] = value()

        return pr

