Esempio n. 1
0
def _apply_global_rank_adjustment(base_result, indexed_dirs, query):
  all_open_filenames = []
  if query.current_filename:
    all_open_filenames.append(query.current_filename)
  all_open_filenames.extend(query.open_filenames)

  # The active_dir_orders goes from a directory prefix to an order
  # value.  We use this to break ties when the same basename crops up in two
  # places: the basename that is in the most recently active directory wins.
  #
  # This is built as a dict, but then converted to a flat list because we iterate it
  # during execution.
  inactive_dirs = set(indexed_dirs)
  active_dir_orders = {}
  for open_filename in all_open_filenames:
    for d in indexed_dirs:
      if open_filename.startswith(d):
        if d not in active_dir_orders:
          active_dir_orders[d] = len(active_dir_orders)
          inactive_dirs.remove(d)
  inactive_dirs = list(inactive_dirs)
  inactive_dirs.sort()
  for i in inactive_dirs:
    active_dir_orders[i] = len(active_dir_orders)
  active_dir_orders = [(x,y) for x,y in active_dir_orders.items()]

  def get_order(f):
    for d,order in active_dir_orders:
      if f.startswith(d):
        return order
    return sys.maxint

  def hit_cmp(x,y):
    # directory order trumps everything
    h = get_order(x[0]) - get_order(y[0])
    if h != 0:
      return h

    # compare on the rank
    j = -cmp(x[1],y[1])
    if j != 0:
      return j

    # if the ranks agree, compare on the filename,
    # first by basename, then by fullname
    x_base = os.path.basename(x[0])
    y_base = os.path.basename(y[0])
    j = cmp(x_base, y_base)
    if j != 0:
      return j

    # last resort is to compare full names
    return cmp(x[0], y[0])

  hits = list(base_result.hits)
  hits.sort(hit_cmp)
  new_hits = _rerank(hits)
  res = QueryResult(new_hits, base_result.truncated)
  res.debug_info = copy.deepcopy(base_result.debug_info)
  return res
Esempio n. 2
0
def _filter_for_base_path(base_result, query):
    res = QueryResult()
    res.debug_info = copy.deepcopy(base_result.debug_info)
    res.truncated = base_result.truncated

    for hit,rank in base_result.hits:
      if _is_in_base_path(hit, query.base_path):
        res.filenames.append(hit)
        res.ranks.append(rank)
    return res
Esempio n. 3
0
def _filter_result_for_exact_matches(query_text, base_result):
  """
  Returns a new QueryResult object containing only filenames that exactly
  match the provided query.
  """
  res = QueryResult()
  res.debug_info = copy.deepcopy(base_result.debug_info)
  res.truncated = base_result.truncated

  for hit,rank in base_result.hits:
    if _is_exact_match(query_text, hit):
      res.filenames.append(hit)
      res.ranks.append(rank)
  return res