# 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)
# 
# def does_tree_import(package, name, node):
#     """ Returns true if name is imported from package at the
#         top level of the tree which node belongs to.
#         To cover the case of an import like 'import foo', use
#         None for the package and 'foo' for the name. """
#     binding = find_binding(name, find_root(node), package)
#     return bool(binding)


from .. import fixer_base
from ..fixer_util import Name, attr_chain, does_tree_import
        return "|".join(build_pattern(self.mapping))

    def compile_pattern(self):
        # We override this, so MAPPING can be pragmatically altered and the
        # changes will be reflected in PATTERN.
        self.PATTERN = self.build_pattern()
        super(FixImports, self).compile_pattern()

    # Don't match the node if it's within another match.
    def match(self, node):
        match = super(FixImports, self).match
        results = match(node)
        if results:
            # Module usage could be in the trailer of an attribute lookup, so we
            # might have nested matches when "bare_with_attr" is present.
            if "bare_with_attr" not in results and \
                    any(match(obj) for obj in attr_chain(node, "parent")):
                return False
            return results
        return False

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

    def transform(self, node, results):
        import_mod = results.get("module_name")
        if import_mod:
            mod_name = import_mod.value
            new_name = unicode(self.mapping[mod_name])

# 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)
# 
# def does_tree_import(package, name, node):
#     """ Returns true if name is imported from package at the
#         top level of the tree which node belongs to.
#         To cover the case of an import like 'import foo', use
#         None for the package and 'foo' for the name. """
#     binding = find_binding(name, find_root(node), package)
#     return bool(binding)


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

        return "|".join(build_pattern(self.mapping))

    def compile_pattern(self):
        # We override this, so MAPPING can be pragmatically altered and the
        # changes will be reflected in PATTERN.
        self.PATTERN = self.build_pattern()
        super(FixImports, self).compile_pattern()

    # Don't match the node if it's within another match.
    def match(self, node):
        match = super(FixImports, self).match
        results = match(node)
        if results:
            # Module usage could be in the trailer of an attribute lookup, so we
            # might have nested matches when "bare_with_attr" is present.
            if "bare_with_attr" not in results and \
                    any(match(obj) for obj in attr_chain(node, "parent")):
                return False
            return results
        return False

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

    def transform(self, node, results):
        import_mod = results.get("module_name")
        if import_mod:
            mod_name = import_mod.value
            new_name = unicode(self.mapping[mod_name])

            if does_tree_import(