src/mercurialserver/ruleset.py
changeset 371 e9ce904b62a9
parent 311 3cbde66305e4
child 372 80f78674c56e
equal deleted inserted replaced
369:c359a85eef93 371:e9ce904b62a9
    29 
    29 
    30 def rule(pairs):
    30 def rule(pairs):
    31     matchers = [(k, globmatcher(v)) for k, v in pairs]
    31     matchers = [(k, globmatcher(v)) for k, v in pairs]
    32     def c(kw):
    32     def c(kw):
    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     c.patterns = [(k, m.pattern) for k, m in matchers]
    34     return c
    35     return c
    35 
    36 
    36 class Ruleset(object):
    37 class Ruleset(object):
    37     '''Class representing the rules in a rule file'''
    38     '''Class representing the rules in a rule file'''
    38 
    39 
    64         return False
    65         return False
    65 
    66 
    66     def readfile(self, fn):
    67     def readfile(self, fn):
    67         f = open(fn)
    68         f = open(fn)
    68         try:
    69         try:
    69             for l in f:
    70             self.buildrules(f)
    70                 l = l.strip()
       
    71                 if len(l) == 0 or l.startswith("#"):
       
    72                     continue
       
    73                 l = l.split()
       
    74                 # Unrecognized actions are off the high end
       
    75                 if l[0] in self.levels:
       
    76                     ix = self.levels.index(l[0])
       
    77                 else:
       
    78                     ix = len(self.levels)
       
    79                 self.rules.append((ix,
       
    80                     rule([c.split("=", 1) for c in l[1:]])))
       
    81         finally:
    71         finally:
    82             f.close()
    72             f.close()
    83 
    73 
       
    74     def buildrules(self, f):
       
    75         """Build rules from f
       
    76 
       
    77         f shoud be iterable per line, each line is like:
       
    78 
       
    79         level [user=pattern] [repo=pattern] [file=pattern] [branch=pattern]
       
    80         """
       
    81         for l in f:
       
    82             l = l.strip()
       
    83             if not l or l.startswith("#"):
       
    84                 continue
       
    85             l = l.split()
       
    86             # Unrecognized actions are off the high end
       
    87             if l[0] in self.levels:
       
    88                 ix = self.levels.index(l[0])
       
    89             else:
       
    90                 ix = len(self.levels)
       
    91             self.rules.append((ix,
       
    92                 rule([c.split("=", 1) for c in l[1:]])))
       
    93 
    84 rules = Ruleset()
    94 rules = Ruleset()