import os #eValue=eDict[eName] #syntax to look up value of eName in eDict # eDict is defined at statement 42 # main program starts at 300 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 isIX_control(eDict,eName): #by D@CC on 2023KNov29 #purpose: look up eName in eDict, return result result=False if not eName[0:3]=="ix_": print("IX error,IX_Ctrl[0:3] is not ix_"); exit else: print("at 29 IX test,eName:",eName) #result=True #if end eValue=eDict[eName] #look up value of eName in eDict # a validList delimiter is always "|" validList="|False|,|True|" if not validList.find("|"+eValue+"|"): #*****|***** print("at 36 invalid IX_control value in eDict for:",eName); exit #if end if eValue=="False": result=False if eValue=="True" : result=True return result def loadControlFile(file): #parm1 is Control File name #returns eDict dictionary file="ix_eDict.txt" # should be passed as parm1 #Control File record format is ix_any=value" dict={} #10 print("count3:"+str(&count3|)+":")#count3 is an undefined int #11 print("count3:",&count3|,":") #count3 is an undefined str #12 print("count3:"+&count3|+":") #count3 is an undefined str #print("count3:",&count3|,":") #count3 is an undefined str #dict.update({"ix_Undefined":""}) #null works 10 not 11 error 12 #dict.update({"ix_Undefined":'""'}) #'null' works 10 & 11 & 12 dict.update( {"ix_Undefined":'"0"'}) #works both 10 & 11 & 12 **BEST** #dict.update( {"ix_Undefined":0}) #works both 10 & 11 error 12 #dict.update( {"ix_Undefined":'"a"'}) #works both 10 & 11 & 12 #Conclusion: ix_Undefined must be in double quotes within single quotes return dict #def end def ixp_Parse(ix_commandIn,eDict): # 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 2023KNov29 #parses a single macro command # that is given to IXp to process #print("at 75 into ix_Parse ix_commandIn:",ix_commandIn) ix_runIn,parm1=ix_commandIn.split(" ",1) #print("at 77 ix_runIn,parm1:",ix_runIn,parm1) #print("at 78, 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 82 ix_runIn:",ix_runIn) else: pass; Doh="should never be here" print("at 85",Doh) #if end macro=parm1 #eDict = loadControlFile("ix_eDict.txt") #load eDict dictionary #print("at 89 after loading, eDict:",eDict) if parm1.find("?")>0: # split the command at the "?" macro,parms=parm1.split("?",1) #print("at 104 macro,parms:",macro,parms) ix_file,ext_ix=macro.split("_",1) #print("at 106 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("at 111 Warning: ixParse error: macro type not .ix") #if end #parmList must ALWAYS be a list, even a list of 1 parmList=forcedList(parms,",") #print("at 115 parmList:",parmList) for parm in parmList : #print("parm:",parm) entity,value=parm.split("=",1) eDict.update({entity:value}) #for end print("at 121, from parmList, eDict:",eDict) else: print("at 125, warning ixparse: there were no parms added") #eDict={"count":"3","count2":"4"} pass ; # there were no parms added #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 136, ix_file:"+ix_file) #print("strIXPY:"+strIXPY) if strIXPY.find(".")>-1: strIX,strPY= strIXPY.split(".",1) if not strIX=="ix" : print("Error at 141 ixParse: macro suffix is not 'ix'") exit() if not strPY=="py" : print("Error at 144 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 at 154 ixParse xxx: ix.py needs a '.'") exit() #end if else: print("at 158 Err xxx: macro name needs a '_'") exit() #if end #print("at 163 before eoP, eDict:",eDict) #print("at 164, end of IX_Parse") return ix_fileInName,ix_fileOutName,ix_runIn,ix_runInExt #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 isTypeStr(vName): # there is a better version of this somewhere on my computer Ttype=type(vName) #print("at 196 type("+"entity"+")"+Ttype) if Ttype=="": return True else: return False #def end def replaceNextEntity(eDict,lineIn,ix_Undefined) : # v2d on 2023KNov29 untested # by D@CC on 2023KNov29 # 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 215 ***entering replaceNextEntity") #print("at 216 ***lineIn:"+lineIn) # if a statement has an undefined entity, append the entity name #print("eDict",eDict) #print("at 220, lineIn:"+lineIn) #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 232 eB:",eB) eE=lineNew.find("|",eB) #find next "|" after the first "&" #print("at 234 eE:",eE) if eB==-1 : isSuccess=False ; break if eE<=eB+2 : isSuccess=False ; break eName = lineNew[eB+1:eE] #print("at 238 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) append="" else: print("at 247 Warning: No value for entity:",eName) #print("at 248 Warning: Using 'ix_Undefined' value for entity:",eName) #print('at 249 Warning: Using "0" value for entity:',eName) #eNew=ix_Undefined # always replace an entity even if no value was found #eNew="0" # always replace an entity even if no value was found eNew=eDict["ix_Undefined"] # always replace an entity even if no value was found #print("at 253 undefined eNew:",eNew,":") append = " # "+eName+" undefined" 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:] isStr = isTypeStr(eNew) if not isStr : useStr=str(eNew) else: useStr=eNew #if end lineNew=pre+useStr+post+append else: print("ReplNextEnt: Soft Warning: couldn't find ["+eName+"]") #print("at 274 eNew:"+eNew) ###lineNew=lineIn #a=input("at else") doh="" #if end #while end #isSuccess=True #lineNew=line.translate(eDict) #print("at 282 lineNew:"+lineNew) #lineNew=line return lineNew,isError,isDiff #def end of eplaceNextEntity # *********************************** begin ***************** #print(strToAsc("a b")) progName="IXp_v2d.py" print("progName:"+progName) #exit #print(" for replaceEntityName() yet.") print(" by D@CC on 2023KNov29") ##################################################################### # process commandIn #print("at 301, defining isUndefinedEntity") isAnyUndefinedEntity=False eDict = loadControlFile("ix_eDict.txt") #load eDict dictionary print("at 304 after loading, eDict:",eDict) # look up ix_Undefined ix_Undefined=eDict["ix_Undefined"] #***** get from eDict #print("at 308 var ix_Undefined:",ix_Undefined) 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 292 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 348 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 352 ix_cmd[0:4]:",ix_cmd[0:4]) #if end #print("at 358 ix_cmd[0:4]:",ix_cmd[0:4]) if not ix_cmd[0:4]=="": #process the -dir or -cd command #print("at 361 ix_cmd:",ix_cmd) #print("at 362 cliStr:",cliStr) pos= cliStr.find(ix_cmd) #print("at 364 pos:",pos) if pos>-1 : #print("at 366 ix_commandIn:"+ix_commandIn+":") lenCmd=len(ix_commandIn) #print("at 368 lenCmd:",lenCmd) osCmd=ix_commandIn[1:lenCmd] #print("at 370, osCmd:"+osCmd+":") os.system(osCmd) isCmdNeeded=True isDoneCmd=True #print("at 374 isDoneCmd:",isDoneCmd) else: #print("at 376 ix_cmd not in cliStr") #a=input("!") isCmdNeeded=False isDoneCmd=False #if end #if end #if end #print("at 361 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 399, 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 406 we need another command") #if end #while end #if end #print out the IX command to process #print("at 411 ix_commandIn:",ix_commandIn) #print("at 412 calling ixp_Parse()") ix_fileInName,ix_fileOutName,ix_runIn,ix_runInExt=ixp_Parse(ix_commandIn,eDict) #print("at 415 ix_runIn:",ix_runIn) #print("at 416, after parse CLI, eDict:",eDict) ix_fileIn=ix_fileInName+"_ix.py" #print("at 418,ix_ruInExt:"+ix_runInExt+":") ix_fileOut=ix_fileOutName+"."+ix_runInExt #ix_entityNew="3" #unused # open ix_fileIn and read it #print("at 422 ix_fileIn ************** :"+ix_fileIn) ix_in = open(ix_fileIn) lines=ix_in.readlines() # open ix_fileOut #print("at 427 ix_fileOut:"+ix_fileOut) ix_out = open(ix_fileOut,"w") # process ix_fileIn #print("at 430, eDict:",eDict) cnt=0 #print("at 435 for loop to process each input record") for lineIn in lines : #print("at 437 lineIn:",strToAsc(lineIn)) #print("at 438 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 445 ix_lineInTest:"+ix_lineInTest+":") # perhaps it is an entity value definition cmd2 = ix_lineInTest[0:2] #print("cmd2:"+cmd2) #print("at 449, before parsing entity definition") if cmd2=="&|": #print("at 453 entity definition found") #print("must process it here") lenLineIn=len(ix_lineInTest)+2 #print("at 456, 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("at 476 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 487, entityName:"+entityName+":") valueLen=colEnd-colEqual valueStr=str(ix_lineInTest[colEqual+1:colEqual+valueLen]) entityValue=valueStr.strip() #print("at 491, entityValue:",entityValue,":") #print("entityName:"+entityName+":") #add to eDict #print("at 494, same eDict index is replaced") eDict.update({entityName:entityValue}) #print("at 496, 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 510 cnt:",cnt) if cnt==10 or cnt==11: pass #print("at 513,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 534 set isChanged to False") #print("at 535 to maybe process each entity (substituting the value)") while isToProcess : if True : #v0f #print("at 538 before rNE ix_lineIn:"+ix_lineIn) #print("at 539 before rNE cnt:",cnt) if cnt==5: print("at 541, ix_lineIn:"+ix_lineIn+":") #if end #print("at 543, ix_lineIn:"+ix_lineIn+":") lineNew,isError,isDiff=replaceNextEntity(eDict,ix_lineIn,ix_Undefined) #print("at 546 after rNE cnt , isDiff:",cnt,isDiff) #print("at 555 rNE returned lineNew:"+lineNew) #print("at 556 rNE returned isError:",isError) #print("at 557 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 563 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 572 isChanged:",isChanged) #print("at 573 still in while . . . each entity") #while end ******************************************************** #print("at 575 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 588 isChanged:",isChanged) if isChanged==True : ix_lineOut=ix_lineOut+chr(10) # add a LF #print("at 592 ix_lineOut:",ix_lineOut) #print("at 593 write ix_lineOut:",ix_lineOut) ix_out.write(ix_lineOut) else: # this line was not changed #print("at 597") 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 611 looping up through the for loop") #for end #print("at 613 at end of the for loop") #print("eof") #print("at 615, before close, eDict:",eDict) #close files ix_in.close() ix_out.close() # run the new fileOut if not isAnyUndefinedEntity : #replace all   by " " ix_fileOut = ix_fileOut.replace(" "," ") ix_run = ix_runIn+" "+ix_fileOut #print("at 634, eDict:",eDict) print("at 635 ***Running2: "+ix_run+ " *********************") print() os.system(ix_run) print("end of "+progName) #/IXp_v2d.py