[Zope3-dev] PythonScript in Zope3?

Steve Alexander steve@cat-box.net
Wed, 19 Feb 2003 10:43:28 +0200


> In Zope2, I can do something like create a folder named "/foo", and 
> inside that folder place a PythonScript named "index_html".  When a 
> browser requests "/foo", it gets back the output of the script.
 >
> How would you achieve a similar result in Zope3?

Add a folder called 'foo'. Inside the folder, add a python script called 
'index.html'.

You don't need to use persistent modules for this.


>  * What would be an appropriate absolute module name? Just a single
>    word like "mymod"? or something dot-separated like "mystuff.mymod"

Yes. You would probably want to use a dot-separated name, though, for 
the same reasons you'd want to put a filesystem module in a package.


>  * Can the sourcecode be as simple as: "print 'hello world'", or does
>    there need to be a function defined.

Yes (but I don't know why you'd want to print anything like this).
No, there doesn't need to be a function defined. However, you might like 
to define a function or class or interface.


>  * What URL would you use to see the output of the script?  I tried
>    some of the obvious guesses like:
> 
>          /foo/++etc++Services/Packages/default/mymod
> 
>    but I get a NotFound error

Are we talking about a script or about code in a module?

Scripts live in 'content space', that is, as python scripts in regular 
folders.
Code, such as functions and classes and interfaces, lives in modules.

If you define a persistent module, and the module name is mystuff.mymod, 
then you can call a function called 'myfunc' that is inside 
mystuff.mymod by writing this in a page template:

   <div tal:define="result mystuff/mymod/myfunc" />

or perhaps this:

   <div tal:define="result python:mystuff.mymod.myfunc()" />


>  * How do you arrange it so the output from this script is
>    what's displayed when "/foo" is requested?  Is it some
>    combination of a "view service" in the package, or a "view"
>    in the package configuration?

Now we're talking about local views, rather than scripts or modules.

You can set up a local views service, configure a view for IFolder, and 
provide a template and possibly a class for that (if you want).

You can redefine the default view of IFolder to be your new view.

This is appropriate if you want to change the default view of all 
folders in your site.
This is not appropriate if you want to put an index.html page in a 
single particular folder.


--
Steve Alexander