Python静态编译器说明研究


使用了wxPython、pychecker编写的Python静态编译器,用于在编译器查找py脚本的错误,开放源码,与各位pythoner共享之,希望本文能给大家带来帮助,下面就一起进去Python的世界吧。

代码如下:

  1.     def OnBuildOne(self, event):  
  2.         if self.paths.count != 0:  
  3.             self.Report.AppendText(self.CompileInfoHead("File"))  
  4.             path = self.paths[self.List.GetSelection()]  
  5.             print "Building " + path + " ..."  
  6.             try:  
  7.                 py_compile.compile(path, None, None)  
  8.             except py_compile.PyCompileError, ex:  
  9.                 print ex  
  10.             self.Report.AppendText("=-- Build Finished.\n\n")  
  11.  
  12.  
  13.     def OnBuildAll(self, event):  
  14.         if self.paths.count != 0:  
  15.             self.Report.AppendText(self.CompileInfoHead("File(s)"))  
  16.             for path in self.paths:  
  17.                 print "Building " + path + " ..."  
  18.                 try:  
  19.                     py_compile.compile(path, None, None)  
  20.                 except py_compile.PyCompileError, ex:  
  21.                     print ex  
  22.             self.Report.AppendText("=-- Build Finished.\n\n")  
  23.  
  24.  
  25.     def OnBuildDirectory(self, event):  
  26.         dlg = wxDirDialog(self, "Select a directory for build", self.cfg[2])  
  27.         if dlg.ShowModal() == wxID_OK:  
  28.             path = dlg.GetPath()  
  29.             self.Report.AppendText(self.CompileInfoHead("Directory:", path))  
  30.             compile_dir(path, 10, None, 1, None)  
  31.             self.Report.AppendText("=-- Build Finished.\n\n")  
  32.             self.cfg[2] = dlg.GetPath()  
  33.                   
  34.         dlg.Destroy()  
  35.       
  36.  
  37.     def OnAbout(self, event):   
  38.         dlg = wxMessageDialog(self, "Present by Dracula 2005\n"   
  39.                                     "Build 2005.05.05\n", "About",   
  40.                                     wxOK | wxICON_INFORMATION)  
  41.         dlg.ShowModal()  
  42.         dlg.Destroy()  
  43.  
  44.  
  45.     def OnResize(self, event):  
  46.         sizeClient = self.GetClientSize()  
  47.         self.List.SetSize(sizeClient)  
  48.         sizeList = self.List.GetClientSize()  
  49.         self.Report.SetSize(wxSize(sizeClient.width, sizeClient.height-sizeList.height))  
  50.  
  51.  
  52.     def OnClose(self, event):  
  53.         try:  
  54.             f = open("config.cfg", "w")  
  55.             f.write(self.cfg[0])  
  56.             if self.cfg[0][-1] != '\n':  
  57.                 f.write("\n")  
  58.             f.write(self.cfg[1])  
  59.             if self.cfg[1][-1] != '\n':  
  60.                 f.write("\n")  
  61.             f.write(self.cfg[2])  
  62.             f.close()  
  63.         except IOError:  
  64.             pass  
  65.  
  66.         sys.path = self.save_sys_path[:]  
  67.           
  68.         self.timer.Stop()  
  69.         del self.timer   
  70.         del self.icon   
  71.         self.Destroy()  
  72.  
  73.  
  74.     def OnQuit(self, event):  
  75.         self.Close(true)  
  76.  
  77.  
  78.     def PyCheck(self, argv):  
  79.         argv2 = ['pychecker']  
  80.         argv2.append(argv)  
  81.         pychecker.checker2.main(argv2)  
  82.         #reload(pychecker.checker2)  
  83.  
  84.  
  85.     def AddPath(self, path):  
  86.         curdir = path 
  87.         system_dir = curdir + '\\data\\script'  
  88.         system_core_dir = curdir + '\\data\\script\\core'  
  89.         subsystem_dir = curdir + '\\data\\subsystem'  
  90.         subsystem_trashbin_dir = curdir + '\\data\\subsystem\\trashbin'  
  91.  
  92.         sys.path = self.save_sys_path[:]  
  93.         sys.path.append(curdir)  
  94.         sys.path.append(system_dir)  
  95.         sys.path.append(system_core_dir)  
  96.         sys.path.append(subsystem_dir)  
  97.         sys.path.append(subsystem_trashbin_dir)  
  98.  
  99.  
  100.     def CompileInfoHead(self, str1, str2=""):  
  101.         return "=-- %s %s Compile %s %s ...\n" % (self.Date(), self.Time(), str1, str2)  
  102.       
  103.  
  104.     def Error(self, error):  
  105.         self.Report.AppendText(error)  
  106.  
  107.  
  108.     def Output(self, info):  
  109.         self.Report.AppendText(info)  
  110.  
  111.  
  112.     def Date(self):  
  113.         t = time.localtime(time.time())   
  114.         strDate = time.strftime("%Y.%m.%d", t)  
  115.         return strDate  
  116.  
  117.  
  118.     def Time(self):  
  119.         t = time.localtime(time.time())   
  120.         strTime = time.strftime("%I:%M:%S", t)  
  121.         return strTime  
  122.  
  123.  
  124.     def Notify(self):  
  125.         self.statusbar.SetStatusText(self.Date() + "   " + self.Time(), 1)  
  126.  
  127.  
  128. class MyApp(wxApp):  
  129. def OnInit(self):  
  130. self.frame = MyFrame(NULL, -1, "cd2Py Compiler")  
  131. self.frame.Show(true)  
  132. return true   
  133. cd2Py = MyApp(0)  
  134. import sys  
  135. class errCatcher:  
  136. def __init__(self):  
  137. pass  
  138. def write(self, stuff):  
  139. cd2Py.frame.Error(stuff)  
  140. class outCatcher:  
  141. def __init__(self):  
  142. passdef write(self, stuff):  
  143. cd2Py.frame.Output(stuff)  
  144. sys.stderr = errCatcher()  
  145. sys.stdout = outCatcher()  
  146. cd2Py.MainLoop() 
  1. 如何使Python嵌入C++应用程序?
  2. 深入探讨Ruby与Python语法比较
  3. Python学习资料介绍分享
  4. Python学习经验谈:版本、IDE选择及编码解决方案
  5. 浅析Python的GIL和线程安全

相关内容

    暂无相关文章

评论关闭