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