fixer_base.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. # Copyright 2006 Google, Inc. All Rights Reserved.
  2. # Licensed to PSF under a Contributor Agreement.
  3. """Base class for fixers (optional, but recommended)."""
  4. # Python imports
  5. import itertools
  6. # Local imports
  7. from .patcomp import PatternCompiler
  8. from . import pygram
  9. from .fixer_util import does_tree_import
  10. class BaseFix(object):
  11. """Optional base class for fixers.
  12. The subclass name must be FixFooBar where FooBar is the result of
  13. removing underscores and capitalizing the words of the fix name.
  14. For example, the class name for a fixer named 'has_key' should be
  15. FixHasKey.
  16. """
  17. PATTERN = None # Most subclasses should override with a string literal
  18. pattern = None # Compiled pattern, set by compile_pattern()
  19. pattern_tree = None # Tree representation of the pattern
  20. options = None # Options object passed to initializer
  21. filename = None # The filename (set by set_filename)
  22. numbers = itertools.count(1) # For new_name()
  23. used_names = set() # A set of all used NAMEs
  24. order = "post" # Does the fixer prefer pre- or post-order traversal
  25. explicit = False # Is this ignored by refactor.py -f all?
  26. run_order = 5 # Fixers will be sorted by run order before execution
  27. # Lower numbers will be run first.
  28. _accept_type = None # [Advanced and not public] This tells RefactoringTool
  29. # which node type to accept when there's not a pattern.
  30. keep_line_order = False # For the bottom matcher: match with the
  31. # original line order
  32. BM_compatible = False # Compatibility with the bottom matching
  33. # module; every fixer should set this
  34. # manually
  35. # Shortcut for access to Python grammar symbols
  36. syms = pygram.python_symbols
  37. def __init__(self, options, log):
  38. """Initializer. Subclass may override.
  39. Args:
  40. options: a dict containing the options passed to RefactoringTool
  41. that could be used to customize the fixer through the command line.
  42. log: a list to append warnings and other messages to.
  43. """
  44. self.options = options
  45. self.log = log
  46. self.compile_pattern()
  47. def compile_pattern(self):
  48. """Compiles self.PATTERN into self.pattern.
  49. Subclass may override if it doesn't want to use
  50. self.{pattern,PATTERN} in .match().
  51. """
  52. if self.PATTERN is not None:
  53. PC = PatternCompiler()
  54. self.pattern, self.pattern_tree = PC.compile_pattern(self.PATTERN,
  55. with_tree=True)
  56. def set_filename(self, filename):
  57. """Set the filename.
  58. The main refactoring tool should call this.
  59. """
  60. self.filename = filename
  61. def match(self, node):
  62. """Returns match for a given parse tree node.
  63. Should return a true or false object (not necessarily a bool).
  64. It may return a non-empty dict of matching sub-nodes as
  65. returned by a matching pattern.
  66. Subclass may override.
  67. """
  68. results = {"node": node}
  69. return self.pattern.match(node, results) and results
  70. def transform(self, node, results):
  71. """Returns the transformation for a given parse tree node.
  72. Args:
  73. node: the root of the parse tree that matched the fixer.
  74. results: a dict mapping symbolic names to part of the match.
  75. Returns:
  76. None, or a node that is a modified copy of the
  77. argument node. The node argument may also be modified in-place to
  78. effect the same change.
  79. Subclass *must* override.
  80. """
  81. raise NotImplementedError()
  82. def new_name(self, template="xxx_todo_changeme"):
  83. """Return a string suitable for use as an identifier
  84. The new name is guaranteed not to conflict with other identifiers.
  85. """
  86. name = template
  87. while name in self.used_names:
  88. name = template + str(next(self.numbers))
  89. self.used_names.add(name)
  90. return name
  91. def log_message(self, message):
  92. if self.first_log:
  93. self.first_log = False
  94. self.log.append("### In file %s ###" % self.filename)
  95. self.log.append(message)
  96. def cannot_convert(self, node, reason=None):
  97. """Warn the user that a given chunk of code is not valid Python 3,
  98. but that it cannot be converted automatically.
  99. First argument is the top-level node for the code in question.
  100. Optional second argument is why it can't be converted.
  101. """
  102. lineno = node.get_lineno()
  103. for_output = node.clone()
  104. for_output.prefix = ""
  105. msg = "Line %d: could not convert: %s"
  106. self.log_message(msg % (lineno, for_output))
  107. if reason:
  108. self.log_message(reason)
  109. def warning(self, node, reason):
  110. """Used for warning the user about possible uncertainty in the
  111. translation.
  112. First argument is the top-level node for the code in question.
  113. Optional second argument is why it can't be converted.
  114. """
  115. lineno = node.get_lineno()
  116. self.log_message("Line %d: %s" % (lineno, reason))
  117. def start_tree(self, tree, filename):
  118. """Some fixers need to maintain tree-wide state.
  119. This method is called once, at the start of tree fix-up.
  120. tree - the root node of the tree to be processed.
  121. filename - the name of the file the tree came from.
  122. """
  123. self.used_names = tree.used_names
  124. self.set_filename(filename)
  125. self.numbers = itertools.count(1)
  126. self.first_log = True
  127. def finish_tree(self, tree, filename):
  128. """Some fixers need to maintain tree-wide state.
  129. This method is called once, at the conclusion of tree fix-up.
  130. tree - the root node of the tree to be processed.
  131. filename - the name of the file the tree came from.
  132. """
  133. pass
  134. class ConditionalFix(BaseFix):
  135. """ Base class for fixers which not execute if an import is found. """
  136. # This is the name of the import which, if found, will cause the test to be skipped
  137. skip_on = None
  138. def start_tree(self, *args):
  139. super(ConditionalFix, self).start_tree(*args)
  140. self._should_skip = None
  141. def should_skip(self, node):
  142. if self._should_skip is not None:
  143. return self._should_skip
  144. pkg = self.skip_on.split(".")
  145. name = pkg[-1]
  146. pkg = ".".join(pkg[:-1])
  147. self._should_skip = does_tree_import(pkg, name, node)
  148. return self._should_skip