]> sipb.mit.edu Git - snippets/.git/blobdiff - TracZephyrPlugin/ZephyrPlugin.py
Display 5 lines instead of 255 chars, and truncate with […].
[snippets/.git] / TracZephyrPlugin / ZephyrPlugin.py
index 688749ada13abb9e351c21909aff6e1806c24e2f..88fc2e1aec58031e0f325b7fdb3d5d70f542fc4d 100644 (file)
@@ -1,32 +1,48 @@
 from trac.core import *
 from trac.ticket import ITicketChangeListener
-import os
+import subprocess
 import textwrap
+import shlex
 
 class ZephyrPlugin(Component):
-       implements(ITicketChangeListener)
-       
-       def zwrite(self, id, message):
-               zclass = self.config.get('ZephyrPlugin', 'class')
-               if zclass == '':
-                       return
-               pipe = os.popen('zwrite -q -l -d -c %s -i trac-#%s' % (zclass, id), 'w')
-               pipe.write("\n".join(textwrap.wrap(message)).encode('utf-8', 'replace'))
-               pipe.close()
-       
-       def ticket_created(self, ticket):
-               message = "%s filed a new ticket:\n%s\n\n%s" % (ticket['reporter'], ticket['summary'], ticket['description'][:255])
-               self.zwrite(ticket.id, message)
-       
-       def ticket_changed(self, ticket, comment, author, old_values):
-               if old_values.has_key('status'):
-                       if ticket['status'] == 'closed':
-                               message = "%s closed ticket as %s\n(%s)" % (author, ticket['resolution'], ticket['summary'])
-                       else:
-                               message = "%s set status to %s\n(%s)" % (author, ticket['status'], ticket['summary'])
-               else:
-                       message = "%s updated this ticket\n(%s)" % (author, ticket['summary'])
-               self.zwrite(ticket.id, message)
-       
-       def ticket_deleted(self, ticket):
-               pass
+    implements(ITicketChangeListener)
+    
+    def zwrite(self, id, message):
+        zclass = self.config.get('ZephyrPlugin', 'class')
+        if zclass == '':
+            return
+        command = shlex.split(self.config.get('ZephyrPlugin', 'command'))
+        if not command:
+            command = ['zwrite', '-q', '-l', '-d']
+        p = subprocess.Popen(command +
+                             ['-c', zclass,
+                              '-i', 'trac-#%s' % id],
+                             stdin=subprocess.PIPE)
+        p.stdin.write(message.replace('@', '@@').encode('utf-8', 'replace'))
+        p.stdin.close()
+        p.wait()
+
+    def format_text(self, text):
+        lines = textwrap.fill(text).split('\n')
+        if len(lines) > 5:
+            lines = lines[:5] + [u'[…]']
+        return '\n'.join(lines)
+    
+    def ticket_created(self, ticket):
+        message = "%s filed a new ticket:\n%s\n\n%s" % (ticket['reporter'],
+                                                        ticket['summary'],
+                                                        self.format_text(ticket['description']))
+        self.zwrite(ticket.id, message)
+    
+    def ticket_changed(self, ticket, comment, author, old_values):
+        if old_values.has_key('status'):
+            if ticket['status'] == 'closed':
+                message = "%s closed ticket as %s\n(%s)" % (author, ticket['resolution'], ticket['summary'])
+            else:
+                message = "%s set status to %s\n(%s)" % (author, ticket['status'], ticket['summary'])
+        else:
+            message = "%s updated this ticket\n(%s)" % (author, ticket['summary'])
+        self.zwrite(ticket.id, message)
+    
+    def ticket_deleted(self, ticket):
+        pass