Remove sipbmp3-itunes.bak, now that /mit/snippets supports submodules.
[snippets/.git] / TracZephyrPlugin / ZephyrPlugin.py
1 # -*- coding: utf-8 -*-
2
3 from trac.core import *
4 from trac.ticket import ITicketChangeListener
5 import subprocess
6 import re
7 import textwrap
8 import shlex
9
10 class ZephyrPlugin(Component):
11     implements(ITicketChangeListener)
12     
13     def zwrite(self, id, message):
14         zclass = self.config.get('ZephyrPlugin', 'class')
15         if zclass == '':
16             return
17         command = shlex.split(self.config.get('ZephyrPlugin', 'command').encode('utf-8'))
18         if not command:
19             command = ['zwrite', '-q', '-l', '-d']
20         p = subprocess.Popen(command +
21                              ['-c', zclass,
22                               '-i', 'trac-#%s' % id],
23                              stdin=subprocess.PIPE)
24         p.stdin.write(message.replace('@', '@@').encode('utf-8', 'replace'))
25         p.stdin.close()
26         p.wait()
27
28     def format_text(self, text):
29         text = re.sub(re.compile('^(?:> .*\n)+', re.MULTILINE), u'> […]\n', text)
30         lines = textwrap.fill(text).split('\n')
31         if len(lines) > 5:
32             lines = lines[:5] + [u'[…]']
33         return '\n'.join(lines)
34     
35     def ticket_created(self, ticket):
36         message = "%s filed a new ticket:\n%s\n\n%s" % (ticket['reporter'],
37                                                         ticket['summary'],
38                                                         self.format_text(ticket['description']))
39         self.zwrite(ticket.id, message)
40     
41     def ticket_changed(self, ticket, comment, author, old_values):
42         message = "(%s)\n" % ticket['summary']
43         for field in ticket.fields:
44             name = field['name']
45             if name not in old_values:
46                 pass
47             elif field['type'] == 'textarea':
48                 message += "%s changed %s to:\n%s\n" % (author, name, self.format_text(ticket[name]))
49             elif ticket[name] and old_values[name]:
50                 message += "%s changed %s from %s to %s.\n" % (author, name, old_values[name], ticket[name])
51             elif ticket[name]:
52                 message += "%s set %s to %s.\n" % (author, name, ticket[name])
53             elif old_values[name]:
54                 message += "%s deleted %s.\n" % (author, name)
55             else:
56                 message += "%s changed %s.\n" % (author, name)
57         if comment:
58             message += "%s commented:\n%s\n" % (author, self.format_text(comment))
59         self.zwrite(ticket.id, message)
60     
61     def ticket_deleted(self, ticket):
62         message = "%s deleted ticket %d" % (author, ticket.id)
63         self.zwrite(ticket.id, message)