import os def forcedList(strTest,chrSplit): #By D@CC on 2023HSep04 mod 2024CMar29 #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" #by D@CC on 2023HSep04 #parses a single macro command #print("at 21 into ix_Parse ix_commandIn:",ix_commandIn) ix_runIn,parm1=ix_commandIn.split(" ",1) # type in "-python3 test1_ix.py?count=10" #no preceding space #print("at 24 ix_runIn,parm1:",ix_runIn,parm1) #print("at 25, ix_runIn[0:1]:"+ix_runIn[0:1]+":") if ix_runIn[0:1]=="-" : lenRunIn=len(ix_runIn) ix_runIn=ix_runIn[1:lenRunIn] else: Doh="should never be here" print("at 31",Doh) #if end macro,parms=parm1.split("?",1) #print("at 34 macro,parms:",macro,parms) ix_file,ext_ix=macro.split("_",1) #print("at 36 ix_file,ext_ix:",ix_file,ext_ix) # ext_ix should be "py.ix" strPY,strIX=ext_ix.split(".",1) #print("@39 strPY,strIX:",strPY,strIX) if not strIX=="ix" : print("@41 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 46 parmList:",parmList) for parm in parmList : #print("parm:",parm) entity,value=parm.split("=",1) eDict.update({entity:value}) #for end #print("eDict:",eDict) #a=input("?") #ix_file,strIX=macro.split(".",1) #if not ix_ext=="ix" :#print("Error ixParse: macro ext is not 'ix'") ix_fileInName=ix_file ix_fileOutName=ix_file #ix_runIn="python3" ix_runInExt="py" #eDict={"count":"3","count2":"4"} ix_fileInName=ix_file ix_fileOutName=ix_file return ix_fileInName,ix_fileOutName,ix_runIn,ix_runInExt,eDict 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 def replaceNextEntity(eDict,lineIn,ix_Undefined) : # v0d on 2023HAug30 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 103 eB:",eB) eE=lineNew.find("|",eB) #find next "|" after the first "&" #print("at 105 eE:",eE) if eB==-1 : isSuccess=False ; break if eE<=eB+2 : isSuccess=False ; break eName = lineNew[eB+1:eE] #print("at 109 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("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 # *********************************** begin ***************** #print(strToAsc("a b")) progName="IXp_v1i.py" print("progName:"+progName) #exit #print(" for replaceEntityName() yet.") print(" by D@CC on 2023ISep05") #print("@156 inFile: *_py.ix, outFile: *.py") print("enter:-python3 test1_py.ix?count=10") # 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_py.ix?count=3,count2=4" eg_commandIn="python3 test1_py.ix?count=3,count2=2" #ix_commandIn="python3 test1_py.ix?" else: isCmdNeeded=True while isCmdNeeded : ix_commandIn=input("IXp: |$") isCmdNeeded=False isDoneCmd=False #print("at 173 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 187 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 191 ix_cmd[0:4]:",ix_cmd[0:4]) #if end #print("at 193 ix_cmd[0:4]:",ix_cmd[0:4]) if not ix_cmd[0:4]=="": #process the -dir or -cd command #print("at 194 ix_cmd:",ix_cmd) #print("at 195 cliStr:",cliStr) pos= cliStr.find(ix_cmd) #print("at 201 pos:",pos) if pos>-1 : #print("at 203 ix_commandIn:"+ix_commandIn+":") lenCmd=len(ix_commandIn) #print("at 205 lenCmd:",lenCmd) osCmd=ix_commandIn[1:lenCmd] #print("at 207, osCmd:"+osCmd+":") os.system(osCmd) isCmdNeeded=True isDoneCmd=True #print("at 211 isDoneCmd:",isDoneCmd) else: #print("at 213 ix_cmd not in cliStr") #a=input("!") isCmdNeeded=False isDoneCmd=False #if end #if end #if end #print("at 220 isDoneCmd:",isDoneCmd) if (not isDoneCmd) : if ix_commandIn=="": ix_commandIn="-python3 test1_py.ix?count=1,count2=2" print("@224 IXp |$"+ix_commandIn) else: #print("at 226, 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 233 we need another command") #if end #while end #if end #print out the IX command to process #print("at 238 ix_commandIn:",ix_commandIn) #print("at 239 begining ixp_Parse()") ix_fileInName,ix_fileOutName,ix_runIn,ix_runInExt,eDict=ixp_Parse(ix_commandIn) #print("at 241 ix_fileInName:",ix_fileInName) #test1 #print("at 242 ix_runIn:",ix_runIn) #print("@243, eDict:",eDict) ix_fileIn=ix_fileInName+"_py.ix" ix_fileOut=ix_fileOutName+"."+ix_runInExt #print("@246 ix_fileOut:"+ix_fileOut) #ix_entityNew="3" #unused # open ix_fileIn and read it #print("at 250 ix_fileIn ************** :"+ix_fileIn) ix_in = open(ix_fileIn) lines=ix_in.readlines() # open ix_fileOut #print("at 255 ix_fileOut:"+ix_fileOut) ix_out = open(ix_fileOut,"w") # process ix_fileIn cnt=0 #print("at 262 for loop to process each input record") for lineIn in lines : #print("at 264 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("at 272 cnt:",cnt) #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 290 set isChanged to False") #print("at 291 to maybe process each entity (substituting the value)") while isToProcess : if True : #v0f #print("at 294 before rNE ix_lineIn:"+ix_lineIn) lineNew,isError,isDiff=replaceNextEntity(eDict,ix_lineIn,ix_Undefined) #print("at 296 rNE returned lineNew:"+lineNew) #print("at 297 rNE returned isError:",isError) #print("at 298 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 304 setting isChanged to True") isChanged=True else: #rNE made no change isToProcess=False ix_lineOut = ix_lineIn #if end #v1i calculates ix_lineOut and ixChanged #if end #print("at 313 isChanged:",isChanged) #print("at 314 still in while . . . each entity") #while end ******************************************************** #print("at 316 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 320 isChanged:",isChanged) if isChanged==True : ix_lineOut=ix_lineOut+chr(10) # add a LF #print("at 323 ix_lineOut:"+out) #print("at 324 ix_lineOut:",ix_lineOut) #print("at 325 write ix_lineOut:",ix_lineOut) #print(ix_lineOut) ix_out.write(ix_lineOut) else: # this line was not changed #print("at 330") out=lineIn check=lineIn+" " first3=check[0:3] #print("@334 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 344 looping up through the for loop") #for end #print("at 346 at end of the for loop") #print("eof") #close files ix_in.close() ix_out.close() # run the new fileOut if not ix_isUndefinedEntity : ix_run = ix_runIn+" "+ix_fileOut print("@355, eDict:",eDict) print("@356 ***Running2: "+ix_run+ " *********************") os.system(ix_run) print("end of "+progName)