1.1 --- a/astgrep.py Sun Oct 26 21:02:17 2008 +0100
1.2 +++ b/astgrep.py Mon Oct 27 00:21:08 2008 +0100
1.3 @@ -31,7 +31,7 @@
1.4 """
1.5
1.6 results = []
1.7 - for path, directories, filenames in os.walk(args["directory"]):
1.8 + for path, directories, filenames in os.walk(directory):
1.9 for filename in filenames:
1.10 if os.path.splitext(filename)[-1] == os.path.extsep + "py":
1.11 results += search_file(os.path.join(path, filename), term_type, term, op)
1.12 @@ -64,7 +64,7 @@
1.13 results.append((node, None, filename))
1.14 else:
1.15 for child in node.getChildren():
1.16 - if isinstance(child, (str, unicode, int, float, long, bool)) and op(unicode(child), term):
1.17 + if isinstance(child, (str, unicode, int, float, long, bool)) and op(unicode(child)):
1.18 results.append((node, child, filename))
1.19 break
1.20
1.21 @@ -75,15 +75,6 @@
1.22
1.23 return results
1.24
1.25 -def get_operator(name):
1.26 -
1.27 - "Return the operator having the given 'name'."
1.28 -
1.29 - if name is None:
1.30 - return unicode.__eq__
1.31 - else:
1.32 - return getattr(unicode, name)
1.33 -
1.34 def expand_results(results):
1.35
1.36 """
1.37 @@ -106,12 +97,11 @@
1.38 # Command syntax.
1.39
1.40 syntax_description = """
1.41 - [ -n ]
1.42 - [ -v ]
1.43 - -t <term-type>
1.44 - [ -o <operator> ]
1.45 - [ -e <expression> ]
1.46 - ( ( -r <directory> ) | <filename> )
1.47 + [ -n | --line-number ]
1.48 + [ -p | --print-token ]
1.49 + ( ( -t TERM_TYPE ) | ( --type=TERM_TYPE ) )
1.50 + [ ( -e PATTERN ) | ( --regexp=PATTERN ) ]
1.51 + ( ( ( -r | -R | --recursive ) DIRECTORY ) | FILENAME )
1.52 """
1.53
1.54 # Main program.
1.55 @@ -119,6 +109,7 @@
1.56 if __name__ == "__main__":
1.57 import sys
1.58 import cmdsyntax
1.59 + import re
1.60 import linecache
1.61
1.62 # Match command arguments.
1.63 @@ -135,20 +126,20 @@
1.64
1.65 # Get the search details.
1.66
1.67 - term_type = args["term-type"]
1.68 - term = args.get("expression")
1.69 + term_type = args["TERM_TYPE"]
1.70 + term = args.get("PATTERN")
1.71
1.72 if term is None:
1.73 op = None
1.74 else:
1.75 - op = get_operator(args.get("operator"))
1.76 + op = re.compile(term).match
1.77
1.78 # Perform the search either in a single file or in a directory hierarchy.
1.79
1.80 - if args.has_key("filename"):
1.81 - results = search_file(args["filename"], term_type, term, op)
1.82 + if args.has_key("FILENAME"):
1.83 + results = search_file(args["FILENAME"], term_type, term, op)
1.84 else:
1.85 - results = search_recursive(args["directory"], term_type, term, op)
1.86 + results = search_recursive(args["DIRECTORY"], term_type, term, op)
1.87
1.88 # Present the results.
1.89
1.90 @@ -156,11 +147,11 @@
1.91 format = "%s:"
1.92 output = [filename]
1.93
1.94 - if args.has_key("n"):
1.95 + if args.has_key("n") or args.has_key("line-number"):
1.96 format += "%d:"
1.97 output.append(lineno)
1.98
1.99 - if args.has_key("v"):
1.100 + if args.has_key("p"):
1.101 if value is not None:
1.102 format += "%r:"
1.103 output.append(value)