def _remove_init_all(r): """Remove any __all__ in __init__.py file.""" new_r = redbaron.NodeList() for n in r.node_list: if n.type == "assignment" and n.target.value == "__all__": pass else: new_r.append(n) return new_r
def insert_import_node(r, dotted_name): new_r = redbaron.NodeList() first = True for v in r.node_list: if v.type == "import" and first: first = False new_r.append(redbaron.RedBaron("import {}\n".format(dotted_name))) new_r.append(v) return new_r
def test_get_node_of_region_slice_list(): """Test get_node_of_region for when the nodes are only a slice in a node list""" testsrc = redbaron.RedBaron("1+1\n" "a=1\n" "for i in range(10):\n" " b=i\n" "c=3") start = reddel_server.Position(2, 3) # a="1" end = reddel_server.Position(4, 6) # b"="1 expected = redbaron.NodeList(testsrc.node_list[2:5]) assert expected.dumps() == reddel_server.get_node_of_region(testsrc, start, end).dumps()
def _remove_module_level_docstrs_in_unit_tests(r): """We previously used more groups for the import statements and named each group.""" logging.info("Removing module level docstrs in tests") new_r = redbaron.NodeList() first = True for v in r.node_list: if v.type == "string" and first: first = False else: new_r.append(v) return new_r
def _add_absolute_import(r): if has_future_absolute_import(r): return r new_r = redbaron.NodeList() first = True for v in r.node_list: if v.type == "import" and first: first = False new_r.append( redbaron.RedBaron("from __future__ import absolute_import\n")) new_r.append(v) return new_r
def _remove_single_line_import_comments(r): """We previously used more groups for the import statements and named each group.""" logging.info("Removing single line import comments") import_r, remaining_r = split_by_last_import(r) new_import_r = redbaron.NodeList() for i, v in enumerate(import_r): if 1 < i < len(import_r) - 2: if not (import_r[i - 2].type != "comment" and v.type == "comment" and import_r[i + 2].type != "comment") or _is_keep_comment(v): new_import_r.append(v) else: new_import_r.append(v) return new_import_r + remaining_r
def get_node_of_region(red, start, end): """Get the node that contains the given region :param red: the red baron source :type red: :class:`redbaron.RedBaron` :param start: position of the beginning of the region :type start: :class:`Position` :param end: position of the end of the region :type end: :class:`Position` :returns: the node that contains the region :rtype: :class:`redbaron.base_nodes.Node` First the nodes at start and end are gathered. Then the common parent is selected. If the common parent is a list, the minimum slice is used. This makes it easier for the user because he can partially select nodes and still gets what he most likely intended to get. For example if your region partially selects several lines in a for loop and you want to extract them (``|`` shows the region bounds):: for i in range(10): a = 1 + 2 b |= 4 c = 5 d =| 7 then we expect to get back:: b = 4 c = 5 d = 7 Note that the leading tab is missing because it doesn't belong to the ``b = 4`` node. .. doctest:: >>> import redbaron >>> import reddel_server >>> testsrc = ("for i in range(10):\\n" ... " a = 1 + 2\\n" ... " b = 4\\n" ... " c = 5\\n" ... " d = 7\\n") >>> start = (3, 7) >>> end = (5, 8) >>> reddel_server.get_node_of_region(redbaron.RedBaron(testsrc), start, end).dumps() 'b = 4\\n c = 5\\n d = 7' You can also partially select a list: .. doctest:: >>> testsrc = "[1, 2, 3, 4, 5, 6]" >>> start = (1, 8) # "3" >>> end = (1, 14) # "5" >>> reddel_server.get_node_of_region(redbaron.RedBaron(testsrc), start, end).dumps() '3, 4, 5' """ snode = red.find_by_position(start) enode = red.find_by_position(end) if snode == enode: return snode snodes = [snode] + list(get_parents(snode)) enodes = [enode] + list(get_parents(enode)) previous = red for snode, enode in list(zip(reversed(snodes), reversed(enodes))): # both list of parents should end with the root node # so we iterate over the parents in reverse until we # reach a parent that is not common. # the previous parent then has to encapsulate both if snode != enode: if hasattr(previous, "node_list"): # For lists we want the minimum slice of the list. # E.g. the region spans over 2 functions in a big module # the common parent would be the root node. # We only want the part containing the 2 functions and not everything. # Unfortunately we might loose some information if we slice the proxy list. # When we use node_list then we keep things like new lines etc. try: sindex = previous.node_list.index(snode) eindex = previous.node_list.index(enode) except ValueError: # if one of the nodes is not in the list, it means it's not part of # previous value. E.g. previous is a function definition and snode # is part of the arguments while enode is part of the function body. # in that case we take the whole previous node (e.g. the whole function) pass else: previous = redbaron.NodeList( previous.node_list[sindex:eindex + 1]) break previous = snode return previous