1. Alex Willmer
  2. trac-ticketlinks

Commits

Alex Willmer  committed 3f88169

Add unique() utility function, use to remove duplicates in ticket fields

  • Participants
  • Parent commits 6d1fb7e
  • Branches ticket-links-trunk

Comments (0)

Files changed (5)

File trac/ticket/api.py

View file
  • Ignore whitespace
 from trac.core import *
 from trac.perm import IPermissionRequestor, PermissionCache, PermissionSystem
 from trac.resource import IResourceManager
-from trac.util import Ranges
+from trac.util import Ranges, unique
 from trac.util.text import shorten_line
 from trac.util.translation import _, N_, gettext
 from trac.wiki import IWikiSyntaxProvider, WikiParser
     def parse_links(self, value):
        if not value:
            return []
-       return [int(id) for id in self.NUMBERS_RE.findall(value)]
+       return list(unique(int(id) for id in self.NUMBERS_RE.findall(value)))
 
     # IResourceManager methods
 

File trac/ticket/links.py

View file
  • Ignore whitespace
 from trac.ticket.model import Ticket
 from copy import copy
 from trac.core import Component, implements
+from trac.util import unique
 
-def uniq(seq):
-    """Yield unique elements from sequence of hashables, preserving order.
-    """
-    seen = set()
-    for x in seq:
-        if x not in seen:
-            seen.add(x)
-            yield x
-    
 class LinksProvider(Component):
     """Link controller that provides links as specified in the [ticket-links]
     section in the trac.ini configuration file.
             if action == 'resolve' and self.is_blocker(end):
                 blockers = self.find_blockers(ticket, end, [])
                 if blockers:
-                    blockers_str = ', '.join('#%s' % x for x in uniq(blockers))
+                    blockers_str = ', '.join('#%s' % x 
+                                             for x in unique(blockers))
                     msg = ("Cannot resolve this ticket because it is "
                            "blocked by '%s' tickets [%s]" 
                            % (end,  blockers_str))

File trac/ticket/tests/links.py

View file
  • Ignore whitespace
         self.assertEquals([1, 2, 42], ticket_system.parse_links('#1 #2 #42'))
         self.assertEquals([1, 2, 42], ticket_system.parse_links('1, 2, 42'))
         self.assertEquals([1, 2, 42], ticket_system.parse_links('#1, #2, #42'))
+        self.assertEquals([1, 2, 42], ticket_system.parse_links('#1 #2 #42 #1'))
         
     def test_get_ticket_fields(self):
         ticket_system = TicketSystem(self.env)

File trac/util/__init__.py

View file
  • Ignore whitespace
     """Strip `/` from the arguments and join them with a single `/`."""
     return '/'.join(filter(None, (each.strip('/') for each in args if each)))
 
+def unique(seq):
+    """Yield unique elements from sequence of hashables, preserving order.
+    (New in 0.13)
+    """
+    seen = set()
+    return (x for x in seq if x not in seen and not seen.add(x))
 
 # Imports for backward compatibility (at bottom to avoid circular dependencies)
 from trac.core import TracError

File trac/util/tests/__init__.py

View file
  • Ignore whitespace
         self.assertEqual('attachment', util.content_disposition('attachment'))
 
 
+class UniqueTestCase(unittest.TestCase):
+
+    def test_unique(self):
+        self.assertEqual([], list(util.unique([])))
+        self.assertEqual([1,2,3], list(util.unique([1,2,3])))
+        self.assertEqual(['a','b','c'], list(util.unique('abc')))
+        self.assertEqual([1,3,2], 
+                         list(util.unique(x for x in (1,3,1,2,3,1,2,2))))
+
+
 def suite():
     suite = unittest.TestSuite()
     suite.addTest(unittest.makeSuite(AtomicFileTestCase, 'test'))
     suite.addTest(unittest.makeSuite(ContentDispositionTestCase, 'test'))
+    suite.addTest(unittest.makeSuite(UniqueTestCase, 'test'))
     suite.addTest(concurrency.suite())
     suite.addTest(datefmt.suite())
     suite.addTest(presentation.suite())