import os def strToAsc(strIn) : # uses ord(c) sList=[] # empty list for ch in strIn : sList.append(ord(ch)) #for end #print(sList) return (sList) #def end def replaceNextEntity(eDict,lineIn) : # by D@CC on 2022FJun18 # replaces eName by eNew in line # e.g. lineNew=replaceEntity(eDict,"count",line) # returns lineNew, True if success # returns Null, False if failure # eDict is of form [(count,cValue), etc] #print("***enterring replaceNextEntity") #print("***lineIn:"+lineIn) #print("eDict",eDict) #find an entity to be replaced in line eB=lineIn.find("&") eE=lineIn.find("+") isSuccess=True isProcessing=True while isProcessing : eName="" # default value if eB==-1 : isSuccess=False ; break if eE<=eB+2 : isSuccess=False ; break eName = lineIn[eB+1:eE] #print("eName:"+eName) eNew=eDict[eName] # look up eName in eDict if eNew=="" : isSuccess=False ; break isProcessing=False #done #while end if isSuccess==True : #print("eNew:"+eNew) pre=lineIn[0:eB] post=lineIn[eE+1:] lineNew=pre+eNew+post else: #print("Soft Warning: couldn't find |"+eName+"|") eNew="" #print("eNew:"+eNew) lineNew=lineIn #if end #isSuccess=True #lineNew=line.translate(eDict) #print("lineNew:"+lineNew) #lineNew=line return lineNew,isSuccess #def end #print(strToAsc("a b")) progName="IXc_v0c.py" print("progName:"+progName) #exit print(" first working version of "+progName) print(" works for multiple occurrences of multiple entityNames") print(" but v0c doesn't create eDict{} ") print(" for replaceEntityName() yet.") print(" comprehensive error checking is not done yet.") print(" by D@CC on 2022FJun18") eDict={"count":"3","count2":"4"} if False : # testing only line = "testing=|&count+|" print("line:"+line) lineNew,isOK=replaceNextEntity(eDict,line) print("isOK:",line+":"+lineNew) exit #if end # process commandIn if True : # if False needs to read command from user. ix_commandIn="python3.py test1.ix?count=3" #if end print(ix_commandIn) ix_fileInName="test1" ix_fileOutName=ix_fileInName ix_runIn= "python3" ix_runInExt =".py" ix_fileIn=ix_fileInName+".ix" ix_fileOut=ix_fileOutName+ix_runInExt ix_entityNew="3" # open ix_fileIn and read it print(" ix_fileIn :"+ix_fileIn) ix_in = open(ix_fileIn) lines=ix_in.readlines() # open ix_fileOut print(" ix_fileOut:"+ix_fileOut) ix_out = open(ix_fileOut,"w") # process ix_fileIn cnt=0 for lineIn in lines : #print("lineIn:",strToAsc(lineIn)) isChanged=False inLen=len(lineIn) #remove trailing LF of record read. ix_lineIn=lineIn[0:inLen-1] #print("ix_lineIn",strToAsc(ix_lineIn)) ix_lineInTest=ix_lineIn+" " cnt=cnt+1 #print() #print(cnt, end="") #print("ix_lineInTest:"+ix_lineInTest) # is there an ampersand in a line without a null entity BOL #print("|"+ix_lineInTest[0:4]+"|") isToProcess=True #print("[0:2]:"+ix_lineInTest[0:2]+"|") if ix_lineInTest[0:2]=="&+" : #print("found &#, record not processed", end="") isToProcess=False #if end #print("[0:1]:"+ix_lineInTest[0:1]+"|", end="") if ix_lineInTest[0:1]=="#" : #print("found #, record not processed", end="") isToProcess=False #if end if isToProcess==True : #print("processing:"+ix_lineIn, end="") if True : #v0c lineNew,isOK=replaceNextEntity(eDict,ix_lineIn) #print("rNE returned lineNew:"+lineNew) if isOK : isChanged=True ix_lineOut = lineNew else: isChanged=False ix_lineOut = lineNew #if end #v0c calculates ix_lineOut # and isChanged #if end #if end # whether isChanged or not, ix_lineOut contains a good line # but perhaps a LF must be added to the line if isChanged==True : ix_lineOut=ix_lineOut+chr(10) # add a LF #print("ix_lineOut:"+out) #print("write ix_lineOut:",strToAsc(ix_lineOut)) ix_out.write(ix_lineOut) else: # this line was not changed out=lineIn check=lineIn+" " first3=check[0:3] #print("first3:"+first3+"|") #print("out:"+out) if first3!="&+ " : # skip entity initial value records #ix_out.write("") #print("write lineIn:",strToAsc(lineIn)) #ix_out.write(lineIn) ix_out.write(lineIn) #if end #if end #for end #print("eof") #close files ix_in.close() ix_out.close() # run the new fileOut ix_run = ix_runIn+" "+ix_fileOut print("***Running: "+ix_run+ " *********************") os.system(ix_run) print("end of "+progName)