3 #------------------------------------------------------------------------------
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.
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
33 # Indentation: I use tabs only, 4 spaces per tab.
34 #------------------------------------------------------------------------------
36 #------------------------------------------------------------------------------
38 #------------------------------------------------------------------------------
39 #-------------------------------
40 def __init__(self
, searchTable
):
41 #-------------------------------
43 Initializes instance variables
45 self
.firstSearchTable
= searchTable
# Original Table
46 self
.caseInsensitive
= True # Case sens search?
48 #--------------------------------
49 def __fix_capitals__(self
, list):
50 #--------------------------------
52 Fixes the capitalization of the column headings.
55 section
[0][0] = section
[0][0].title()
58 #----------------------------------
59 def __get_regex_ready_list__(self
):
60 #----------------------------------
62 Takes a list in treeview format ("type"(newrow)"table-name") and
63 converts it to regex format ("type/table-name")
66 for section
in self
.firstSearchTable
:
69 for element
in section
[1:]:
70 tmpList
.append([title
+"/"+element
[0]])
73 readyList
.append(tmpList
)
77 #------------------------------------------------
78 def __get_treeview_ready_list__(self
, regexList
):
79 #------------------------------------------------
81 Takes a list in regex format ("type/table-name") and converts it back
82 to treeview format ("type"(newrow)"table-name")
84 regexList
= self
.__remove
_empties
__(regexList
)
87 for section
in regexList
:
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
)
97 #-------------------------------------
98 def __remove_empties__(self
, newList
):
99 #-------------------------------------
101 Removes any sections that don't have any contents
104 for section
in newList
:
105 if len(section
) == 0:
106 newList
.remove(section
)
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
__()
119 if self
.caseInsensitive
:
120 regex
= re
.compile(pattern
, re
.I
)
122 regex
= re
.compile(pattern
)
124 regex
= re
.compile("")
127 for section
in searchTable
:
129 for element
in section
:
130 if re
.search(regex
, element
[0]) != None:
131 tmpList
.append(element
)
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.
146 list = self
.__refine
_list
__(pattern
)
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