#!/usr/bin/env python #**************************************************************************** # searchstr.py, provides search string classes # # FlyWay, a VFR/IFR Route Planner for Pilots # Copyright (C) 2002, Douglas W. Bell # # This is free software; you can redistribute it and/or modify it under the # terms of the GNU General Public License, Version 2. This program is # distributed in the hope that it will be useful, but WITTHOUT ANY WARRANTY. #***************************************************************************** class SearchStr: """Class to contain and search for key words with wildcards""" def __init__(self, keys): self.keyList = [SearchWord(key) for key in keys.split()] def isMatch(self, text): """Check for match of keywords in text, return 1/0""" for word in self.keyList: if not word.isMatch(text): return 0 if self.keyList: return 1 return 0 class SearchWord: """Class to store a keyword with wildcard info""" def __init__(self, word): self.wild = (word[0] == '*', word[-1] == '*') self.word = word.replace('*', '') def isMatch(self, text): """Check for match of the keyword in the text, return 1/0""" pos = text.find(self.word, 0) while pos >= 0: prevChar = nextChar = ' ' if pos > 0: prevChar = text[pos - 1] if pos + len(self.word) < len(text): nextChar = text[pos + len(self.word)] if (not prevChar.isalnum() or self.wild[0]) and \ (not nextChar.isalnum() or self.wild[1]): return 1 pos = text.find(self.word, pos + 1) return 0