summaryrefslogtreecommitdiff
path: root/coverage/files.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2010-06-13 21:46:35 -0400
committerNed Batchelder <ned@nedbatchelder.com>2010-06-13 21:46:35 -0400
commitcf7fa58279cf644c47864485260a7139d9608b2d (patch)
treeb568ddf59350961a2c4e137a4321ce12093d685e /coverage/files.py
parentf198d9d2c0df551ce79d97eb448a62f8bdb0cf26 (diff)
downloadpython-coveragepy-git-cf7fa58279cf644c47864485260a7139d9608b2d.tar.gz
The 'source' option is a list of directories or packages to limit coverage's attention.
Diffstat (limited to 'coverage/files.py')
-rw-r--r--coverage/files.py36
1 files changed, 35 insertions, 1 deletions
diff --git a/coverage/files.py b/coverage/files.py
index 5690679f..d74b4d79 100644
--- a/coverage/files.py
+++ b/coverage/files.py
@@ -1,6 +1,6 @@
"""File wrangling."""
-import os, sys
+import fnmatch, os, sys
class FileLocator(object):
"""Understand how filenames work."""
@@ -76,3 +76,37 @@ class FileLocator(object):
data = data.decode('utf8') # TODO: How to do this properly?
return data
return None
+
+
+class TreeMatcher(object):
+ """A matcher for files in a tree."""
+ def __init__(self, directories):
+ self.dirs = directories[:]
+
+ def add(self, directory):
+ """Add another directory to the list we match for."""
+ self.dirs.append(directory)
+
+ def match(self, fpath):
+ """Does `fpath` indicate a file in one of our trees?"""
+ for d in self.dirs:
+ if fpath.startswith(d):
+ if fpath == d:
+ # This is the same file!
+ return True
+ if fpath[len(d)] == os.sep:
+ # This is a file in the directory
+ return True
+ return False
+
+class FnmatchMatcher(object):
+ """A matcher for files by filename pattern."""
+ def __init__(self, pats):
+ self.pats = pats[:]
+
+ def match(self, fpath):
+ """Does `fpath` match one of our filename patterns?"""
+ for pat in self.pats:
+ if fnmatch.fnmatch(fpath, pat):
+ return True
+ return False