File: config.py

package info (click to toggle)
pglistener 4
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 224 kB
  • ctags: 132
  • sloc: python: 594; perl: 364; sh: 120; sql: 89; makefile: 2
file content (40 lines) | stat: -rw-r--r-- 993 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

import glob
import os
import sys

from ConfigParser import RawConfigParser

def expand_dirs(paths):
    file_paths = []

    for path in paths:
        if os.path.isdir(path):
            file_paths.extend([
                p for p in glob.glob('%s/*.cfg' % path)
                if os.path.isfile(p)])
        else:
            file_paths.append(path)

    return file_paths

def read_configs(paths):
    file_paths = expand_dirs(paths)
    cf = RawConfigParser()

    if cf.read(file_paths) != file_paths:
        raise RuntimeError("couldn't read config file")

    for section in cf.sections():
        classname = cf.get(section, 'class')
        modname = 'pglistener.%s' % classname.lower()
        __import__(modname)
        cls = getattr(sys.modules[modname], classname)

        options = dict(cf.items(section))
        options['name'] = section
        options['notifications'] = [
            n.strip() for n in options['notifications'].split(',')]

        yield cls(options)