[Python-checkins] r53172 - sandbox/trunk/2to3/fixes/basefix.py sandbox/trunk/2to3/fixes/fix_apply.py

guido.van.rossum python-checkins at python.org
Thu Dec 28 22:19:41 CET 2006


Author: guido.van.rossum
Date: Thu Dec 28 22:19:41 2006
New Revision: 53172
Added:
 sandbox/trunk/2to3/fixes/basefix.py (contents, props changed)
Modified:
 sandbox/trunk/2to3/fixes/fix_apply.py
Log:
Add a fixer base class that provides most of the boiler plate code.
Use in in one example fix (the rest will come later).
Added: sandbox/trunk/2to3/fixes/basefix.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/fixes/basefix.py	Thu Dec 28 22:19:41 2006
@@ -0,0 +1,52 @@
+# Copyright 2006 Google, Inc. All Rights Reserved.
+# Licensed to PSF under a Contributor Agreement.
+
+"""Base class for fixers (optional, but recommended)."""
+
+# Local imports
+import patcomp
+import pygram
+
+
+class BaseFix(object):
+
+ """Optional base class for fixers."""
+
+ PATTERN = None # Subclass *must* override with a string literal
+ pattern = None # Compiled pattern, set by compile_pattern()
+ options = None # Options object passed to initializer
+
+ # Shortcut for access to Python grammar symbols
+ syms = pygram.python_symbols
+
+ def __init__(self, options):
+ """Initializer. Subclass may override."""
+ self.options = options
+ self.compile_pattern()
+
+ def compile_pattern(self):
+ """Compiles self.PATTERN into self.pattern.
+
+ Subclass may override if it doesn't want to use
+ self.{pattern,PATTERN} in .match().
+ """
+ self.pattern = patcomp.PatternCompiler().compile_pattern(self.PATTERN)
+
+ def match(self, node):
+ """Returns match for a given parse tree node.
+
+ Subclass may override.
+ """
+ results = {}
+ return self.pattern.match(node, results) and results
+
+ def transform(self, node):
+ """Returns the transformation for a given parse tree node.
+
+ Subclass must override.
+ """
+ return None
+
+ def parenthesize(self, node):
+ """Wrapper around pygram.parenthesize()."""
+ return pygram.parenthesize(node)
Modified: sandbox/trunk/2to3/fixes/fix_apply.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_apply.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_apply.py	Thu Dec 28 22:19:41 2006
@@ -8,38 +8,27 @@
 
 # Local imports
 import pytree
-import patcomp
-import pygram
+from fixes import basefix
 
-syms = pygram.python_symbols
-pat_compile = patcomp.PatternCompiler().compile_pattern
 
-PATTERN = """
-power< 'apply'
- trailer<
- '('
- arglist<
- (not argument<NAME '=' any>) func=any ','
- (not argument<NAME '=' any>) args=any [','
- (not argument<NAME '=' any>) kwds=any] [',']
+class FixApply(basefix.BaseFix):
+
+ PATTERN = """
+ power< 'apply'
+ trailer<
+ '('
+ arglist<
+ (not argument<NAME '=' any>) func=any ','
+ (not argument<NAME '=' any>) args=any [','
+ (not argument<NAME '=' any>) kwds=any] [',']
+ >
+ ')'
 >
- ')'
 >
->
-"""
-
-
-class FixApply(object):
-
- def __init__(self, options):
- self.options = options
- self.pattern = pat_compile(PATTERN)
-
- def match(self, node):
- results = {}
- return self.pattern.match(node, results) and results
+ """
 
 def transform(self, node):
+ syms = self.syms
 results = self.match(node)
 assert results
 func = results["func"]
@@ -51,7 +40,7 @@
 (func.type != syms.power or
 func.children[-2].type == token.DOUBLESTAR)):
 # Need to parenthesize
- func = pygram.parenthesize(func)
+ func = self.parenthesize(func)
 func.set_prefix("")
 args = args.clone()
 args.set_prefix("")


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /