Changeset 2810

Show
Ignore:
Timestamp:
11/20/07 18:15:19 (1 year ago)
Author:
TerryBrown
Message:

Simplified version with the help of osimons on #trac

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • componentsprocessormacro/0.11/ComponentsListMacro.py

    r2809 r2810  
    88import sys 
    99 
    10 from StringIO import StringIO 
    1110from trac.core import Component, implements 
    1211from trac.wiki.api import IWikiMacroProvider 
    13 from trac.wiki.formatter import Formatter 
     12from trac.wiki import format_to_html 
    1413 
    1514class ComponentsProcessor(Component): 
     
    2221 
    2322    def get_macro_description(self, name): 
    24         #return inspect.getdoc(inspect.getmodule(self)) # works only in py >= 2.4 
    2523        return inspect.getdoc(sys.modules.get(self.__module__)) 
    2624 
    2725    def expand_macro(self, formatter, name, content): 
    28         content = [] 
     26 
    2927        cursor = self.env.get_db_cnx().cursor() 
    3028 
    31         query = "SELECT name, description from component;" 
     29        query = "SELECT name, description from component order by name;" 
    3230        cursor.execute(query) 
    3331 
    3432        comps = [comp for comp in cursor] 
    35         comps.sort(cmp=lambda x,y: cmp(x[0], y[0])) 
    3633 
    37         # get a list of all components for which there are tickets 
    38         query = "SELECT distinct component from ticket;" 
     34        # get a distinct list of all components for which there are tickets 
     35        query = "SELECT component from ticket group by component;" 
    3936        cursor.execute(query) 
    4037        tickets = [page[0] for page in cursor] 
     38 
     39        content = [] 
    4140 
    4241        for name, descrip in comps: 
     
    5150        content = '\n'.join(content) 
    5251 
    53         out = StringIO() 
    54         Formatter(self.env, formatter.context).format(content, out) 
    55         return out.getvalue() 
     52        content = format_to_html(self.env, formatter.context, content) 
    5653 
    57     def _parse_args(self, args): 
    58         firstArgs = None; 
    59         if args.rfind(")"): 
    60             firstArgs = args[args.find("(") + 1 : args.rfind(")")]; 
    61             args      = args[:args.find("(")] + args[args.rfind(")") + 1:] 
     54        content = '<div class="component-list">%s</div>' % content 
    6255 
    63         macros = map(lambda s: s.strip(), args.split(',')); 
     56        # to avoid things like the above it might be nicer to use 
     57        # Genshi tag() construction, but this way the wiki formatter 
     58        # gets to deal with '[query:component=%s tickets]' etc. 
     59        # if going the Genshi route you'd replace that with something 
     60        # like req.href.query(component="mycomp", status="open") 
    6461 
    65         return macros, firstArgs 
     62        return content