(original) (raw)
#!/bin/env python from path import path from platform import system as ostype from os.path import basename from os.path import exists from os import chdir from os import remove from os import getcwd from os import chmod from os import popen from pickle import loads from pickle import dumps from time import localtime import Pmw import Tkinter class CheckerWindow: def __init__( self, parent, checker, command ): self.Status = 0 self.myparent = parent self.window = Tkinter.Toplevel( parent ) self.window.title( "Output for checker : " + checker ) self.fixedFont = Pmw.logicalfont( 'Fixed' ) self.output = Pmw.ScrolledText( self.window, hull_width = 300, hull_height = 250, text_font = self.fixedFont, text_padx = 2, text_pady = 2 ) self.output.pack( padx = 1, pady = 3 ) self.button_group = Pmw.Group( self.window, tag_text = "Options" ) self.button_group.pack( pady = 2, padx = 2, expand = 'yes', fill = 'both' ) Tkinter.Label( self.button_group.interior(), text = "Click 'Accept' if the report ran correctly." ).pack( anchor = 'w' ) Tkinter.Label( self.button_group.interior(), text = "Click 'Cancel' if there were errors." ).pack( anchor = 'w' ) self.buttons = Pmw.ButtonBox( self.button_group.interior() ) self.buttons.pack( padx = 5, pady = 5, anchor = 'e' ) self.buttons.add( 'Execute', command = lambda arg1 = command: self.RunChecker( arg1 ) ) self.buttons.add( 'Accept', command = self.Accept ) self.buttons.add( 'Cancel', command = self.Cancel ) def RunChecker( self, command ): input = popen( command, 'r' ) self.Clear() while 1: line = input.readline() if not line: break self.Insert( str( line ) ) def Accept( self ): self.myparent.CheckerAccept() self.window.destroy() def Cancel( self ): self.window.destroy() def Clear( self ): self.output.clear() def Insert( self, data ): self.output.appendtext( data ) class CheckerApp: STATUSFILE = 'fe_status' OPTIONS = {} if ostype() == "Windows": BINPATH = path( '' ) SSBIN = path( 'c:\\usr\\geoset\\bin\\start_socet.bat -single ' ) PROJPATH = path( 'c:\\usr\\geoset\\data\\' ) elif ostype() == "SunOS": BINPATH = path( '/usr/geoset/bin/' ) SSBIN = BINPATH.joinpath( 'start_socet_no_record -single ' ) PROJPATH = path( '/usr/geoset/data/' ) else: BINPATH = path( '' ) def __init__( self, parent, project, database, dbpath ): # Window Initialization routines self.PROJECT = project self.DATABASE = database self.DBPATH = dbpath self.newroot = Tkinter.Toplevel() self.newroot.resizable( width = False, height = False ) self.newroot.option_add( "*Font", "Arial 12" ) self.newroot.title( "SAIC Checker Front End" ) self.newroot.focus_set() self.Status = 0 # Change the current working directory to the database directory # so that the checkers function correctly. chdir( path( dbpath ).joinpath( path( database ) ) ) # Check for the existance of 'fe_status' if it doesn't, exist, create it with defaults # if it does exist, load it. if not exists( self.STATUSFILE ): self.OPTIONS = { 'ftr_count' : [ "", ".cnt" ], 'find_dup_feat' : [ "", ".df" ], '3dc_weed_gp' : [ "", ".wg " ], '3dc_populate' : [ "", ".pop" ], '3dc_validate' : [ "", ".val" ] } self.SaveStatus() else: self.LoadStatus() # Create a status bar self.statusbar = Tkinter.Label( self.newroot, bd = 1, relief = Tkinter.SUNKEN, anchor = Tkinter.W ) self.statusbar.pack( side = Tkinter.BOTTOM, fill = 'x' ) # Quit Button Tkinter.Button( self.newroot, text = "Quit", command = self.newroot.destroy ).pack( pady = 5, side = Tkinter.BOTTOM ) # Information Group self.info_group = Pmw.Group( self.newroot, tag_text = "Information" ) self.info_group.pack( pady = 2, padx = 2, expand = 'yes', fill = 'both' ) Tkinter.Label( self.info_group.interior(), text = "Project : " + project ).pack( anchor = Tkinter.W ) Tkinter.Label( self.info_group.interior(), text = "Database : " + database ).pack( anchor = Tkinter.W ) # Feature Counter Group self.ftr_count_group = Pmw.Group( self.newroot, tag_text = "Feature Counter" ) self.ftr_count_group.pack( pady = 2, padx = 2, expand = 'yes', fill = 'both' ) self.ftr_count_run = Tkinter.StringVar() if self.OPTIONS['ftr_count'][ 0 ] == "": self.ftr_count_run.set( "Last ran on : Never" ) else: self.ftr_count_run.set( "Last ran on : " + self.OPTIONS['ftr_count'][ 0 ] ) Tkinter.Label( self.ftr_count_group.interior(), textvariable = self.ftr_count_run ).pack( anchor = Tkinter.W ) self.ftr_count_btns = Pmw.ButtonBox( self.ftr_count_group.interior() ) self.ftr_count_btns.pack( fill = 'both', expand = 'yes', padx = 5, pady = 5 ) self.ftr_count_btns.add( 'Run Checker', command = lambda arg1 = 'ftr_count': self.RunChecker( arg1 ) ) self.ftr_count_btns.add( 'View Report', command = self.ViewReport ) # Find Duplicate Features Group self.fnd_dup_group = Pmw.Group( self.newroot, tag_text = "Find Duplicate Features" ) self.fnd_dup_group.pack( pady = 2, padx = 2, expand = 'yes', fill = 'both' ) self.fnd_dup_run = Tkinter.StringVar() if self.OPTIONS['find_dup_feat'][ 0 ] == "": self.fnd_dup_run.set( "Last ran on : Never" ) else: self.fnd_dup_run.set( "Last ran on : " + self.OPTIONS['find_dup_feat'][ 0 ] ) Tkinter.Label( self.fnd_dup_group.interior(), textvariable = self.fnd_dup_run ).pack( anchor = Tkinter.W ) self.fnd_dup_btns = Pmw.ButtonBox( self.fnd_dup_group.interior() ) self.fnd_dup_btns.pack( fill = 'both', expand = 'yes', padx = 5, pady = 5 ) self.fnd_dup_btns.add( 'Run Checker', command = lambda arg1 = 'find_dup_feat': self.RunChecker( arg1 ) ) self.fnd_dup_btns.add( 'View Report', command = self.ViewReport ) # ctyity Weed Ground Point Group self.weed_gp_group = Pmw.Group( self.newroot, tag_text = "3D City Weed Ground Point" ) self.weed_gp_group.pack( pady = 2, padx = 2, expand = 'yes', fill = 'both' ) self.weed_gp_run = Tkinter.StringVar() if self.OPTIONS['3dc_weed_gp'][ 0 ] == "": self.weed_gp_run.set( "Last ran on : Never" ) else: self.weed_gp_run.set( "Last ran on : " + self.OPTIONS['3dc_weed_gp'][ 0 ] ) Tkinter.Label( self.weed_gp_group.interior(), textvariable = self.weed_gp_run ).pack( anchor = Tkinter.W ) self.weed_gp_btns = Pmw.ButtonBox( self.weed_gp_group.interior() ) self.weed_gp_btns.pack( fill = 'both', expand = 'yes', padx = 5, pady = 5 ) self.weed_gp_btns.add( 'Run Checker', command = lambda arg1 = '3dc_weed_gp': self.RunChecker( arg1 ) ) self.weed_gp_btns.add( 'View Report', command = self.ViewReport ) # ctyity Populator Group self.cty_pop_group = Pmw.Group( self.newroot, tag_text = "3D City Populator" ) self.cty_pop_group.pack( pady = 2, padx = 2, expand = 'yes', fill = 'both' ) self.cty_pop_run = Tkinter.StringVar() if self.OPTIONS['3dc_weed_gp'][ 0 ] == "": self.cty_pop_run.set( "Last ran on : Never" ) else: self.cty_pop_run.set( "Last ran on : " + self.OPTIONS['3dc_populate'][ 0 ] ) Tkinter.Label( self.cty_pop_group.interior(), textvariable = self.cty_pop_run ).pack( anchor = Tkinter.W ) self.cty_pop_btns = Pmw.ButtonBox( self.cty_pop_group.interior() ) self.cty_pop_btns.pack( fill = 'both', expand = 'yes', padx = 5, pady = 5 ) self.cty_pop_btns.add( 'Run Checker', command = lambda arg1 = '3dc_populate': self.RunChecker( arg1 ) ) self.cty_pop_btns.add( 'View Report', command = self.ViewReport ) # ctyity Validator Group self.cty_val_group = Pmw.Group( self.newroot, tag_text = "3D City Validator" ) self.cty_val_group.pack( pady = 2, padx = 2, expand = 'yes', fill = 'both' ) self.cty_val_run = Tkinter.StringVar() if self.OPTIONS['3dc_validate'][ 0 ] == "": self.cty_val_run.set( "Last ran on : Never" ) else: self.cty_val_run.set( "Last ran on " + self.OPTIONS['3dc_validate'][ 0 ] ) Tkinter.Label( self.cty_val_group.interior(), textvariable = self.cty_val_run ).pack( anchor = Tkinter.W ) self.cty_val_btns = Pmw.ButtonBox( self.cty_val_group.interior() ) self.cty_val_btns.pack( fill = 'both', expand = 'yes', padx = 5, pady = 5 ) self.cty_val_btns.add( 'Run Checker', command = lambda arg1 = '3dc_weed_gp': self.RunChecker( arg1 ) ) self.cty_val_btns.add( 'View Report', command = self.ViewReport ) def CheckerAccept( self ): print "Accepted!" def RunChecker( self, checker ): if checker == "ftr_count": self.RunFtrCount() elif checker == "find_dup_feat": self.RunFindDupFeat() elif checker == "3dc_weed_gp": self.RunWeedGP() elif checker == "3dc_populate": self.RunPopulate() elif checker == "3dc_validate": self.RunValidate() else: pass def RunFtrCount( self ): # Build the commandline sequence command = self.SSBIN + self.BINPATH + "ftr_count " + self.PROJPATH + \ self.PROJECT + ".prj" + " " + path.joinpath( self.DBPATH, self.DATABASE ) # Build the path to the report file report = self.DBPATH.joinpath( self.DATABASE ) + ".cnt" # If the report file exists, remove it. The creation of the report file # is used to determine if the program ran correctly. if exists( report ): remove( report ) # Run the checker command CheckerWindow( self.newroot, 'Feature Count', command ) print self.Status if not exists( report ): self.CheckerError( 'ftr_count' ) return else: date = str( + localtime()[ 1 ] ) + "/" + str( localtime()[ 2 ] ) self.ftr_count_run.set( "Last ran on : " + date ) self.OPTIONS['ftr_count'][ 0 ] = date self.SaveStatus() self.newroot.focus_set() chmod( report, 0777 ) self.ViewReport( report ) def RunFindDupFeat( self ): # Build the commandline sequence command = self.SSBIN + self.BINPATH + "find_dup_feat " + self.PROJPATH + \ self.PROJECT + ".prj" + " " + path.joinpath( self.DBPATH, self.DATABASE ) # Build the path to the report file report = self.DBPATH.joinpath( self.DATABASE ) + ".df" # If the report file exists, remove it. The creation of the report file # is used to determine if the program ran correctly. if exists( report ): remove( report ) # Run the checker command print command popen( command ) if not exists( report ): self.CheckerError( 'find_dup_feat' ) return else: date = str( + localtime()[ 1 ] ) + "/" + str( localtime()[ 2 ] ) self.fnd_dup_run.set( "Last ran on : " + date ) self.OPTIONS['find_dup_feat'][ 0 ] = date self.SaveStatus() self.newroot.focus_set() chmod( report, 0777 ) self.ViewReport( report ) def RunWeedGP( self ): # Build the commandline sequence command = self.SSBIN + self.BINPATH + "3dc_weed_gp " + self.PROJPATH + \ self.PROJECT + ".prj" + " " + path.joinpath( self.DBPATH, self.DATABASE ) # Build the path to the report file report = self.DBPATH.joinpath( self.DATABASE ) + ".wg" # If the report file exists, remove it. The creation of the report file # is used to determine if the program ran correctly. if exists( report ): remove( report ) # Run the checker command print command popen( command ) if not exists( report ): self.CheckerError( '3dc_weed_gp' ) return else: date = str( + localtime()[ 1 ] ) + "/" + str( localtime()[ 2 ] ) self.weed_gp_run.set( "Last ran on : " + date ) self.OPTIONS['3dc_weed_gp'][ 0 ] = date self.SaveStatus() self.newroot.focus_set() chmod( report, 0777 ) self.ViewReport( report ) def RunValidate( self ): # Build the commandline sequence command = self.SSBIN + self.BINPATH + "3dc_validate " + self.PROJPATH + \ self.PROJECT + ".prj" + " " + path.joinpath( self.DBPATH, self.DATABASE ) + " 100 1200" # Build the path to the report file report = self.DBPATH.joinpath( self.DATABASE ) + ".val" # If the report file exists, remove it. The creation of the report file # is used to determine if the program ran correctly. if exists( report ): remove( report ) # Run the checker command print command popen( command ) if not exists( report ): self.CheckerError( '3dc_validate' ) return else: date = str( + localtime()[ 1 ] ) + "/" + str( localtime()[ 2 ] ) self.cty_val_run.set( "Last ran on : " + date ) self.OPTIONS['3dc_validate'][ 0 ] = date self.SaveStatus() self.newroot.focus_set() chmod( report, 0777 ) self.ViewReport( report ) def RunPopulate( self ): # Build the commandline sequence command = self.SSBIN + self.BINPATH + "3dc_populate " + self.PROJPATH + \ self.PROJECT + ".prj" + " " + path.joinpath( self.DBPATH, self.DATABASE ) # Build the path to the report file report = self.DBPATH.joinpath( self.DATABASE ) + ".pop" # If the report file exists, remove it. The creation of the report file # is used to determine if the program ran correctly. if exists( report ): remove( report ) # Run the checker command print command popen( command ) if not exists( report ): print "Pop doesn't exist?" self.CheckerError( '3dc_populate' ) return else: date = str( + localtime()[ 1 ] ) + "/" + str( localtime()[ 2 ] ) self.cty_pop_run.set( "Last ran on : " + date ) self.OPTIONS['3dc_populate'][ 0 ] = date self.SaveStatus() self.newroot.focus_set() chmod( report, 0777 ) self.ViewReport( report ) def ViewReport( self, report ): pass def CheckerError( self, checker ): pass def SaveStatus( self ): """ Saves a pickled self.OPTIONS dictionary to self.STATUSFILE """ file = open( self.STATUSFILE, 'wb' ) file.write( dumps( self.OPTIONS, 1 ) ) file.close() def LoadStatus( self ): """ Loads a picked self.OPTIONS dictionary from self.STATUSFILE """ file = open( self.STATUSFILE, 'rb' ) buffer = "" while 1: data = file.read() if data == "": break buffer += data self.OPTIONS = loads( buffer ) file.close() class MainApp: def __init__( self, parent ): self.myparent = parent if ostype() == "Windows": self.projpath = path('c:\usr\geoset\data') elif ostype() == "SunOS": self.projpath = path('/usr/geoset/data' ) else: print "Non-supported platform" return # Create a status bar self.statusbar = Tkinter.Label( parent, bd = 1, relief = Tkinter.SUNKEN, anchor = Tkinter.W ) self.statusbar.grid( row = 99, column = 0, columnspan = 2, sticky = Tkinter.NE + Tkinter.SW ) # Setup the dropdown list and select button self.projmenu = Pmw.ComboBox( parent, label_text = "Select a Project : ", labelpos = 'w', scrolledlist_items = self.InitProjList(), selectioncommand = self.PrjSelection) self.projmenu.grid( row = 0, column = 0, pady = 5, sticky = Tkinter.W ) # Setup the List Box with available databases self.dblist = Pmw.ScrolledListBox( parent, labelpos = 'w', label_text = 'Databases', listbox_height = 10, listbox_width = 30, scrollmargin = 1, hull_width = 200, hull_height = 200, vscrollmode = 'static' ) self.dblist.grid( row = 2, column = 0, sticky = Tkinter.W, pady = 5 ) # Button Box for Select and Quit buttons self.buttons = Pmw.ButtonBox( parent ) self.buttons.grid( row = 3, column = 0, pady = 10 ) self.buttons.add( 'Select Database', command = self.DBSelectButton ) self.buttons.add( 'Quit', command = parent.destroy ) def PrjSelection( self, event ): # Callback for selecting a project from the dropdown list if self.projmenu.get() == "": self.statusbar.config( text = "You must select a project first!") else: self.statusbar.config( text = "" ) databases = self.InitDBList( self.projmenu.get() ) self.dblist.clear() if databases: for database in databases: self.dblist.insert( Tkinter.END, basename( database ) ) else: self.dblist.insert( Tkinter.END, "NO DATABASES AVAILABLE" ) def DBSelectButton( self ): # Callback for selecting a database then clicking the button. if self.projmenu.get() == "": self.statusbar.config( text = "You must select a project, then a database!" ) return if not self.dblist.getvalue(): self.statusbar.config( text = "You must select a database first!" ) return if self.dblist.getvalue()[0] == "NO DATABASES AVAILABLE": self.statusbar.config( text = "This is not a valid database" ) return else: self.statusbar.config( text = "" ) checkerwindow = CheckerApp( self.myparent, self.projmenu.get(), self.dblist.getvalue()[ 0 ], self.dbpath ) def InitProjList( self ): # Initialization of the project dropdown list projlist = [] for file in self.projpath.files( '*.prj' ): projlist.append( basename( file ).split( "." )[ 0 ] ) projlist.sort() return projlist def InitDBList( self, project ): # Initialization of the database listing databases = [] projfile = self.projpath.joinpath( project + ".prj" ) fpoint = open( projfile, 'r' ) while 1: data = fpoint.readline() if data == "": break if data.count( 'DATA_PATH' ) is 0: continue else: datapath = data.split()[ 1 ] break self.dbpath = path( datapath ) if not exists( self.dbpath ): return for dir in self.dbpath.dirs(): temppath = self.dbpath.joinpath( dir ).joinpath( 'fdb.history' ) if exists( temppath ): databases.append( dir ) fpoint.close() return databases def main(): try: root = Tkinter.Tk() except: print print "An error occured when attempting to initialize the GUI." print "Is your DISPLAY variable set correctly?" print print "Exiting" return root.resizable( width = False, height = False ) root.option_add( "*Font", "Arial 12" ) root.focus_set() Pmw.initialise( root ) root.title( "SAIC Checker Front End" ) app = MainApp( root ) root.mainloop() if __name__ == "__main__": main()