diff --git a/tools/datafileBuilder.py b/tools/datafileBuilder.py index cfa971fe..2ddfdd5d 100755 --- a/tools/datafileBuilder.py +++ b/tools/datafileBuilder.py @@ -7,7 +7,8 @@ from pathlib import Path from lib import LaTeX from lib.Format import Format from TexSoup import TexSoup,TexCmd -from lib.data import dataFileBase,DataType +from lib.data import dataFileBase,DataType,state +from collections import defaultdict import argparse parser = argparse.ArgumentParser() parser.add_argument('--file', type=argparse.FileType('r')) @@ -16,7 +17,7 @@ args = parser.parse_args() lines=args.file.readlines() soup=TexSoup(lines) opt=soup.dfbOptions -dfb_Opt= {"defaultType":DataType.ABS,"format":Format.LINE,"suffix":None} +dfb_Opt= {"defaultType":DataType.ABS,"format":Format.LINE,"suffix":None,"firstStates":defaultdict(lambda : state(1,1,"A_1"))} dfbDefaultTypeNode=opt.defaultType if dfbDefaultTypeNode!=None: dfbDefaultType=dfbDefaultTypeNode.expr @@ -34,6 +35,18 @@ if dfbSuffixNode!=None: dfbSuffix=dfbSuffixNode.expr if type(dfbSuffix) is TexCmd: dfb_Opt["suffix"]=dfbSuffix.args[0].value +dfbFirstStateNodes=list(opt.find_all("firstState")) +for node in dfbFirstStateNodes: + firstState=node.expr + if type(firstState) is TexCmd: + vRArgs=[arg.value for arg in firstState.args if arg.type=="required"] + vOArgs=[arg.value for arg in firstState.args if arg.type=="optional"] + if len(vOArgs)==0: + defaultstate=state.fromString(vRArgs[0]) + dfb_Opt["firstStates"].default_factory=lambda : defaultstate + else: + mystate=state.fromString(vRArgs[0]) + dfb_Opt["firstStates"][vOArgs[0]]=mystate commands=[LaTeX.newCommand(cmd) for cmd in soup.find_all("newcommand")] dat=LaTeX.tabularToData(soup.tabular,commands) scriptpath=Path(sys.argv[0]).resolve() @@ -42,6 +55,6 @@ if args.debug: datapath=datapath/"test" if not datapath.exists(): datapath.mkdir() -datalst=dataFileBase.readFromTable(dat,format=dfb_Opt["format"],default=dfb_Opt["defaultType"],commands=commands) +datalst=dataFileBase.readFromTable(dat,dfb_Opt["firstStates"],format=dfb_Opt["format"],default=dfb_Opt["defaultType"],commands=commands) for data in datalst: data.toFile(datapath,dfb_Opt["suffix"]) \ No newline at end of file diff --git a/tools/lib/data.py b/tools/lib/data.py index bc47e38f..ceac9091 100644 --- a/tools/lib/data.py +++ b/tools/lib/data.py @@ -14,8 +14,13 @@ class state: self.number = number self.multiplicity = multiplicity self.symetry = symetry - - + @staticmethod + def fromString(string): + m=re.match(r"^(?P\d)\s*\^(?P\d)(?P\S*)",string) + num=m.group('number') + mul=m.group('multiplicity') + sym=m.group('sym') + return state(num,mul,sym) @unique class DataType(IntEnum): ABS=auto() @@ -38,7 +43,7 @@ class dataFileBase(object): pass @staticmethod - def convertState(StateTablelist,default=DataType.ABS,firstState=state(1,1,"A_1"),commands=[]): + def convertState(StateTablelist,firstState,default=DataType.ABS,commands=[]): tmplst=[] for TexState in StateTablelist: math=TexState.find("$") @@ -71,9 +76,8 @@ class dataFileBase(object): count=countlst.count(countitem) lst.append((state(count,item[0],item[1]),item[2],item[3])) return lst - @staticmethod - def readFromTable(table,format=Format.LINE,default=DataType.ABS ,firstState=state(1,1,"A_1"),commands=[]): + def readFromTable(table,firstStates,format=Format.LINE,default=DataType.ABS, commands=[]): def getSubtableIndex(table,firstindex=2,column=0,count=1): subtablesindex=list() i=firstindex+count @@ -97,7 +101,8 @@ class dataFileBase(object): col=table[:,col] mymolecule=str(col[0]) mymethod=method(str(col[2]),str(col[1])) - finsts=dataFileBase.convertState(table[3:,0],default=default,firstState=firstState,commands=commands) + firstState=firstStates[mymolecule] + finsts=dataFileBase.convertState(table[3:,0],firstState,default=default,commands=commands) datacls=dict() for index,cell in enumerate(col[3:]): if str(cell)!="": @@ -123,8 +128,9 @@ class dataFileBase(object): datacls=dict() col=table[:,col] mymolecule=str(table[first,0]) + firstState=firstStates[mymolecule] mymethod=method(str(col[1]),str(col[0])) - finsts=dataFileBase.convertState(table[first:last+1,1],default=default,firstState=firstState,commands=commands) + finsts=dataFileBase.convertState(table[first:last+1,1],firstState,default=default,commands=commands) for index,cell in enumerate(col[first:last+1]): if str(cell)!="": val,unsafe=getValFromCell(cell) @@ -203,6 +209,7 @@ class dataFileBase(object): for first, last in subtablesindex: valDic=dict() mymolecule=str(table[first,0]) + firstState=firstStates[mymolecule] for col in range(2,np.size(table,1)): col=table[:,col] basis=str(col[0]) @@ -221,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],default=default,firstState=firstState,commands=commands) + finsts=dataFileBase.convertState(table[first:last+1,1],firstState,default=default,commands=commands) for index,cell in enumerate(col[first:last+1]): if str(cell)!="": val,unsafe=getValFromCell(cell) @@ -264,8 +271,9 @@ class dataFileBase(object): for first, last in subtablesindex: datacls=dict() mymolecule=str(table[first,0]) + firstState=firstStates[mymolecule] mymethod=(method("TBE(FC)"),method("TBE")) - finsts=dataFileBase.convertState(table[first:last+1,1],default=default,firstState=firstState,commands=commands) + finsts=dataFileBase.convertState(table[first:last+1,1],firstState,default=default,commands=commands) for index,row in enumerate(table[first:last+1,]): oscilatorForces=checkFloat(str(row[2])) T1 = checkFloat(str(row[3]))