Changeset 7554:79ade3f326da in orange


Ignore:
Timestamp:
02/04/11 22:26:38 (3 years ago)
Author:
ales_erjavec <ales.erjavec@…>
Branch:
default
Convert:
39c26edd6560847f05df073a28dc9476d83ecb56
Message:
  • fix_changed_names_aggressive checks for a proper unqualified 'from module import *' before transforming the code
File:
1 edited

Legend:

Unmodified
Added
Removed
  • orange/fixes/fix_changed_names_aggressive.py

    r7470 r7554  
    11from lib2to3 import pytree 
    2 from lib2to3.fixer_util import Name, Dot, Node, attr_chain, touch_import 
     2from lib2to3.fixer_util import Name, Dot, Node, attr_chain, touch_import, find_root 
    33from lib2to3 import fixer_base 
     4from lib2to3 import patcomp 
    45from collections import defaultdict 
    56 
    67from .fix_changed_names import FixChangedNames, MAPPING 
    78 
     9def find_matches(pattern, tree): 
     10    for node in tree.pre_order(): 
     11        res = {} 
     12        match = pattern.match(node, res) 
     13        if match: 
     14            yield res 
     15     
    816def build_pattern(mapping=MAPPING): 
    917    PATTERN = """ 
     
    1422    return PATTERN % "|".join("'%s'" % key.split(".")[-1] for key in mapping.keys()) 
    1523 
     24 
     25## Pattern for finding from module import *  
     26from_import_pattern = """import_from<'from' module=(%s) 'import' star='*'>""" 
     27module_names = set(["'%s'" % key.split(".", 1)[0] for key in MAPPING.keys()]) 
     28from_import_pattern = from_import_pattern % "|".join(module_names) 
     29print from_import_pattern 
     30 
     31from_import_expr = patcomp.compile_pattern(from_import_pattern) 
     32  
    1633class FixChangedNamesAggressive(FixChangedNames): 
    1734    mapping = MAPPING  
     
    2239        # changes will be reflected in PATTERN. 
    2340        self.PATTERN = build_pattern(self.mapping) 
    24 #        self._modules_to_change = [key.split(".", 1)[0] for key in self.mapping.keys()] 
    2541        name2mod = defaultdict(set) 
    2642        for key in self.mapping.keys(): 
     
    3046         
    3147        fixer_base.BaseFix.compile_pattern(self) 
     48         
     49    def start_tree(self, tree, filename): 
     50        super(FixChangedNamesAggressive, self).start_tree(tree, filename) 
     51        ## Find unqualified imports 
     52        self._import_matches = list(find_matches(from_import_expr, tree)) 
     53         
     54    def finish_tree(self, tree, filename): 
     55        del self._import_matches 
    3256                 
    3357    def transform(self, node, results): 
     
    3660        if local: 
    3761            local = local[0] 
    38             tail = tail[0] 
    3962            local_name = local.value 
    4063            modules = self._names_to_modules[local_name] 
    4164            if len(modules) > 1: 
    42                 import warnings 
    43                 warnings.warn("Conflicting name '%s' is present in %s! Ignoring transformation!" % local_name, modules) 
     65                self.warnings(node, "Conflicting name '%s' is present in %s! Ignoring transformation!" % local_name, modules) 
    4466                return 
    4567             
    46             ## TODOL check if tree contains a from module import * statement. if not ignore the transformation 
    4768            module = list(modules)[0] 
     69             
     70            if all("module" not in res for res in self._import_matches):  
     71                self.warning(node, "Aggressive name matched '%s' but no corresponding import! Fix manualy." % local) 
     72                return 
     73                 
    4874            new_name = unicode(self.mapping[module + "." + local_name]) 
    4975             
    5076            syms = self.syms 
    5177             
    52             tail = tail.clone() 
     78            if tail: 
     79                tail = [t.clone() for t in tail] 
     80#            tail = tail.clone() 
    5381            new = self.package_tree(new_name) 
    54             new = pytree.Node(syms.power, new + [tail]) 
     82            new = pytree.Node(syms.power, new + tail, prefix=local.prefix) 
    5583             
    5684            # Make sure the proper package is imported 
    5785            package = new_name.rsplit(".", 1)[0] 
    5886            touch_import(None, package, node) 
    59             return new     
     87            return new 
     88         
Note: See TracChangeset for help on using the changeset viewer.