rules.py
changeset 21 59540181a4bb
parent 18 538d6b198f4a
equal deleted inserted replaced
20:f4daa224dc7e 21:59540181a4bb
    48     
    48     
    49     levels = ["init", "write", "read", "deny"]
    49     levels = ["init", "write", "read", "deny"]
    50 
    50 
    51     def __init__(self):
    51     def __init__(self):
    52         self.rules = []
    52         self.rules = []
       
    53         self.preset = {}
    53 
    54 
    54     def add(self, action, conditions):
    55     def add(self, action, conditions):
    55         self.rules.append((action, conditions))
    56         self.rules.append((action, conditions))
    56 
    57 
       
    58 
       
    59     def set(self, **kw):
       
    60         self.preset.update(kw)
       
    61         
    57     def matchrule(self, **kw):
    62     def matchrule(self, **kw):
       
    63         d = self.preset.copy()
       
    64         d.update(**kw)
    58         for a, c in self.rules:
    65         for a, c in self.rules:
    59             if c(**kw):
    66             if c(**d):
    60                 return a
    67                 return a
    61         return None
    68         return None
    62 
    69 
    63     def allow(self, level, **kw):
    70     def allow(self, level, **kw):
    64         a = matchrule(self, **kw)
    71         a = matchrule(self, **kw)
    75                     continue
    82                     continue
    76                 res.add(l[0], rule([c.split("=", 1) for c in l[1:]]))
    83                 res.add(l[0], rule([c.split("=", 1) for c in l[1:]]))
    77         finally:
    84         finally:
    78             f.close()
    85             f.close()
    79         return res
    86         return res
    80 
       
    81