home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 June / maximum-cd-2011-06.iso / DiscContents / LibO_3.3.1_Win_x86_install_multi.exe / libreoffice1.cab / fix_imports.py < prev    next >
Encoding:
Python Source  |  2011-02-15  |  4.8 KB  |  130 lines

  1. """Fix incompatible imports and module references."""
  2. # Authors: Collin Winter, Nick Edds
  3.  
  4. # Local imports
  5. from .. import fixer_base
  6. from ..fixer_util import Name, attr_chain
  7.  
  8. MAPPING = {'StringIO':  'io',
  9.            'cStringIO': 'io',
  10.            'cPickle': 'pickle',
  11.            '__builtin__' : 'builtins',
  12.            'copy_reg': 'copyreg',
  13.            'Queue': 'queue',
  14.            'SocketServer': 'socketserver',
  15.            'ConfigParser': 'configparser',
  16.            'repr': 'reprlib',
  17.            'FileDialog': 'tkinter.filedialog',
  18.            'tkFileDialog': 'tkinter.filedialog',
  19.            'SimpleDialog': 'tkinter.simpledialog',
  20.            'tkSimpleDialog': 'tkinter.simpledialog',
  21.            'tkColorChooser': 'tkinter.colorchooser',
  22.            'tkCommonDialog': 'tkinter.commondialog',
  23.            'Dialog': 'tkinter.dialog',
  24.            'Tkdnd': 'tkinter.dnd',
  25.            'tkFont': 'tkinter.font',
  26.            'tkMessageBox': 'tkinter.messagebox',
  27.            'ScrolledText': 'tkinter.scrolledtext',
  28.            'turtle': 'tkinter.turtle',
  29.            'Tkconstants': 'tkinter.constants',
  30.            'Tix': 'tkinter.tix',
  31.            'Tkinter': 'tkinter',
  32.            'markupbase': '_markupbase',
  33.            '_winreg': 'winreg',
  34.            'thread': '_thread',
  35.            'dummy_thread': '_dummy_thread',
  36.            # anydbm and whichdb are handled by fix_imports2
  37.            'dbhash': 'dbm.bsd',
  38.            'dumbdbm': 'dbm.dumb',
  39.            'dbm': 'dbm.ndbm',
  40.            'gdbm': 'dbm.gnu',
  41.            'xmlrpclib': 'xmlrpc.client',
  42.            'DocXMLRPCServer': 'xmlrpc.server',
  43.            'SimpleXMLRPCServer': 'xmlrpc.server',
  44.            'httplib': 'http.client',
  45.            'Cookie': 'http.cookies',
  46.            'cookielib': 'http.cookiejar',
  47.            'BaseHTTPServer': 'http.server',
  48.            'SimpleHTTPServer': 'http.server',
  49.            'CGIHTTPServer': 'http.server',
  50.            #'test.test_support': 'test.support',
  51.            'commands': 'subprocess',
  52.            'UserString' : 'collections',
  53.            'UserList' : 'collections',
  54.            'urlparse' : 'urllib.parse',
  55.            'robotparser' : 'urllib.robotparser',
  56. }
  57.  
  58.  
  59. def alternates(members):
  60.     return "(" + "|".join(map(repr, members)) + ")"
  61.  
  62.  
  63. def build_pattern(mapping=MAPPING):
  64.     mod_list = ' | '.join(["module_name='%s'" % key for key in mapping])
  65.     bare_names = alternates(mapping.keys())
  66.  
  67.     yield """name_import=import_name< 'import' ((%s)
  68.                           | dotted_as_names< any* (%s) any* >) >
  69.           """ % (mod_list, mod_list)
  70.     yield """import_from< 'from' (%s) 'import' ['(']
  71.               ( any | import_as_name< any 'as' any > |
  72.                 import_as_names< any* >)  [')'] >
  73.           """ % mod_list
  74.     yield """import_name< 'import'
  75.                           dotted_as_name< (%s) 'as' any > >
  76.           """ % mod_list
  77.  
  78.     # Find usages of module members in code e.g. thread.foo(bar)
  79.     yield "power< bare_with_attr=(%s) trailer<'.' any > any* >" % bare_names
  80.  
  81.  
  82. class FixImports(fixer_base.BaseFix):
  83.  
  84.     order = "pre" # Pre-order tree traversal
  85.  
  86.     # This is overridden in fix_imports2.
  87.     mapping = MAPPING
  88.  
  89.     def build_pattern(self):
  90.         return "|".join(build_pattern(self.mapping))
  91.  
  92.     def compile_pattern(self):
  93.         # We override this, so MAPPING can be pragmatically altered and the
  94.         # changes will be reflected in PATTERN.
  95.         self.PATTERN = self.build_pattern()
  96.         super(FixImports, self).compile_pattern()
  97.  
  98.     # Don't match the node if it's within another match.
  99.     def match(self, node):
  100.         match = super(FixImports, self).match
  101.         results = match(node)
  102.         if results:
  103.             # Module usage could be in the trailier of an attribute lookup, so
  104.             # we might have nested matches when "bare_with_attr" is present.
  105.             if "bare_with_attr" not in results and \
  106.                     any([match(obj) for obj in attr_chain(node, "parent")]):
  107.                 return False
  108.             return results
  109.         return False
  110.  
  111.     def start_tree(self, tree, filename):
  112.         super(FixImports, self).start_tree(tree, filename)
  113.         self.replace = {}
  114.  
  115.     def transform(self, node, results):
  116.         import_mod = results.get("module_name")
  117.         if import_mod:
  118.             new_name = self.mapping[(import_mod or mod_name).value]
  119.             if "name_import" in results:
  120.                 # If it's not a "from x import x, y" or "import x as y" import,
  121.                 # marked its usage to be replaced.
  122.                 self.replace[import_mod.value] = new_name
  123.             import_mod.replace(Name(new_name, prefix=import_mod.get_prefix()))
  124.         else:
  125.             # Replace usage of the module.
  126.             bare_name = results["bare_with_attr"][0]
  127.             new_name = self.replace.get(bare_name.value)
  128.             if new_name:
  129.                 bare_name.replace(Name(new_name, prefix=bare_name.get_prefix()))
  130.