import os def forcedList(strTest,chrSplit): #By D@CC on 2023KNov10 #Purpose: always creates a List #if impossible to split # the list will only contain 1 item #if item is null, list is empty newList="" if not strTest=="" : if strTest.find(chrSplit)<0: newList=[strTest] else: newList=strTest.split(",") #if end #if end return newList def ixp_Parse(ix_commandIn): # ix_commandIn eg="-python3 test1_py.ix?count=3,count2=4" #or #ix_commandIn eg=ix_commandIn eg="-python3 test1_py.ix # #by D@CC on 2023KNov11 #parses a single macro command # that is given to IXp to process #print("at 32 into ix_Parse ix_commandIn:",ix_commandIn) ix_runIn,parm1=ix_commandIn.split(" ",1) #print("at 34 ix_runIn,parm1:",ix_runIn,parm1) #print("at 35, ix_runIn[0:1]:"+ix_runIn[0:1]+":") if ix_runIn[0:1]=="-" : lenRunIn=len(ix_runIn) ix_runIn=ix_runIn[1:lenRunIn] #print("at 39 ix_runIn:",ix_runIn) else: pass; Doh="should never be here" print("at 42",Doh) #if end macro=parm1 if parm1.find("?")>0: # split the command at the "?" macro,parms=parm1.split("?",1) #print("at 54 macro,parms:",macro,parms) ix_file,ext_ix=macro.split("_",1) #print("at 56 ix_file,ext_ix:",ix_file,ext_ix) # ext_ix should be "py.ix" strIX,strPY=ext_ix.split(".",1) #print("strPY,strIX:",strPY,strIX) if not strIX=="ix" : print("Warning: ixParse error: macro type not .ix") #if end eDict={} #parmList must ALWAYS be a list, even a list of 1 parmList=forcedList(parms,",") #print("at 66 parmList:",parmList) for parm in parmList : #print("parm:",parm) entity,value=parm.split("=",1) eDict.update({entity:value}) #for end #print("eDict:",eDict) else: print("at 74") print("warning ixparse: there were no parms") eDict={} #eDict={"count":"3","count2":"4"} pass ; # there were no parms #if end #a=input("?") #process the macro part if macro.find("_")>-1: #split into ix_file name and the strIXPY "ix.py" ix_file,strIXPY=macro.split("_",1) #process the strIXPY #print("at 86, ix_file:"+ix_file) #print("strIXPY:"+strIXPY) if strIXPY.find(".")>-1: strIX,strPY= strIXPY.split(".",1) if not strIX=="ix" : print("Error ixParse: macro suffix is not 'ix'") exit() if not strPY=="py" : print("Error ixParse xxx: macro ext. is not 'py'") exit() ix_fileInName=ix_file ix_fileOutName=ix_file #ix_runIn="python3" ix_runInExt="py" #ix_fileInName=ix_file #ix_fileOutName=ix_file else: print("Error ixParse xxx: ix.py needs a '.'") exit() #end if else: print("Err xxx: macro name needs a '_'") exit() #if end print("at 112 before eoP, eDict:",eDict) #print("at 111, end of IX_Parse") return ix_fileInName,ix_fileOutName,ix_runIn,ix_runInExt,eDict #def end ixp_Parse def strToAsc(strIn) : #by D@CC on 2023HSep04 #Purpose: converts a string of characters into a List of ASC # uses ord(c) sList=[] # empty list for ch in strIn : sList.append(ord(ch)) #for end #print(sList) return (sList) #def end of strToAsc def replaceNextEntity(eDict,lineIn,ix_Undefined) : # v0d on 2023KNov10 untested # by D@CC on 2023HAug30 # replaces eName by eNew in line # e.g. lineNew,isError,isDiff=replaceEntity(eDict,"count",line) # returns lineNew, False, True if a replacement was made # returns line, True, True, if entity value was not found) # returns line, False, False if no replacement was necessary # ename ends with "|" #v0e # eDict is of form ["count":"cValue", etc] #print("at 85 ***entering replaceNextEntity") #print("at 86 ***lineIn:"+lineIn) #print("eDict",eDict) #find an entity to be replaced in line isError=False isSuccess=True isProcessing=True isDiff=False lineNew=lineIn while isProcessing : eName="" # default value eNew="" eB=lineNew.find("&") #print("at 164 eB:",eB) eE=lineNew.find("|",eB) #find next "|" after the first "&" #print("at 166 eE:",eE) if eB==-1 : isSuccess=False ; break if eE<=eB+2 : isSuccess=False ; break eName = lineNew[eB+1:eE] #print("at 170 eName:"+eName) #a=input("?") isSuccess=True #a replacement will be made isDiff=True if eName in eDict : eNew=eDict[eName] # look up eName in eDict #print("Value for entity:",eName," is:",eNew) else: print("Warning: No value for entity:",eName) print("Warning: Using 'ix_Undefined' value for entity:",eName) eNew=ix_Undefined # always replace an entity even if no value was found isError=True # but an error will be noted #lineNew=lineIn #if end isSuccess=True ###isProcessing=False #done if isSuccess==True : #found entity in eDict #print("eNew:"+eNew) pre=lineNew[0:eB] post=lineNew[eE+1:] lineNew=pre+eNew+post else: print("ReplNextEnt: Soft Warning: couldn't find ["+eName+"]") ###eNew="" #print("eNew:"+eNew) ###lineNew=lineIn #a=input("at else") doh="" #if end #print("lineNew:",lineNew) #while end #isSuccess=True #lineNew=line.translate(eDict) #print("lineNew:"+lineNew) #lineNew=line return lineNew,isError,isDiff #def end of eplaceNextEntity # *********************************** begin ***************** #print(strToAsc("a b")) progName="IXp_v2b.py" print("progName:"+progName) #exit #print(" for replaceEntityName() yet.") print(" by D@CC on 2023KNov10") # process commandIn ix_isUndefinedEntity=False ix_Undefined="?ix|?" cliStr="-dir|-cd |" if False : # if False needs to read command from user. ix_commandIn="python3 test1_ix.py?count=3,count2=4" #eg_commandIn="python3 test1_ix.py?count=3,count2=2" #ix_commandIn="python3 test1_ix.py?" else: isCmdNeeded=True while isCmdNeeded : ix_commandIn=input("IXp:| $") isCmdNeeded=False isDoneCmd=False #print("at 242 isDoneCmd:",isDoneCmd) ix_cmd=ix_commandIn if (not isDoneCmd) and ix_cmd=="-help": print(" first working version of "+progName) print(" works for multiple occurrences of multiple entityNames") print(" v0f does create eDict{} ") print(" v0f ignores entity value definitions in macros") print(" v1g prompts to accept an IXp command") print(" comprehensive error checking is not being done yet.") print("-help end") isDoneCmd=True isCmdNeeded=True #if end if not isDoneCmd : #print("at 258 ix_cmd[0:4]:",ix_cmd[0:4]) if ix_cmd[0:4]=="-cd ": #look up only the first word ix_cmd=ix_cmd[0:4] #print("at 262 ix_cmd[0:4]:",ix_cmd[0:4]) #if end #print("at 266 ix_cmd[0:4]:",ix_cmd[0:4]) if not ix_cmd[0:4]=="": #process the -dir or -cd command #print("at 267 ix_cmd:",ix_cmd) #print("at 270 cliStr:",cliStr) pos= cliStr.find(ix_cmd) #print("at 272 pos:",pos) if pos>-1 : #print("at 274 ix_commandIn:"+ix_commandIn+":") lenCmd=len(ix_commandIn) #print("at 276 lenCmd:",lenCmd) osCmd=ix_commandIn[1:lenCmd] #print("at 278, osCmd:"+osCmd+":") os.system(osCmd) isCmdNeeded=True isDoneCmd=True #print("at 282 isDoneCmd:",isDoneCmd) else: #print("at 284 ix_cmd not in cliStr") #a=input("!") isCmdNeeded=False isDoneCmd=False #if end #if end #if end #print("at 291 isDoneCmd:",isDoneCmd) if (not isDoneCmd) : if ix_commandIn=="": # will process "-python3 test1_ix.py?count=1,count2=2" correctly ix_commandIn="-python3 test1_ix.py?count=1,count2=2" # will process ix_commandIn="-python3 test1_ix.py" correctly print("IXp |$"+ix_commandIn) else: #print("at 301, will process this new command") Doh="!" #if end isCmdNeeded=False #We don't need another command break # out of while to process ix_commandIn else: isCmdNeeded=True print("Warning at 308 we need another command") #if end #while end #if end #print out the IX command to process #print("at 314 ix_commandIn:",ix_commandIn) #print("at 315 calling ixp_Parse()") ix_fileInName,ix_fileOutName,ix_runIn,ix_runInExt,eDict=ixp_Parse(ix_commandIn) #print("at 317 ix_runIn:",ix_runIn) #print("at 318, eDict:",eDict) ix_fileIn=ix_fileInName+"_ix.py" #print("at 321,ix_ruInExt:"+ix_runInExt+":") ix_fileOut=ix_fileOutName+"."+ix_runInExt #ix_entityNew="3" #unused # open ix_fileIn and read it #print("at 325 ix_fileIn ************** :"+ix_fileIn) ix_in = open(ix_fileIn) lines=ix_in.readlines() # open ix_fileOut #print("at 330 ix_fileOut:"+ix_fileOut) ix_out = open(ix_fileOut,"w") # process ix_fileIn #print("at 333, eDict:",eDict) cnt=0 #print("at 335 for loop to process each input record") for lineIn in lines : #print("at 337 lineIn:",strToAsc(lineIn)) #print("at 338 lineIn:"+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+" " #print("at 345 ix_lineInTest:"+ix_lineInTest+":") # perhaps it is an entity value definition cmd2 = ix_lineInTest[0:2] #print("cmd2:"+cmd2) #print("at 349, before parsing entity definition") if cmd2=="&|": #print("at 352 entity definition found") #print("must process it here") lenLineIn=len(ix_lineInTest)+2 #print("at 354, entity in ix_lineInTest:"+ix_lineInTest[3:lenLineIn]+":") colAmp= ix_lineInTest.find("&",2) #print("colAmp:",colAmp) if colAmp<0: print("Error: Bad Entity name, no &") exit() #if end colVert= ix_lineInTest.find("|",colAmp) #print("colVert:",colVert) if colVert<0: print("Error: Bad Entity name, no |") exit() #if end colEqual=ix_lineInTest.find("=",colVert) if colEqual<0: print("Error: Bad Entity name, no '='") exit() colEnd=ix_lineInTest.find(" &|",colEqual) #print("colEnd:",colEnd) if colEnd<0: print("Error: Bad Entity name, no trailing ' &|'") exit() else: #process Entity value #print("colVert:",colVert) #print("colAmp:",colAmp) entityLen=colVert-colAmp entityName= ix_lineInTest[colAmp+1:colAmp+entityLen].strip() entityLen=len(entityName) #print("entityLen:",entityLen) #print("at 387, entityName:"+entityName+":") valueLen=colEnd-colEqual valueStr=str(ix_lineInTest[colEqual+1:colEqual+valueLen]) entityValue=valueStr.strip() #print("at 391, entityValue:",entityValue,":") #print("entityName:"+entityName+":") #add to eDict #print("at 394, same eDict index is replaced") eDict.update({entityName:entityValue}) #print("at 395, after eDict.update, eDict:",eDict) isToProcess=False #it is Not a macro body line # it IS an entity value definition #if end else: pass #it must be a line in the body of the macro definition #if end cnt=cnt+1 #print("at 410 cnt:",cnt) if cnt==5 : pass print("at 413,cnt, ix_lineIn:"+str(cnt)+","+ix_lineIn+":") #if 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 isDiff=False #if end #print("[0:1]:"+ix_lineInTest[0:1]+"]", end="") if ix_lineInTest[0:1]=="#" : #print("found #, record not processed", end="") isToProcess=False isDiff=False #if end isChanged=False #print("at 434 set isChanged to False") #print("at 435 to maybe process each entity (substituting the value)") while isToProcess : if True : #v0f #print("at 438 before rNE ix_lineIn:"+ix_lineIn) if cnt==5: print("at 441, ix_lineIn:"+ix_lineIn+":") lineNew,isError,isDiff=replaceNextEntity(eDict,ix_lineIn,ix_Undefined) #print("at 455 rNE returned lineNew:"+lineNew) #print("at 456 rNE returned isError:",isError) #print("at 457 rNE returned isDiff:",isDiff) if isDiff : #process the line again #(for a possible second (or 3rd...) entity replacement) isToProcess=True ix_lineIn = lineNew #print("at 463 setting isChanged to True") isChanged=True else: #rNE made no change isToProcess=False ix_lineOut = ix_lineIn #if end #v0c calculates ix_lineOut and ixChanged #if end #print("at 472 isChanged:",isChanged) #print("at 473 still in while . . . each entity") #while end ******************************************************** #print("at 475 after end of while") # whether isChanged or not, ix_lineOut contains a non-commented line # but perhaps a LF must be added to the line #print("at 482 isChanged:",isChanged) if isChanged==True : ix_lineOut=ix_lineOut+chr(10) # add a LF #print("at 485 ix_lineOut:"+out) #print("at 486 ix_lineOut:",ix_lineOut) #print("at 487 write ix_lineOut:",ix_lineOut) ix_out.write(ix_lineOut) else: # this line was not changed #print("at 491") 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) #print(lineIn) ix_out.write(lineIn) #if end #if end #print("at 505 looping up through the for loop") #for end #print("at 507 at end of the for loop") #print("eof") print("at 509, before close, eDict:",eDict) #close files ix_in.close() ix_out.close() # run the new fileOut if not ix_isUndefinedEntity : #replace all   by " " ix_fileOut = ix_fileOut.replace(" "," ") ix_run = ix_runIn+" "+ix_fileOut #print("at 519, eDict:",eDict) print("at 520 ***Running2: "+ix_run+ " *********************") os.system(ix_run) print("end of "+progName)