#ix_pkg.py stored in . . /Desktop/IX_assets/ix # by D@CC on 2023KNov04 #tested with Script_Test_preParse_v00.py #tested with Test_readCD2.py #::::::::::textpak=>getSub.py def getSub(s1,s2,s3): import sys sys.path.append("/home/pi/Desktop/IX_assets/ix") from ix_pkg import getSub_v00 #By D@CC #On: 2023LDec01 #print("6 invoking getSub_v00()") return getSub_v00(s1,s2,s3) #def end getSub(s1,s2,s3) #/getSub.py #::::::::::textpak=>getSub_v00.py #getSub_v00.py def getSub_v00(parm1,q1,q2): #By: D@CC #On: 2023LDec01 #Purpose: get subString BETWEEN columns q1 & q2 #status: works # get sub string from q1 to q2 sub=parm1[q1+1:q2] #print("6 sub:"+sub+":") return sub #def end #/getSub_v00.py #::::::::::textpak=>initCD2.py def initCD2(PPin): import RPi.GPIO as GPIO #By D@CC on 2023KNov01 #PPin-2 is PPout (tested with PPin = 40) GPIO.setmode(GPIO.BOARD) #physical Board numbers # GPIO.BCM BroadCoM numbering eg GPIO #s isBadPin= ((PPin-2==1) or (PPin-2==17)) # they are 3v3 # Ground pins won't work either, nor 5v0 pins if not isBadPin: GPIO.setup(PPin-2,GPIO.OUT) print("turn On phys pin:", PPin-2) GPIO.output(PPin-2, True) #if end GPIO.setup(PPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # on a Raspberry Pi, PullUpDown is a 10k resistor # a photoresistor photocell (CD) when lit is 100 ohms. # when dark is >10k0 ohms. return #def end initCD2(PPin) #::::::::::textpak=>ix_grep.py def ix_grep(s1,s2,s3,s4,s5): #by D@CC #on 2023LDec03 #Purpose to call ix_grep_v00 from ix_grep_v00 import ix_grep_v00 return ix_grep_v00(s1,s2,s3,s4,s5) #def end ix_grep #::::::::::textpak=>ix_grep_v00.py #ix_grep_v00.py def ix_grep_v00(targetStr,file,splitStr,stripStr,commStr): #by D@CC #on 2023LDec03 #status: works well #parm1: targetStr, string to find in file #parm2 file, fileName eg ix_eDict.txt # file="/home/pi/IX_assets/ix/ix_eDict.txt" #parm3 splitStr, "=" #parm4 stripStr eg '"' #parm5 comment character eg "#" #returns: last result #Purpose: grep-like targetStr in file # ignore comments (begin with "#" selectable) # split on splitStr eg "=" unless null # strip unprintables # if any '"' # strip '"' beg and end '"' # Note: if splitStr="", it works like grep # except only returns the last record found # except optionally skips all comment recs # Note: if targetStr is null, the file is loaded into a list # one record per item in the list # comment records are ignored # D@CC uses this mode # result=ix_grep(parm1,file,"=",'"',"#")#one rec containing 1 value, skip # #print("30 targetStr:"+targetStr) if targetStr=="": # print("creatng list") result=[] #making a list in result f=open(file,"r") for rec in f: rec=rec.strip("\n") #print("read rec:"+rec) if not rec[0:1]==commStr: result.append(rec) #of emd #result.append("record ",cnt) #for end return result else: pass #process this target string #if end #print("48 targetStr:"+targetStr) cnt=0 result="none" for rec in open("ix_eDict.txt"): #result="none" cnt+=1 #print("at 55 rec:"+rec) if not rec[0:1]==commStr: #if cnt==7: if True: if splitStr=="": #print("at 40 splitStr is null") colT=rec.find(targetStr) if colT>-1: #print("at 63, returning whole rec (like greb)") result=rec #if end else: #splitStr is not null #print("at 67 rec:"+rec) rec=rec.strip("\n") #print("at 50 splitStr:"+splitStr) colTarget=rec.find(splitStr) #print("at 72 colTarget:",colTarget) if colTarget>-1: #print("at 75 colTarget:",colTarget) colSplit= rec.find(splitStr) #print("at 77 colSplit:",colSplit) if colSplit>-1: target,strValue=rec.split(splitStr) #print("at 80 target:"+target+":") #print("at 81 targetStr:"+targetStr+":") if target==targetStr : #print("at 83 strValue:"+strValue+":") strValue=strValue.strip() # strips spaces value=strValue.strip(stripStr) # strips '"' #print("at 86 target:"+target+":") #print("at 87 cnt,value:",cnt,value+":") result=value #print("at 89 result:"+result+":") return result #if end #if end #if end #if end #if end #if end #for end #print("at 98 result:"+result) return result #def end ix_grep_v00() #/#ix_grep_v00.py #::::::::::textpak=>ixTestB.py def ixTestB(s): #ixTestB.py return s #def end ixTestB.py #::::::::::textpak=>ixTestC.py def ixTestC(s): #ixTestC.py return s #def end ixTestC.py #::::::::::textpak=>preParse.py #preParse.py def preParse(s): import sys sys.path.append("/home/pi/Desktop/IX_assets/ix") from ix_pkg import preParse_v00 #By D@cc #On 2023LDec01 #print("5 invoking preParse_v00") return preParse_v00(s) #return s #def end #/preparse.py #::::::::::textpak=>preParse_v00.py #preParse_v00.py def preParse_v00(parm): import sys sys.path.append("/home/pi/Desktop/IX_assets/ix") from ix_pkg import getSub from ix_pkg import replSub #By D@CC #On 2023LDec01 #Purpose: to find pairs of ' or " # and replace each space (or spaces) in # each pair with &20 #parm1: cli str to preParse #Eg: any"any any"any"any any"any #Returns:any"any&20any"any"any&20any"any #Status: no fails by Test_preParse() #TestFile: Test_preParse_File.txt #print("at 35 parm1:"+parm1) parm1=parm #so we can change parm1 result=parm1 isAnyDQ=True if parm1.find('"')<0: isAnyDQ=False isAnySQ=True if parm1.find("'")<0: isAnySQ=False if isAnyDQ and isAnySQ: return parm1 if (not isAnyDQ) and (not isAnySQ): return parm1 if isAnyDQ or isAnySQ: for qStr in ('"',"'"): # search for both DQ and SQ q=qStr qCol= 0 cntQ =-1 #print("at 49 parm1:"+parm1) while qCol>-1 : #find next col containing q qCol =parm1.find(q,qCol+1) #count it (them) cntQ+=1 #print("55 qCol:",qCol) #while end #print("cntQ:",cntQ) modQ=cntQ%2 #print("modQ:",modQ) if modQ==1 : print("p 61: Warning:found odd number of ' or " +'"') return parm1 else: #rescan to process each pair of q's qCol=0 cntQ=0 cntPair=1 while qCol>-1: #print("p 67 check parm1:"+parm1) qCol =parm1.find(q,qCol+1) if qCol==-1: #print("next qCol:",qCol) break #done else: #print("next qCol:",qCol) pass #if end cntQ+=1 #print("77 before test cntQ,2*cntPair:",cntQ,2*cntPair) if cntQ==2*cntPair: #print("78 check need to process Pair:",cntPair) #process this pair #q1=qCol q2=qCol #print("84 q1,q2:",q1,q2) oldSub=getSub(parm1,q1,q2) # if oldSub needs spaces replaced if oldSub.find(" ")>-1: #print("p 88 replacing spaces between pair") newSub=oldSub.replace(" ","&20") parm1=replSub(parm1,q1,q2,newSub) #print("p 91 new parm1:"+parm1) #subStr=subStr.replace(" ","&20") #parm1 has changed, so restart qCol=0 cntQ=0 cntPair=1 #print("qCol:",qCol) else: #print("p 98 process next pair") cntPair+=1 #if end else: q1=qCol #if end #while end #for end result=parm1 #if end #if end return result #def end #/preParse_v00.py #::::::::::textpak=>readCD2.py def readCD2(PPin): import RPi.GPIO as GPIO #By D@CC on 2023KNov01 #Purpose: to return True if both CDs are covered #HW: my device "CD2_Dongle(c)" #Tested with PPin=40 reading=GPIO.input(PPin) # reading is True if the CD photoCells are Dark # Make them dark by covering them with a finger if reading: state=False else: state=True #if end return state #def end readCD2(PPin) #::::::::::textpak=>replSub.py #replSub.py def replSub(s1,s2,s3,s4): import sys sys.path.append("/home/pi/Desktop/IX_assets/ix") from ix_pkg import replSub_v00 #by $D@CC #on 2023LDec01 #print("calling replSub_v00()") return replSub_v00(s1,s2,s3,s4) #def end #/replSub.py #::::::::::textpak=>replSub_v00.py #replSub_v00.py def replSub_v00(parm1,q1,q2,newSub): #By: D@CC #On: 2023LDec01 #Purpose: # sister func to getSub() # insert newSub BETWEEN cols q1 & q2 #q1,q2 are col numbers of a pair of '"' # replace sub string between q1 to q2 #pre = before sub pre=parm1[0:q1+1] #print("11 pre:"+pre) #post = after sub lenParm1=len(parm1) #print("lenParm1:",lenParm1) post=parm1[q2:lenParm1] #print("14 post:"+post) sub=parm1[q1+1:q2] #print("18 sub:"+sub) newSub=pre+newSub+post return newSub #def end #/replSub_v00.py #::::::::::textpak=>requires.py def requires(inStr,v=""): #was requires_v01 #by D@CC on 2023KNov04 # IX Software Family component #Purpose: tells prepIX how to deal with # required functions #Parm 1 "func(s,'v=1')" #Parm 2 tells prepIX what to do # v="" create a function pair # from ix_pkg.py import func # from ix_pkg.py import func_v01 # v="import" just import normally # from func import func # v="?" returns version # of requires # v=0,1,2 future isValidv=False if v=="?": isValidv=True pass ; ix_v=0 return ix_v #if end if v=="": isValidv=True if v=="import": isValidv=True if not isValidv: print("Error in IX: retrieve, Bad v parametr:",v) #if end print("## requires("+inStr+") in future.") ############################# return inStr #normal exit #def end requires.py #::::::::::textpak=>requires_v00.py #requires.py def requires_v00(funcS1): #By D@CC #On: 2023LDec01 print("fut. requires("+funcS1+")") return funcS1 #def end requires_v00(funcS1) #/requires_v00.py #::::::::::textpak=>requires_v01.py def requires_v01(inStr,v=""): #by D@CC on 2023KNov04 # IX Software Family component #Purpose: tells prepIX how to deal with # required functions #Parm 1 "func(s,'v=1')" #Parm 2 tells prepIX what to do # v="" create a function pair # from ix_pkg.py import func # from ix_pkg.py import func_v01 # v="import" just import normally # from func import func # v="?" returns version # of requires # v=0,1,2 future isValidv=False if v=="?": isValidv=True pass ; ix_v=0 return ix_v #if end if v=="": isValidv=True if v=="import": isValidv=True if not isValidv: print("Error in IX: retrieve, Bad v parametr:",v) #if end return inStr #normal exit #def end requires_v01.py #/pkg end ix_pkg.py stored in . . /Desktop/IX_assets/ix