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)
Exemplo n.º 2
0
def decode_abstract_vertical(root,atok, target,current_line, index,direction,
	 want_node = False, selector = None, want_alternatives = False):
	nodes = find_matching(root, selector)  if  selector else  find_all_nodes(root,target)
	line_information = set()
	for n in nodes:	
		line_information.add(n.first_token.start[0])
	line_information = sorted(list(line_information))
	ll=len(line_information)
	if direction=="below":	
		i = bisect_right(line_information,current_line)
		i = min(ll-1,i+index-1)
	elif direction=="above":
		i = bisect_left(line_information,current_line)
		i  = max(0,i-index)
	else:
		return None
	if want_node:
		if want_alternatives:
			candidates = [x  for x in nodes if x.first_token.start[0] == line_information[i]]
			return obtain_result(None,candidates) 
		for n in nodes:
			if n.first_token.start[0] == line_information[i]:
				return n
		else:
			return None
	else:
		return line_information[i]
    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)
Exemplo n.º 4
0
	def case_two(self,view_information,query_description, extra = {}):
		################################################################	
		#		<vertical_direction> <ndir> <adjective> argument <argument_index>
		###############################################################	
		selection = self._get_selection(view_information,extra)
		vertical_direction = query_description["vertical_direction"]
		ndir = query_description["ndir"]

		if vertical_direction in ["up","down"]:
			row, column = view_information["rowcol"](m.backward(selection)[0])
			nr = max(0,row + ndir if vertical_direction=="down" else row - ndir)
			t = view_information["text_point"](nr,0)
			selection = (t,t)

		build = self.general_build if self.general_build else line_partial(selection[0])
		if not build  or not build[0] :
			return None,None
		root,atok,m,r  = build 
		selection = m.forward(selection)

		# this is the wrong but for some reason it is working:)
		if vertical_direction in ["above","below"]:
			row, column = view_information["rowcol"](m.backward(selection)[0]) 
			nr = decode_abstract_vertical(root,atok,ast.Call,row+1, ndir,vertical_direction)-1
			t = view_information["text_point"](nr,0)
			selection = (t,t)
			selection = m.forward(selection)		

		origin = nearest_node_from_offset(root,atok, selection[0]) if selection[0]==selection[1] else node_from_range(root,atok, selection)
		statement_node = search_upwards(origin,ast.stmt)


		# 
		alternative_logical_lines = find_all_nodes(statement_node.parent,selector = lambda x:match_node(x,ast.stmt) and x.first_token.start[0]==origin.first_token.start[0]
			,visit_all_levels=False)
		sharing_physical = alternative_logical_lines not in [[] ,[statement_node]]

		priority = {"root_lexical_order":1} if ( statement_node.first_token.start[0] != origin.first_token.start[0]
									or sharing_physical) else {}
		result, alternatives = self.process_line(
			q = query_description,
			root = statement_node if not sharing_physical else statement_node.parent,
			atok = atok,
			origin = None,
			select_node = None,
			tiebreaker = lambda x: tiebreak_on_lca(statement_node if not sharing_physical else statement_node.parent,origin,x),
			line = nr+1,
			priority = priority,
			constrained_space = m.forward((view_information["text_point"](nr ,0),view_information["text_point"](nr + 1,0))),
			second_tiebreaker = lambda x,y : tiebreak_on_visual(row + 1,x,y)
		)
		if sharing_physical:
			if alternatives:
				alternatives = [x  for x in alternatives if  x.first_token.start[0]==origin.first_token.start[0]]
			if result and result.first_token.start[0]!=origin.first_token.start[0]:
				result, alternatives = obtain_result(result, alternatives)
		return self._backward_result(result, alternatives,build)
    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)
Exemplo n.º 6
0
    def case_four(self, view_information, query_description, extra={}):
        ################################################################
        #		<level> [<level_index>] <adjective> (argument <argument_index>|caller [<sub_index>])
        ###############################################################
        selection = self._get_selection(view_information, extra)
        build = self.general_build if self.general_build else line_partial(
            self.code, selection[0])
        if not build or not build[0]:
            return None, None
        root, atok, m, r = build
        selection = m.forward(selection)
        origin = nearest_node_from_offset(
            root, atok,
            selection[0]) if selection[0] == selection[1] else node_from_range(
                root, atok, selection)
        statement_node = self.get_statement(origin, atok)

        ###############################################################
        # transformationszooming
        ################################################################
        every_caller = find_all_nodes(root, (ast.Call))
        level = LevelVisitor(root, every_caller, atok)
        lca = LCA(statement_node)

        def transformation(node):
            calling_parent, field, field_index = level[node]
            if not calling_parent or calling_parent is level.root:
                return None
            if query_description[
                    "level_index"] == 0 or correspond_to_index_in_call(
                        calling_parent, query_description["level_index"] - 1,
                        field, field_index):
                adj = translate_adjective[query_description["nth"]] - 1
                n = level(node, 3, adj)
                return node if n is node else None
            else:
                return None

        def inverse_transformation(node):
            return [node]

        q = deepcopy(query_description)
        del q["nth"]
        result, alternatives = self.process_line(
            q=q,
            root=statement_node,
            atok=atok,
            origin=origin,
            select_node=origin if selection[0] != selection[1] else None,
            tiebreaker=lambda x: tiebreak_on_lca(statement_node, origin, x, lca
                                                 ),
            transformation=transformation,
            inverse_transformation=inverse_transformation,
            invert_then_tiebreak=False)
        return self._backward_result(result, alternatives, build)
Exemplo n.º 7
0
 def handle_single(self, view_information, query_description, extra={}):
     build, selection, origin = self._preliminary(view_information,
                                                  query_description, extra)
     if not build:
         return None, None
     root, atok, m, r = build
     definition_nodes = find_all_nodes(root, (ast.FunctionDef))
     name_nodes = [x.name for x in definition_nodes]
     names = list(OrderedDict([(x, 0) for x in name_nodes]).keys())
     result = None
     return names
 def handle_single(self, view_information, query_description, extra={}):
     build, selection, origin = self._preliminary(view_information,
                                                  query_description, extra)
     if not build:
         return None, None
     root, atok, m, r = build
     definition_nodes = find_all_nodes(root, (ast.Import, ast.ImportFrom))
     name_nodes = make_flat(
         [get_imported_value_names(x) for x in definition_nodes])
     names = list(OrderedDict([(x, 0) for x in name_nodes]).keys())
     result = names[query_description["collect_index"] -
                    1] if query_description["format"] == 2 else None
     return result, names
Exemplo n.º 9
0
	def handle_single(self,view_information,query_description,extra = {}):
		build, origin, selection = self._preliminary(view_information,query_description,extra)
		if not  build: 
			return None,None
		root,atok,m,r = build 
		definition_nodes = find_all_nodes(root,(ast.Import,ast.ImportFrom))
		name_nodes = make_flat([get_module_names(x)  for x in definition_nodes])
		for name in name_nodes:
			smaller = name.split(".")
			if len( smaller)>1:
				name_nodes.append(".".join(smaller[:-1]))
		names = list(OrderedDict([(x,0)  for x in name_nodes]).keys())
		result = None
		return  names
Exemplo n.º 10
0
 def case_one(self, view_information, query_description, extra={}):
     ################################################################
     #		<big_roi>
     ###############################################################
     build, selection, origin, definition_node = self.preliminary(
         view_information, query_description, extra)
     targets, exclusions, information = self.decode(query_description)
     information = getattr(information, "secondary", information)
     candidates = tiebreak_on_lca(
         definition_node, origin,
         find_all_nodes(definition_node, targets, exclusions))
     candidates = [information(x) for x in candidates if information(x)]
     result, alternatives = obtain_result(None, candidates)
     return self._backward_result(result, alternatives, build)
Exemplo n.º 11
0
 def handle_single(self, view_information, query_description, extra={}):
     build, selection, origin = self._preliminary(view_information,
                                                  query_description, 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)
     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())
     result = names[query_description["collect_index"] -
                    1] if query_description["format"] == 2 else None
     return result, names
Exemplo n.º 12
0
 def handle_single(self, view_information, query_description, extra={}):
     build, selection, origin = self._preliminary(view_information,
                                                  query_description, extra)
     if not build:
         return None, None
     root, atok, m, r = build
     definition_node = search_upwards(
         origin,
         ast.FunctionDef) if query_description["format"] == 2 else root
     name_nodes = [(get_id(x), 0)
                   for x in find_all_nodes(definition_node, ast.Name)
                   if is_store(x)]
     names = list(OrderedDict(name_nodes).keys())
     print(names, "names")
     result = names[query_description["collect_index"] -
                    1] if query_description["format"] == 2 else None
     return result, names
	def handle_single(self,view_information,query_description,extra = {}):
		build, selection, origin = self._preliminary(view_information,query_description,extra)
		if not  build: 
			return None,None
		root,atok,m,r = build 
		definition_node = search_upwards(origin,ast.FunctionDef) if query_description["format"]!=1 else root
		name_nodes = [(get_id(x),0)  for x in find_all_nodes(definition_node,ast.Name) if is_store(x)]
		names = list(OrderedDict(name_nodes).keys())
		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
Exemplo n.º 14
0
    def case_one(self, v, q):
        """	
				<adjective> argument <argument_index> 
		"""
        selection = v["selection"]
        build = self.general_build if self.general_build else line_partial(
            selection[0])
        print(build)
        if not build:
            return None, None
        root, atok, m, r = build
        selection = m.forward(selection)

        # after build we set up some variables
        result = None
        alternatives = None

        origin = nearest_node_from_offset(root, atok, selection[0])
        calling_node = search_upwards(origin, ast.Call)
        statement_node, callers = search_upwards_log(origin,
                                                     ast.stmt,
                                                     log_targets=ast.Call)
        information = lambda x: info.get_argument_from_call(
            x, q["argument_index"] - 1)
        candidates = sorted_by_source_region(
            atok, find_matching(statement_node, info.identity(information)))
        every_caller = find_all_nodes(statement_node, (ast.Call))

        ################################################################
        # if no adjective is given
        ################################################################
        if q["adjective"] == "None":
            if calling_node:
                result = information(calling_node)
            candidates = [
                information(x)
                for x in tiebreak_on_lca(statement_node, origin, candidates)
                if x != calling_node
            ]
            result, alternatives = obtain_result(result, candidates)

        ################################################################
        # adjective is even
        ################################################################
        if q["adjective"] != "None":
            additional_parameters = {}
            if selection[0] != selection[1]:
                small_root = node_from_range(root, atok, selection)
                additional_parameters["small_root"] = small_root
                print("dumping the small root\n", ast.dump(small_root))
            additional_parameters["special"] = [calling_node]
            if calling_node:
                print(ast.dump(calling_node))
                additional_parameters["penalized"] = [calling_node]
            result, alternatives = adjective_strategy(
                atok=atok,
                root=root,
                adjective_word=q["adjective"],
                level_nodes=every_caller,
                information_nodes=candidates,
                **additional_parameters)
            result = information(result) if result else m.forward(
                v["selection"])
            alternatives = [information(x)
                            for x in alternatives] if alternatives else []

        # translate those nodes back to offsets and forward them through the modification under
        print("\n\nnow finally printing result and alternatives\n\n")
        print(result)
        print(
            ast.dump(result) if isinstance(result, ast.AST
                                           ) else " not known node")
        print(alternatives)
        result = m.backward(get_source_region(atok, result))
        alternatives = [
            m.backward(get_source_region(atok, x)) for x in alternatives
        ]
        return result, alternatives
	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
Exemplo n.º 16
0
    def process_line(self,
                     q,
                     root,
                     atok,
                     origin=None,
                     select_node=None,
                     tiebreaker=lambda x: x,
                     line=None,
                     transformation=None,
                     inverse_transformation=None,
                     priority={},
                     constrained_space=(),
                     second_tiebreaker=None,
                     invert_then_tiebreak=True):
        result = None
        alternatives = None
        additional_parameters = {"priority": priority}
        if constrained_space:
            additional_parameters["constrained_space"] = constrained_space

        information = self.get_information(q)
        information_nodes = sorted_by_source_region(
            atok, find_matching(root, information))

        if origin:
            calling_node = search_upwards(origin, ast.Call)
            statement_node, callers = search_upwards_log(origin,
                                                         ast.stmt,
                                                         log_targets=ast.Call)

        every_caller = find_all_nodes(root, (ast.Call))
        additional = find_all_nodes(root, (ast.Tuple, ast.List, ast.ListComp))
        if additional:
            additional_parameters["additional_level_nodes"] = additional
            additional_parameters["try_alternative"] = True

        ################################################################
        # go to handle the special Casey's
        ################################################################
        if transformation:
            temporary = {transformation(x) for x in information_nodes}
            information_nodes = sorted_by_source_region(
                atok, [x for x in temporary if x])

        ################################################################
        # if no adjective is given
        ################################################################
        if "nth" not in q:
            if origin and calling_node:
                result = calling_node if calling_node in information_nodes else None
                information_nodes = [
                    x for x in tiebreaker(information_nodes)
                    if x is not calling_node
                ]
            else:
                result = None
                information_nodes = [x for x in tiebreaker(information_nodes)]
            result, alternatives = obtain_result(result, information_nodes)

        ################################################################
        # adjective is given
        ################################################################
        if "nth" in q:
            additional_parameters["small_root"] = select_node
            if origin and calling_node:
                additional_parameters["penalized"] = [
                    calling_node
                ] if calling_node else []
                additional_parameters["special"] = [calling_node]
            result, alternatives = adjective_strategy(
                atok=atok,
                root=root,
                adjective_word=q["nth"],
                level_nodes=every_caller,
                information_nodes=information_nodes,
                **additional_parameters)

        ##############################################################
        # reverse transformation if any
        ##############################################################
        if transformation:
            helpful = [result] if result else []
            if alternatives:
                helpful.extend(alternatives)
            if invert_then_tiebreak:
                temporary = make_flat(
                    [tiebreaker(inverse_transformation(x)) for x in helpful])
            else:
                temporary = tiebreaker(
                    make_flat([inverse_transformation(x) for x in helpful]))
            result, alternatives = obtain_result(None, temporary)

        ################################################################
        # Extract information
        ################################################################
        if line and result and alternatives:
            temporary = [result] + alternatives
            temporary = [(i, x) for i, x in enumerate(temporary)]
            temporary = sorted(
                temporary,
                key=lambda x: (atok._line_numbers.offset_to_line(
                    get_source_region(atok, x[1])[0])[0] != line, x[0]))
            temporary = [x[1] for x in temporary]
            result, alternatives = obtain_result(None, temporary)

        if result and second_tiebreaker:
            alternatives = second_tiebreaker(result, alternatives)

        if self.global_constrained_space:
            inside = lambda x, y: (y[0] <= x[0] < y[1] and y[0] < x[1] <= y[1])
            result = result if inside(get_source_region(atok, result),
                                      self.global_constrained_space) else None
            alternatives = [
                x for x in alternatives if inside(
                    get_source_region(atok, x), self.global_constrained_space)
            ]
            result, alternatives = obtain_result(result, alternatives)

        result = information(result) if result else None
        alternatives = [information(x)
                        for x in alternatives] if alternatives else []
        return result, alternatives