Exemple #1
0
def add_global_assignment_after_imports(_name, assignment, node):
    """
	Big copy paste + modification from touch_import
	"""
    root = find_root(node)
    if find_binding(_name, root):
        return

    # figure out where to insert the assignment.
    # First try to find the first import and then skip to the last one.
    insert_pos = offset = 0
    for idx, node in enumerate(root.children):
        if not is_import_ish_stmt(node):
            continue
        for offset, node2 in enumerate(root.children[idx:]):
            if not is_import_ish_stmt(node2):
                break
        insert_pos = idx + offset
        break

    # if there are no imports where we can insert, find the docstring.
    # if that also fails, we stick to the beginning of the file
    if insert_pos == 0:
        for idx, node in enumerate(root.children):
            if (node.type == syms.simple_stmt and node.children
                    and node.children[0].type == token.STRING):
                insert_pos = idx + 1
                break

    children = [assignment, Newline()]
    root.insert_child(insert_pos, Node(syms.simple_stmt, children))
Exemple #2
0
 def _find_bind_rec(self, name, node):
     # Search a tree for a binding -- used to find the starting
     # point for these tests.
     c = fixer_util.find_binding(name, node)
     if c: return c
     for child in node.children:
         c = self._find_bind_rec(name, child)
         if c: return c
 def _find_bind_rec(self, name, node):
     # Search a tree for a binding -- used to find the starting
     # point for these tests.
     c = fixer_util.find_binding(name, node)
     if c: return c
     for child in node.children:
         c = self._find_bind_rec(name, child)
         if c: return c
 def _find_bind_rec(self, name, node):
     c = fixer_util.find_binding(name, node)
     if c:
         return c
     for child in node.children:
         c = self._find_bind_rec(name, child)
         if c:
             return c
Exemple #5
0
    def start_tree(self, tree, filename):
        super(FixNextCall, self).start_tree(tree, filename)

        n = find_binding('next', tree)
        if n:
            self.warning(n, bind_warning)
            self.shadowed_next = True
        else:
            self.shadowed_next = False
Exemple #6
0
    def start_tree(self, tree, filename):
        super(FixNextCall, self).start_tree(tree, filename)

        n = find_binding('next', tree)
        if n:
            self.warning(n, bind_warning)
            self.shadowed_next = True
        else:
            self.shadowed_next = False
Exemple #7
0
 def find_binding(self, name, string, package=None):
     return fixer_util.find_binding(name, parse(string), package)
Exemple #8
0
 def find_binding(self, name, string, package=None):
     return fixer_util.find_binding(name, parse(string), package)
Exemple #9
0
    def transform(self, node, results):
        name = results.get("name")
        binding = results.get("binding")
        pre = results.get("pre")
        post = results.get("post")
        simple = results.get("simple")
        if simple:
            binding = find_binding("intern", find_root(node), "sys")
            binding.remove()
            return
        if binding:
            if not pre and not post:
                new_binding = find_binding("intern", find_root(node), "sys")
                new_binding.remove()
                return
            elif not pre and post:
                for ch in node.children:
                    if type(ch) == pytree.Node:
                        assert ch.children[0].prefix + "intern" \
                                                       == str(ch.children[0])
                        ch.children[0].remove()  # intern
                        assert ch.children[0].prefix + "," \
                                                       == str(ch.children[0])
                        ch.children[0].remove()  # ,
                return
            elif not post and pre:
                for ch in node.children:
                    if type(ch) == pytree.Node:
                        assert ch.children[-1].prefix + "intern" \
                                                       == str(ch.children[-1])
                        ch.children[-1].remove()  # intern
                        assert ch.children[-1].prefix + "," \
                                                       == str(ch.children[-1])
                        ch.children[-1].remove()  # ,
                return
            elif post and pre:
                for ch in node.children:
                    if type(ch) == pytree.Node:
                        for ch_ in ch.children:
                            if ch_ and ch_.prefix + "intern" == str(ch_):
                                last_ch_ = ch_.prev_sibling
                                ch_.remove()  # intern
                                assert last_ch_.prefix + "," \
                                                       == str(last_ch_)
                                last_ch_.remove()  # ,
                return
        syms = self.syms
        obj = results["obj"].clone()
        if obj.type == syms.arglist:
            newarglist = obj.clone()
        else:
            newarglist = pytree.Node(syms.arglist, [obj.clone()])
        after = results["after"]
        if after:
            after = [n.clone() for n in after]

        new = pytree.Node(syms.power, [Name("intern")] + [
            pytree.Node(syms.trailer, [
                results["lpar"].clone(), newarglist, results["rpar"].clone()
            ] + after)
        ])
        new.prefix = node.prefix
        return new
Exemple #10
0
    def transform(self, node, results):
        name = results.get("name")
        binding = results.get("binding")
        pre = results.get("pre")
        post = results.get("post")
        simple = results.get("simple")
        if simple:
            binding = find_binding("intern", find_root(node), "sys")
            binding.remove()
            return
        if binding:
            if not pre and not post:
                new_binding = find_binding("intern", find_root(node), "sys")
                new_binding.remove()
                return
            elif not pre and post:
                for ch in node.children:
                    if type(ch) == pytree.Node:
                        assert ch.children[0].prefix + "intern" \
                                                       == str(ch.children[0])
                        ch.children[0].remove() # intern
                        assert ch.children[0].prefix + "," \
                                                       == str(ch.children[0])
                        ch.children[0].remove() # ,
                return
            elif not post and pre:
                for ch in node.children:
                    if type(ch) == pytree.Node:
                        assert ch.children[-1].prefix + "intern" \
                                                       == str(ch.children[-1])
                        ch.children[-1].remove() # intern
                        assert ch.children[-1].prefix + "," \
                                                       == str(ch.children[-1])
                        ch.children[-1].remove() # ,
                return
            elif post and pre:
                for ch in node.children:
                    if type(ch) == pytree.Node:
                        for ch_ in ch.children:
                            if ch_ and ch_.prefix + "intern" == str(ch_):
                                last_ch_ = ch_.prev_sibling
                                ch_.remove() # intern
                                assert last_ch_.prefix + "," \
                                                       == str(last_ch_)
                                last_ch_.remove() # ,
                return
        syms = self.syms
        obj = results["obj"].clone()
        if obj.type == syms.arglist:
            newarglist = obj.clone()
        else:
            newarglist = pytree.Node(syms.arglist, [obj.clone()])
        after = results["after"]
        if after:
            after = [n.clone() for n in after]

        new = pytree.Node(syms.power,
                          [Name("intern")] +
                          [pytree.Node(syms.trailer,
                                       [results["lpar"].clone(),
                                        newarglist,
                                        results["rpar"].clone()] + after)])
        new.prefix = node.prefix
        return new