summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Server/Lint/GroupPatterns.py
blob: f50118ce198bf479ee50fb827e535849277efa55 (plain)
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
import sys
import Bcfg2.Server.Lint
from Bcfg2.Server.Plugins.GroupPatterns import PatternMap

class GroupPatterns(Bcfg2.Server.Lint.ServerPlugin):
    """ Check Genshi templates for syntax errors """

    def Run(self):
        """ run plugin """
        if 'GroupPatterns' in self.core.plugins:
            cfg = self.core.plugins['GroupPatterns'].config
            for entry in cfg.xdata.xpath('//GroupPattern'):
                groups = [g.text for g in entry.findall('Group')]
                self.check(entry, groups, ptype='NamePattern')
                self.check(entry, groups, ptype='NameRange')

    def Errors(self):
        return {"pattern-fails-to-initialize":"error"}

    def check(self, entry, groups, ptype="NamePattern"):
        if ptype == "NamePattern":
            pmap = lambda p: PatternMap(p, None, groups)
        else:
            pmap = lambda p: PatternMap(None, p, groups)

        for el in entry.findall(ptype):
            pat = el.text
            try:
                pmap(pat)
            except:
                err = sys.exc_info()[1]
                self.LintError("pattern-fails-to-initialize",
                               "Failed to initialize %s %s for %s: %s" %
                               (ptype, pat, entry.get('pattern'), err))