def handle_single(self, view_information, query_description, extra={}):
     state = extra["state"]
     candidates = result_alternatives_sequence(state, location=True)
     if query_description["format"] == 1:
         if state["mode"] == "single":
             selection = [
                 candidates[query_description["color" + i]]
                 for i in ["", "2", "3", "4"]
                 if "color" + i in query_description
             ]
             selection = selection if isinstance(selection,
                                                 list) else [selection]
         elif state["mode"] == "multiple":
             try:
                 print("candidates", candidates, "\n")
                 selection = [
                     x[query_description["color" + i]] for x in candidates
                     for i in ["", "2", "3", "4"]
                     if "color" + i in query_description
                 ]
             except IndexError as e:
                 raise Exception(
                     "tried to obtain an alternative color that is not common!"
                 )
             if any(isinstance(x, list) for x in selection):
                 print("inside here the selection has become ", selection)
                 selection = make_flat(
                     [x if isinstance(x, list) else [x] for x in selection])
                 print(
                     "inside here the selection has become after wardsit's",
                     selection)
     selection = self.filter_overlapping(selection)
     print("selection is ", selection, "\n")
     return [(x, "") for x in selection]
Пример #2
0
def find_information(root, information, flatten=False, visit_all_levels=True):
    reachable = ast.walk if visit_all_levels else ast.iter_child_nodes
    initial_result = [
        information(node) for node in reachable(root)
        if information(node) is not None
    ]
    return make_flat(initial_result) if flatten else initial_result
	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
Пример #4
0
 def handle_single(self, view_information, query_description, extra={}):
     state = extra["state"]
     # print("state:\n",state)
     candidates = result_alternatives_sequence(state, location=True)
     # print("candidates:\n",candidates)
     if "alternative_index" in query_description:
         name = "alternative_index"
     elif "color" in query_description:
         name = "color"
     else:
         return None, None
     if state["mode"] == "single":
         result = decode_item_selection(candidates,
                                        query_description,
                                        "individual",
                                        name,
                                        decrement=False)
         if len(result) == 1:
             result = result[0]
     else:
         result = [
             decode_item_selection(x,
                                   query_description,
                                   "individual",
                                   name,
                                   decrement=False) for x in candidates
         ]
         selection = self._get_selection(view_information, extra)
         if not isinstance(selection, list) or len(selection) == 1:
             try:
                 result = make_flat(result)
             except:
                 pass
         # print("result is ",result)
     return result, []
 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
Пример #6
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
Пример #7
0
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
Пример #8
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
Пример #9
0
def split_string(s,even_letters = True):
	s = s.strip()
	y = urlparse(s)
	if  not (y.scheme=="" and y.netloc==""):
		return [z  for z in make_flat( [split_string(x,False)  for x in y ]) if z]
	first_attempt = [x  for x in re.split("[., :/]",s) if not x.isspace()]
	if len(first_attempt) > 1:
		return first_attempt
	second_attempt = [x  for x in re.split("[_]",s) if not x.isspace()]
	if len(second_attempt) > 1:
		return second_attempt
	# https://stackoverflow.com/questions/29916065/how-to-do-camelcase-split-in-python answer from Jossef Harush 
	third_attempt = re.sub('([A-Z][a-z]+)', r' \1', re.sub('([A-Z]+)', r' \1', s)).split()
	if len(third_attempt) > 1:
		return third_attempt

	return list(s) if even_letters else [s]
def convert_multiple_to_single(state,mode,initial_mode,lenient = False):
	names = []
	if mode == "single":
		names = names + ["result","origin","alternatives"]
	if initial_mode == "single":
		names = names + ["initial_origin"]
	for k in names:
		data = state[k]
		if k not in ["alternatives"]:
			if data == []:
				data = None
			elif isinstance(data,list) and len(data)==1 and isinstance(data[0],list) and len(data[0])==1:
				data = data[0][0]
			else:
				if lenient:
					pass
				else:
					raise Exception("when converting from multiple mode In single_mode "+k+" cannot be a nested list!")
		else:
			if isinstance(data,list) and len(data)==1:
				data = make_flat(data)
			else:
				raise Exception(" when converting into single mode, each of the items in the alternatives must have length of one")
		state[k] = data
Пример #11
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