# Path: py3kwarn2to3/fixer_util.py
# def Name(name, prefix=None):
#     """Return a NAME leaf"""
#     return Leaf(token.NAME, name, prefix=prefix)
# 
# def attr_chain(obj, attr):
#     """Follow an attribute chain.
# 
#     If you have a chain of objects where a.foo -> b, b.foo-> c, etc,
#     use this to iterate over all objects in the chain. Iteration is
#     terminated by getattr(x, attr) is None.
# 
#     Args:
#         obj: the starting object
#         attr: the name of the chaining attribute
# 
#     Yields:
#         Each successive object in the chain.
#     """
#     next = getattr(obj, attr)
#     while next:
#         yield next
#         next = getattr(next, attr)


from .. import fixer_base
from ..fixer_util import Name, attr_chain


class FixRenames(fixer_base.BaseFix):
    BM_compatible = True
    PATTERN = "|".join(build_pattern())

    order = "pre" # Pre-order tree traversal

    # Don't match the node if it's within another match
    def match(self, node):
        match = super(FixRenames, self).match
        results = match(node)
        if results:
            if any(match(obj) for obj in attr_chain(node, "parent")):
                return False
            return results
        return False

    #def start_tree(self, tree, filename):
    #    super(FixRenames, self).start_tree(tree, filename)
    #    self.replace = {}

    def transform(self, node, results):
        mod_name = results.get("module_name")
        attr_name = results.get("attr_name")
        #bare_name = results.get("bare_name")
        #import_mod = results.get("module")

        if mod_name and attr_name:
            new_attr = unicode(LOOKUP[(mod_name.value, attr_name.value)])

# Path: py3kwarn2to3/fixer_util.py
# def Name(name, prefix=None):
#     """Return a NAME leaf"""
#     return Leaf(token.NAME, name, prefix=prefix)
# 
# def attr_chain(obj, attr):
#     """Follow an attribute chain.
# 
#     If you have a chain of objects where a.foo -> b, b.foo-> c, etc,
#     use this to iterate over all objects in the chain. Iteration is
#     terminated by getattr(x, attr) is None.
# 
#     Args:
#         obj: the starting object
#         attr: the name of the chaining attribute
# 
#     Yields:
#         Each successive object in the chain.
#     """
#     next = getattr(obj, attr)
#     while next:
#         yield next
#         next = getattr(next, attr)


# Path: py3kwarn2to3/fixes/fix_renames.py
from .. import fixer_base
from ..fixer_util import Name, attr_chain



class FixRenames(fixer_base.BaseFix):
    BM_compatible = True
    PATTERN = "|".join(build_pattern())

    order = "pre" # Pre-order tree traversal

    # Don't match the node if it's within another match
    def match(self, node):
        match = super(FixRenames, self).match
        results = match(node)
        if results:
            if any(match(obj) for obj in attr_chain(node, "parent")):
                return False
            return results
        return False

    #def start_tree(self, tree, filename):
    #    super(FixRenames, self).start_tree(tree, filename)
    #    self.replace = {}

    def transform(self, node, results):
        mod_name = results.get("module_name")
        attr_name = results.get("attr_name")
        #bare_name = results.get("bare_name")
        #import_mod = results.get("module")

        if mod_name and attr_name:
            new_attr = unicode(LOOKUP[(mod_name.value, attr_name.value)])

            attr_name.replace(Name(new_attr, prefix=attr_name.prefix))