[Zope] Traversal Issue (brain fart)

Richard Shebora richard at apogaion.com
Wed Sep 17 12:08:41 EDT 2003


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










More information about the Zope mailing list