10
0
mirror of https://github.com/LCPQ/QUESTDB_website.git synced 2024-12-25 13:53:48 +01:00

LaTeX options are now object oriented

This commit is contained in:
Mickaël Véril 2020-06-26 14:36:35 +02:00
parent 6f15d73054
commit c2ec537ab7
3 changed files with 68 additions and 48 deletions

View File

@ -5,8 +5,9 @@ from enum import IntEnum,auto,unique
import numpy as np import numpy as np
from pathlib import Path from pathlib import Path
from lib import LaTeX from lib import LaTeX
from lib.dfbOptions import dfbOptions
from lib.Format import Format 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 lib.data import dataFileBase,DataType,state
from collections import defaultdict from collections import defaultdict
import argparse import argparse
@ -17,36 +18,10 @@ args = parser.parse_args()
lines=args.file.readlines() lines=args.file.readlines()
soup=TexSoup(lines) soup=TexSoup(lines)
opt=soup.dfbOptions opt=soup.dfbOptions
dfb_Opt= {"defaultType":DataType.ABS,"format":Format.LINE,"suffix":None,"initialStates":defaultdict(lambda : state(1,1,"A_1"))} if type(opt) is TexNode and type(opt.expr) is TexEnv:
dfbDefaultTypeNode=opt.defaultType texOps=dfbOptions.readFromEnv(opt)
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: else:
mystate=state.fromString("1 "+vRArgs[0]) texOps=dfbOptions()
dfb_Opt["initialStates"][vOArgs[0]]=mystate
commands=[LaTeX.newCommand(cmd) for cmd in soup.find_all("newcommand")] commands=[LaTeX.newCommand(cmd) for cmd in soup.find_all("newcommand")]
dat=LaTeX.tabularToData(soup.tabular,commands) dat=LaTeX.tabularToData(soup.tabular,commands)
scriptpath=Path(sys.argv[0]).resolve() scriptpath=Path(sys.argv[0]).resolve()
@ -55,6 +30,6 @@ if args.debug:
datapath=datapath/"test" datapath=datapath/"test"
if not datapath.exists(): if not datapath.exists():
datapath.mkdir() 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: for data in datalst:
data.toFile(datapath,dfb_Opt["suffix"]) data.toFile(datapath,texOps.suffix)

View File

@ -77,7 +77,7 @@ class dataFileBase(object):
lst.append((state(count,item[0],item[1]),item[2],item[3])) lst.append((state(count,item[0],item[1]),item[2],item[3]))
return lst return lst
@staticmethod @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): def getSubtableIndex(table,firstindex=2,column=0,count=1):
subtablesindex=list() subtablesindex=list()
i=firstindex+count i=firstindex+count
@ -96,13 +96,13 @@ class dataFileBase(object):
DataType.ABS:AbsDataFile, DataType.ABS:AbsDataFile,
DataType.FLUO:FluoDataFile, DataType.FLUO:FluoDataFile,
} }
if format==Format.LINE: if TexOps.format==Format.LINE:
for col in range(1,np.size(table,1)): for col in range(1,np.size(table,1)):
col=table[:,col] col=table[:,col]
mymolecule=str(col[0]) mymolecule=str(col[0])
mymethod=method(str(col[2]),str(col[1])) mymethod=method(str(col[2]),str(col[1]))
initialState=initialStates[mymolecule] initialState=TexOps.initialStates[mymolecule]
finsts=dataFileBase.convertState(table[3:,0],initialState,default=default,commands=commands) finsts=dataFileBase.convertState(table[3:,0],initialState,default=TexOps.defaultType,commands=commands)
datacls=dict() datacls=dict()
for index,cell in enumerate(col[3:]): for index,cell in enumerate(col[3:]):
if str(cell)!="": if str(cell)!="":
@ -121,16 +121,16 @@ class dataFileBase(object):
for value in datacls.values(): for value in datacls.values():
datalist.append(value) datalist.append(value)
return datalist return datalist
elif format==Format.COLUMN: elif TexOps.format==Format.COLUMN:
subtablesindex=getSubtableIndex(table) subtablesindex=getSubtableIndex(table)
for first, last in subtablesindex: for first, last in subtablesindex:
for col in range(2,np.size(table,1)): for col in range(2,np.size(table,1)):
datacls=dict() datacls=dict()
col=table[:,col] col=table[:,col]
mymolecule=str(table[first,0]) mymolecule=str(table[first,0])
initialState=initialStates[mymolecule] initialState=TexOps.initialStates[mymolecule]
mymethod=method(str(col[1]),str(col[0])) 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]): for index,cell in enumerate(col[first:last+1]):
if str(cell)!="": if str(cell)!="":
val,unsafe=getValFromCell(cell) val,unsafe=getValFromCell(cell)
@ -148,7 +148,7 @@ class dataFileBase(object):
for value in datacls.values(): for value in datacls.values():
datalist.append(value) datalist.append(value)
return datalist return datalist
elif format==Format.DOUBLECOLUMN: elif TexOps.format==Format.DOUBLECOLUMN:
datacls=dict() datacls=dict()
subtablesMol=getSubtableIndex(table) subtablesMol=getSubtableIndex(table)
for firstMol, lastMol in subtablesMol: for firstMol, lastMol in subtablesMol:
@ -203,13 +203,13 @@ class dataFileBase(object):
for value in datacls.values(): for value in datacls.values():
datalist.append(value) datalist.append(value)
return datalist return datalist
elif format==Format.EXOTICCOLUMN: elif TexOps.format==Format.EXOTICCOLUMN:
import json import json
subtablesindex=getSubtableIndex(table) subtablesindex=getSubtableIndex(table)
for first, last in subtablesindex: for first, last in subtablesindex:
valDic=dict() valDic=dict()
mymolecule=str(table[first,0]) mymolecule=str(table[first,0])
initialState=initialStates[mymolecule] initialState=TexOps.initialStates[mymolecule]
for col in range(2,np.size(table,1)): for col in range(2,np.size(table,1)):
col=table[:,col] col=table[:,col]
basis=str(col[0]) basis=str(col[0])
@ -228,7 +228,7 @@ class dataFileBase(object):
methodname=str(methtex) methodname=str(methtex)
mymethod=method(methodname,basis) mymethod=method(methodname,basis)
methkey=json.dumps(mymethod.__dict__) 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]): for index,cell in enumerate(col[first:last+1]):
if str(cell)!="": if str(cell)!="":
val,unsafe=getValFromCell(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)) data.excitations.append(excitationValue(initialState,st,val,type=ty,T1=T1,isUnsafe=unsafe,oscilatorForces=oF))
datalist.append(data) datalist.append(data)
return datalist return datalist
elif format==Format.TBE: elif TexOps.format==Format.TBE:
subtablesindex=getSubtableIndex(table) subtablesindex=getSubtableIndex(table)
for first, last in subtablesindex: for first, last in subtablesindex:
datacls=dict() datacls=dict()
mymolecule=str(table[first,0]) mymolecule=str(table[first,0])
initialState=initialStates[mymolecule] initialState=TexOps.initialStates[mymolecule]
mymethod=(method("TBE(FC)"),method("TBE")) 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,]): for index,row in enumerate(table[first:last+1,]):
oscilatorForces=checkFloat(str(row[2])) oscilatorForces=checkFloat(str(row[2]))
T1 = checkFloat(str(row[3])) T1 = checkFloat(str(row[3]))
@ -300,7 +300,7 @@ class dataFileBase(object):
for dat in value: for dat in value:
datalist.append(dat) datalist.append(dat)
return datalist return datalist
elif format==Format.DOUBLETBE: elif TexOps.format==Format.DOUBLETBE:
datacls=dict() datacls=dict()
subtablesMol=getSubtableIndex(table) subtablesMol=getSubtableIndex(table)
for firstMol, lastMol in subtablesMol: for firstMol, lastMol in subtablesMol:

45
tools/lib/dfbOptions.py Normal file
View File

@ -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