Changeset 795

Show
Ignore:
Timestamp:
05/30/06 12:42:49 (2 years ago)
Author:
coderanger
Message:

TracRewritePlugin:

  • Lock out local rewrites
  • Force rule ordering
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • tracrewriteplugin/0.9/tracrewrite/web_ui.py

    r794 r795  
    3131        so any changes to the config will require a reload.""" 
    3232         
    33         self.rewrites = {} 
     33        self.rewrites = [] 
    3434        # Config format: tag = /match/rewrite/[options] 
    3535        # Data format: { tag: [ match, rewrite, options ], ... } 
     
    3737            md = config_re.match(v) 
    3838            if md: 
     39                tag = int(k.strip()) 
    3940                try: 
    40                     match = re.compile(md.group('match')) 
     41                    delim = md.group('delim') 
     42                    match_pat = re.sub('\\'+delim,delim,md.group('match')) 
     43                    match = re.compile(match_pat)                                         
    4144                except re.error: 
    42                     self.log.warn("TracRewrite: Unable to compile pattern '%s'"%md.group(1)) 
     45                    self.log.warn("TracRewrite: Unable to compile pattern '%s'"%md.group('match')) 
    4346                    continue 
    4447                rewrite = md.group('rewrite') 
    45                 options = md.group('opts') or 'l
     48                options = md.group('opts') or 'r
    4649                self.log.debug("TracRewrite: Loaded rewrite ('%s', '%s', %s')"%( match.pattern, rewrite, options.lower() ) ) 
    47                 self.rewrites[k.strip()] = ( match, rewrite, options.lower() ) 
     50                self.rewrites.append( (tag, match, rewrite, options.lower()) ) 
    4851            else: 
    4952                self.log.warn("TracRewrite: Unable to parse value '%s'"%v) 
     53        self.rewrites.sort(lambda a,b: cmp(a[0],b[0])) 
     54         
     55        # Generate tag location cache 
     56        i = 0 
     57        self.tag_index = {} 
     58        for t,_,_,_ in self.rewrites: 
     59            self.tag_index[t] = i 
     60            i += 1 
    5061                 
    5162    # IRequestHandler methods 
    5263    def match_request(self, req): 
    5364        path = req.path_info 
    54         for k,v in self.rewrites.iteritems()
    55             md = v[0].match(path) 
     65        for t,m,_,_ in self.rewrites
     66            md = m.match(path) 
    5667            if md: 
    57                 req.tag = k 
     68                req.tag = t 
    5869                req.md = md 
    5970                return True 
     
    6172         
    6273    def process_request(self, req): 
    63         _, rewrite, options = self.rewrites[req.tag
     74        _, _, rewrite, options = self.rewrites[self.tag_index[req.tag]
    6475        try: 
    6576            new_path = req.md.expand(rewrite) 
     
    7283            raise RewriteError, "TracRewrite doesn't support proxying yet. Sorry" 
    7384        elif 'l' in options: 
     85            raise RewriteError, "Internal rewrites don't actually work yet. Check back after 0.10 get finalized a bit more" 
    7486            dispatch_request(new_path, req, self.env) 
    7587            return False # This indicates that a response has already been sent