Author: hwright
Date: Tue Jul 27 18:24:21 2010
New Revision: 979793
URL: http://svn.apache.org/viewvc?rev=979793&view=rev
Log:
Add the Item class, instead of relying upon unnamed tuple members.
* mouse.py
(Item): New class.
(Resource): Use the Item object, instead of a tuple.
(generate_report): Same (and improve docs).
(filter_excludes): Same.
(get_items): Same.
(match): Remove (don't recall the purpose here).
(main): Remove the no-op call to match().
Modified:
labs/mouse/mouse.py
Modified: labs/mouse/mouse.py
URL:
http://svn.apache.org/viewvc/labs/mouse/mouse.py?rev=979793&r1=979792&r2=979793&view=diff
==============================================================================
--- labs/mouse/mouse.py (original)
+++ labs/mouse/mouse.py Tue Jul 27 18:24:21 2010
@@ -58,14 +58,32 @@ class _UnknownArchiveError(Exception):
return "Unable to read archive '%s'" % self.target
-class Resource(object):
+class Item(object):
+ '''An item which needs to be matched.'''
def __init__(self, name, file):
- self._name = name
+ '''NAME is a label for the object. FILE is a file-like object from which
+ the file contents will be grabbed. It does not need to be seekable,
+ and its file pointer may not be preserved.'''
+ self.name = name
+ self.file = file
+ self._content = None
+
+ def get_content(self):
+ '''Return the contents of this item.'''
+ if not self._content:
+ self._content = file.read()
+ return self._content
+
+
+class Resource(object):
+
+ def __init__(self, item):
+ self._item = item
def to_element(self):
elem = ElementTree.Element('resource')
- elem.set('name', self._name)
+ elem.set('name', self._item.name)
elem.append(ElementTree.Element('header-sample'))
elem.append(ElementTree.Element('header-type'))
@@ -77,20 +95,16 @@ class Resource(object):
def generate_report(items):
- '''Run mouse on the given TARGET, using the OPTIONS to modify behavior.
-
- In general usage, OPTIONS will be a result of using an optparse.OptionParser
- object created as a result of parsing the command line. Direct callers
- of this function should mimic the behavior of such an object.
+ '''Return a generator which will produce Resource objects for each item
+ in ITEMS.
ITEMS is a generator (or other callable which will produce an iterable)
- which returns objects which mouse is to process. ITEMS should return tuples
- of the form (LABEL, FILE), where LABEL is a textual label for the object, and
- FILE is a file-like object which can be used to get the text of the
object.'''
+ which returns objects which mouse is to process. ITEMS should return
+ objects of type Item (which see for details).'''
def report_generator():
for item in items():
- yield Resource(item[0], item[1])
+ yield Resource(item)
return report_generator
@@ -123,7 +137,7 @@ def filter_excludes(items, excludes):
for item in items():
matched = False
for exclude in excludes:
- if exclude.match(item[0]):
+ if exclude.match(item.name):
matched = True
break
if matched:
@@ -148,7 +162,7 @@ def get_items(target):
for filename in filenames:
name = os.path.join(dirpath, filename)
- yield (name, open(name))
+ yield Item(name, open(name))
return dir_gen_func
@@ -160,7 +174,7 @@ def get_items(target):
for info in tar.getmembers():
if info.isdir():
continue
- yield (info.name, tar.extractfile(info))
+ yield Item(info.name, tar.extractfile(info))
return tar_gen_func
@@ -172,7 +186,7 @@ def get_items(target):
for info in zip.infolist():
if info.filename[-1] == '/':
continue
- yield (info.filename, zip.open(info))
+ yield Item(info.filename, zip.open(info))
return zip_gen_func
@@ -181,14 +195,6 @@ def get_items(target):
raise _UnknownArchiveError(target)
-def match(items):
- def match_generator():
- for item in items():
- yield (item[0],)
-
- return match_generator
-
-
def main():
'Parse the command line arguments, and use them to generate a mouse report.'
@@ -237,8 +243,7 @@ def main():
items = filter_excludes(get_items(args[0]),
open(options.exclude).read().split())
- report_items = match(items)
- report = generate_report(report_items)
+ report = generate_report(items)
output = process_report(report)
if options.stylesheet:
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]