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

Source Code for Module PyFoam.Basics.Utilities

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