pyparsing batch file

Fabian Braennstroem f.braennstroem at gmx.de
Wed Oct 17 17:47:28 EDT 2007


Hi,

me again :-)

I would like to parse a small batch file:

file/read-case kepstop.cas
file/read-data keps1500.dat
solve/monitors/residual/plot no
solve/monitors/residual/print yes
/define/boundary-conditions in velocity-inlet 10 0.1 0.1 no 1
it 500
wd keps1500_500.dat
yes
exit

Right now, I use this little example:

from pyparsing import *

input =
open("/home/fab/HOME/Dissertation/CFD/Fluent/Batch/fluent_batch",
'r')
data = input.read()

#------------------------------------------------------------------------
# Define Grammars
#------------------------------------------------------------------------

integer = Word(nums)
hexnums = Word(alphanums)
end = Literal("\n").suppress()
all = SkipTo(end)
#threadname = dblQuotedString
threadname_read_case = Literal("file/read-case")
threadname_read_data= Literal("file/read-data")
threadname_it = Literal("it")
write_data=Literal("wd")
cas_datei= Word(alphanums)
iteration= Word(nums)
write= Word(alphanums)
file_read_data= "file/read-data " + hexnums.setResultsName("rd")

logEntry = threadname_read_case.setResultsName("threadname")
+ cas_datei.setResultsName("cas_datei")+file_read_data
logEntry = file_read_data
logEntryNew = threadname_it.setResultsName("threadname") +
iteration.setResultsName("iteration")
logEntryWD = write_data.setResultsName("threadname") +
write.setResultsName("write")

#------------------------------------------------------------------------

for tokens in logEntryNew.searchString(data):
    print
    print "Iteration Command=\t "+ tokens.threadname
    print "Number of Iterations=\t "+ tokens.iteration
    for x in tokens.condition:
       print x
    print 50*"-"

for tokens in logEntryWD.searchString(data):
    print
    print "Write Data Command=\t "+ tokens.threadname
    print "Data File Name=\t "+ tokens.write
    for x in tokens.condition:
       print x
    print 50*"-"

for tokens in logEntry.searchString(data):
    print
    print "no idea=\t "+ tokens.threadname
    print "Data File=\t "+ tokens.rd
    print
    for x in tokens.condition:
       print x
    print 50*"-"


Unfortunately, it does not parse the whole file names with
the underscore and I do not know yet, how I can access the
line with 'define/boundary-conditions'. Every 'argument' of
that command should become a separate python variable!?
Does anyone have an idea, how I can achieve this!?
Regards!
Fabian




More information about the Python-list mailing list