source: orange/orange/fixes/fix_orange_imports_aggressive.py @ 7631:6b6ff7657b10

Revision 7631:6b6ff7657b10, 1.6 KB checked in by ales_erjavec <ales.erjavec@…>, 3 years ago (diff)
  • added fix_orange_imports_aggressive
  • added modules / changed names mapping
Line 
1"""Same as fix_orange_imports but will fix orange module name uses even if
2they are not imported. To use this fixer you must pass the -a (--aggressive)
3command line option."""
4
5from .fix_orange_imports import FixOrangeImports, Name
6from lib2to3.fixer_util import touch_import
7
8class FixOrangeImportsAggressive(FixOrangeImports):
9    run_order = 7
10    def transform(self, node, results):
11        import_mod = results.get("module_name")
12        if import_mod:
13            mod_name = import_mod.value
14            new_name = unicode(self.mapping[mod_name])
15            import_mod.replace(Name(new_name, prefix=import_mod.prefix))
16            if "name_import" in results:
17                # If it's not a "from x import x, y" or "import x as y" import,
18                # marked its usage to be replaced.
19                self.replace[mod_name] = new_name
20            if "multiple_imports" in results:
21                # This is a nasty hack to fix multiple imports on a line (e.g.,
22                # "import StringIO, urlparse"). The problem is that I can't
23                # figure out an easy way to make a pattern recognize the keys of
24                # MAPPING randomly sprinkled in an import statement.
25                results = self.match(node)
26                if results:
27                    self.transform(node, results)
28        else:
29            # Replace usage of the module.
30            bare_name = results["bare_with_attr"][0]
31            new_name = self.mapping.get(bare_name.value)
32            if new_name:
33                bare_name.replace(Name(new_name, prefix=bare_name.prefix))
34                touch_import(None, new_name, node)
35               
Note: See TracBrowser for help on using the repository browser.