Changeset 365

Show
Ignore:
Timestamp:
01/19/06 01:15:25 (3 years ago)
Author:
athomas
Message:

RepoSearchPlugin:

  • Added include and exclude globbing options under the [repo-search] trac.ini section.
  • Searches will now match against directory names.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • reposearchplugin/0.9/tracreposearch/tracreposearch.py

    r364 r365  
    66import re 
    77import posixpath 
     8import os 
     9from fnmatch import fnmatch 
    810 
    911class TracRepoSearchPlugin(Component): 
     
    2426            return 
    2527 
     28        includes = [glob for glob in self.env.config.get('repo-search', 
     29                   'include', '').split(os.path.pathsep) if glob] 
     30        excludes = [glob for glob in self.env.config.get('repo-search', 
     31                   'exclude', '').split(os.path.pathsep) if glob] 
     32 
     33        if includes and not excludes: 
     34            excludes = ['*'] 
     35 
    2636        repo = self.env.get_repository(req.authname) 
    2737 
     
    2939        db = self.env.get_db_cnx() 
    3040 
     41        self.env.log.debug(str(includes)) 
     42        self.env.log.debug(str(excludes)) 
     43 
     44        def searchable(path): 
     45            # Exclude paths 
     46            searchable = 1 
     47            for exclude in excludes: 
     48                if fnmatch(path, exclude): 
     49                    searchable = 0 
     50                    break 
     51 
     52            # Include paths 
     53            for include in includes: 
     54                if fnmatch(path, include): 
     55                    searchable = 1 
     56                    break 
     57 
     58            return searchable 
     59 
     60        def match_name(name): 
     61            for term in query: 
     62                if term not in name: 
     63                    return 0 
     64            return 1 
     65 
    3166        def walk_repo(path): 
    3267            node = repo.get_node(path) 
     68            basename = posixpath.basename(path) 
     69 
    3370            if node.kind == Node.DIRECTORY: 
     71                if match_name(basename): 
     72                    change = repo.get_changeset(node.rev) 
     73                    yield (self.env.href.browser(path), 
     74                           path, change.date, change.author, 
     75                           'Directory') 
     76 
    3477                for subnode in node.get_entries(): 
    3578                    for result in walk_repo(subnode.path): 
    3679                        yield result 
    3780            else: 
    38                 # Search name 
    39                 basename = posixpath.basename(path) 
    40                 match_name = 1 
    41                 match_content = 1 
    42                 excerpt = None 
    43                 for term in query: 
    44                     if term not in basename: 
    45                         match_name = 0 
    46                         break 
     81                if not searchable(path): 
     82                    return 
    4783 
    4884                # Search content 
     85                match_content = 1 
    4986                content = node.get_content().read() 
    5087                for term in query: 
     
    5289                        match_content = 0 
    5390                        break 
    54                 if not (match_name or match_content): 
     91                if not (match_name(basename) or match_content): 
    5592                    return 
    5693                change = repo.get_changeset(node.rev)