How to import files via FTP and set attributes.
I'm mildly experienced at Zope, yet haven't found the 'simple' way to execute the following. I been going in circles on this and all suggestions as to how to approach the problem would be appreciated. Rather than correct the approaches I have been trying, I think it best to get a fresh perspective. Big Picture Objective: User FTPs a batch of files into an empty zope folder along with a text document called contents.txt. contents.txt has one line per uploaded file in CSV or similar parseable layout that begins with the file name. After the upload, the user triggers a posting process (a 'process the upload' form buttom with Action=Do_It(). The executed Do_it object reads the text file and loops once per line, for each line, it parses it into a file name and several attributes. The object then writes the uploaded file content to a target folder as a zope object having these attributes. I am stuck at looping on and parsing the content of the contents.txt file to get and pass the attributes to the add object method. I figure the solution is best executed with Python in the context of Zope, but I'm stuck at figuring out the syntax for this namespace. 1) syntax to loop on the content of a file, one line at a time. 2) string syntax to parse the line into variables (I don't think an array is needed.) 3) modify object syntax to update each file's attributes with the variables (or add a new object such as an image object with the imported file's contents.) Suggestions, syntax, or pointers to how-tos all gratefully accepted. Thank you! Gary
Gary Speer writes:
... I am stuck at looping on and parsing the content of the contents.txt file to get and pass the attributes to the add object method. When you have a file object "F", "str(F)" gives you the file content (a string).
When you have a string "s", "s.split(separator)" splits "s" at "separators" into a list of strings. Provided your CSV separators are not imbedded into the content, applyind this twice will parse the CSV string. Look at the PropertyManager API (Embedded Zope help -> "Zope Help" -> "API Reference" -> "PropertyManager") to learn how to add and change properties (aka attributes). Look at the "Image" API (I guess, you will find it ;-)) how to create an Image object from a string or file. Dieter
[[Gary Speer]
I am stuck at looping on and parsing the content of the contents.txt file to get and pass the attributes to the add object method. I figure the solution is best executed with Python in the context of Zope, but I'm stuck at figuring out the syntax for this namespace. 1) syntax to loop on the content of a file, one line at a time.
I see that Dieter responded to your other questions, so I'll just answer this one. If you have an open file-like object named File, in Python you could say: line =File.readline() while line: #do something with the line line=File.readline() Or you could read the file into a list all at once: lines=File.readlines() for line in lines: # do something with the line Remember, line will include any trailing newline character. Cheers, Tom P
participants (3)
-
Dieter Maurer -
Gary Speer -
Thomas B. Passin