Hi,
I have 2 modules, grepforapp.py and myApp.py. Basically i'm trying to make a 'search' utility. myApp makes the UI with one textbox for taking input, start button and a list box which displays the file names where the pattern was found.
grepforApp walks through the dir tree and searches the pattern in each file. In my previous code i used to read and store everything and then would display the results in one shot. Then i changed the code to call the grepforapp.search in a loop and try to display the results as they come for each dir. but this is not happening. If you see the code i'm doing a ListBox.Append in a loop but still the final result is shown in one shot and not in steps. Can anyone see what's wrong with the code?
myApp module
import wx
import grepforapp
import os
import pdb
import time
#pdb.set_trace()
class myApp(wx.Frame):
def __init__(self,parent,id,title):
wx.Frame.__init__(self,parent,id,title)
self.tc1 = None
self.Show()
def addButton(self,ID,butStr,coord):
wx.Button(self,ID,butStr,coord)
self.Bind(wx.EVT_BUTTON,self.addStaticBox,id=ID)
def addStaticBox(self,event):
superDict = {}
tc2 = None
tc2 = wx.ListBox(self, 26, (5,80), (350, 150),style=wx.LB_SINGLE)
tc2.SetForegroundColour('#RRGGBB')
pattern = self.tc1.GetValue()
superDict = {}
if pattern != '':
for roots,dirs,files in os.walk(os.getcwd()):
myFileList = []
fileList = []
superDict.clear()
myFileList = self.getFileList(files)
superDict = grepforapp.searchPattern(pattern,1,myFileList,roots)
# for key in superDict.keys():
# print key
fileList = superDict.keys()
tc2.AppendItems(fileList)
time.sleep(10)
def getFileList(self,files):
# print roots,dirs,files
i = 0
i = len(files)
searchAll = 1
# print i
myFileList = []
for j in range(i):
filename,fileext = os.path.splitext(files[j])
# print fileext
if(searchAll):
myFileList.append(files[j])
else:
if fileext == '.cpp' or fileext == '.h' or fileext == '.hpp' or fileext == '.py':
myFileList.append(files[j])
return myFileList
# print myFileList
def addTextCtrl(self):
self.tc1 = wx.TextCtrl(self,pos=(5,5),size=(200,30))
def main():
App = wx.App()
app = myApp(None,-1,'Search')
#app.addMenu()
app.addTextCtrl()
app.addButton(1,'Stop',(100,40))
app.addButton(2,'Search',(5,40))
#app.addStaticBox()
App.MainLoop()
if __name__ == "__main__":
main()
pgrepforapp code
import os,re,sys
import glob,getopt
import pdb
#pdb.set_trace()
def searchPattern(pattern,searchAll,myFileList,roots):
if pattern == '':
usage()
valueList = []
superDict = {}
superDict.clear()
print '---------------Results---------------\n'
for fil in myFileList:
# print fil
try:
valueList = []
matchFound = 0
fullpath = roots + '/' + fil
# print fullpath
fileObj = open(fullpath)
lines = fileObj.readlines()
count = 0
for count,line in enumerate(lines):
if (re.search(pattern.lower(),line.lower())):
matchFound = 1
# print fullpath
matchline = 'line#'+ str(count+1),line
# #print matchline
valueList.append(matchline)
if matchFound:#
superDict[fullpath] = valueList
# print superDict.keys()
except IOError:
pass
except KeyboardInterrupt:
print 'so u got bored.. me too.. ok bye..'
sys.exit(2)
# print superDict.keys()
return superDict
def usage():
print 'invalid option'
print 'usage: pgrep [-a] -p pattern'
print '-a: search in all files'
sys.exit()