Subversion Repositories SE.SVN

Rev

Blame | Last modification | View Log | RSS feed

import configparser
import collections

class Cfg(configparser.ConfigParser):
    def __init__(self,filename,sections=()):
        self.filename=filename
        configparser.ConfigParser.__init__(self,None,allow_no_value=True)
        configparser.ConfigParser.read(self,self.filename)
        for s in sections:
            if not self.has_section(s):
                self.add_section(s)

#    def get(self,section,option,default=None):
#        print ("Thats our config parser!")
#        try:
#            return configparser.ConfigParser.get(self,section,option)
#        except configparser.NoOptionError:
#            return default


config_file = "supos.conf"
reread = True
config = Cfg(config_file)


def set (section,option,val):
        global config,reread
        if not config.has_section(section):
                config.add_section(section)
                config.set(section,option,val)
        reread=True

def get (section,option):
        """
        Get a config option from specified section.
        """
        global reread,config,config_file
        if reread:
                print("reread config")
                config.read(config_file)
                reread=False
        print("hello:",section,option)
        return config.get(section,option,"aaaadefault")


def get_list (section,option,delimiter=':'):
    """
    Get a config option wich is a list and 
    return it as list.
    """
    l = get(section,option)
    return l.split(delimiter)


dynhandlers = []

def add_handler(h):
    dynhandlers.append(h)



def get_handlers():
    import handlers
#    return dynhandler.append(handlers.Handlers)
    return handlers.Handlers



import glob,re



def loadHandlers(mod):
    handlers = {}
    e = __import__ (mod)

    try:
        e = __import__ (mod)
    except xx:
        raise xx;
        print("Cannot load ",mod)
        return handlers

    for submod in e.__all__:
        s = __import__(mod+"."+submod)    
        list = eval ("e."+submod+".list")
        for se in list:
            print(se)




def initModule(modpath,submods=False):
    """
    A helper function wich can be used by __init.py__ files
    to initialize the __all_ variable by scanning for all 
    .py files in the directory
    Returns a list submodules
    Example for __init__.py:
    import minimal.config
    __all__=minimal.config.initModule

    """
    all = []

    # search all *.py files in directory
    p = modpath+'/*.py'
    for entry in glob.glob(p):
        s = re.split('.*\\/(.*?)\\.py',entry);
        if s[1] != '__init__':
            m = s[1]
            all.append (m)

        
    return all