# HG changeset patch # User Paul Boddie # Date 1225412874 -3600 # Node ID 8d9a908005426ea21ff036b9cddb1aefbf357e36 # Parent 4a0acdfbb1a5331700d4abfa3daabfbd112c4a59 Removed redundant term parameter from various functions. Added support for arguments, globals and imports. Moved script installation into the setup script. diff -r 4a0acdfbb1a5 -r 8d9a90800542 astgrep.py --- a/astgrep.py Wed Oct 29 02:17:19 2008 +0100 +++ b/astgrep.py Fri Oct 31 01:27:54 2008 +0100 @@ -34,26 +34,26 @@ # Search functions. -def search_recursive(directory, term_type, term, op=None): +def search_recursive(directory, term_type, op=None): """ Search files within the filesystem below 'directory' for terms having the - given 'term_type' whose value matches the specified 'term'. + given 'term_type', using 'op' (if specified) to match a search term. """ results = [] for path, directories, filenames in os.walk(directory): for filename in filenames: if os.path.splitext(filename)[-1] == os.path.extsep + "py": - results += search_file(os.path.join(path, filename), term_type, term, op) + results += search_file(os.path.join(path, filename), term_type, op) return results -def search_file(filename, term_type, term, op=None): +def search_file(filename, term_type, op=None): """ Search the file with the given 'filename' for terms having the given - 'term_type' whose value matches the specified 'term'. If 'term_type' is - given as "*", attempt to match any term type. + 'term_type', using 'op' (if specified) to match a search term. If + 'term_type' is given as "*", attempt to match any term type. """ try: @@ -66,14 +66,14 @@ else: cls = None - return search_tree(node, cls, term, op, filename) + return search_tree(node, cls, op, filename) -def search_tree(node, cls, term, op=None, filename=None): +def search_tree(node, cls, op=None, filename=None): """ Search the tree rooted at the given 'node' for nodes of the given class - 'cls' for content matching the specified 'term'. If 'cls' is None, all node - types will be considered for matches. + 'cls', using 'op' (if specified) to match a search term. If 'cls' is None, + all node types will be considered for matches. Return a list of results of the form (node, value, filename). """ @@ -91,24 +91,60 @@ if op is None: results.append((node, None, filename)) else: + found_str = 0 + for child in node.getChildren(): # Test literals. - if isinstance(child, (str, int, float, long, bool)): + if isinstance(child, (int, float, long, bool)): if op(str(child)): results.append((node, child, filename)) - # Only check a single string child value since subsequent - # values are typically docstrings. + # Only check a single string child value since subsequent + # values are typically docstrings. - if isinstance(child, str): - break + elif not found_str and isinstance(child, str): + found_str = 1 + if op(child): + results.append((node, child, filename)) + + # Argument lists, globals and imports. + + elif isinstance(child, list): + results += search_list(child, node, op, filename) # Search within nodes, even if matches have already been found. for child in node.getChildNodes(): - results += search_tree(child, cls, term, op, filename) + results += search_tree(child, cls, op, filename) + + return results + +def search_list(values, node, op=None, filename=None): + + """ + Search the given 'values' from the given 'node', using 'op' (if specified) + to match a search term. + + Return a list of results of the form (node, value, filename). + """ + + results = [] + + for value in values: + + # Test strings. + + if isinstance(value, str) and op(str(value)): + results.append((node, value, filename)) + + # Test import tuples. + + elif isinstance(value, tuple): + for subvalue in value: + if isinstance(value, str) and op(str(subvalue)): + results.append((node, subvalue, filename)) return results @@ -209,9 +245,9 @@ for filename in args["FILENAME"]: if os.path.isfile(filename): - results += search_file(filename, term_type, term, op) + results += search_file(filename, term_type, op) elif recursive and os.path.isdir(filename): - results += search_recursive(filename, term_type, term, op) + results += search_recursive(filename, term_type, op) # Present the results. diff -r 4a0acdfbb1a5 -r 8d9a90800542 packages/ubuntu-gutsy/python-astgrep/debian/python-astgrep.install --- a/packages/ubuntu-gutsy/python-astgrep/debian/python-astgrep.install Wed Oct 29 02:17:19 2008 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -scripts/astgrep usr/bin/ diff -r 4a0acdfbb1a5 -r 8d9a90800542 setup.py --- a/setup.py Wed Oct 29 02:17:19 2008 +0100 +++ b/setup.py Fri Oct 31 01:27:54 2008 +0100 @@ -11,5 +11,6 @@ author_email = "paul@boddie.org.uk", url = "http://www.boddie.org.uk/python/astgrep.html", version = astgrep.__version__, - py_modules = ["astgrep"] + py_modules = ["astgrep"], + scripts = ["scripts/astgrep"] )