diff --git a/tools/datafileBuilder.py b/tools/datafileBuilder.py index 31817d66..e1150d3c 100755 --- a/tools/datafileBuilder.py +++ b/tools/datafileBuilder.py @@ -5,8 +5,9 @@ from enum import IntEnum,auto,unique import numpy as np from pathlib import Path from lib import LaTeX +from lib.dfbOptions import dfbOptions from lib.Format import Format -from TexSoup import TexSoup,TexCmd +from TexSoup import TexSoup,TexNode,TexCmd,TexEnv from lib.data import dataFileBase,DataType,state from collections import defaultdict import argparse @@ -17,36 +18,10 @@ args = parser.parse_args() lines=args.file.readlines() soup=TexSoup(lines) opt=soup.dfbOptions -dfb_Opt= {"defaultType":DataType.ABS,"format":Format.LINE,"suffix":None,"initialStates":defaultdict(lambda : state(1,1,"A_1"))} -dfbDefaultTypeNode=opt.defaultType -if dfbDefaultTypeNode!=None: - dfbDefaultType=dfbDefaultTypeNode.expr - if type(dfbDefaultType) is TexCmd: - dfb_Opt["defaultType"]=DataType[dfbDefaultType.args[0].value.upper()] - -dfbFormatNode=opt.format -if dfbFormatNode!=None: - dfbFormat=dfbFormatNode.expr - if type(dfbFormat) is TexCmd: - dfb_Opt["format"]=Format[dfbFormat.args[0].value.upper()] - -dfbSuffixNode=opt.suffix -if dfbSuffixNode!=None: - dfbSuffix=dfbSuffixNode.expr - if type(dfbSuffix) is TexCmd: - dfb_Opt["suffix"]=dfbSuffix.args[0].value -dfbInitialStateNodes=list(opt.find_all("initialState")) -for node in dfbInitialStateNodes: - initialState=node.expr - if type(initialState) is TexCmd: - vRArgs=[arg.value for arg in initialState.args if arg.type=="required"] - vOArgs=[arg.value for arg in initialState.args if arg.type=="optional"] - if len(vOArgs)==0: - defaultstate=state.fromString("1 "+vRArgs[0]) - dfb_Opt["initialStates"].default_factory=lambda : defaultstate - else: - mystate=state.fromString("1 "+vRArgs[0]) - dfb_Opt["initialStates"][vOArgs[0]]=mystate +if type(opt) is TexNode and type(opt.expr) is TexEnv: + texOps=dfbOptions.readFromEnv(opt) +else: + texOps=dfbOptions() commands=[LaTeX.newCommand(cmd) for cmd in soup.find_all("newcommand")] dat=LaTeX.tabularToData(soup.tabular,commands) scriptpath=Path(sys.argv[0]).resolve() @@ -55,6 +30,6 @@ if args.debug: datapath=datapath/"test" if not datapath.exists(): datapath.mkdir() -datalst=dataFileBase.readFromTable(dat,dfb_Opt["initialStates"],format=dfb_Opt["format"],default=dfb_Opt["defaultType"],commands=commands) +datalst=dataFileBase.readFromTable(dat,texOps,commands=commands) for data in datalst: - data.toFile(datapath,dfb_Opt["suffix"]) \ No newline at end of file + data.toFile(datapath,texOps.suffix) \ No newline at end of file diff --git a/tools/lib/data.py b/tools/lib/data.py index cf717b6e..d32ea0c3 100644 --- a/tools/lib/data.py +++ b/tools/lib/data.py @@ -77,7 +77,7 @@ class dataFileBase(object): lst.append((state(count,item[0],item[1]),item[2],item[3])) return lst @staticmethod - def readFromTable(table,initialStates,format=Format.LINE,default=DataType.ABS, commands=[]): + def readFromTable(table,TexOps, commands=[]): def getSubtableIndex(table,firstindex=2,column=0,count=1): subtablesindex=list() i=firstindex+count @@ -96,13 +96,13 @@ class dataFileBase(object): DataType.ABS:AbsDataFile, DataType.FLUO:FluoDataFile, } - if format==Format.LINE: + if TexOps.format==Format.LINE: for col in range(1,np.size(table,1)): col=table[:,col] mymolecule=str(col[0]) mymethod=method(str(col[2]),str(col[1])) - initialState=initialStates[mymolecule] - finsts=dataFileBase.convertState(table[3:,0],initialState,default=default,commands=commands) + initialState=TexOps.initialStates[mymolecule] + finsts=dataFileBase.convertState(table[3:,0],initialState,default=TexOps.defaultType,commands=commands) datacls=dict() for index,cell in enumerate(col[3:]): if str(cell)!="": @@ -121,16 +121,16 @@ class dataFileBase(object): for value in datacls.values(): datalist.append(value) return datalist - elif format==Format.COLUMN: + elif TexOps.format==Format.COLUMN: subtablesindex=getSubtableIndex(table) for first, last in subtablesindex: for col in range(2,np.size(table,1)): datacls=dict() col=table[:,col] mymolecule=str(table[first,0]) - initialState=initialStates[mymolecule] + initialState=TexOps.initialStates[mymolecule] mymethod=method(str(col[1]),str(col[0])) - finsts=dataFileBase.convertState(table[first:last+1,1],initialState,default=default,commands=commands) + finsts=dataFileBase.convertState(table[first:last+1,1],initialState,default=TexOps.defaultType,commands=commands) for index,cell in enumerate(col[first:last+1]): if str(cell)!="": val,unsafe=getValFromCell(cell) @@ -148,7 +148,7 @@ class dataFileBase(object): for value in datacls.values(): datalist.append(value) return datalist - elif format==Format.DOUBLECOLUMN: + elif TexOps.format==Format.DOUBLECOLUMN: datacls=dict() subtablesMol=getSubtableIndex(table) for firstMol, lastMol in subtablesMol: @@ -203,13 +203,13 @@ class dataFileBase(object): for value in datacls.values(): datalist.append(value) return datalist - elif format==Format.EXOTICCOLUMN: + elif TexOps.format==Format.EXOTICCOLUMN: import json subtablesindex=getSubtableIndex(table) for first, last in subtablesindex: valDic=dict() mymolecule=str(table[first,0]) - initialState=initialStates[mymolecule] + initialState=TexOps.initialStates[mymolecule] for col in range(2,np.size(table,1)): col=table[:,col] basis=str(col[0]) @@ -228,7 +228,7 @@ class dataFileBase(object): methodname=str(methtex) mymethod=method(methodname,basis) methkey=json.dumps(mymethod.__dict__) - finsts=dataFileBase.convertState(table[first:last+1,1],initialState,default=default,commands=commands) + finsts=dataFileBase.convertState(table[first:last+1,1],initialState,default=TexOps.default,commands=commands) for index,cell in enumerate(col[first:last+1]): if str(cell)!="": val,unsafe=getValFromCell(cell) @@ -266,14 +266,14 @@ class dataFileBase(object): data.excitations.append(excitationValue(initialState,st,val,type=ty,T1=T1,isUnsafe=unsafe,oscilatorForces=oF)) datalist.append(data) return datalist - elif format==Format.TBE: + elif TexOps.format==Format.TBE: subtablesindex=getSubtableIndex(table) for first, last in subtablesindex: datacls=dict() mymolecule=str(table[first,0]) - initialState=initialStates[mymolecule] + initialState=TexOps.initialStates[mymolecule] mymethod=(method("TBE(FC)"),method("TBE")) - finsts=dataFileBase.convertState(table[first:last+1,1],initialState,default=default,commands=commands) + finsts=dataFileBase.convertState(table[first:last+1,1],initialState,default=TexOps.defaultType,commands=commands) for index,row in enumerate(table[first:last+1,]): oscilatorForces=checkFloat(str(row[2])) T1 = checkFloat(str(row[3])) @@ -300,7 +300,7 @@ class dataFileBase(object): for dat in value: datalist.append(dat) return datalist - elif format==Format.DOUBLETBE: + elif TexOps.format==Format.DOUBLETBE: datacls=dict() subtablesMol=getSubtableIndex(table) for firstMol, lastMol in subtablesMol: diff --git a/tools/lib/dfbOptions.py b/tools/lib/dfbOptions.py new file mode 100644 index 00000000..43b4cf0a --- /dev/null +++ b/tools/lib/dfbOptions.py @@ -0,0 +1,45 @@ +from TexSoup import TexSoup,TexCmd +from .Format import Format +from .data import dataFileBase,DataType,state +from collections import defaultdict + +class dfbOptions(object): + def __init__(self): + self.defaultType=DataType.ABS + self.format=Format.LINE + self.suffix=None + self.initialStates=defaultdict(lambda : state(1,1,"A_1")) + + @staticmethod + def readFromEnv(lateEnv): + dfb_Opt=dfbOptions() + dfbDefaultTypeNode=lateEnv.defaultType + if dfbDefaultTypeNode!=None: + dfbDefaultType=dfbDefaultTypeNode.expr + if type(dfbDefaultType) is TexCmd: + dfb_Opt.defaultType=DataType[dfbDefaultType.args[0].value.upper()] + + dfbFormatNode=lateEnv.format + if dfbFormatNode!=None: + dfbFormat=dfbFormatNode.expr + if type(dfbFormat) is TexCmd: + dfb_Opt.format=Format[dfbFormat.args[0].value.upper()] + + dfbSuffixNode=lateEnv.suffix + if dfbSuffixNode!=None: + dfbSuffix=dfbSuffixNode.expr + if type(dfbSuffix) is TexCmd: + dfb_Opt.suffix=dfbSuffix.args[0].value + dfbInitialStateNodes=list(lateEnv.find_all("initialState")) + for node in dfbInitialStateNodes: + initialState=node.expr + if type(initialState) is TexCmd: + vRArgs=[arg.value for arg in initialState.args if arg.type=="required"] + vOArgs=[arg.value for arg in initialState.args if arg.type=="optional"] + if len(vOArgs)==0: + defaultstate=state.fromString("1 "+vRArgs[0]) + dfb_Opt.initialStates.default_factory=lambda : defaultstate + else: + mystate=state.fromString("1 "+vRArgs[0]) + dfb_Opt.initialStates[vOArgs[0]]=mystate + return dfb_Opt \ No newline at end of file