[Zope] newbie: Stupid question -- Simple mechanism for publishing secondary modules unveiled

Mike Fletcher mcfletch@vrtelecom.com
Mon, 30 Aug 1999 12:25:23 -0400


I am sure either the PUB or the Zopatistas will get me for revealing this,
but at least the information will be out there.  You can't stop the truth!
:)

Your question reminded me of the comment I had read in the z2.py module when
I was trying to figure this out myself.  I had been misled by various
comments in that module which claimed only the "Main" and "Zope" modules are
valid values for the MODULE variable. A little testing and the method for
directly publishing secondary python modules appears...

around line # 412 in z2.py (in the latest beta) you will find the following:

if HTTP_PORT:
    hs = zhttp_server(
        ip=IP_ADDRESS,
        port=HTTP_PORT,
        resolver=rs,
        logger_object=lg)

    # Handler for a published module. zhttp_handler takes 3 arguments:
    # The name of the module to publish, and optionally the URI base
    # which is basically the SCIRPT_NAME, and optionally a dictionary
    # with CGI environment variables which override default
    # settings. The URI base setting is useful when you want to
    # publish more than one module with the same HTTP server. The CGI
    # environment setting is useful when you want to proxy requests
    # from another web server to ZServer, and would like the CGI
    # environment to reflect the CGI environment of the other web
    # server.
    zh = zhttp_handler(MODULE, '', HTTP_ENV)
    hs.install_handler(zh)

if you add this:

    # testing secondary module publisher...
    zh = zhttp_handler('hellozope', '/hellozope', HTTP_ENV)
    hs.install_handler(zh)

You will get the module "hellozope" published under the universal resource
identifier "/hellozope".  I think a command-line option that allowed
specifying an arbitrary list of modules to publish might be added to z2.py .
Any comments?  Something like:

-modules = "Zope;hellozope;someweirdmodule"
-baseModuleURIs = "/;/hellozope/;/weird/"

At the very least, this should be entered in the FAQ, as anyone coming from
a Python/Bobo background (or reading the documentation which was written for
Bobo) will spend quite some time trying to figure it out.  Oh, and remember
to use the debug flag when you first start out, can save a great deal of
headache.

Do note, you will have to manually support (for instance) object persistence
etc. which is normally handled by Zope.  This will be familiar to you if
you've used Bobo before.  You can get access to the Zope persistence
mechanisms by poking around inside the guts of Zope and importing Zope's
mechanisms directly into your modules.

I wound up creating a large product for lack of such a mechanism.  In
general, I would suggest this if you're dealing with a production product
(or even a demonstration (such as I'm working on)).  For learning, I think
this level of direct access is very useful for those coming from a
programming background.

I hope this helps,
Mike

With the above alteration to z2.py, put this module on your python path as
hellozope.py . Access it with the url:
http://server:port/hellozope/v/sayHello where you substitute server and port
with your Zope HTTP server's values.

It should print "Hello world!" in your browser window.


8<_____ hellozope.py ... ___

class Test:
	'''
	An example object (this string required)...
	'''
	def sayHello (self, REQUEST = None):
		"Says hello to the world (this string required)"
		return "Hello world!"
v = Test()