WvStream: in the destructor assert() statement, say the type of stream.
[versaplex.git] / veranda / Searcher.py
blob15cd942cc884c058b4b3c5c77a44033ab4a2d8de
1 #!/usr/bin/python
3 #------------------------------------------------------------------------------
4 # Veranda
5 # *Searcher
6 # ~--------------~
8 # Original Author: Andrei "Garoth" Thorp <garoth@gmail.com>
10 # Description: This module handles the search function of Veranda (for the
11 # sidebar). The concept of the search bar goes as follows:
12 # * As you type, it narrows down the list
13 # * It will expand the categories, but only if there are few
14 # matches there (this is done in Main, actually.)
15 # * The format of each entry is category/entry (so while the
16 # text says "table_name" it will actually be Table/table_name)
17 # * All search queries will be regular expressions.
19 # Examples: Basic Usage: stuff
20 # Returns: Everything that has the word "stuff"
21 # in it, such as sp_get_stuff.
22 # Regex Usage: ..uff
23 # Returns: Everything that matches the regex pattern. For example,
24 # sp_useless_fluff would match, as well as sp_get_stuff.
25 # Type Usage: table/.*stuff
26 # Returns: All tables that have the word stuff in their names.
27 # Complex Usage: table.*/.*.?.?uff[0-9]?
28 # Returns: All tables and tablefunctions that match the pattern
29 # "maybe 2 characters, then uff, then maybe a digit".
30 # Example: fluff9 or stuff6
32 # Notes:
33 # Indentation: I use tabs only, 4 spaces per tab.
34 #------------------------------------------------------------------------------
35 import re
36 #------------------------------------------------------------------------------
37 class Searcher:
38 #------------------------------------------------------------------------------
39 #-------------------------------
40 def __init__(self, searchTable):
41 #-------------------------------
42 """
43 Initializes instance variables
44 """
45 self.firstSearchTable = searchTable # Original Table
46 self.caseInsensitive = True # Case sens search?
48 #--------------------------------
49 def __fix_capitals__(self, list):
50 #--------------------------------
51 """
52 Fixes the capitalization of the column headings.
53 """
54 for section in list:
55 section[0][0] = section[0][0].title()
56 return list
58 #----------------------------------
59 def __get_regex_ready_list__(self):
60 #----------------------------------
61 """
62 Takes a list in treeview format ("type"(newrow)"table-name") and
63 converts it to regex format ("type/table-name")
64 """
65 readyList = []
66 for section in self.firstSearchTable:
67 tmpList = []
68 title = section[0][0]
69 for element in section[1:]:
70 tmpList.append([title+"/"+element[0]])
72 if tmpList != []:
73 readyList.append(tmpList)
75 return readyList
77 #------------------------------------------------
78 def __get_treeview_ready_list__(self, regexList):
79 #------------------------------------------------
80 """
81 Takes a list in regex format ("type/table-name") and converts it back
82 to treeview format ("type"(newrow)"table-name")
83 """
84 regexList = self.__remove_empties__(regexList)
86 readyList = []
87 for section in regexList:
88 tmpList = []
89 title = section[0][0].split("/")[0] # heh
90 tmpList.append([title.title()])
91 for element in section:
92 tmpList.append([element[0].split("/")[1]])
93 readyList.append(tmpList)
95 return readyList
97 #-------------------------------------
98 def __remove_empties__(self, newList):
99 #-------------------------------------
101 Removes any sections that don't have any contents
103 # Unused for now
104 for section in newList:
105 if len(section) == 0:
106 newList.remove(section)
108 return newList
110 #----------------------------------
111 def __refine_list__(self, pattern):
112 #----------------------------------
114 Builds a list that contains only the items that match the pattern
116 searchTable = self.__get_regex_ready_list__()
118 try:
119 if self.caseInsensitive:
120 regex = re.compile(pattern, re.I)
121 else:
122 regex = re.compile(pattern)
123 except:
124 regex = re.compile("")
126 newList = []
127 for section in searchTable:
128 tmpList = []
129 for element in section:
130 if re.search(regex, element[0]) != None:
131 tmpList.append(element)
133 if tmpList != []:
134 newList.append(tmpList)
136 return self.__get_treeview_ready_list__(newList)
138 #----------------------
139 def find(self, pattern):
140 #----------------------
142 Takes a search pattern (a regex) and returns a new list that has only
143 the elements that match that pattern.
145 if pattern != "":
146 list = self.__refine_list__(pattern)
147 return list
148 else:
149 return self.__fix_capitals__(self.firstSearchTable)
151 #--------------------------
152 def getOriginalTable(self):
153 #--------------------------
155 Returns the table that the searcher was originally given.
157 return self.firstSearchTable