Traversal Issue (brain fart)
Hi all, I have a python script that reads a file in from LocalFS on the hard drive. I am parsing it and creating objects in a tree structure based on the parsed data. I have found various examples that have gotten me this far. I cannot seem to get a grip on how to cleanly move around the ZODB in Zope to create the Folders, Sub Folders, and then add properties to specific folders. At the last step in my program which adds a final set of folders and will need to add properties to them, I seem to completely loose the "Context" of the current folder. Any pointers or examples to clarify how to programmatically specify where my objects are created would be much appreciated. I am sure it is much simpler than I have made it. Thanks, Richard ======= PYTHON SCRIPT CODE SNIPPET =========== import string #01 READS SOURCE FILE INTO MEMORY file='IN_02.NEW' lfs=context.archive f=lfs[file] data=f.data line=string.replace(data,'\r','') lines=line.split('\n') #02 CREATES INBOUND FOLDER IF NEEDED. folderid='INbound' foldertitle='Inbound Documents' try: container.manage_addProduct['PropertyFolder'].manage_addPropertyFolder(folderid, foldertitle,'') print 'Inbox Folder '+folderid+' Created' except: print 'Inbox Folder '+folderid+' Already Exists' INbound=container.INbound # sets current folder #03 DECLARING VARIABLES JUST TO KEEP THEM. fields=0 # flag=0 # ISAindex=0 # TPid=0 # TPid=str(string.replace(fields[2],' ','')) OURsrid=0 # sr id sent to DATErec=0 # Date doc was received TIMErec=0 # Time doc was received DOCver=0 # EDI Document Version DOCtitle=0 # EDI Document Title TPdocs=0 # sets current folder DOCindex=0 # index of document received DOCtype=0 # doctype received DOCid=0 # sets current document id DOClocation=0 # sets current folder location SEGid=0 # sets current segment id SEGnum=0 # sets current segment number #04 BEGINS DATA LINE AND SEGMENT LOOPING for line in lines: fields=line.split('*') # Splits the fields on the '*' character #05 COPIES CURRENT ISA HEADER DETAILS TO VARIABLES if fields[0]=='ISA': flag=fields[15] # Production or Testing Flag (P or T) ISAindex=str(string.replace(fields[13],'0','')) # ISA index #06 COPIES CURRENT GS HEADER DETAILS TO VARIABLES # AND CREATES CURRENT TRADING PARTNER FOLDER IF NEEDED if fields[0]=='GS': TPid=str(fields[2]) # TPid=str(string.replace(fields[2],' ','')) OURsrid=str(fields[3]) # sr id sent to DATErec=str(fields[5]) # Date doc was received TIMErec=str(fields[6]) # Time doc was received DOCver=str(fields[8]) # EDI Document Version DOCtitle=str(fields[1]) # EDI Document Title try: INbound.manage_addProduct['PropertyFolder'].manage_addPropertyFolder(TPid,OURsrid,'') print TPid+' '+OURsrid+' Created' except: print TPid+' '+OURsrid+' Already Exists' #07 CREATES CURRENT TRADING PARTNER'S DOCUMENT FOLDER if fields[0]=='ST': TPdocs=INbound[TPid] # sets current folder DOCindex=str(string.replace(fields[2],'0','')) # index of document received DOCtype=str(fields[1]) # doctype received try: TPdocs.manage_addProduct['PropertyFolder'].manage_addPropertyFolder(DOCtype,DOCtitle,'') print DOCtype+' '+DOCtitle+' Created' except: print DOCtype+' '+DOCtitle+' Already Exists' #08 CREATES DOCUMENTS IN CURRENT TRADING PARTNER'S DOCUMENT FOLDER if fields[0]=='ST': DOCid=DOCtype+'-'+DOCindex DOClocation = TPdocs[DOCtype] # sets current folder try: DOClocation.manage_addProduct['PropertyFolder'].manage_addPropertyFolder(DOCid,DOCtitle,'') print DOCid+' '+DOCtitle+' Created' except: print DOCid+' '+DOCtitle+' Already Exists' 09 CREATE DOCUMENT SEGMENTS # This is the goofy section that fails. SEGlocation=context.DOCid # sets current folder SEGid=str(string.zfill(SEGnum,4))+'-'+fields[0] SEGlocation.manage_addProduct['PropertyFolder'].manage_addPropertyFolder(SEGid,line,'') SEGpre=SEGpre+1 print '======================' print TPid print TPdocs print DOCid print SEGid print fields print flag print ISAindex print OURsrid print DATErec print TIMErec print DOCver print DOCtitle print DOCindex print DOCtype print DOClocation print SEGnum print '======================' return printed
On Wed, Sep 17, 2003 at 12:08:41PM -0400, Richard Shebora wrote: (snip)
#02 CREATES INBOUND FOLDER IF NEEDED. folderid='INbound' foldertitle='Inbound Documents' try:
container.manage_addProduct['PropertyFolder'].manage_addPropertyFolder(folderid, foldertitle,'')
hmm, the indentation is screwy. I assume that is just an email cut/paste problem, or this script would never run at all.
print 'Inbox Folder '+folderid+' Created' except: print 'Inbox Folder '+folderid+' Already Exists'
Note that this is very risky. YOu should find out what error happens when the folder already exists, and catch only that error. If anything else goes wrong you will hide it behind a misleading "Already exists" message. (snip)
#08 CREATES DOCUMENTS IN CURRENT TRADING PARTNER'S DOCUMENT FOLDER if fields[0]=='ST': DOCid=DOCtype+'-'+DOCindex DOClocation = TPdocs[DOCtype] # sets current folder try:
DOClocation.manage_addProduct['PropertyFolder'].manage_addPropertyFolder(DOCid,DOCtitle,'') print DOCid+' '+DOCtitle+' Created' except: print DOCid+' '+DOCtitle+' Already Exists'
09 CREATE DOCUMENT SEGMENTS # This is the goofy section that fails. SEGlocation=context.DOCid # sets current folder
Look at that last line. DOCid is a variable. You are asking context for something literally named "DOCid", when I think you want to ask for something whose name is the value of DOCid. Be sure you understand the difference. You already know one way to do this correctly in zope, since you do it numerous times elsewhere in the script ;-) Also I doubt that context is where you want to look. Since you just created the DOCid-named item in DOClocation, maybe that's where you should look for it. -- Paul Winkler http://www.slinkp.com Look! Up in the sky! It's HATEFUL RASPUTIN! (random hero from isometric.spaceninja.com)
Paul and All, Thanks for the reply. I see your point about the error trapping. I just got started in Python because it all seems more elegant than the DTML I am used to. The indents were just cut and paste problems. It seems I am not that far off the target. I do know the difference between a variable name and the value it represents, but I tried many ways to represent the following example in a loop that assigned the values to "folder1" and so on and have failed. I don't know the syntax to get it done and over the past two weeks have not found it. I have noticed that many times the answer is out there, but I just don't know the right keywords to find it. I have often found the answer to a different question when searching for something. Fixed paths to create the folders... This make sense to me. ======================================================= container.manage_addProduct['PropertyFolder'].manage_addPropertyFolder(folder1, 'Folder 1','') container.folder1.manage_addProduct['PropertyFolder'].manage_addPropertyFolder(folder2, 'Folder 2','') container.folder1.folder2.manage_addProduct['PropertyFolder'].manage_addPropertyFolder(folder3, 'Folder 3','') container.folder1.folder2.folder3.manage_addProduct['PropertyFolder'].manage_addPropertyFolder(folder4, 'Folder 4','') container.folder1.folder2.folder3.folder4.manage_addProduct['PropertyFolder'].manage_addPropertyFolder(folder5, 'Folder 5','') print 'Done' return printed Variable paths to create the folders... This is where I get confused about the syntax. The first two are created fine. I get a key error on the third level. Which means I am using the variables incorrectly. But how? ======================================================= folder1='folder01' folder2='folder02' folder3='folder03' folder4='folder04' folder5='folder05' container.manage_addProduct['PropertyFolder'].manage_addPropertyFolder(folder1, folder1,'') folder=container[folder1] folder.manage_addProduct['PropertyFolder'].manage_addPropertyFolder(folder2, folder2,'') folder=container[folder2] folder.manage_addProduct['PropertyFolder'].manage_addPropertyFolder(folder3, folder3,'') folder=container[folder3] folder.manage_addProduct['PropertyFolder'].manage_addPropertyFolder(folder4, folder4,'') folder=container[folder4] folder.manage_addProduct['PropertyFolder'].manage_addPropertyFolder(folder5, folder5,'') print 'Done' return printed Any clarity on this specific issue would make my day. Thanks, Richard Paul Winkler wrote:
On Wed, Sep 17, 2003 at 12:08:41PM -0400, Richard Shebora wrote: (snip)
#02 CREATES INBOUND FOLDER IF NEEDED. folderid='INbound' foldertitle='Inbound Documents' try:
container.manage_addProduct['PropertyFolder'].manage_addPropertyFolder(folderid, foldertitle,'')
hmm, the indentation is screwy. I assume that is just an email cut/paste problem, or this script would never run at all.
print 'Inbox Folder '+folderid+' Created' except: print 'Inbox Folder '+folderid+' Already Exists'
Note that this is very risky. YOu should find out what error happens when the folder already exists, and catch only that error. If anything else goes wrong you will hide it behind a misleading "Already exists" message.
(snip)
#08 CREATES DOCUMENTS IN CURRENT TRADING PARTNER'S DOCUMENT FOLDER if fields[0]=='ST': DOCid=DOCtype+'-'+DOCindex DOClocation = TPdocs[DOCtype] # sets current folder try:
DOClocation.manage_addProduct['PropertyFolder'].manage_addPropertyFolder(DOCid,DOCtitle,'') print DOCid+' '+DOCtitle+' Created' except: print DOCid+' '+DOCtitle+' Already Exists'
09 CREATE DOCUMENT SEGMENTS # This is the goofy section that fails. SEGlocation=context.DOCid # sets current folder
Look at that last line. DOCid is a variable. You are asking context for something literally named "DOCid", when I think you want to ask for something whose name is the value of DOCid. Be sure you understand the difference.
You already know one way to do this correctly in zope, since you do it numerous times elsewhere in the script ;-)
Also I doubt that context is where you want to look. Since you just created the DOCid-named item in DOClocation, maybe that's where you should look for it.
On Wed, Sep 17, 2003 at 02:16:05PM -0400, Richard Shebora wrote:
Variable paths to create the folders... This is where I get confused about the syntax. The first two are created fine. I get a key error on the third level. Which means I am using the variables incorrectly. But how? ======================================================= folder1='folder01' folder2='folder02' folder3='folder03' folder4='folder04' folder5='folder05' container.manage_addProduct['PropertyFolder'].manage_addPropertyFolder(folder1, folder1,'')
Fine so far. As we go on, remember that container is bound to the folder that contains the script. It doesn't change (unless you were to explicitly bind something else to the name "container")
folder=container[folder1]
ok, folder now refers to container.folder01
folder.manage_addProduct['PropertyFolder'].manage_addPropertyFolder(folder2, folder2,'')
you have now created container.folder01.folder02...
folder=container[folder2]
container still means the container of the script. That's not where you put folder02! YOu're asking for container.folder02 and there is no such thing. You could instead get to the new folder this way: folder = container[folder1][folder2] ... but that's unnecessarily long; you've already got folder = container[folder1] so it's perfectly valid to use that like so: folder = folder[folder02] folder.manage_addProduct['PropertyFolder'].manage_addPropertyFolder(folder3, folder3,'') folder = folder[folder3] ... and so on. Once you have it working, your next assignment is to figure out how a single "for" loop would make this script a whole lot shorter and easier to maintain. :-) -- Paul Winkler http://www.slinkp.com Look! Up in the sky! It's SUPER FANG-HOLIC! (random hero from isometric.spaceninja.com)
participants (2)
-
Paul Winkler -
Richard Shebora