Subversion Repositories SE.SVN

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
35 7u83 1
import configparser
2
import collections
3
 
4
class Cfg(configparser.ConfigParser):
5
    def __init__(self,filename,sections=()):
6
        self.filename=filename
7
        configparser.ConfigParser.__init__(self,None,allow_no_value=True)
8
        configparser.ConfigParser.read(self,self.filename)
9
        for s in sections:
10
            if not self.has_section(s):
11
                self.add_section(s)
12
 
13
#    def get(self,section,option,default=None):
14
#        print ("Thats our config parser!")
15
#        try:
16
#            return configparser.ConfigParser.get(self,section,option)
17
#        except configparser.NoOptionError:
18
#            return default
19
 
20
 
21
config_file = "supos.conf"
22
reread = True
23
config = Cfg(config_file)
24
 
25
 
26
def set (section,option,val):
27
        global config,reread
28
        if not config.has_section(section):
29
                config.add_section(section)
30
                config.set(section,option,val)
31
        reread=True
32
 
33
def get (section,option):
34
        """
35
        Get a config option from specified section.
36
        """
37
        global reread,config,config_file
38
        if reread:
39
                print("reread config")
40
                config.read(config_file)
41
                reread=False
42
        print("hello:",section,option)
43
        return config.get(section,option,"aaaadefault")
44
 
45
 
46
def get_list (section,option,delimiter=':'):
47
    """
48
    Get a config option wich is a list and 
49
    return it as list.
50
    """
51
    l = get(section,option)
52
    return l.split(delimiter)
53
 
54
 
55
dynhandlers = []
56
 
57
def add_handler(h):
58
    dynhandlers.append(h)
59
 
60
 
61
 
62
def get_handlers():
63
    import handlers
64
#    return dynhandler.append(handlers.Handlers)
65
    return handlers.Handlers
66
 
67
 
68
 
69
import glob,re
70
 
71
 
72
 
73
def loadHandlers(mod):
74
    handlers = {}
75
    e = __import__ (mod)
76
 
77
    try:
78
        e = __import__ (mod)
79
    except xx:
80
        raise xx;
81
        print("Cannot load ",mod)
82
        return handlers
83
 
84
    for submod in e.__all__:
85
        s = __import__(mod+"."+submod)    
86
        list = eval ("e."+submod+".list")
87
        for se in list:
88
            print(se)
89
 
90
 
91
 
92
 
93
def initModule(modpath,submods=False):
94
    """
95
    A helper function wich can be used by __init.py__ files
96
    to initialize the __all_ variable by scanning for all 
97
    .py files in the directory
98
    Returns a list submodules
99
    Example for __init__.py:
100
    import minimal.config
101
    __all__=minimal.config.initModule
102
 
103
    """
104
    all = []
105
 
106
    # search all *.py files in directory
107
    p = modpath+'/*.py'
108
    for entry in glob.glob(p):
109
        s = re.split('.*\\/(.*?)\\.py',entry);
110
        if s[1] != '__init__':
111
            m = s[1]
112
            all.append (m)
113
 
114
 
115
    return all
116
 
117