#Boa:Dialog:MakePyDialogfromwxPython.wximport*fromwxPython.lib.anchorsimportLayoutAnchorsfromwin32com.clientimportselecttlb,makepyimporttraceback,typesdefcreate(parent):returnMakePyDialog(parent)[wxID_MAKEPYDIALOG,wxID_MAKEPYDIALOGBFORDEMAND,wxID_MAKEPYDIALOGCANCEL,wxID_MAKEPYDIALOGDIRECTSPECIFICATION,wxID_MAKEPYDIALOGOK,wxID_MAKEPYDIALOGTYPELIBRARYLIST]=map(lambda_init_ctrls:wxNewId(),range(6))classMakePyDialog(wxDialog):def_init_coll_typeLibraryList_Columns(self,parent):parent.InsertColumn(col=0,format=wxLIST_FORMAT_CENTRE,heading='Library Name',width=372)def_init_utils(self):passdef_init_ctrls(self,prnt):wxDialog.__init__(self,id=wxID_MAKEPYDIALOG,name='MakePyDialog',parent=prnt,pos=wxPoint(498,119),size=wxSize(420,433),style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER,title='COM Library Generator')self._init_utils()self.SetAutoLayout(true)EVT_INIT_DIALOG(self,self.OnMakepydialogInitDialog)self.typeLibraryList=wxListCtrl(id=wxID_MAKEPYDIALOGTYPELIBRARYLIST,name='typeLibraryList',parent=self,pos=wxPoint(16,40),size=wxSize(376,280),style=wxLC_NO_HEADER|wxLC_REPORT,validator=wxDefaultValidator)self.typeLibraryList.SetToolTipString('List of the registered COM type libraries on your system')self.typeLibraryList.SetConstraints(LayoutAnchors(self.typeLibraryList,true,true,true,true))self._init_coll_typeLibraryList_Columns(self.typeLibraryList)EVT_LEFT_DCLICK(self.typeLibraryList,self.OnTypelibrarylistLeftDclick)self.OK=wxButton(id=wxID_MAKEPYDIALOGOK,label='Generate',name='OK',parent=self,pos=wxPoint(200,360),size=wxSize(88,27),style=0)self.OK.SetToolTipString('Click to generate a wrapper for the selected library')self.OK.SetConstraints(LayoutAnchors(self.OK,false,false,true,true))EVT_BUTTON(self.OK,wxID_MAKEPYDIALOGOK,self.OnOkButton)self.Cancel=wxButton(id=wxID_CANCEL,label='Cancel',name='Cancel',parent=self,pos=wxPoint(304,360),size=wxSize(88,27),style=0)self.Cancel.SetToolTipString('Cancel wrapper generation')self.Cancel.SetConstraints(LayoutAnchors(self.Cancel,false,false,true,true))self.bForDemand=wxCheckBox(id=wxID_MAKEPYDIALOGBFORDEMAND,label='Generate Classes on Demand',name='bForDemand',parent=self,pos=wxPoint(144,328),size=wxSize(248,20),style=0)self.bForDemand.SetToolTipString('Minimises amount of code generated by only wrapping used classes (recommended). Clear to generate a single-file wrapper.')self.bForDemand.SetValue(true)self.bForDemand.SetConstraints(LayoutAnchors(self.bForDemand,false,false,true,true))EVT_CHECKBOX(self.bForDemand,wxID_MAKEPYDIALOGBFORDEMAND,self.OnBfordemandCheckbox)self.directSpecification=wxTextCtrl(id=wxID_MAKEPYDIALOGDIRECTSPECIFICATION,name='directSpecification',parent=self,pos=wxPoint(16,8),size=wxSize(376,28),style=0,value='')self.directSpecification.SetToolTipString('Type text here to search for matching type libraries')self.directSpecification.SetConstraints(LayoutAnchors(self.directSpecification,true,true,true,false))EVT_TEXT(self.directSpecification,wxID_MAKEPYDIALOGDIRECTSPECIFICATION,self.OnDirectspecificationText)def__init__(self,parent):self._init_ctrls(parent)self.generatedFilename=''defOnTypelibrarylistLeftDclick(self,event):returnself.OnOkButton(event)defOnOkButton(self,event):index=self.typeLibraryList.GetNextItem(-1,state=wxLIST_STATE_SELECTED)# there could be multiple selected, should decide what to do then...ifindex!=-1:self.generatedFilename=self.Generate(self.libraryList[index])print'generated to filename',self.generatedFilenamereturnself.EndModal(wxID_OK)defGenerate(self,typeLibrary):""" Generate wrapper for a given type library """progress=Progress(self)try:makepy.GenerateFromTypeLibSpec(typeLibrary,None,bForDemand=1,bBuildHidden=1,progressInstance=progress,)filename=progress.filenameprogress.Destroy()exceptException,error:traceback.print_exc()errorMessage=wxMessageDialog(self,str(error),"Generation Failure!",style=wxOK)progress.Destroy()errorMessage.ShowModal()errorMessage.Destroy()returnNonereturnfilenamedefOnDirectspecificationText(self,event):'''Set focus to any matching item while typing'''text=self.directSpecification.GetValue()iftext:items=self.SearchList(text)ifitems:forindexinrange(self.typeLibraryList.GetItemCount()):self.typeLibraryList.SetItemState(index,0,wxLIST_STATE_SELECTED)for(index,item)initems:self.typeLibraryList.SetItemState(index,wxLIST_STATE_SELECTED,wxLIST_STATE_SELECTED)self.typeLibraryList.EnsureVisible(items[0][0])event.Skip()defSearchList(self,text):""" Attempt to find the specified text in the list of DLL names, classID, and descriptions."""importrefinder=re.compile(text,re.IGNORECASE)items=[]wxBeginBusyCursor()try:forattributein('desc','clsid','dll',):forindexinrange(len(self.libraryList)):librarySpecification=self.libraryList[index]try:value=getattr(librarySpecification,attribute)ifvalueandtype(value)==types.StringType:iffinder.search(value):items.append((index,librarySpecification))exceptException,error:pass## print error, librarySpecification, attribute, repr(getattr( librarySpecification, attribute))finally:wxEndBusyCursor()returnitemsdefOnMakepydialogInitDialog(self,event):'''Initialisation of the dialog starts up a process of loading the type library definitions.'''wxBeginBusyCursor()try:self.libraryList=libraryList=selecttlb.EnumTlbs()libraryList.sort()forindexinrange(len(libraryList)):librarySpecification=libraryList[index]self.typeLibraryList.InsertStringItem(index,librarySpecification.desc)finally:wxEndBusyCursor()defOnBfordemandCheckbox(self,event):passclassProgress(wxProgressDialog):verboseLevel=1filename=""def__init__(self,parent):wxProgressDialog.__init__(self,"MakePy Progress","Generating type library wrappers",parent=parent,style=wxPD_AUTO_HIDE|wxPD_APP_MODAL,)defClose(self,event=None):passdefStarting(self,description=None):passdefFinished(self):passdefSetDescription(self,desc,maxticks=None):self.Update(-1,newmsg=desc)defTick(self,desc=None):passdefVerboseProgress(self,desc,verboseLevel=2):ifself.verboseLevel>=verboseLevel:self.SetDescription(desc)defLogBeginGenerate(self,filename):self.VerboseProgress("Generating to %s"%filename,1)self.filename=filenamedefLogWarning(self,desc):self.VerboseProgress("WARNING: "+desc,1)if__name__=="__main__":classDemoFrame(wxFrame):def__init__(self,parent):wxFrame.__init__(self,parent,2400,"File entry with browse",size=(500,150))dialog=MakePyDialog(self)dialog.ShowModal()dialog.Destroy()classDemoApp(wxApp):defOnInit(self):wxImage_AddHandler(wxJPEGHandler())wxImage_AddHandler(wxPNGHandler())wxImage_AddHandler(wxGIFHandler())frame=DemoFrame(None)frame.Show(true)self.SetTopWindow(frame)returntruedeftest():app=DemoApp(0)app.MainLoop()print'Creating dialog'test()