Package PyFoam :: Package Basics :: Module Utilities
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Basics.Utilities

 1  #  ICE Revision: $Id: Utilities.py 7581 2007-06-27 15:29:14Z bgschaid $  
 2  """ Utility functions 
 3   
 4  Can be used via a class or as functions""" 
 5   
 6  from os import popen4 
 7  from os import listdir 
 8   
 9  import re 
10   
11 -class Utilities(object):
12 """Class with utility methods 13 14 Can be inherited without side effects by classes that need these 15 methods""" 16
17 - def __init__(self):
18 pass
19
20 - def execute(self,cmd,debug=False):
21 """Execute the command cmd 22 23 Currently no error-handling is done 24 @return: A list with all the output-lines of the execution""" 25 if debug: 26 print cmd 27 28 rein,raus=popen4(cmd) 29 tmp=raus.readlines() 30 # line=raus.readline() 31 # while line!="": 32 # print line 33 # line=raus.readline() 34 35 return tmp
36
37 - def writeDictionaryHeader(self,f):
38 """Writes a dummy header so OpenFOAM accepts the file as a dictionary 39 @param f: The file to write to 40 @type f: file""" 41 42 f.write(""" 43 // * * * * * * * * * // 44 FoamFile 45 { 46 version 0.5; 47 format ascii; 48 root "ROOT"; 49 case "CASE"; 50 class dictionary; 51 object nix; 52 } 53 """)
54 55 excludeNames=["^.svn$" , "~$"] 56
57 - def listDirectory(self,d):
58 """Lists the files in a directory, but excludes certain names 59 and files with certain endings 60 @param d: The directory to list 61 @return: List of the found files and directories""" 62 63 result=[] 64 65 excludes=map(re.compile,self.excludeNames) 66 67 for n in listdir(d): 68 ok=True 69 70 for e in excludes: 71 if e.search(n): 72 ok=False 73 break 74 75 if ok: 76 result.append(n) 77 78 return result
79
80 -def execute(cmd,debug=False):
81 """Calls the method of the same name from the Utilites class""" 82 return Utilities().execute(cmd,debug)
83
84 -def writeDictionaryHeader(f):
85 """Calls the method of the same name from the Utilites class""" 86 Utilities().writeDictionaryHeader(f)
87
88 -def listDirectory(d):
89 """Calls the method of the same name from the Utilites class""" 90 return Utilities().listDirectory(d)
91