src/ruleset.py
changeset 48 f0cb7ad9e4ab
parent 45 59dee3c04279
child 50 77d97aa18f29
equal deleted inserted replaced
47:ebcc230f21e4 48:f0cb7ad9e4ab
    37     # None matches everything
    37     # None matches everything
    38     return lambda x: x is None or rex.match(x) is not None
    38     return lambda x: x is None or rex.match(x) is not None
    39 
    39 
    40 def rule(pairs):
    40 def rule(pairs):
    41     matchers = [(k, globmatcher(v)) for k, v in pairs]
    41     matchers = [(k, globmatcher(v)) for k, v in pairs]
    42     def c(**kw):
    42     def c(kw):
    43         for k, m in matchers:
    43         for k, m in matchers:
    44             if k not in kw or not m(kw[k]):
    44             if k not in kw or not m(kw[k]):
    45                 return False
    45                 return False
    46         return True
    46         return True
    47     return c
    47     return c
    50     '''Class representing the rules in a rule file'''
    50     '''Class representing the rules in a rule file'''
    51     
    51     
    52     levels = ["init", "write", "read", "deny"]
    52     levels = ["init", "write", "read", "deny"]
    53 
    53 
    54     def __init__(self):
    54     def __init__(self):
       
    55         self.rules = []
    55         self.preset = {}
    56         self.preset = {}
    56 
    57 
    57     def add(self, action, conditions):
    58     def add(self, action, conditions):
    58         self.rules.append((action, conditions))
    59         self.rules.append((action, conditions))
    59 
    60 
    60 
       
    61     def set(self, **kw):
    61     def set(self, **kw):
    62         self.preset.update(kw)
    62         self.preset.update(kw)
    63         
    63         
    64     def matchrule(self, **kw):
    64     def matchrule(self, kw):
    65         d = self.preset.copy()
    65         d = self.preset.copy()
    66         d.update(**kw)
    66         d.update(kw)
    67         for a, c in self.rules:
    67         for a, c in self.rules:
    68             if c(**d):
    68             if c(d):
    69                 return a
    69                 return a
    70         return None
    70         return None
    71 
    71 
    72     def allow(self, level, **kw):
    72     def allow(self, level, **kw):
    73         a = self.matchrule(**kw)
    73         a = self.matchrule(kw)
    74         return a in self.levels and self.levels.index(a) <= self.levels.index(level)
    74         return a in self.levels and self.levels.index(a) <= self.levels.index(level)
    75     
    75     
    76     def readfile(self, fn):
    76     def readfile(self, fn):
    77         try:
    77         try:
    78             f = open(fn)
    78             f = open(fn)