def fix_argument_list(root, atok):
    if not match_node(root, ast.arguments):
        return False
    if already_fixed(root) or match_node(root.parent,
                                         (ast.FunctionDef)) and fix_definition(
                                             root.parent, atok):
        return True
    return False
def get_arg_from_definition(root, atok):
    if not match_node(root, ast.arg):
        if match_node(root, ast.FunctionDef):
            fix_definition(root, atok)
        return None
    if match_parent(root, ast.FunctionDef):
        fix_definition(root.parent, atok)
    elif match_parent(root.parent, ast.FunctionDef):
        fix_definition(root.parent.parent, atok)
    else:
        return None
    return root
def get_raw(root):
	return (
		root.n if match_node(root,(ast.Num)) else 
		root.s if match_node(root,(ast.Str,ast.Bytes )) else 
		root.value if match_node(root,(ast.NameConstant)) else 
		root.id if match_node(root,(ast.Name )) else 
		get_raw(root.func) if match_node(root,(ast.Call )) else 
		root.arg if match_node(root,(ast.keyword)) else 
		root.attr+"."+get_raw(root.value) if match_node(root,(ast.Attribute )) else 
		get_raw(root.value) if match_node(root,(ast.Subscript,ast.Index	)) else 
		(get_raw(root.type),root.name) if match_node(root,(ast.ExceptHandler)) else 
		root.name if match_node(root,(ast.FunctionDef)) else None
						
	)
def is_of_higher_priority(parent_node,child_node):
	operator_priority = [
		ast.BitOr,ast.BitXor, ast.BitAnd,
		(ast.LShift,ast.RShift),(ast.Add,ast.Sub),
		(ast.Mult, ast.Div, ast.FloorDiv, ast.Mod)   ,
		ast.Pow, 
	]
	print(ast.dump(parent_node),parent_node.op)
	for operator in operator_priority:
		p = match_node(parent_node.op, operator)
		c = match_node(child_node.op, operator)
		print("\ntesting operator ",operator,p,c,"\n")
		if p and not c:
			return True
	return False
def fix_argument(root, atok, token=None):
    if already_fixed(root):
        return token
    # the following check was introduced to work around issue #17
    if not match_node(root.parent.parent, ast.FunctionDef):
        return None
    if token is None:
        fix_definition(root.parent.parent, atok)
        if not already_fixed(root):
            raise Exception("these ARG node has not been marked as fixed")
        return None
    mark_fixed(root)

    root.first_token = token

    fake_node = create_fake(root,
                            ast.Name,
                            real_tokens=token,
                            id=token.string,
                            ctx=ast.Load())
    set_fake(root, "arg", fake_node)
    if getattr(root, "annotation", False):
        root.last_token = root.annotation.last_token
        return root.annotation.last_token
    else:
        root.last_token = token
        return token
def get_class_name(root, atok):
    if not match_node(root, ast.ClassDef):
        return None
    if not already_fixed(root):
        generic_fix(root, atok)
    assert already_fixed(root), "Class Name has not been fixed"
    return get_fake(root, "name")
def get_definition_parameter_name(root, atok):
    if not match_node(root, ast.arg):
        return None
    if not already_fixed(root):
        generic_fix(root, atok)
    assert already_fixed(root), "Arg has not been fixed"
    return get_fake(root, "arg")
def get_definition_name(root, atok):
    if not match_node(root, ast.FunctionDef):
        return None
    if not already_fixed(root):
        generic_fix(root, atok)
    assert already_fixed(root), "Definition has not been fixed"
    return get_fake(root, "name")
def get_boolean_middle(root):
    if not match_node(root, ast.BoolOp) or match_parent(root, ast.BoolOp):
        return None
    items = root.values
    if len(items) == 3:
        return items[1]
    return None
def get_boolean_right(root):
    if not match_node(root, ast.BoolOp) or match_parent(root, ast.BoolOp):
        return None
    items = root.values
    if len(items) >= 2:
        return items[-1]
    return None
def get_arithmetic_middle(root):
    if not match_node(root, ast.BinOp) or match_parent(root, ast.BinOp):
        return None
    items = get_sub_index(root, None)
    if len(items) == 3:
        return items[1]
    return None
def get_arithmetic_right(root):
    if not match_node(root, ast.BinOp) or match_parent(root, ast.BinOp):
        return None
    items = get_sub_index(root, None)
    if len(items) >= 2:
        return items[-1]
    return None
Example #13
0
 def get_statement(self, origin, atok):
     # print("\norigin\n",ast.dump(origin))
     self.global_constrained_space = None
     candidate_statement = search_upwards(origin, ast.stmt)
     big = (ast.If, ast.While, ast.For, ast.FunctionDef, ast.With,
            ast.ClassDef, ast.Try, ast.ExceptHandler)
     if match_node(candidate_statement, big):
         candidate_statement = search_upwards_for_parent(origin, ast.stmt)
         candidate_statement = candidate_statement if candidate_statement else search_upwards(
             origin, ast.stmt)
         if match_node(candidate_statement, big):
             region = get_source_region(
                 atok, get_weak_header(candidate_statement, atok))
             if region:
                 self.global_constrained_space = region
     return candidate_statement
	def handle_single(self,view_information,query_description,extra = {}):
		build, selection, origin = self._preliminary(view_information = view_information,query_description = query_description,extra = extra)
		if not  build: 
			return None,None
		root,atok,m,r = build 
		definition_nodes = [search_upwards(origin,ast.FunctionDef)] if query_description["format"]>=2 else find_all_nodes(root,ast.FunctionDef)
		if query_description["format"]>=2:
			if "vertical_direction"  in query_description:
				definition_node = definition_nodes[0]
				temporary_information = lambda x: match_node(x,ast.FunctionDef) 
				direction = query_description["vertical_direction"]
				ndir = query_description["ndir"]
				row = view_information["rowcol"](m.backward(selection)[0])[0] + 1 if definition_node is None else definition_node.first_token.start[0]
				bonus = 1 if definition_node.first_token.startpos > selection[1]  else 0
				t = decode_abstract_vertical(root,atok,(),row, ndir + bonus,direction,True,temporary_information)
				definition_nodes = [t]

		name_nodes = make_flat([get_argument_from_definition(x)  for x in definition_nodes])
		names = list(OrderedDict([(x,0)  for x in name_nodes]).keys())
		if "experimental"  in query_description:
			names = [x + "=" + x  for x in names] 
		if query_description["format"]==1:
			result = None
		else:
			mode = {
				2:"individual",
				3:"range",
			}[query_description["format"]]
			result = ",".join(decode_item_selection(names,query_description,mode,"item_index"))
		return result, names
    def case_two(self, view_information, query_description, extra={}):
        ################################################################
        #		<adjective> <big_roi>
        ###############################################################
        build, selection, origin, definition_node = self.preliminary(
            view_information, query_description, extra)
        targets, exclusions, information = self.decode(query_description,
                                                       build)
        root, atok, m, r = build
        temporary_information = lambda x: information(x) if match_node(
            x, targets, exclusions) and generic_fix(x, atok) else None
        additional_parameters = {}

        if selection[0] != selection[1]:
            additional_parameters["small_root"] = origin
            additional_parameters["penalized"] = [origin]
        additional_parameters["only_information"] = True
        # just looking on the shape of this code you know there's a bug in here somewhere:)
        result, alternatives = adjective_strategy(
            atok=atok,
            root=definition_node,
            adjective_word=query_description["nth"],
            level_nodes=find_all_nodes(
                definition_node, (ast.If, ast.While, ast.For, ast.Try,
                                  ast.With, ast.FunctionDef, ast.ClassDef)),
            information_nodes=find_matching(definition_node,
                                            temporary_information),
            **additional_parameters)
        print("result, alternatives", result, alternatives, "\n")
        information = getattr(information, "secondary", information)
        result = information(result) if result else None
        alternatives = [information(x)
                        for x in alternatives] if alternatives else []
        return self._backward_result(result, alternatives, build)
def get_keyword_argument(root,index = None,only_value = True):
	temporary =  root.keywords if match_node(root,(ast.Call )) else None	
	temporary = [x.value  for x in temporary if only_value] if temporary else None
	return (
		(temporary[index] if index and len(temporary)>index else temporary) 
			if  temporary else None
	)
    def case_four(self, view_information, query_description, extra={}):
        ################################################################
        # [smart] <vertical_abstract_only_direction> [<ndir>] <block> [<adjective>] <big_roi> [<big_roi_sub_index>]
        ###############################################################
        build, selection, origin, definition_node = self.preliminary(
            view_information, query_description, extra)
        targets, exclusions, information = self.decode(query_description,
                                                       build)
        temporary_information = lambda x: match_node(x, ast.FunctionDef)
        root, atok, m, r = build

        direction = query_description["vertical_direction"]
        ndir = query_description["ndir"]
        row = view_information["rowcol"](
            m.backward(selection)[0]
        )[0] + 1 if definition_node is root else definition_node.first_token.start[
            0]
        bonus = 1 if definition_node.first_token.startpos > selection[1] else 0
        t = decode_abstract_vertical(root, atok, targets, row, ndir + bonus,
                                     direction, True, temporary_information)
        if "nth" not in query_description:
            information = getattr(information, "secondary", information)
            selector = lambda x: match_node(x, targets, exclusions
                                            ) and generic_fix(x, build[1])
            candidates = tiebreak_on_lca(root, definition_node,
                                         find_all_nodes(t, selector=selector))
            candidates = [information(x) for x in candidates if information(x)]
            result, alternatives = obtain_result(None, candidates)
            return self._backward_result(result, alternatives, build)
        else:
            additional_parameters = {}
            result, alternatives = adjective_strategy(
                atok=atok,
                root=t,
                adjective_word=query_description["nth"],
                level_nodes=find_all_nodes(
                    t, (ast.If, ast.While, ast.For, ast.Try, ast.With,
                        ast.FunctionDef)),
                information_nodes=find_matching(
                    t, lambda x: information(x)
                    if match_node(x, targets, exclusions) else None),
                **additional_parameters)
            information = getattr(information, "secondary", information)
            result = information(result) if result else None
            alternatives = [information(x)
                            for x in alternatives] if alternatives else []
            return self._backward_result(result, alternatives, build)
def get_subparts_of_binary_operation(root):
	left = (
		[root.left] 
		if not match_node(root.left,ast.BinOp)  or is_of_higher_priority(root,root.left)  
		else get_subparts_of_binary_operation(root.left)
	)
	right = [root.right]
	return left + right
def get_decorator_text(root, atok, everything):
    if not is_decorator(root):
        return None
    if everything:
        return atok.get_text(root)
    else:
        if match_node(root, ast.Call):
            return atok.get_text(root.func)
def get_module(root, atok):
    if not match_node(root, (ast.Import, ast.ImportFrom)):
        return None
    if not already_fixed(root):
        fix_import(root, atok)
    assert already_fixed(
        root), "inside get_module I received an node that is not fixed"
    return get_fake(root, "module")
def get_subparts_of_attribute(root):
    if not match_node(root, ast.Attribute):
        return None
    l = root.last_token
    if already_fixed(root):
        fake_node = get_fake(root, "attr")
    else:
        fake_node = create_fake(root,
                                ast.Name,
                                text=l.string,
                                start_position=l.startpos,
                                id=l.string,
                                ctx=root.ctx)
    if match_node(root.value, ast.Attribute):
        return get_subparts_of_attribute(root.value) + [fake_node]
    else:
        return [root.value, fake_node]
Example #22
0
def node_from_range_new(root, atok, r, special=False, lenient=False):
    # notes like ast.Store result in (0,0) with atok.get_text_range()
    # which causes problems if the cursor is right at the beginning of the file
    inside = lambda x, y: (y[0] <= x[0] < y[1] and y[0] < x[1] <= y[1] and
                           not y[0] == y[1] == 0)
    if lenient:
        inside = lambda x, y: (y[0] <= x[0] <= y[1] and y[0] <= x[1] <= y[1]
                               and not y[0] == y[1] == 0)
    generic_fix(root, atok)
    for child in ast.iter_child_nodes(root):
        # print(" just to check something out",child,atok.get_text_range(child))
        # print(" and the child fields are ",child._fields)
        if inside(r, atok.get_text_range(child)):
            # print(" success with",child,"special = ",special)
            return node_from_range_new(child, atok, r, special, lenient)
    if special:
        # print("Special:\n",ast.dump(root))
        if match_node(root,
                      (ast.Tuple, ast.List, ast.Set, ast.Dict, ast.DictComp)):
            # print("Inside Here After Special")
            temporary = get_sub_index(root, None)

            l = [
                x for x in temporary for y in [get_source_region(atok, x)]
                if inside((r[0], r[0]), y) or inside((
                    r[1], r[1]), y) or inside(y, r)
            ]
            # print("temporary:\n",temporary)
            # print("L:\n",l)
            if l and l != temporary:
                return l

    if match_node(root, (ast.FunctionDef, ast.ClassDef)):
        converter = atok._line_numbers
        sr, sc = converter.offset_to_line(r[0])
        er, ec = converter.offset_to_line(r[1])
        for decorator in root.decorator_list:
            if sr != decorator.first_token.start[0]:
                continue
            if er != decorator.last_token.start[0]:
                if er != decorator.last_token.start[0] + 1 or ec != 0:
                    continue
            return decorator

    return root
def get_argument_from_definition(root,raw = True,index = None):
	if not match_node(root,ast.FunctionDef):
		return None
	x= root.args
	temporary = x.args  + [x.vararg] + x.kwonlyargs + [x.kwarg]
	temporary = [y  for y in temporary if y]
	if raw:
		temporary = [(y.arg if isinstance(y,ast.AST) else y) for y in temporary]
	return temporary[index] if (index is not None) and len(temporary)>index else temporary
    def case_three(self, view_information, query_description, extra={}):
        ################################################################
        # <vertical_abstract_only_direction> [<ndir>] <big_roi> [<big_roi_sub_index>]
        ###############################################################
        build, selection, origin, definition_node = self.preliminary(
            view_information, query_description, extra)
        targets, exclusions, information = self.decode(query_description,
                                                       build)
        root, atok, m, r = build
        temporary_information = lambda x: information(x) if match_node(
            x, targets, exclusions) and generic_fix(x, atok) else None

        direction = query_description["vertical_direction"]
        ndir = query_description["ndir"]
        row, column = view_information["rowcol"](m.backward(selection)[0])

        # bug fixing
        test_result = decode_abstract_vertical(root,
                                               atok,
                                               targets,
                                               row + 1,
                                               1,
                                               direction,
                                               True,
                                               temporary_information,
                                               want_alternatives=False)
        l = search_upwards_log(origin, ast.stmt)
        if (test_result in [l[0]] + l[1]
                and row + 1 >= test_result.first_token.start[0]
                and row + 1 <= test_result.last_token.end[0]):

            ndir = ndir + 1

        result, alternatives = decode_abstract_vertical(root,
                                                        atok,
                                                        targets,
                                                        row + 1,
                                                        ndir,
                                                        direction,
                                                        True,
                                                        temporary_information,
                                                        want_alternatives=True)

        if result:
            new_definition_node = search_upwards(result, ast.FunctionDef)
            if definition_node is not new_definition_node and new_definition_node is not None:
                alternatives = tiebreak_on_lca(
                    new_definition_node, result,
                    find_all_nodes(new_definition_node, targets, exclusions))

        result, alternatives = obtain_result(result, alternatives)
        information = getattr(information, "secondary", information)
        result = information(result) if result else None
        alternatives = [information(x)
                        for x in alternatives] if alternatives else []
        return self._backward_result(result, alternatives, build)
Example #25
0
 def tiebreaker(x):
     depth, node = lca(x, origin, node_and_depth=True)
     v = 3
     if match_node(node, ast.Dict):
         if node is not x and node is not origin:
             field, field_index = lca.get_field_with_respect_to(x, node)
             ofield, ofield_index = lca.get_field_with_respect_to(
                 origin, node)
             v = abs(field_index - ofield_index)
             v = v if v < 3 else 3
     return (-1 * depth, v, lca.get_depth(x),
             abs(x.first_token.start[0] - origin.first_token.start[0]))
def get_argument_from_empty_call(root):
    if not match_node(root, (ast.Call)):
        return None

    if get_argument_from_call(root, 0):
        return None
    return create_fake(root,
                       ast.Name,
                       text="",
                       start_position=root.last_token.startpos,
                       parent=root,
                       parent_field="args")
def get_keyword_argument(root,
                         index=None,
                         only_value=True,
                         only_keyword=False):
    temporary = root.keywords if match_node(root, (ast.Call)) else None
    if only_value and not only_keyword:
        temporary = [x.value for x in temporary] if temporary else None
    elif only_keyword:
        temporary = [
            get_fake(x, "arg") for x in temporary if generic_fix(x, None)
        ] if temporary else None
    return ((temporary[index] if index is not None and len(temporary) > index
             else temporary) if temporary else None)
def get_argument_from_call(root, index):
	if  not match_node(root,(ast.Call)):
		return None
	temporary = [
		get_positional_argument(root,None),
		get_keyword_argument(root,None, True),
		[get_star_argument(root,None)],
		[get_keyword_star_argument(root,None)]
	]
	temporary = make_flat([x  for x in temporary if x])
	temporary = [x  for x in temporary if x]
	temporary = sorted(temporary,key= lambda x:x.first_token.startpos)
	# print(" I would regret face", temporary)
	return temporary[index] if temporary and len(temporary)>index else None
def fix_attribute(root, atok):
    if already_fixed(root):
        return True
    l = root.last_token
    fake_node = create_fake(root,
                            ast.Name,
                            real_tokens=l,
                            parent=root,
                            parent_field="attr",
                            id=l.string,
                            ctx=root.ctx)
    set_fake(root, "attr", fake_node)
    if match_node(root.value, ast.Attribute):
        fix_attribute(root.value, atok)
    mark_fixed(root)
def get_weak_header(root, atok):
    return (root.test if match_node(root, (ast.While, ast.If)) else
            root.arguments if match_node(root, (ast.FunctionDef)) else
            [root.target, root.iter] if match_node(root,
                                                   (ast.For)) else root.bases +
            root.keywords if match_node(root, (
                ast.ClassDef)) else root.items if match_node(root, (
                    ast.With
                )) else root.type if match_node(root,
                                                (ast.ExceptHandler)) else None)