#
# Copyright (c) 2005 Conectiva, Inc.
#
# Written by Gustavo Niemeyer <niemeyer@conectiva.com>
#
# This file is part of Smart Package Manager.
#
# Smart Package Manager is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# Smart Package Manager is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Smart Package Manager; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
from smart.util.strtools import globdistance
from smart.cache import Provides
from smart import *
import fnmatch
import re

def _stripeol(pattern):
    if pattern.endswith("$"):
         pattern = pattern[:-1]
    elif pattern.endswith('\Z(?ms)'):
        pattern = pattern[:-7]
    return pattern

class Searcher(object):
    """
    The search mechanism is smart is accessed mainly by giving a
    Searcher instance to the cache.search() method.
    
    Internally, the searching may go through different paths depending
    on the kind of information being looked for. More specifically:

    - nameversion is matched in Package.search(), so that backends
      may implement their own details in the searching mechanism.

    - provides is matched in Provides.search(), for the same reason.

    - requires, recommends, upgrades, and conflicts don't have special
      searching methods. Instead, their usual match() method is given
      an instance of the Provides type.

    - group, path, url, and other information which is found by
      PackageInfo, is searched by the Loader.search() method and
      the overloaded methods in Loader's subclasses. This ensures
      that Loaders are able to speedup the searching process, since
      many times it's necessary to access huge sequential files for
      looking up information.
    """

    def __init__(self):
        self._results = {}
        self.nameversion = []
        self.provides = []
        self.requires = []
        self.recommends = []
        self.upgrades = []
        self.conflicts = []
        self.path = []
        self.url = []
        self.group = []
        self.summary = []
        self.description = []
        self.ignorecase = True

    def reset(self):
        self._results.clear()
        del self.nameversion[:]
        del self.provides[:]
        del self.requires[:]
        del self.recommends[:]
        del self.upgrades[:]
        del self.conflicts[:]
        del self.path[:]
        del self.url[:]
        del self.group[:]
        del self.summary[:]
        del self.description[:]

    def addResult(self, obj, ratio=1.0):
        results = self._results
        if obj not in results or ratio > results[obj]:
            results[obj] = ratio

    def getResult(self, obj, default=None):
        return self._results.get(obj, default)

    def getResults(self):
        results = self._results
        lst = [(results[obj], obj) for obj in results]
        lst.sort()
        lst.reverse()
        return lst

    def getBestResults(self):
        results = self._results
        lst = [(results[obj], obj) for obj in results]
        if lst:
            lst.sort()
            lst.reverse()
            best = lst[0][0]
            lst = [x for x in lst if x[0] == best]
        return lst

    def searchCache(self, cache):