diff scripts/contributors.py @ 4824:b83576aa3f74

contributors.py: Reorganize code for easy testing
author anatoly techtonik <techtonik@gmail.com>
date Sun, 25 Aug 2013 16:06:29 +0300
parents dfef44485418
children b3bf0aa48368
line wrap: on
line diff
--- a/scripts/contributors.py	Sun Aug 25 12:14:49 2013 +0300
+++ b/scripts/contributors.py	Sun Aug 25 16:06:29 2013 +0300
@@ -16,48 +16,26 @@
 # /--
 
 
-if verbose:
-  print("Getting HG log...")
-authorship = check_output('hg log --template "{date(date,\\"%Y\\")},{author}\n"')
-# authorship are strings like
-# 2003,Richard Jones <richard@users.sourceforge.net>
-# ...
 
-if verbose:
-  print("Splitting...")
-# transform to a list of tuples
-authorship = [line.split(',', 1) for line in authorship.splitlines()]
-
-if verbose:
-  print("Sorting...")
-years = {}  # year -> set(author1, author2, ...)
-names = {}  # author -> set(years)
-for year,author in authorship:
-  # years
-  if not year in years:
-    years[year] = set()
-  years[year].add(author)
-  # names
-  if not author in names:
-    names[author] = set()
-  names[author].add(int(year))
-
-
-if contributors_by_year:
-  if verbose:
-    print("Contributors by year...")
-  print('')
-  for year in sorted(years, reverse=True):
-    print(year)
-    for author in sorted(years[year]):
-      print("  " + author)
-  print('')
-
-
-def compress_years(years):
+def compress(years):
   """
   Given a list of years like [2003, 2004, 2007],
   compress it into string like '2003-2004, 2007'
+
+  >>> compress([2002])
+  '2002'
+  >>> compress([2003, 2002])
+  '2002-2003'
+  >>> compress([2009, 2004, 2005, 2006, 2007])
+  '2004-2007, 2009'
+  >>> compress([2001, 2003, 2004, 2005])
+  '2001, 2003-2005'
+  >>> compress([2009, 2011])
+  '2009, 2011'
+  >>> compress([2009, 2010, 2011, 2006, 2007])
+  '2006-2007, 2009-2011'
+  >>> compress([2002, 2003, 2004, 2005, 2006, 2009, 2012])
+  '2002-2006, 2009, 2012'
   """
   years = sorted(years)
   # compress years into string
@@ -81,16 +59,54 @@
   return yearstr
 
 
-if years_for_contributors:
+if __name__ == '__main__':
+  if verbose:
+    print("Getting HG log...")
+  authorship = check_output('hg log --template "{date(date,\\"%Y\\")},{author}\n"')
+  # authorship are strings like
+  # 2003,Richard Jones <richard@users.sourceforge.net>
+  # ...
+
+  if verbose:
+    print("Splitting...")
+  # transform to a list of tuples
+  authorship = [line.split(',', 1) for line in authorship.splitlines()]
+
   if verbose:
-    print("Years for each contributor...")
-  print('')
-  for author in sorted(names):
-    years = list(names[author])
-    yearstr = compress_years(years)
-    
-    if 1: #DEBUG
-      print(years, yearstr, author)
-    else:
-      print(yearstr, author)
-  print('')
+    print("Sorting...")
+  years = {}  # year -> set(author1, author2, ...)
+  names = {}  # author -> set(years)
+  for year,author in authorship:
+    # years
+    if not year in years:
+      years[year] = set()
+    years[year].add(author)
+    # names
+    if not author in names:
+      names[author] = set()
+    names[author].add(int(year))
+
+
+  if contributors_by_year:
+    if verbose:
+      print("Contributors by year...")
+    print('')
+    for year in sorted(years, reverse=True):
+      print(year)
+      for author in sorted(years[year]):
+        print("  " + author)
+    print('')
+
+  if years_for_contributors:
+    if verbose:
+      print("Years for each contributor...")
+    print('')
+    for author in sorted(names):
+      years = list(names[author])
+      yearstr = compress(years)
+
+      if 1: #DEBUG
+        print(years, yearstr, author)
+      else:
+        print(yearstr, author)
+    print('')

Roundup Issue Tracker: http://roundup-tracker.org/