changeset 0:559393b8973f

Initial version: support for searching the archives
author Sebastien Decugis <sdecugis@nict.go.jp>
date Tue, 10 Aug 2010 15:07:25 +0900
parents
children 76549bed2e2f
files trac_plugin/MailmanPlugin.py
diffstat 1 files changed, 61 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/trac_plugin/MailmanPlugin.py	Tue Aug 10 15:07:25 2010 +0900
@@ -0,0 +1,61 @@
+from trac.core import *
+from trac.search import ISearchSource, shorten_result
+from trac.mimeview.api import Mimeview
+from datetime import datetime
+import pytz
+import SwishE
+
+""" Mailing-list search plugin for Trac
+  This plugin relies on a working swish-e index for mailing-list.
+  See for example:
+  http://svn.rot13.org/index.cgi/swish/view/trunk/mailman/index_mailman.pl
+  (assuming the archives are generated by pipermail)
+  
+  It also requires the swish-e python wrapper from 
+  http://py-swish-e.berlios.de/
+  
+  The configuration is static in this file for now, it might be moved to 
+  configuration file or an admin panel later. Each mailing-list is defined by:
+  ( short name, index file, label ).
+"""
+
+MailmanConfig=( 
+    ( 'help', '/var/swish/indexes/help.index', 'help@freediameter.net'),
+    ( 'dev',  '/var/swish/indexes/dev.index',  'dev@freediameter.net')
+  )
+	     
+class MailmanPlugin(Component):
+    """ Search mailing-list archives. """
+    implements(ISearchSource)
+
+    # ISearchSource methods
+    def get_search_filters(self, req):
+        for MLShort, MLIndex, MLLabel in MailmanConfig:
+	   yield ('ML-' + MLShort, MLLabel)
+
+    def get_search_results(self, req, terms, filters):
+	# Prepare keywords
+	query = ''
+	for word in terms :
+		query = query + word + ' '
+	query.rstrip(' ')
+	tokyo_tz = pytz.timezone('Asia/Tokyo') 
+	
+        for MLShort, MLIndex, MLLabel in MailmanConfig:
+            if 'ML-' + MLShort in filters:
+	        self.env.log.debug ('Searching "%s" in index %s' % (query, MLIndex) )
+		# perform the search
+		handle = SwishE.new( MLIndex )
+		#handle.setSort('swishrank')
+		for result in handle.query(query):
+		   date = datetime.fromtimestamp(result.getproperty('sent'))
+		   content = Mimeview(self.env).to_unicode(result.getproperty('swishdescription'))
+		   #(href, title, date, author, excerpt).
+		   yield(result.getproperty('swishdocpath'),
+		      result.getproperty('swishtitle') ,
+		      date.replace(tzinfo=tokyo_tz),
+		      # result.getproperty('name') + ' <' + result.getproperty('email') + '> in ' + MLLabel,
+		      result.getproperty('name') + ' <' + result.getproperty('email') + '>',
+		      shorten_result(content, query))
+
+
"Welcome to our mercurial repository"