Author: hwright
Date: Thu Jul 29 19:48:58 2010
New Revision: 980553

URL: http://svn.apache.org/viewvc?rev=980553&view=rev
Log:
Make the Mouse source determining logic a bit easier to extend.

Modified:
    labs/mouse/sources.py

Modified: labs/mouse/sources.py
URL: 
http://svn.apache.org/viewvc/labs/mouse/sources.py?rev=980553&r1=980552&r2=980553&view=diff
==============================================================================
--- labs/mouse/sources.py (original)
+++ labs/mouse/sources.py Thu Jul 29 19:48:58 2010
@@ -17,19 +17,14 @@
 #  specific language governing permissions and limitations
 #  under the License.
 #
-'''mouse is a lighter, friendlier version of RAT (the ASF Release Audit Tool).
+'''The sources module of Mouse.
 
-mouse is intended to replace RAT as a tool for checking license compliance
-within source code repositories, release archives, and directory trees.  It
-is intended to be modular, so as to be invoked from any number of places,
-including build scripts and post-commit hooks.
-
-mouse generates reports in the same format as RAT, and accepts the same
-arguments, excepting the fact that mouse does not modify files as part of
-its operation.  mouse will not attempt to insert missing license headers, but
-it will notify the user of their absence.
+This module has one public function, get_items(), which returns a generator
+for use in Mouse.  It can automatically determine the source type if one is
+not specified.  Sources are checked in the order specified by _source_list.
 
-For information about how to use mouse, run 'mouse.py --help'.'''
+If you want to add another source, simply implement a generator and validator
+function pair, and add them to _source_list.'''
 
 
 import os
@@ -66,47 +61,63 @@ class Item(object):
     return self._content
 
 
+def _get_dir_gen(target):
+  '''Return a generator for Items in the filesystem directory given by 
TARGET'''
+  def dir_gen_func():
+    for (dirpath, dirnames, filenames) in os.walk(target):
+      # ignore .svn directories, if in a working copy
+      if '.svn' in dirnames:
+        dirnames.remove('.svn')
+
+      for filename in filenames:
+        name = os.path.join(dirpath, filename)
+        yield Item(name, open(name))
+
+  return dir_gen_func
+
+
+def _get_tar_gen(target):
+  '''Return a generator for Items in the tarfile (potentially bzip'd or gzip'd)
+     given by TARGET'''
+  tar = tarfile.open(target, 'r')
+
+  def tar_gen_func():
+    for info in tar.getmembers():
+      if info.isdir():
+        continue
+      yield Item(info.name, tar.extractfile(info))
+
+  return tar_gen_func
+
+
+def _get_zip_gen(target):
+  '''Return a generator for Items in the zipfile given by TARGET'''
+  zip = zipfile.ZipFile(target, 'r')
+
+  def zip_gen_func():
+    for info in zip.infolist():
+      if info.filename[-1] == '/':
+        continue
+      yield Item(info.filename, zip.open(info))
+
+  return zip_gen_func
+
+
+_source_list = [
+    ("dir", os.path.isdir,      _get_dir_gen),
+    ("tar", tarfile.is_tarfile, _get_tar_gen),
+    ("zip", zipfile.is_zipfile, _get_zip_gen),
+  ]
+
+
 def get_items(target):
   '''Using TARGET as the source, return a generator or iterable suitable for
   use as input to generate_report().'''
 
-  # is the thing a directory?
-  if os.path.isdir(target):
-    def dir_gen_func():
-      for (dirpath, dirnames, filenames) in os.walk(target):
-        # ignore .svn directories, if in a working copy
-        if '.svn' in dirnames:
-          dirnames.remove('.svn')
-
-        for filename in filenames:
-          name = os.path.join(dirpath, filename)
-          yield Item(name, open(name))
-
-    return dir_gen_func
-  
-  # is the thing a tarfile?
-  if tarfile.is_tarfile(target):
-    tar = tarfile.open(target, 'r')
-
-    def tar_gen_func():
-      for info in tar.getmembers():
-        if info.isdir():
-          continue
-        yield Item(info.name, tar.extractfile(info))
-
-    return tar_gen_func
-
-  # is the thing a zipfile?
-  if zipfile.is_zipfile(target):
-    zip = zipfile.ZipFile(target, 'r')
-
-    def zip_gen_func():
-      for info in zip.infolist():
-        if info.filename[-1] == '/':
-          continue
-        yield Item(info.filename, zip.open(info))
-
-    return zip_gen_func
+  for name, check_func, get_gen_func in _source_list:
+    if not check_func(target):
+      continue
+    return get_gen_func(target)
 
   # if we get to this point, we've no idea what the user gave us, so throw
   # an appropriate exception



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to