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

Source Code for Module PyFoam.Basics.Utilities

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