Put a python static check compiler written half a year ago

Source: Internet
Author: User

The Python static compiler compiled by wxpython and pychecker is used to find errors in the py script in the compiler.
Open source code, share it with pythoner!

From wxpython. wx import *
Import time
Import sys
Import pychecker. checker2
Import py_compile
From compileall import compile_dir

Id_set_work_directory = wxnewid ()
Id_clear = wxnewid ()
Id_exit = wxnewid ()

Id_compile_files = wxnewid ()
Id_compile_one = wxnewid ()
Id_compile_all = wxnewid ()
Id_compile_directory = wxnewid ()

Id_build_files = wxnewid ()
Id_build_one = wxnewid ()
Id_build_all = wxnewid ()
Id_build_directory = wxnewid ()

Id_about = wxnewid ()

Id_list = wxnewid ()
Id_report = wxnewid ()

Class myframe (wxframe ):
Def _ init _ (self, parent, ID, title ):
Wxframe. _ init _ (self, parent, ID, title, wxdefaultposition, wxsize (640,480 ))

Self. Icon = wxicon ('cd2py. ICO ', wxbitmap_type_ico)
Self. seticon (self. Icon)

Self. statusbar = self. createstatusbar (2)
Self. statusbar. setstatuswidths ([-1,130])
Self. statusbar. setstatustext ("Thanks for use cd2py compiler...", 0)
Self. statusbar. setstatustext (self. Time (), 1)
Self. centreonscreen ()

Self. Timer = wxpytimer (self. Sort y)
Self. Timer. Start (1000)
Self. Policy ()

Fmenu = wxmenu ()
Fmenu. append (id_set_work_directory, "& work directory", "Choose tuntown2 directory ")
Fmenu. append (id_clear, "& Clear", "Clear the compile history ")
Fmenu. appendseparator ()
Fmenu. append (id_exit, "E & xit", "Terminate the program ")

cmenu = wxmenu ()
cmenu. append (id_compile_one, "compile & one", "compile one selected file")
cmenu. append (id_compile_all, "compile & all", "compile all selected file (s)")
cmenu. appendseparator ()
cmenu. append (id_compile_files, "& Files", "compile selected file (s)")
cmenu. append (id_compile_directory, "& directory", "compile selected directory")

bmenu = wxmenu ()
bmenu. append (id_build_one, "Build & one", "build one selected file")
bmenu. append (id_build_all, "Build & all", "Build all selected file (s)")
bmenu. appendseparator ()
bmenu. append (id_build_files, "& Files", "Build selected file (s)")
bmenu. append (id_compile_directory, "& directory", "Build selected directory")

hmenu = wxmenu ()
hmenu. append (id_about, "& about", "more information about this program")
menubar = wxmenubar ()
menubar. append (fmenu, "& Files");
menubar. append (cmenu, "& compile");
menubar. append (bmenu, "& build");
menubar. append (hmenu, "& Help");
self. setmenubar (menubar)

evt_menu (self, id_set_work_directory, self. onsetworkdirectory)
evt_menu (self, id_clear, self. onclearhistory)
evt_menu (self, id_exit, self. onquit)

evt_menu (self, id_compile_files, self. oncompilefiles)
evt_menu (self, id_compile_one, self. oncompileone)
evt_menu (self, id_compile_all, self. oncompileall)
evt_menu (self, id_compile_directory, self. oncompiledirectory)

Evt_menu (self, id_build_files, self. onbuildfiles)
Evt_menu (self, id_build_one, self. onbuildone)
Evt_menu (self, id_build_all, self. onbuildall)
Evt_menu (self, id_build_directory, self. onbuilddirectory)

Evt_menu (self, id_about, self. onabout)

Evt_size (self, self. onresize)
Evt_close (self, self. onclose)

Sizeclient = self. getclientsize ()

Self. List = wxcombobox (self, id_list, "", wxpoint (0, 0 ),
Sizeclient, [""], wxcb_dropdown | wxcb_readonly)
Sizelist = self. List. getclientsize ()
Self. Report = wxtextctrl (self, id_report, "", wxpoint (0, sizelist. Height ),
Wxsize (sizeclient. Width, sizeclient. height-sizeList.height ),
Wxte_auto_url |
Wxhscroll |
Wxte_multiline |
Wxte_readonly)

Self. cfg = ["\ n", "\ n", "\ n"]
Try:
F = open ("config. cfg", "R ")
Try:
Self. cfg [0] = f. Readline ()
Self. cfg [1] = f. Readline ()
Self. cfg [2] = f. Readline ()
Except t:
Pass
F. Close ()
Handle t ioerror:
Pass

Self. save_sys_path = SYS. path [:]
Curdir = self. cfg [0]
Curdir = curdir [0: Len (curdir)-1]
Self. addpath (curdir)

Def onsetworkdirectory (self, event ):
DLG = wxdirdialog (self, "Choose tuntown directory", self. cfg [0])
If DLG. showmodal () = wxid_ OK:
Self. addpath (DLG. getpath ())
Self. cfg [0] = DLG. getpath ()

DLG. Destroy ()

Def onclearhistory (self, event ):
Self. Report. Clear ()

Def oncompilefiles (self, event ):
DLG = wxfiledialog (self, "open file (s) for compile", self. cfg [1],
"", "Python script (*. py) | *. py", wxopen | wxmultiple)
If DLG. showmodal () = wxid_ OK:
Self. Paths = DLG. getpaths ()
Self. List. Clear ()
Self. Report. appendtext (self. compileinfohead ("file (s )"))
For Path in self. paths:
Print "Compiling" + path + "..."
Self. pycheck (PATH)
Self. List. append (PATH)
Self. List. Select (0)
Self. Report. appendtext ("= -- compile finished. \ n ")
Self. cfg [1] = DLG. getdirectory ()

DLG. Destroy ()

Def oncompileone (self, event ):
If self. paths. Count! = 0:
Self. Report. appendtext (self. compileinfohead ("file "))
Path = self. Paths [self. List. getselection ()]
Print "Compiling" + path + "..."
Self. pycheck (PATH)
Self. Report. appendtext ("= -- compile finished. \ n ")

Def oncompileall (self, event ):
If self. paths. Count! = 0:
Self. Report. appendtext (self. compileinfohead ("file (s )"))
For Path in self. paths:
Print "Compiling" + path + "..."
Self. pycheck (PATH)
Self. Report. appendtext ("= -- compile finished. \ n ")

Def oncompiledirectory (self, event ):
DLG = wxdirdialog (self, "select a directory for compile", self. cfg [2])
If DLG. showmodal () = wxid_ OK:
Path = DLG. getpath ()
Self. Report. appendtext (self. compileinfohead ("Directory:", PATH ))
SYS. Path. append (PATH)
Self. pycheck (path + '\ *. py ')
SYS. Path. Pop ()
Self. Report. appendtext ("= -- compile finished. \ n ")
Self. cfg [2] = DLG. getpath ()

DLG. Destroy ()

Def onbuildfiles (self, event ):
DLG = wxfiledialog (self, "open file (s) for build", self. cfg [1],
"", "Python script (*. py) | *. py", wxopen | wxmultiple)
If DLG. showmodal () = wxid_ OK:
Self. Paths = DLG. getpaths ()
Self. List. Clear ()
Self. Report. appendtext (self. compileinfohead ("file (s )"))
For Path in self. paths:
Print "building" + path + "..."
Py_compile.compile (path, none, none)
Self. List. append (PATH)
Self. List. Select (0)
Self. Report. appendtext ("= -- Build finished. \ n ")
Self. cfg [1] = DLG. getdirectory ()

DLG. Destroy ()

Def onbuildone (self, event ):
If self. paths. Count! = 0:
Self. Report. appendtext (self. compileinfohead ("file "))
Path = self. Paths [self. List. getselection ()]
Print "building" + path + "..."
Try:
Py_compile.compile (path, none, none)
Failed t py_compile.pycompileerror, EX:
Print ex
Self. Report. appendtext ("= -- Build finished. \ n ")

Def onbuildall (self, event ):
If self. paths. Count! = 0:
Self. Report. appendtext (self. compileinfohead ("file (s )"))
For Path in self. paths:
Print "building" + path + "..."
Try:
Py_compile.compile (path, none, none)
Failed t py_compile.pycompileerror, EX:
Print ex
Self. Report. appendtext ("= -- Build finished. \ n ")

def onbuilddirectory (self, event):
DLG = wxdirdialog (self, "select a directory for build", self. CFG [2])
If DLG. showmodal () = wxid_ OK:
Path = DLG. getpath ()
self. report. appendtext (self. compileinfohead ("Directory:", PATH)
compile_dir (path, 10, none, 1, none)
self. report. appendtext ("= -- Build finished. \ n ")
self. CFG [2] = DLG. getpath ()
DLG. destroy ()

def onabout (self, event):
DLG = wxmessagedialog (self, "present by Dracula 2005 \ n"
"build 2005.05.05 \ n ", "About",
wxok | wxicon_information)
DLG. showmodal ()
DLG. destroy ()

Def onresize (self, event ):
Sizeclient = self. getclientsize ()
Self. List. setsize (sizeclient)
Sizelist = self. List. getclientsize ()
Self. Report. setsize (wxsize (sizeclient. Width, sizeclient. height-sizeList.height ))

Def onclose (self, event ):
Try:
F = open ("config. cfg", "W ")
F. Write (self. cfg [0])
If self. cfg [0] [-1]! = '\ N ':
F. Write ("\ n ")
F. Write (self. cfg [1])
If self. cfg [1] [-1]! = '\ N ':
F. Write ("\ n ")
F. Write (self. cfg [2])
F. Close ()
Handle t ioerror:
Pass

SYS. Path = self. save_sys_path [:]

Self. Timer. Stop ()
Del self. Timer
Del self. icon
Self. Destroy ()

Def onquit (self, event ):
Self. Close (true)

Def pycheck (self, argv ):
Argv2 = ['pychecker ']
Argv2.append (argv)
Pychecker. checker2.main (argv2)
# Reload (pychecker. checker2)

Def addpath (self, PATH ):
Curdir = path
System_dir = curdir + '\ data \ script'
System_core_dir = curdir + '\ data \ Script \ core'
Subsystem_dir = curdir + '\ data \ SubSystem'
Subsystem_trashbin_dir = curdir + '\ data \ SubSystem \ trashbin'

SYS. Path = self. save_sys_path [:]
SYS. Path. append (curdir)
SYS. Path. append (system_dir)
SYS. Path. append (system_core_dir)
SYS. Path. append (subsystem_dir)
SYS. Path. append (subsystem_trashbin_dir)

Def compileinfohead (self, str1, str2 = ""):
Return "= -- % S % s compile % S % s... \ n" % (self. Date (), self. Time (), str1, str2)

Def error (self, error ):
Self. Report. appendtext (error)

Def output (self, Info ):
Self. Report. appendtext (Info)

Def date (Self ):
T = time. localtime (Time. Time ())
Strdate = time. strftime ("% Y. % m. % d", T)
Return strdate

Def time (Self ):
T = time. localtime (Time. Time ())
Strtime = time. strftime ("% I: % m: % s", T)
Return strtime

Def Policy (Self ):
Self. statusbar. setstatustext (self. Date () + "" + self. Time (), 1)

Class MyApp (wxapp ):
Def oninit (Self ):
Self. Frame = myframe (null,-1, "cd2py compiler ")
Self. Frame. Show (true)
Self. settopwindow (self. Frame)
Return true

Cd2py = MyApp (0)

Import sys

Class errcatcher:
Def _ init _ (Self ):
Pass

Def write (self, stuff ):
Cd2py. Frame. Error (stuff)

Class outcatcher:
Def _ init _ (Self ):
Pass

Def write (self, stuff ):
Cd2py. Frame. Output (stuff)

SYS. stderr = errcatcher ()
SYS. stdout = outcatcher ()

Cd2py. mainloop ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.