Rev 11: Change the formatter a bit. in http://bazaar.launchpad.net/~bzr/bzr-update-copyright/trunk

John Arbash Meinel john at arbash-meinel.com
Tue Jan 12 16:05:37 GMT 2010


At http://bazaar.launchpad.net/~bzr/bzr-update-copyright/trunk

------------------------------------------------------------
revno: 11
revision-id: john at arbash-meinel.com-20100112160509-p0s9puwm3uagz96o
parent: john at arbash-meinel.com-20100111181839-r90gzh3ycj2jueca
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: trunk
timestamp: Tue 2010-01-12 10:05:09 -0600
message:
  Change the formatter a bit.
  
  If we have more than 3 years continuously, collapse to a range.
  So you'll get '2008, 2009, 2010', but '2007-2010'.
  I'm trying to decide between collapsing at 3 instead of 2.
-------------- next part --------------
=== modified file 'test_update_copyright.py'
--- a/test_update_copyright.py	2010-01-08 21:36:39 +0000
+++ b/test_update_copyright.py	2010-01-12 16:05:09 +0000
@@ -69,6 +69,17 @@
             '# Copyright (c) ', '2008-2010 ', 'Foobar\r',
             '# Copyright (c) 2008-2010 Foobar\r\n')
 
+    def assertFormatYears(self, expected, years):
+        self.assertEqual(expected, update_copyright.format_years(set(years)))
+
+    def test_format_years(self):
+        self.assertFormatYears('2008, 2009', [2008, 2009])
+        self.assertFormatYears('2007, 2009', [2007, 2009])
+        self.assertFormatYears('2007, 2008, 2009', [2007, 2008, 2009])
+        self.assertFormatYears('2007-2010', [2007, 2008, 2009, 2010])
+        self.assertFormatYears('2005, 2007-2010',
+                               [2005, 2007, 2008, 2009, 2010])
+
 
 class TestUpdateCopyrightDisk(tests.TestCaseWithTransport):
 

=== modified file 'update_copyright.py'
--- a/update_copyright.py	2010-01-08 21:36:39 +0000
+++ b/update_copyright.py	2010-01-12 16:05:09 +0000
@@ -51,7 +51,29 @@
 
 def format_years(years):
     """Format a bunch of years back into a date string."""
-    return ', '.join(map(str, sorted(years)))
+    pending_years = []
+    last_year = None
+    out_years = []
+
+    def update():
+        if len(pending_years) > 3:
+            # Use a range
+            out_years.append('%d-%d'
+                             % (pending_years[0], pending_years[-1]))
+        else:
+            # Add them individually
+            out_years.extend(map(str, pending_years))
+    for year in sorted(years):
+        if last_year is None:
+            pending_years = [year]
+        elif year == (last_year + 1):
+            pending_years.append(year)
+        else:
+            update()
+            pending_years = [year]
+        last_year = year
+    update()
+    return ', '.join(out_years)
 
 
 def _iter_all_files(tree, relpaths, basis_tree):



More information about the bazaar-commits mailing list