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.     def OnBuildAll(self, event):
  12.         if self.paths.count != 0:
  13.             self.Report.AppendText(self.CompileInfoHead("File(s)"))
  14.             for path in self.paths:
  15.                 print "Building " + path + " ..."
  16.                 try:
  17.                     py_compile.compile(path, None, None)
  18.                 except py_compile.PyCompileError, ex:
  19.                     print ex
  20.             self.Report.AppendText("=-- Build Finished.\n\n")
  21.     def OnBuildDirectory(self, event):
  22.         dlg = wxDirDialog(self, "Select a directory for build", self.cfg[2])
  23.         if dlg.ShowModal() == wxID_OK:
  24.             path = dlg.GetPath()
  25.             self.Report.AppendText(self.CompileInfoHead("Directory:", path))
  26.             compile_dir(path, 10, None, 1, None)
  27.             self.Report.AppendText("=-- Build Finished.\n\n")
  28.             self.cfg[2] = dlg.GetPath()
  29.                 
  30.         dlg.Destroy()
  31.     
  32.     def OnAbout(self, event): 
  33.         dlg = wxMessageDialog(self, "Present by Dracula 2005\n" 
  34.                                     "Build 2005.05.05\n", "About", 
  35.                                     wxOK | wxICON_INFORMATION)
  36.         dlg.ShowModal()
  37.         dlg.Destroy()
  38.     def OnResize(self, event):
  39.         sizeClient = self.GetClientSize()
  40.         self.List.SetSize(sizeClient)
  41.         sizeList = self.List.GetClientSize()
  42.         self.Report.SetSize(wxSize(sizeClient.width, sizeClient.height-sizeList.height))
  43.     def OnClose(self, event):
  44.         try:
  45.             f = open("config.cfg", "w")
  46.             f.write(self.cfg[0])
  47.             if self.cfg[0][-1] != '\n':
  48.                 f.write("\n")
  49.             f.write(self.cfg[1])
  50.             if self.cfg[1][-1] != '\n':
  51.                 f.write("\n")
  52.             f.write(self.cfg[2])
  53.             f.close()
  54.         except IOError:
  55.             pass
  56.         sys.path = self.save_sys_path[:]
  57.         
  58.         self.timer.Stop()
  59.         del self.timer 
  60.         del self.icon 
  61.         self.Destroy()
  62.     def OnQuit(self, event):
  63.         self.Close(true)
  64.     def PyCheck(self, argv):
  65.         argv2 = ['pychecker']
  66.         argv2.append(argv)
  67.         pychecker.checker2.main(argv2)
  68.         #reload(pychecker.checker2)
  69.     def AddPath(self, path):
  70.         curdir = path
  71.         system_dir = curdir + '\\data\\script'
  72.         system_core_dir = curdir + '\\data\\script\\core'
  73.         subsystem_dir = curdir + '\\data\\subsystem'
  74.         subsystem_trashbin_dir = curdir + '\\data\\subsystem\\trashbin'
  75.         sys.path = self.save_sys_path[:]
  76.         sys.path.append(curdir)
  77.         sys.path.append(system_dir)
  78.         sys.path.append(system_core_dir)
  79.         sys.path.append(subsystem_dir)
  80.         sys.path.append(subsystem_trashbin_dir)
  81.     def CompileInfoHead(self, str1, str2=""):
  82.         return "=-- %s %s Compile %s %s ...\n" % (self.Date(), self.Time(), str1, str2)
  83.     
  84.     def Error(self, error):
  85.         self.Report.AppendText(error)
  86.     def Output(self, info):
  87.         self.Report.AppendText(info)
  88.     def Date(self):
  89.         t = time.localtime(time.time()) 
  90.         strDate = time.strftime("%Y.%m.%d", t)
  91.         return strDate
  92.     def Time(self):
  93.         t = time.localtime(time.time()) 
  94.         strTime = time.strftime("%I:%M:%S", t)
  95.         return strTime
  96.     def Notify(self):
  97.         self.statusbar.SetStatusText(self.Date() + "   " + self.Time(), 1)
  98. class MyApp(wxApp):
  99. def OnInit(self):
  100. self.frame = MyFrame(NULL, -1, "cd2Py Compiler")
  101. self.frame.Show(true)
  102. return true 
  103. cd2Py = MyApp(0)
  104. import sys
  105. class errCatcher:
  106. def __init__(self):
  107. pass
  108. def write(self, stuff):
  109. cd2Py.frame.Error(stuff)
  110. class outCatcher:
  111. def __init__(self):
  112. passdef write(self, stuff):
  113. cd2Py.frame.Output(stuff)
  114. sys.stderr = errCatcher()
  115. sys.stdout = outCatcher()
  116. cd2Py.MainLoop()

当前文章:Python静态编译器说明研究
当前链接:http://www.mswzjz.cn/qtweb/news49/328299.html

攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 贝锐智能