"""
Lists all tickets matching a given regular expression

This macro takes one parameter, the regexp
"""

import time
import re
from StringIO import StringIO
from trac.util.html import escape

def execute(hdf, args, env):
    db = env.get_db_cnx()
    cursor = db.cursor()

    regexp = 'ZZ'
    if args:
        argv = [arg.strip() for arg in args.split(',')]
        if len(argv) > 0:
            regexp = argv[0].replace("'", "''")

    sql = "SELECT id, owner, summary, description FROM ticket WHERE status IN ('new', 'assigned', 'reopened')"

    cursor.execute(sql)

    buf = StringIO()
    while 1:
        row = cursor.fetchone()
        if row == None:
            break
        m = re.search(regexp,row[2])
        if m:
          buf.write('<a href="%s" title="(%s) %s">[#%s(%s)]</a> ' % (env.href.ticket(row[0]), 
                                                   escape(row[2]),
                                                   escape(row[3]),
                                                   row[0],
                                                   escape(row[1])))

    return buf.getvalue()


