home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / lib2to3 / fixer_base.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  6.9 KB  |  188 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Base class for fixers (optional, but recommended).'''
  5. import logging
  6. import itertools
  7. from patcomp import PatternCompiler
  8. from  import pygram
  9. from fixer_util import does_tree_import
  10.  
  11. class BaseFix(object):
  12.     """Optional base class for fixers.
  13.  
  14.     The subclass name must be FixFooBar where FooBar is the result of
  15.     removing underscores and capitalizing the words of the fix name.
  16.     For example, the class name for a fixer named 'has_key' should be
  17.     FixHasKey.
  18.     """
  19.     PATTERN = None
  20.     pattern = None
  21.     options = None
  22.     filename = None
  23.     logger = None
  24.     numbers = itertools.count(1)
  25.     used_names = set()
  26.     order = 'post'
  27.     explicit = False
  28.     run_order = 5
  29.     syms = pygram.python_symbols
  30.     
  31.     def __init__(self, options, log):
  32.         '''Initializer.  Subclass may override.
  33.  
  34.         Args:
  35.             options: an dict containing the options passed to RefactoringTool
  36.             that could be used to customize the fixer through the command line.
  37.             log: a list to append warnings and other messages to.
  38.         '''
  39.         self.options = options
  40.         self.log = log
  41.         self.compile_pattern()
  42.  
  43.     
  44.     def compile_pattern(self):
  45.         """Compiles self.PATTERN into self.pattern.
  46.  
  47.         Subclass may override if it doesn't want to use
  48.         self.{pattern,PATTERN} in .match().
  49.         """
  50.         if self.PATTERN is not None:
  51.             self.pattern = PatternCompiler().compile_pattern(self.PATTERN)
  52.         
  53.  
  54.     
  55.     def set_filename(self, filename):
  56.         '''Set the filename, and a logger derived from it.
  57.  
  58.         The main refactoring tool should call this.
  59.         '''
  60.         self.filename = filename
  61.         self.logger = logging.getLogger(filename)
  62.  
  63.     
  64.     def match(self, node):
  65.         '''Returns match for a given parse tree node.
  66.  
  67.         Should return a true or false object (not necessarily a bool).
  68.         It may return a non-empty dict of matching sub-nodes as
  69.         returned by a matching pattern.
  70.  
  71.         Subclass may override.
  72.         '''
  73.         results = {
  74.             'node': node }
  75.         if self.pattern.match(node, results):
  76.             pass
  77.         return results
  78.  
  79.     
  80.     def transform(self, node, results):
  81.         '''Returns the transformation for a given parse tree node.
  82.  
  83.         Args:
  84.           node: the root of the parse tree that matched the fixer.
  85.           results: a dict mapping symbolic names to part of the match.
  86.  
  87.         Returns:
  88.           None, or a node that is a modified copy of the
  89.           argument node.  The node argument may also be modified in-place to
  90.           effect the same change.
  91.  
  92.         Subclass *must* override.
  93.         '''
  94.         raise NotImplementedError()
  95.  
  96.     
  97.     def new_name(self, template = 'xxx_todo_changeme'):
  98.         '''Return a string suitable for use as an identifier
  99.  
  100.         The new name is guaranteed not to conflict with other identifiers.
  101.         '''
  102.         name = template
  103.         while name in self.used_names:
  104.             name = template + str(self.numbers.next())
  105.         self.used_names.add(name)
  106.         return name
  107.  
  108.     
  109.     def log_message(self, message):
  110.         if self.first_log:
  111.             self.first_log = False
  112.             self.log.append('### In file %s ###' % self.filename)
  113.         
  114.         self.log.append(message)
  115.  
  116.     
  117.     def cannot_convert(self, node, reason = None):
  118.         """Warn the user that a given chunk of code is not valid Python 3,
  119.         but that it cannot be converted automatically.
  120.  
  121.         First argument is the top-level node for the code in question.
  122.         Optional second argument is why it can't be converted.
  123.         """
  124.         lineno = node.get_lineno()
  125.         for_output = node.clone()
  126.         for_output.set_prefix('')
  127.         msg = 'Line %d: could not convert: %s'
  128.         self.log_message(msg % (lineno, for_output))
  129.         if reason:
  130.             self.log_message(reason)
  131.         
  132.  
  133.     
  134.     def warning(self, node, reason):
  135.         """Used for warning the user about possible uncertainty in the
  136.         translation.
  137.  
  138.         First argument is the top-level node for the code in question.
  139.         Optional second argument is why it can't be converted.
  140.         """
  141.         lineno = node.get_lineno()
  142.         self.log_message('Line %d: %s' % (lineno, reason))
  143.  
  144.     
  145.     def start_tree(self, tree, filename):
  146.         '''Some fixers need to maintain tree-wide state.
  147.         This method is called once, at the start of tree fix-up.
  148.  
  149.         tree - the root node of the tree to be processed.
  150.         filename - the name of the file the tree came from.
  151.         '''
  152.         self.used_names = tree.used_names
  153.         self.set_filename(filename)
  154.         self.numbers = itertools.count(1)
  155.         self.first_log = True
  156.  
  157.     
  158.     def finish_tree(self, tree, filename):
  159.         '''Some fixers need to maintain tree-wide state.
  160.         This method is called once, at the conclusion of tree fix-up.
  161.  
  162.         tree - the root node of the tree to be processed.
  163.         filename - the name of the file the tree came from.
  164.         '''
  165.         pass
  166.  
  167.  
  168.  
  169. class ConditionalFix(BaseFix):
  170.     ''' Base class for fixers which not execute if an import is found. '''
  171.     skip_on = None
  172.     
  173.     def start_tree(self, *args):
  174.         super(ConditionalFix, self).start_tree(*args)
  175.         self._should_skip = None
  176.  
  177.     
  178.     def should_skip(self, node):
  179.         if self._should_skip is not None:
  180.             return self._should_skip
  181.         pkg = self.skip_on.split('.')
  182.         name = pkg[-1]
  183.         pkg = '.'.join(pkg[:-1])
  184.         self._should_skip = does_tree_import(pkg, name, node)
  185.         return self._should_skip
  186.  
  187.  
  188.