diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/Server/Plugins/GroupPatterns.py | 51 |
1 files changed, 46 insertions, 5 deletions
diff --git a/src/lib/Server/Plugins/GroupPatterns.py b/src/lib/Server/Plugins/GroupPatterns.py index 1a5d590c1..56195602b 100644 --- a/src/lib/Server/Plugins/GroupPatterns.py +++ b/src/lib/Server/Plugins/GroupPatterns.py @@ -2,13 +2,49 @@ import re, lxml.etree import Bcfg2.Server.Plugin +class PackedDigitRange(object): + def __init__(self, digit_range): + self.sparse = list() + self.ranges = list() + for item in digit_range.split(','): + if '-' in item: + self.ranges.append(tuple([int(x) for x in item.split('-')])) + else: + self.sparse.append(int(item)) + + def includes(self, other): + iother = int(other) + return iother in self.sparse or True in \ + [iother in range(rng[0], rng[1]+1) for rng in self.ranges] + class PatternMap(object): - def __init__(self, pattern, groupname): + range_finder = '\\[\\[[\d\-,]+\\]\\]' + def __init__(self, pattern, rangestr, groupname): self.pattern = pattern - self.re = re.compile(pattern) + self.rangestr = rangestr self.groupname = groupname + if pattern != None: + self.re = re.compile(pattern) + self.process = self.process_re + elif rangestr != None: + self.process = self.process_range + self.re = re.compile('^' + re.subn(self.range_finder, '(\d+)', rangestr)[0]) + dmatcher = re.compile(re.subn(self.range_finder, '\\[\\[([\d\-,]+)\\]\\]', rangestr)[0]) + self.dranges = [PackedDigitRange(x) for x in dmatcher.match(rangestr).groups()] + else: + raise Exception + + def process_range(self, name): + match = self.re.match(name) + if not match: + return None + digits = match.groups() + for i in range(len(digits)): + if not self.dranges[i].includes(digits[i]): + return None + return self.groupname - def process(self, name): + def process_re(self, name): match = self.re.match(name) if not match: return None @@ -32,9 +68,14 @@ class PatternFile(Bcfg2.Server.Plugin.SingleXMLFileBacked): return for entry in parsed.findall('GroupPattern'): try: - pat = entry.find('NamePattern').text + pat = None + rng = None + if entry.find('NamePattern') is not None: + pat = entry.find('NamePattern').text + if entry.find('NameRange') is not None: + rng = entry.find('NameRange').text grp = entry.find('Group').text - self.patterns.append(PatternMap(pat, grp)) + self.patterns.append(PatternMap(pat, rng, grp)) except: Bcfg2.Server.Plugin.logger.error(\ "GroupPatterns: Failed to initialize pattern %s" % \ |