def main(filename): pid = startGoldwave() print "goldwave started with pid %d to process file %s" % (pid,filename) selectionFunc = makePIDFilter(pid) hgw = findTopWindow(wantedText="GoldWave", wantedClass="TMainForm", selectionFunction = selectionFunc, maxWait = 5, retryInterval = 0.3) activateMenuItem(hgw,("File",1)) hos = findTopWindow(wantedText="Open Sound", wantedClass="#32770",selectionFunction=selectionFunc,maxWait=5,retryInterval=0.3) editArea = findControl(hos,wantedClass="Edit") setEditText(editArea,filename) selectfunc = lambda x: winGuiAuto._normaliseText(win32gui.GetWindowText(x)) == "open" openButton = findControl(hos,wantedText="&Open",wantedClass="Button", selectionFunction = selectfunc) clickButton(openButton) time.sleep(5) while True: try: c = findControl(hgw, wantedText = os.path.basename(filename), wantedClass = "TWaveView", maxWait=1, retryInterval=1) break except winGuiAuto.WinGuiAutoError, err: if str(err).startswith("No control found for"): print "waiting for file to load.. sleeping for 5 seconds" time.sleep(5) #make this more robust by making sure the Audio decompression progress bar thingie is still on ? pass else: raise
def waitToFinish(wantedText=None,wantedClass="TProgressForm",selectionFunction=None,maxWait=5,retryInterval=0.1): htpf = None try: htpf = findTopWindow(wantedText=wantedText,wantedClass=wantedClass,selectionFunction=selectionFunction,maxWait=maxWait,retryInterval=retryInterval) except: #assume file was so short that processing is already over. Practically this wont happen for our needs pass while htpf: try: htpf = findTopWindow(wantedText=wantedText,wantedClass=wantedClass,selectionFunction=selectionFunction,maxWait=maxWait,retryInterval=retryInterval) try: htst = findControl(htpf,wantedClass="TStaticText",maxWait=maxWait,retryInterval=retryInterval) text = win32gui.GetWindowText(htst) m = re.search(r"(?P<hours>\d+):(?P<minutes>\d+):(?P<seconds>\d+)",text) if m: sleeptime = "%s*3600 + %s*60 + %s" % (m.group("hours"),m.group("minutes"),m.group("seconds")) sleeptime = eval(sleeptime)/2 + 1 # random formula as estimates are not always accurate if sleeptime > 90: # random threshold sleeptime = 90 print "waiting for processing to finish... sleeping %d seconds" % (sleeptime,) time.sleep(sleeptime) else: time.sleep(2) except: time.sleep(2) except: # processing done. htpf = None break
def saveAsWave(hgw,filename): activateMenuItem(hgw,("File",7)) # save selection as menu item pidFunc = makePIDFilter(getPIDfromHandle(hgw)) hssa = findTopWindow(wantedText="Save Sound As",wantedClass="#32770",selectionFunction=pidFunc,maxWait = 5, retryInterval=0.3) editArea = findControl(hssa,wantedClass="Edit") setEditText(editArea,os.path.splitext(filename)[0]) def selectFunc(x): if win32gui.GetClassName(x) != "ComboBox": return False if getComboboxItems(x) and getComboboxItems(x)[0] == "Wave (*.wav)": return True return False hsat = findControl(hssa,wantedClass="ComboBox",selectionFunction=selectFunc) selectComboboxItem(hsat,"Wave (*.wav)") htcsf = findControl(hssa,wantedClass="TCustomSaveForm") hcb = findControl(htcsf,wantedClass="TComboBox") options = getComboboxItems(hcb) selectComboboxItem(hcb,"PCM signed 16 bit, mono") waveFile = os.path.splitext(filename)[0] + ".wav" try: if os.path.exists(waveFile): os.remove(waveFile) print "old wav file removed" except: print "error in removing file. do error logging/reporting here" saveButton = findControl(hssa,wantedClass="Button",wantedText="Save") clickButton(saveButton) waitToFinish(wantedText="Processing Saving",selectionFunction=pidFunc)
def upload_photo(self, imgpath): u''' @处理图片上传WINDOWS窗口问题 @使用方法:传入图片路,格式为:r'X:\XX\XX.XXX' ''' driver = self.driver if not os.path.exists(imgpath): return False if driver.name == 'chrome': title = u'打开' elif driver.name == 'Firefox': title = u'文件上传' elif driver.name == 'internet explorer': title = u'打开' else: title = u'打开' try: #form=findTopWindow(wantedClass="#32770") form=findTopWindow(wantedText=title.encode("gb2312")) button=findControl(form,wantedText=u'打开'.encode("gb2312")) editbox=findControl(form,wantedClass='Edit') setEditText(editbox,[imgpath]) clickButton(button) except: print traceback.format_exc() return False else: return True
def doNoiseReduction(hgw): activateMenuItem(hgw,("Effect",4,3)) selectFunc = makePIDFilter(getPIDfromHandle(hgw)) hnr = findTopWindow(wantedText="Noise Reduction", wantedClass="TEffectWrapper",selectionFunction = selectFunc, maxWait=5, retryInterval=0.3) hpr = findControl(hnr,wantedText="Presets",wantedClass="TGroupBox") hcb = findControl(hpr,wantedClass="TComboBox") cl = getComboboxItems(hcb) selectComboboxItem(hcb,"Hiss removal") okButton = findControl(hnr,wantedText="OK",wantedClass="TButton") clickButton(okButton) waitToFinish(wantedText="Processing Noise Reduction",selectionFunction=selectFunc)
def quitGoldwave(hgw): activateMenuItem(hgw,("File",10)) hc = findTopWindow(wantedText="Confirm",wantedClass="TMessageForm",selectionFunction=makePIDFilter(getPIDfromHandle(hgw)), maxWait=5, retryInterval=0.3) noButton = findControl(hc,wantedText="No",wantedClass="TButton",maxWait=5, retryInterval=0.3) clickButton(noButton)
def findGoldwaveHelpWindow(pid): selectionFunc = makePIDFilter(pid) return findTopWindow(wantedText="GoldWave Help", wantedClass="MS_WINHELP", selectionFunction = selectionFunc, maxWait = 5, retryInterval = 0.3)