(self, node, results)
Transform for imports of specific module elements. Replaces
           the module to be imported from with the appropriate new
           module.
Transform for imports of specific module elements. Replaces
           the module to be imported from with the appropriate new
           module.
Transform for imports of specific module elements . Replaces the module to be imported from with the appropriate new module .
def transform_member(self, node, results):
        """Transform for imports of specific module elements. Replaces
           the module to be imported from with the appropriate new
           module.
        """
        mod_member = results.get("mod_member")
        pref = mod_member.prefix
        member = results.get("member")

        # Simple case with only a single member being imported
        if member:
            # this may be a list of length one, or just a node
            if isinstance(member, list):
                member = member[0]
            new_name = None
            for change in MAPPING[mod_member.value]:
                if member.value in change[1]:
                    new_name = change[0]
                    break
            if new_name:
                mod_member.replace(Name(new_name, prefix=pref))
            else:
                self.cannot_convert(node, "This is an invalid module element")

        # Multiple members being imported
        else:
            # a dictionary for replacements, order matters
            modules = []
            mod_dict = {}
            members = results["members"]
            for member in members:
                # we only care about the actual members
                if member.type == syms.import_as_name:
                    as_name = member.children[2].value
                    member_name = member.children[0].value
                else:
                    member_name = member.value
                    as_name = None
                if member_name != ",":
                    for change in MAPPING[mod_member.value]:
                        if member_name in change[1]:
                            if change[0] not in mod_dict:
                                modules.append(change[0])
                            mod_dict.setdefault(change[0], []).append(member)

            new_nodes = []
            indentation = find_indentation(node)
            first = True
            def handle_name(name, prefix):
                if name.type == syms.import_as_name:
                    kids = [Name(name.children[0].value, prefix=prefix),
                            name.children[1].clone(),
                            name.children[2].clone()]
                    return [Node(syms.import_as_name, kids)]
                return [Name(name.value, prefix=prefix)]
            for module in modules:
                elts = mod_dict[module]
                names = []
                for elt in elts[:-1]:
                    names.extend(handle_name(elt, pref))
                    names.append(Comma())
                names.extend(handle_name(elts[-1], pref))
                new = FromImport(module, names)
                if not first or node.parent.prefix.endswith(indentation):
                    new.prefix = indentation
                new_nodes.append(new)
                first = False
            if new_nodes:
                nodes = []
                for new_node in new_nodes[:-1]:
                    nodes.extend([new_node, Newline()])
                nodes.append(new_nodes[-1])
                node.replace(nodes)
            else:
                self.cannot_convert(node, "All module elements are invalid")
def transform_member ( self , node , results ) : mod_member = results . get ( "mod_member" ) pref = mod_member . prefix member = results . get ( "member" ) # Simple case with only a single member being imported if member : # this may be a list of length one, or just a node if isinstance ( member , list ) : member = member [ 0 ] new_name = None for change in MAPPING [ mod_member . value ] : if member . value in change [ 1 ] : new_name = change [ 0 ] break if new_name : mod_member . replace ( Name ( new_name , prefix = pref ) ) else : self . cannot_convert ( node , "This is an invalid module element" ) # Multiple members being imported else : # a dictionary for replacements, order matters modules = [ ] mod_dict = { } members = results [ "members" ] for member in members : # we only care about the actual members if member . type == syms . import_as_name : as_name = member . children [ 2 ] . value member_name = member . children [ 0 ] . value else : member_name = member . value as_name = None if member_name != "," : for change in MAPPING [ mod_member . value ] : if member_name in change [ 1 ] : if change [ 0 ] not in mod_dict : modules . append ( change [ 0 ] ) mod_dict . setdefault ( change [ 0 ] , [ ] ) . append ( member ) new_nodes = [ ] indentation = find_indentation ( node ) first = True def handle_name ( name , prefix ) : if name . type == syms . import_as_name : kids = [ Name ( name . children [ 0 ] . value , prefix = prefix ) , name . children [ 1 ] . clone ( ) , name . children [ 2 ] . clone ( ) ] return [ Node ( syms . import_as_name , kids ) ] return [ Name ( name . value , prefix = prefix ) ] for module in modules : elts = mod_dict [ module ] names = [ ] for elt in elts [ : - 1 ] : names . extend ( handle_name ( elt , pref ) ) names . append ( Comma ( ) ) names . extend ( handle_name ( elts [ - 1 ] , pref ) ) new = FromImport ( module , names ) if not first or node . parent . prefix . endswith ( indentation ) : new . prefix = indentation new_nodes . append ( new ) first = False if new_nodes : nodes = [ ] for new_node in new_nodes [ : - 1 ] : nodes . extend ( [ new_node , Newline ( ) ] ) nodes . append ( new_nodes [ - 1 ] ) node . replace ( nodes ) else : self . cannot_convert ( node , "All module elements are invalid" )