#============================================================================== # # Project: SharpImage # Module: Dir.py # Language: IronPython # Author: Dan Mueller # Date: $Date: 2007-07-06 10:57:00 +1000 (Fri, 06 Jul 2007) $ # Revision: $Revision: 2 $ # # Copyright (c) Queensland University of Technology (QUT) 2007. # All rights reserved. # # This software is distributed WITHOUT ANY WARRANTY; without even # the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR # PURPOSE. See the above copyright notices for more information. # #============================================================================== # Import the base script class import Script from Script import * class DirScript(ScriptObject): Name = "dir" Help = """Searches the Application StartupPath for *.py scripts.""" Parameters = """(string) Default = the search filter Eg. \"General\", \"Gaussian\".""" ParentApplication = None Handlers = None Default = None ScriptsGeneral = None ScriptsNormal = None ScriptsFiltered = None def Run(self): """ The entry-point for this script. """ # Setup for the search searchPath = Application.StartupPath searchPattern = "*.py" searchOption = SearchOption.AllDirectories # Get a collection of scripts self.ScriptsNormal = List[str]() for scriptPath in System.IO.Directory.GetFiles( searchPath, searchPattern, searchOption ): self.ScriptsNormal.Add( scriptPath ) # Remove internal "Script" scripts self.ScriptsNormal.RemoveAll( Predicate[str](self.RemoveInternalScripts) ) # Remove "General" scripts from main list and add them to ListGeneral self.ScriptsGeneral = List[str]() self.ScriptsNormal.RemoveAll( Predicate[str](self.RemoveGeneralScripts) ) # Sort in alpha-numeric order self.ScriptsNormal.Sort() # Make list to be filtered self.ScriptsFiltered = List[str]() for scriptPath in self.ScriptsGeneral: self.ScriptsFiltered.Add( scriptPath ) for scriptPath in self.ScriptsNormal: self.ScriptsFiltered.Add( scriptPath ) # Filter if (self.Default != None): self.ScriptsFiltered.RemoveAll( Predicate[str](self.RemoveFilteredScripts) ) # Write scripts to console for scriptPath in self.ScriptsFiltered: self.WriteScriptToConsole( scriptPath, searchPath ) # Finish up self.ParentApplication.SetApplicationAsReady( 0 ) self.Handlers["ScriptFinalised"]() def WriteScriptToConsole(self, scriptPath, searchPath): """ Writes the script to the console. """ scriptName = scriptPath.Replace( searchPath + "\\", str.Empty ) scriptName = Path.Combine( Path.GetDirectoryName(scriptName), Path.GetFileNameWithoutExtension(scriptName) ) self.Handlers["ScriptWriteLineToConsole"]( scriptName ) def RemoveInternalScripts(self, scriptPath): """ A Predicate method for removing all internal "Script" scripts. """ return Path.GetFileNameWithoutExtension(scriptPath).Contains( "Script" ) def RemoveGeneralScripts(self, scriptPath): """ A Predicate method for removing all "General" scripts. """ result = Path.GetDirectoryName(scriptPath).Contains( "General" ) if (result): self.ScriptsGeneral.Add( scriptPath ) return result def RemoveFilteredScripts(self, scriptPath): """ A Predicate method for removing all scripts which do not contain text. """ return not scriptPath.ToLower().Contains( self.Default.ToLower() )