| 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 |
|---|
| 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() |
|---|
| | 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 |
|---|