summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Plugins/Rules.py
blob: e8412c6f1220d37822938c8a22b945f842ba580d (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
35
36
37
38
39
40
41
"""This generator provides rule-based entry mappings."""
__revision__ = '$Revision$'

import re
import Bcfg2.Server.Plugin

class Rules(Bcfg2.Server.Plugin.PrioDir):
    """This is a generator that handles service assignments."""
    name = 'Rules'
    __version__ = '$Id$'
    __author__ = 'bcfg-dev@mcs.anl.gov'

    def HandlesEntry(self, entry, metadata):
        if entry.tag in self.Entries:
            if entry.get("name") in self.Entries[entry.tag]:
                return True
            else:
                for rule in self.Entries[entry.tag].keys():
                    if re.search(rule, entry.get('name')):
                        return True
        return False

    def HandleEntry(self, entry, metadata):
        return self.BindEntry(entry, metadata)

    def BindEntry(self, entry, metadata):
        attrs = self.get_attrs(entry, metadata)
        for key, val in list(attrs.items()):
            if key not in entry.attrib:
                entry.attrib[key] = val

    def _matches(self, entry, metadata, rules):
        if Bcfg2.Server.Plugin.PrioDir._matches(self, entry, metadata, rules):
            return True
        else:
            # attempt regular expression matching
            for rule in rules:
                if re.search(rule, entry.get('name')):
                    return True
        return False