Changeset 872

Show
Ignore:
Timestamp:
06/15/06 13:54:16 (2 years ago)
Author:
coderanger
Message:

TracRewritePlugin:

Moving development to a new machine.

Files:

Legend:

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

    r795 r872  
    99config_re = re.compile("""^ 
    1010                          \s* 
    11                           (?P<delim>\S)      # Opening delim 
    12                           (?P<match>.*?)     # Match RE 
    13                           (?<!\\\)(?P=delim) # Middle delim 
    14                           (?P<rewrite>.*?)   # Rewrite string 
    15                           (?<!\\\)(?P=delim) # End delim 
    16                           \s* 
    17                           (?P<opts>\S*)      # Possible options 
     11                          (?P<match>\S*?)        # Match RE 
     12                          \s+ 
     13                          (?P<rewrite>\S*?)      # Rewrite string 
     14                          \s+ 
     15                          (?:\[(?P<opts>\S*)\])? # Possible options 
    1816                          \s*  
    1917                          $""",re.X) 
     
    2826     
    2927    def __init__(self): 
    30         """Note: I am loading and compiling all regexs here, 
    31         so any changes to the config will require a reload.""" 
    32          
    33         self.rewrites = [] 
    34         # Config format: tag = /match/rewrite/[options] 
    35         # Data format: { tag: [ match, rewrite, options ], ... } 
    36         for k,v in self.config.options('rewrites'): 
    37             md = config_re.match(v) 
    38             if md: 
    39                 tag = int(k.strip()) 
    40                 try: 
    41                     delim = md.group('delim') 
    42                     match_pat = re.sub('\\'+delim,delim,md.group('match')) 
    43                     match = re.compile(match_pat)                                         
    44                 except re.error: 
    45                     self.log.warn("TracRewrite: Unable to compile pattern '%s'"%md.group('match')) 
    46                     continue 
    47                 rewrite = md.group('rewrite') 
    48                 options = md.group('opts') or 'r' 
    49                 self.log.debug("TracRewrite: Loaded rewrite ('%s', '%s', %s')"%( match.pattern, rewrite, options.lower() ) ) 
    50                 self.rewrites.append( (tag, match, rewrite, options.lower()) ) 
    51             else: 
    52                 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 
     28        self.mtime = 0 
     29        self._load_config() 
    6130                 
    6231    # IRequestHandler methods 
    6332    def match_request(self, req): 
     33        self._load_config_if_needed() 
    6434        path = req.path_info 
    6535        for t,m,_,_ in self.rewrites: 
     
    8050        if 'r' in options: 
    8151            req.redirect(new_path) 
    82         elif 'p' in options: 
    83             raise RewriteError, "TracRewrite doesn't support proxying yet. Sorry" 
     52        elif 'n' in options: 
     53            pass 
    8454        elif 'l' in options: 
    8555            raise RewriteError, "Internal rewrites don't actually work yet. Check back after 0.10 get finalized a bit more" 
     
    8959            raise RewriteError, "No valid options found in '%s'. Please specify a valid rewrite type."%options 
    9060                 
     61 
     62    def _load_config(self): 
     63        """(Re)load config from trac.ini"""         
     64        self.rewrites = [] 
     65        # Config format: tag = /match/rewrite/[options] 
     66        # Data format: { tag: [ match, rewrite, options ], ... } 
     67        for k,v in self.config.options('rewrites'): 
     68            md = config_re.match(v) 
     69            if md: 
     70                tag = k.strip() 
     71                try: 
     72                    match = re.compile(md.group('match')) 
     73                except re.error: 
     74                    self.log.warn("TracRewrite: Unable to compile pattern '%s'"%md.group('match')) 
     75                    continue 
     76                rewrite = md.group('rewrite') 
     77                opt_string = md.group('opts') or 'r' 
     78                options = [] 
     79                for opt in opt_string.split(','): 
     80                    n = opt.split('=') 
     81                    if len(n) == 1: 
     82                        n = (n, None) 
     83                    options.append(n[0:2]) 
     84                self.log.debug("TracRewrite: Loaded rewrite ('%s', '%s', %s')"%( match.pattern, rewrite, options ) ) 
     85                self.rewrites.append( (tag, match, rewrite, options) ) 
     86            else: 
     87                self.log.warn("TracRewrite: Unable to parse value '%s'"%v) 
     88        self.rewrites.sort(lambda a,b: cmp(a[0],b[0])) 
     89         
     90        # Generate tag location cache 
     91        i = 0 
     92        self.tag_index = {} 
     93        for t,_,_,_ in self.rewrites: 
     94            self.tag_index[t] = i 
     95            i += 1 
     96             
     97    def _load_config_if_needed(self): 
     98        """Reload the config only if needed.""" 
     99        config_mtime = max(self.config._lastmtime,self.config._lastsitemtime) 
     100        if self.mtime < config_mtime: 
     101            self.log.info('TracRewrite: Config mtime is newer than ours; reloading config.') 
     102            self._load_config() 
     103            self.mtime = config_mtime