On Wed, Jun 01, 2005 at 11:05:04AM -0500, J Cameron Cooper wrote:
John Poltorak wrote:
On Tue, May 31, 2005 at 07:17:46PM +0300, Vital Lobachevsky wrote:
John Poltorak wrote:
Well, it's really easy. Create 'linkList' (Python Script) in the folder where you page lives or somewhere higher in folder hierarchy:
## Script (Python) "linkList" return [ ('http://www.google.com/', 'Google'), ('http://www.yahoo.com/', 'Yahoo'), ]
If your page is Page Template, it maybe something like this
<html> <body> <h1>Search Engines</h1> <tal:block repeat="item here/linkList"> <a tal:content="python:item[1]" tal:attributes="href python:item[0]"></a><br /> </tal:block> </body> </html>
If you page is DTML Method, you can do the same using <dtml-in> tag.
How would I extend this so that the Python script could read data from an independently maintained text file which could uploaded periodically?
ie using a file containing something like:-
To get to the local file system, you must use an external method, which works very much like a Python script, but gets its code from a Python file on the file system. This is for security. You can read all about this in the Zope book.
Your external method should return a list just like the Python script above. How you generate it is up to you. For your file format example, you could use the Python 'file' object's 'readlines' method and use 'split' to decode the lines::
f = open('myfile.links') retval = () for line in f.readlines(): elt = line.split(',') retval += (elt,) return retval
You might also be able to use the 'csv' module.
Now, if you're uploading files to Zope, rather than the file system, it's a little different, in that you have to get the data from an object, and you'll probably have to split it by newline yourself.
This is the way I had been thinking of setting it up, but can't find an example of opening and reading such an object. I thought there may be something here:- http://www.plope.com/Books/2_7Edition/BasicScripting.stx#1-2 but couldn't find anything which looked relevant.
--jcc
-- "Building Websites with Plone" http://plonebook.packtpub.com/
-- John