1.1 --- a/astgrep.py Wed Oct 29 02:17:19 2008 +0100
1.2 +++ b/astgrep.py Fri Oct 31 01:27:54 2008 +0100
1.3 @@ -34,26 +34,26 @@
1.4
1.5 # Search functions.
1.6
1.7 -def search_recursive(directory, term_type, term, op=None):
1.8 +def search_recursive(directory, term_type, op=None):
1.9
1.10 """
1.11 Search files within the filesystem below 'directory' for terms having the
1.12 - given 'term_type' whose value matches the specified 'term'.
1.13 + given 'term_type', using 'op' (if specified) to match a search term.
1.14 """
1.15
1.16 results = []
1.17 for path, directories, filenames in os.walk(directory):
1.18 for filename in filenames:
1.19 if os.path.splitext(filename)[-1] == os.path.extsep + "py":
1.20 - results += search_file(os.path.join(path, filename), term_type, term, op)
1.21 + results += search_file(os.path.join(path, filename), term_type, op)
1.22 return results
1.23
1.24 -def search_file(filename, term_type, term, op=None):
1.25 +def search_file(filename, term_type, op=None):
1.26
1.27 """
1.28 Search the file with the given 'filename' for terms having the given
1.29 - 'term_type' whose value matches the specified 'term'. If 'term_type' is
1.30 - given as "*", attempt to match any term type.
1.31 + 'term_type', using 'op' (if specified) to match a search term. If
1.32 + 'term_type' is given as "*", attempt to match any term type.
1.33 """
1.34
1.35 try:
1.36 @@ -66,14 +66,14 @@
1.37 else:
1.38 cls = None
1.39
1.40 - return search_tree(node, cls, term, op, filename)
1.41 + return search_tree(node, cls, op, filename)
1.42
1.43 -def search_tree(node, cls, term, op=None, filename=None):
1.44 +def search_tree(node, cls, op=None, filename=None):
1.45
1.46 """
1.47 Search the tree rooted at the given 'node' for nodes of the given class
1.48 - 'cls' for content matching the specified 'term'. If 'cls' is None, all node
1.49 - types will be considered for matches.
1.50 + 'cls', using 'op' (if specified) to match a search term. If 'cls' is None,
1.51 + all node types will be considered for matches.
1.52
1.53 Return a list of results of the form (node, value, filename).
1.54 """
1.55 @@ -91,24 +91,60 @@
1.56 if op is None:
1.57 results.append((node, None, filename))
1.58 else:
1.59 + found_str = 0
1.60 +
1.61 for child in node.getChildren():
1.62
1.63 # Test literals.
1.64
1.65 - if isinstance(child, (str, int, float, long, bool)):
1.66 + if isinstance(child, (int, float, long, bool)):
1.67 if op(str(child)):
1.68 results.append((node, child, filename))
1.69
1.70 - # Only check a single string child value since subsequent
1.71 - # values are typically docstrings.
1.72 + # Only check a single string child value since subsequent
1.73 + # values are typically docstrings.
1.74
1.75 - if isinstance(child, str):
1.76 - break
1.77 + elif not found_str and isinstance(child, str):
1.78 + found_str = 1
1.79 + if op(child):
1.80 + results.append((node, child, filename))
1.81 +
1.82 + # Argument lists, globals and imports.
1.83 +
1.84 + elif isinstance(child, list):
1.85 + results += search_list(child, node, op, filename)
1.86
1.87 # Search within nodes, even if matches have already been found.
1.88
1.89 for child in node.getChildNodes():
1.90 - results += search_tree(child, cls, term, op, filename)
1.91 + results += search_tree(child, cls, op, filename)
1.92 +
1.93 + return results
1.94 +
1.95 +def search_list(values, node, op=None, filename=None):
1.96 +
1.97 + """
1.98 + Search the given 'values' from the given 'node', using 'op' (if specified)
1.99 + to match a search term.
1.100 +
1.101 + Return a list of results of the form (node, value, filename).
1.102 + """
1.103 +
1.104 + results = []
1.105 +
1.106 + for value in values:
1.107 +
1.108 + # Test strings.
1.109 +
1.110 + if isinstance(value, str) and op(str(value)):
1.111 + results.append((node, value, filename))
1.112 +
1.113 + # Test import tuples.
1.114 +
1.115 + elif isinstance(value, tuple):
1.116 + for subvalue in value:
1.117 + if isinstance(value, str) and op(str(subvalue)):
1.118 + results.append((node, subvalue, filename))
1.119
1.120 return results
1.121
1.122 @@ -209,9 +245,9 @@
1.123
1.124 for filename in args["FILENAME"]:
1.125 if os.path.isfile(filename):
1.126 - results += search_file(filename, term_type, term, op)
1.127 + results += search_file(filename, term_type, op)
1.128 elif recursive and os.path.isdir(filename):
1.129 - results += search_recursive(filename, term_type, term, op)
1.130 + results += search_recursive(filename, term_type, op)
1.131
1.132 # Present the results.
1.133