]> xenbits.xensource.com Git - people/larsk/xenproject-org-gitdm.git/commitdiff
Use a dict of patterns instead of several global variables
authorGermán Póo-Caamaño <gpoo@gnome.org>
Thu, 23 Jun 2011 01:46:49 +0000 (18:46 -0700)
committerGermán Póo-Caamaño <gpoo@gnome.org>
Thu, 23 Jun 2011 02:27:47 +0000 (19:27 -0700)
The dictionary used allows the use of a single meaningful
variable with a cleaner code.  Also, it is not harder to add
new patterns.

Signed-off-by: Germán Póo-Caamaño <gpoo@gnome.org>
gitdm
patterns.py

diff --git a/gitdm b/gitdm
index 67eb77b54d7a9cbf9aa096ce0b82d0817185fcad..87e64469676345e74c037a36d463a550d010ad4a 100755 (executable)
--- a/gitdm
+++ b/gitdm
@@ -14,7 +14,7 @@
 import database, csvdump, ConfigFile, reports
 import getopt, datetime
 import os, re, sys, rfc822, string
-from patterns import *
+from patterns import patterns
 
 Today = datetime.date.today()
 
@@ -168,7 +168,7 @@ def grabpatch():
     global NextLine
     
     while (1):
-        m = Pcommit.match (NextLine)
+        m = patterns['commit'].match (NextLine)
         if m:
             break;
         NextLine = sys.stdin.readline ()
@@ -183,14 +183,14 @@ def grabpatch():
         #
         # If this line starts a new commit, drop out.
         #
-        m = Pcommit.match (Line)
+        m = patterns['commit'].match (Line)
         if m:
             break
         NextLine = sys.stdin.readline ()
         #
         # Maybe it's an author line?
         #
-        m = Pauthor.match (Line)
+        m = patterns['author'].match (Line)
         if m:
             p.email = database.RemapEmail (m.group (2))
             p.author = LookupStoreHacker(m.group (1), p.email)
@@ -198,7 +198,7 @@ def grabpatch():
         #
         # Could be a signed-off-by:
         #
-        m = Psob.match (Line)
+        m = patterns['signed-off-by'].match (Line)
         if m:
             email = database.RemapEmail (m.group (2))
             sobber = LookupStoreHacker(m.group (1), email)
@@ -208,24 +208,26 @@ def grabpatch():
         #
         # Various other tags of interest.
         #
-        m = Preview.match (Line)  # Reviewed-by:
+        m = patterns['reviewed-by'].match (Line)
         if m:
             email = database.RemapEmail (m.group (2))
             p.addreviewer (LookupStoreHacker(m.group (1), email))
             continue
-        m = Ptest.match (Line)    # Tested-by:
+        m = patterns['tested-by'].match (Line)
         if m:
             email = database.RemapEmail (m.group (2))
             p.addtester (LookupStoreHacker (m.group (1), email))
             p.author.testcredit (patch)
             continue
-        m = Prep.match (Line)     # Reported-by:
+        # Reported-by:
+        m = patterns['reported-by'].match (Line)
         if m:
             email = database.RemapEmail (m.group (2))
             p.addreporter (LookupStoreHacker (m.group (1), email))
             p.author.reportcredit (patch)
             continue
-        m = Preptest.match (Line)  # Reported-and-tested-by:
+        # Reported-and-tested-by:
+        m = patterns['reported-and-tested-by'].match (Line)
         if m:
             email = database.RemapEmail (m.group (2))
             h = LookupStoreHacker (m.group (1), email)
@@ -237,14 +239,14 @@ def grabpatch():
         #
         # If this one is a merge, make note of the fact.
         #
-        m = Pmerge.match (Line)
+        m = patterns['merge'].match (Line)
         if m:
             p.merge = 1
             continue
         #
         # See if it's the date.
         #
-        m = Pdate.match (Line)
+        m = patterns['date'].match (Line)
         if m:
             dt = rfc822.parsedate(m.group (2))
             p.date = datetime.date (dt[0], dt[1], dt[2])
@@ -262,26 +264,30 @@ def grabpatch():
             # OK, maybe it's part of the diff itself.
             #
             if not ignore:
-                if Padd.match (Line):
+                if patterns['add'].match (Line):
                     p.added += 1
                     continue
-                if Prem.match (Line):
+                if patterns['rem'].match (Line):
                     p.removed += 1
         else:
             # Get the statistics (lines added/removes) using numstats
             # and without requiring a diff (--numstat instead -p)
-            m = Pnumstat.match (Line)
+            m = patterns['numstat'].match (Line)
             if m:
+                filename = m.group(3)
                 # If we have a file filter, check for file lines.
-                if FileFilter and not FileFilter.search (m.group(3)):
+                if FileFilter and not FileFilter.search (filename):
                     continue
 
                 try:
-                    p.added += int(m.group(1))
-                    p.removed += int(m.group(2))
+                    added = int(m.group(1))
+                    removed = int(m.group(2))
                 except ValueError:
                     # A binary file (image, etc.) is marked with '-'
-                    pass
+                    added = removed = 0
+
+                p.added += added
+                p.removed += removed
 
     if '@' in p.author.name:
         GripeAboutAuthorName (p.author.name)
@@ -299,7 +305,7 @@ def ApplyFileFilter (line, ignore):
     # If this is the first file line (--- a/), set ignore one way
     # or the other.
     #
-    m = Pfilea.match (line)
+    m = patterns['filea'].match (line)
     if m:
         file = m.group (1)
         if FileFilter.search (file):
@@ -308,7 +314,7 @@ def ApplyFileFilter (line, ignore):
     #
     # For the second line, we can turn ignore off, but not on
     #
-    m = Pfileb.match (line)
+    m = patterns['fileb'].match (line)
     if m:
         file = m.group (1)
         if FileFilter.search (file):
index 423c5214486e043809ebf65dc8ef1ddec04e767d..b679049423d9bca4316a7932f78df62699376b30 100644 (file)
@@ -1,10 +1,12 @@
 #
+# -*- coding:utf-8 -*-
 # Pull together regular expressions used in multiple places.
 #
 # This code is part of the LWN git data miner.
 #
 # Copyright 2007-11 Eklektix, Inc.
 # Copyright 2007-11 Jonathan Corbet <corbet@lwn.net>
+# Copyright 2011 Germán Póo-Caamaño <gpoo@gnome.org>
 #
 # This file may be distributed under the terms of the GNU General
 # Public License, version 2.
@@ -16,28 +18,32 @@ import re
 # expressions." Now they have two problems.
 #    -- Jamie Zawinski
 #
-Pemail = r'\s+"?([^<"]+)"?\s<([^>]+)>' # just email addr + name
-Pcommit = re.compile (r'^commit ([0-9a-f ]+)$')
-Pauthor = re.compile (r'^Author:' + Pemail + '$')
-Psob = re.compile (r'^\s+Signed-off-by:' + Pemail + '.*$')
-Pmerge = re.compile (r'^Merge:.*$')
-Padd = re.compile (r'^\+[^+].*$')
-Prem = re.compile (r'^-[^-].*$')
-Pdate = re.compile (r'^(Commit)?Date:\s+(.*)$')
-Pfilea = re.compile (r'^---\s+(.*)$')
-Pfileb = re.compile (r'^\+\+\+\s+(.*)$')
-Preview = re.compile (r'^\s+Reviewed-by:' + Pemail + '.*$')
-Ptest = re.compile (r'^\s+tested-by:' + Pemail + '.*$', re.I)
-Prep = re.compile (r'^\s+Reported-by:' + Pemail + '.*$')
-Preptest = re.compile (r'^\s+reported-and-tested-by:' + Pemail + '.*$', re.I)
-#
-# Merges are described with a variety of lines.
-#
-PExtMerge = re.compile(r'^ +Merge( branch .* of)? ([^ ]+:[^ ]+)\n$')
-PIntMerge = re.compile(r'^ +(Merge|Pull) .* into .*$')
-# PIntMerge2 = re.compile(r"^ +Merge branch(es)? '.*$")
-PIntMerge2 = re.compile(r"^ +Merge .*$")
-#
-# Another way to get the statistics (per file).  It implies --numstat
-Pnumstat = re.compile('^(\d+|-)\s+(\d+|-)\s+(.*)$')
-Prename = re.compile('(.*)\{(.*) => (.*)\}(.*)')
+_pemail = r'\s+"?([^<"]+)"?\s<([^>]+)>' # just email addr + name
+
+patterns = {
+    'commit': re.compile (r'^commit ([0-9a-f ]+)$'),
+    'author': re.compile (r'^Author:' + _pemail + '$'),
+    'signed-off-by': re.compile (r'^\s+Signed-off-by:' + _pemail + '.*$'),
+    'merge': re.compile (r'^Merge:.*$'),
+    'add': re.compile (r'^\+[^+].*$'),
+    'rem': re.compile (r'^-[^-].*$'),
+    'date': re.compile (r'^(Commit)?Date:\s+(.*)$'),
+    # filea, fileb are used only in 'parche mode' (-p)
+    'filea': re.compile (r'^---\s+(.*)$'),
+    'fileb': re.compile (r'^\+\+\+\s+(.*)$'),
+    'reviewed-by': re.compile (r'^\s+Reviewed-by:' + _pemail+ '.*$'),
+    'tested-by': re.compile (r'^\s+tested-by:' + _pemail + '.*$', re.I),
+    'reported-by': re.compile (r'^\s+Reported-by:' + _pemail + '.*$'),
+    'reported-and-tested-by': re.compile (r'^\s+reported-and-tested-by:' + _pemail + '.*$', re.I),
+    #
+    # Merges are described with a variety of lines.
+    #
+    'ExtMerge': re.compile(r'^ +Merge( branch .* of)? ([^ ]+:[^ ]+)\n$'),
+    'IntMerge': re.compile(r'^ +(Merge|Pull) .* into .*$'),
+    # PIntMerge2 = re.compile(r"^ +Merge branch(es)? '.*$"),
+    'IntMerge2': re.compile(r"^ +Merge .*$"),
+    # Another way to get the statistics (per file).
+    # It implies --numstat
+    'numstat': re.compile('^(\d+|-)\s+(\d+|-)\s+(.*)$'),
+    'rename' : re.compile('(.*)\{(.*) => (.*)\}(.*)'),
+}