Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(17)

Side by Side Diff: Lib/lib2to3/fixer_base.py

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/inspect.py ('k') | Lib/logging/handlers.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2006 Google, Inc. All Rights Reserved. 1 # Copyright 2006 Google, Inc. All Rights Reserved.
2 # Licensed to PSF under a Contributor Agreement. 2 # Licensed to PSF under a Contributor Agreement.
3 3
4 """Base class for fixers (optional, but recommended).""" 4 """Base class for fixers (optional, but recommended)."""
5 5
6 # Python imports 6 # Python imports
7 import logging 7 import logging
8 import itertools 8 import itertools
9 9
10 # Local imports 10 # Local imports
11 from .patcomp import PatternCompiler 11 from .patcomp import PatternCompiler
12 from . import pygram 12 from . import pygram
13 from .fixer_util import does_tree_import 13 from .fixer_util import does_tree_import
14 14
15 class BaseFix(object): 15 class BaseFix(object):
16 16
17 """Optional base class for fixers. 17 """Optional base class for fixers.
18 18
19 The subclass name must be FixFooBar where FooBar is the result of 19 The subclass name must be FixFooBar where FooBar is the result of
20 removing underscores and capitalizing the words of the fix name. 20 removing underscores and capitalizing the words of the fix name.
21 For example, the class name for a fixer named 'has_key' should be 21 For example, the class name for a fixer named 'has_key' should be
22 FixHasKey. 22 FixHasKey.
23 """ 23 """
24 24
25 PATTERN = None # Most subclasses should override with a string literal 25 PATTERN = None # Most subclasses should override with a string literal
26 pattern = None # Compiled pattern, set by compile_pattern() 26 pattern = None # Compiled pattern, set by compile_pattern()
27 pattern_tree = None # Tree representation of the pattern 27 pattern_tree = None # Tree representation of the pattern
28 options = None # Options object passed to initializer 28 options = None # Options object passed to initializer
29 filename = None # The filename (set by set_filename) 29 filename = None # The filename (set by set_filename)
30 logger = None # A logger (set by set_filename)
30 numbers = itertools.count(1) # For new_name() 31 numbers = itertools.count(1) # For new_name()
31 used_names = set() # A set of all used NAMEs 32 used_names = set() # A set of all used NAMEs
32 order = "post" # Does the fixer prefer pre- or post-order traversal 33 order = "post" # Does the fixer prefer pre- or post-order traversal
33 explicit = False # Is this ignored by refactor.py -f all? 34 explicit = False # Is this ignored by refactor.py -f all?
34 run_order = 5 # Fixers will be sorted by run order before execution 35 run_order = 5 # Fixers will be sorted by run order before execution
35 # Lower numbers will be run first. 36 # Lower numbers will be run first.
36 _accept_type = None # [Advanced and not public] This tells RefactoringTool 37 _accept_type = None # [Advanced and not public] This tells RefactoringTool
37 # which node type to accept when there's not a pattern. 38 # which node type to accept when there's not a pattern.
38 39
39 keep_line_order = False # For the bottom matcher: match with the 40 keep_line_order = False # For the bottom matcher: match with the
(...skipping 22 matching lines...) Expand all
62 63
63 Subclass may override if it doesn't want to use 64 Subclass may override if it doesn't want to use
64 self.{pattern,PATTERN} in .match(). 65 self.{pattern,PATTERN} in .match().
65 """ 66 """
66 if self.PATTERN is not None: 67 if self.PATTERN is not None:
67 PC = PatternCompiler() 68 PC = PatternCompiler()
68 self.pattern, self.pattern_tree = PC.compile_pattern(self.PATTERN, 69 self.pattern, self.pattern_tree = PC.compile_pattern(self.PATTERN,
69 with_tree=True) 70 with_tree=True)
70 71
71 def set_filename(self, filename): 72 def set_filename(self, filename):
72 """Set the filename. 73 """Set the filename, and a logger derived from it.
73 74
74 The main refactoring tool should call this. 75 The main refactoring tool should call this.
75 """ 76 """
76 self.filename = filename 77 self.filename = filename
78 self.logger = logging.getLogger(filename)
77 79
78 def match(self, node): 80 def match(self, node):
79 """Returns match for a given parse tree node. 81 """Returns match for a given parse tree node.
80 82
81 Should return a true or false object (not necessarily a bool). 83 Should return a true or false object (not necessarily a bool).
82 It may return a non-empty dict of matching sub-nodes as 84 It may return a non-empty dict of matching sub-nodes as
83 returned by a matching pattern. 85 returned by a matching pattern.
84 86
85 Subclass may override. 87 Subclass may override.
86 """ 88 """
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 self._should_skip = None 180 self._should_skip = None
179 181
180 def should_skip(self, node): 182 def should_skip(self, node):
181 if self._should_skip is not None: 183 if self._should_skip is not None:
182 return self._should_skip 184 return self._should_skip
183 pkg = self.skip_on.split(".") 185 pkg = self.skip_on.split(".")
184 name = pkg[-1] 186 name = pkg[-1]
185 pkg = ".".join(pkg[:-1]) 187 pkg = ".".join(pkg[:-1])
186 self._should_skip = does_tree_import(pkg, name, node) 188 self._should_skip = does_tree_import(pkg, name, node)
187 return self._should_skip 189 return self._should_skip
OLDNEW
« no previous file with comments | « Lib/inspect.py ('k') | Lib/logging/handlers.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+