home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / search.py < prev    next >
Encoding:
Python Source  |  2007-11-12  |  2.8 KB  |  86 lines

  1. # Miro - an RSS based video player application
  2. # Copyright (C) 2005-2007 Participatory Culture Foundation
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  17.  
  18. import item
  19. import re
  20.  
  21. quotekiller = re.compile(r'(?<!\\)"')
  22. slashkiller = re.compile(r'\\.')
  23.  
  24. searchObjects = {}
  25.  
  26. def match (searchString, comparisons):
  27.     if not searchObjects.has_key(searchString):
  28.         searchObjects[searchString] = BooleanSearch(searchString)
  29.     return searchObjects[searchString].match(comparisons)
  30.  
  31. class BooleanSearch:
  32.     def __init__ (self, string):
  33.         self.string = string
  34.         self.parse_string()
  35.  
  36.     def parse_string(self):
  37.         inquote = False
  38.         i = 0
  39.         while i < len (self.string) and self.string[i] == ' ':
  40.             i += 1
  41.         laststart = i
  42.         self.rules = []
  43.         while (i < len(self.string)):
  44.             i = laststart
  45.             while (i < len(self.string)):
  46.                 if self.string[i] == '"':
  47.                     inquote = not inquote
  48.                 if not inquote and self.string[i] == ' ':
  49.                     break
  50.                 if self.string[i] == '\\':
  51.                     i += 1
  52.                 i += 1
  53.             if inquote:
  54.                 self.rules.append(self.process(self.string[laststart:]))
  55.             else:
  56.                 self.rules.append(self.process(self.string[laststart:i]))
  57.             while i < len (self.string) and self.string[i] == ' ':
  58.                 i += 1
  59.             laststart = i
  60.  
  61.     def process (self, substring):
  62.         positive = True
  63.         if substring[0] == '-':
  64.             substring = substring[1:]
  65.             positive = False
  66.         substring = quotekiller.sub("", substring)
  67.         substring = slashkiller.sub(lambda x: x.group(0)[1], substring)
  68.         #print substring
  69.         return [positive, substring]
  70.  
  71.     def match (self, comparisons):
  72.         for rule in self.rules:
  73.             matched = False
  74.             for comparison in comparisons:
  75.                 if rule[1] in comparison:
  76.                     matched = True
  77.                     break
  78.             if rule[0] != matched:
  79.                 return False
  80.         return True
  81.  
  82. #        r'(([^" ]\"|"([^"]|\")*")*)'
  83.  
  84.     def as_string(self):
  85.         return self.string
  86.