def packFromSerialFile(pkgInName,moduleName,fileName,isHashTotal): #By: d@CC #On: 2024CMar23 #parm0 pkgName pkg to receive new module #parm1 moduleName module name to add #parm2 fileName contents of module #parm3 isHashTotal if True, adds HashTotal #NB: This very important python function # is accompanied by many checks & conditions # in an effort to prevent bad files from # being created , but throwing a minimum # number of errors. # #Purpose: To create a module in a package from # a serial file. A full path is expected to # be included in the file name, if not # the file will be written to the current # folder. An invalid module name (eg null) # will throw an error. The Serial file will # have records, each terminated by a line feed. # A single line feed ("\n") should follow # the last records. If not, one will be added. # The package name must be valid. If no # package exists, one will be created. A # full pather is expected to be included in the # package name. If not, the package will be # written to the current folder. # The IX marker will be used. Nothng will # precede the first module in the package. # If the package is a python function definition, # no lines should precede the statement # containing the " def" statement. # The module will be added to the end of # the package, not the beginning. # If the module already exists in the package # an error will be thrown. If isHashTotal # is True, an MD5 hash total will be in the # first line of the module. This is the only # case of text being written before a # python " def" statement. When a module # is retrieved, the hash total will be removed # before the module is retreived. # If duplicate modules exist in the package, # the package will not be rejected, as no # errors will be thrown. # Some modules in the package may have hash # totals while others may not. # Multiple line feeds will not be written to # package because each line feed will be # considered as a separate records. Multiple # python definitions are permitted in a # module but this is not recommended. # print("BEWARE: w/o any error checking.") print(" Excludes Hash Totals.") marker="#"+":"*10+"textpak=>" newPkgName="new"+pkgInName print("marker:"+marker) pkgIn=open(pkgInName,"r") pkgOut=open(newPkgName,"w") f=open(fileName,"r") for line in pkgIn: pkgOut.write(line) #line in already has \n #for end #make marker newMarker=marker+moduleName pkgOut.write(newMarker+"\n") #needs \n lineCnt=0 for lineIn in f: lineCnt+=1 pkgOut.write(lineIn) #line in already has \n #for end f.close() pkgOut.close() print("Added module named:"+moduleName) print("To new package named:"+newPkgName) print("New module linecount:",lineCnt) print("No changes to old package named:"+pkgInName) print("done") return #def end def packFromSerialFile_test(): #by D@CC #on 2024CMar23 #Status: tested ok w/o error checking print("begin: packFromSerialFile_test") pkgName="testPkg.py" fP=open(pkgName,"w") #new file fP.write("::::::::["+pkgName+"]"+"\n") fP.write("class by D@CC 2024CMar23"+"\n") fP.close() moduleName="newModule" fileName="fileWith2Lines.txt" fN=open(fileName,"w") fN.write("line 1"+"\n") fN.write("last line of module to add"+"\n") fN.close() isHashTotal=False packFromSerialFile(pkgName,moduleName,fileName,isHashTotal) print("after calling packFromSerialFile()") fIn=open("new"+pkgName,"r") print(":", end=' ') for line in fIn: print("line:"+line+":", end=' ') #line in already has \n #for end print("end of "+"new"+pkgName) fIn.close() print("end of program") return #def end if __name__ == "__main__" : packFromSerialFile_test() #/packFromSerialFile.py.txt