src/mercurialserver/ruleset.py
changeset 311 3cbde66305e4
parent 301 f88549f44c8e
child 371 e9ce904b62a9
equal deleted inserted replaced
310:38c222727017 311:3cbde66305e4
    33         return min(rmatch(k, m, kw) for k, m in matchers)
    33         return min(rmatch(k, m, kw) for k, m in matchers)
    34     return c
    34     return c
    35 
    35 
    36 class Ruleset(object):
    36 class Ruleset(object):
    37     '''Class representing the rules in a rule file'''
    37     '''Class representing the rules in a rule file'''
    38     
    38 
    39     levels = ["init", "write", "read", "deny"]
    39     levels = ["init", "write", "read", "deny"]
    40 
    40 
    41     def __init__(self):
    41     def __init__(self):
    42         self.rules = []
    42         self.rules = []
    43         self.preset = {}
    43         self.preset = {}
    44 
    44 
    45     def set(self, **kw):
    45     def set(self, **kw):
    46         self.preset.update(kw)
    46         self.preset.update(kw)
    47         
    47 
    48     def get(self, k):
    48     def get(self, k):
    49         return self.preset.get(k, None)
    49         return self.preset.get(k, None)
    50 
    50 
    51     def allow(self, level, **kw):
    51     def allow(self, level, **kw):
    52         levelindex = self.levels.index(level)
    52         levelindex = self.levels.index(level)
    60             elif m == 0:
    60             elif m == 0:
    61                 # "Maybe match" - allow if it says yes, ignore if no
    61                 # "Maybe match" - allow if it says yes, ignore if no
    62                 if a <= levelindex:
    62                 if a <= levelindex:
    63                     return True
    63                     return True
    64         return False
    64         return False
    65     
    65 
    66     def readfile(self, fn):
    66     def readfile(self, fn):
    67         f = open(fn)
    67         f = open(fn)
    68         try:
    68         try:
    69             for l in f:
    69             for l in f:
    70                 l = l.strip()
    70                 l = l.strip()
    74                 # Unrecognized actions are off the high end
    74                 # Unrecognized actions are off the high end
    75                 if l[0] in self.levels:
    75                 if l[0] in self.levels:
    76                     ix = self.levels.index(l[0])
    76                     ix = self.levels.index(l[0])
    77                 else:
    77                 else:
    78                     ix = len(self.levels)
    78                     ix = len(self.levels)
    79                 self.rules.append((ix, 
    79                 self.rules.append((ix,
    80                     rule([c.split("=", 1) for c in l[1:]])))
    80                     rule([c.split("=", 1) for c in l[1:]])))
    81         finally:
    81         finally:
    82             f.close()
    82             f.close()
    83 
    83 
    84 rules = Ruleset()
    84 rules = Ruleset()
    85