[Zope-dev] ZModules - A loaded gun

Kapil Thangavelu kthangavelu@earthlink.net
Wed, 27 Sep 2000 18:22:50 -0700


This is a multi-part message in MIME format.
--------------FF669FA7649E2DABDD3D5AA9
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

i wrote up an interface to allow for module registration in the dtml
namespace, _

its got major caveats, but it might be useful for some.

at the moments its just a stop gap till the new python methods come
along. at the moment if just allows for people to write more code in
dtml:( or ttw python methods.

it might be useful to allow ttw developers access to some modules
without allowing them use of XXXpython_methods since the configuration
file that defines the imported modules resides on the FS.

an example use

<dtml-let y="_.poplib.POP3('mail.earthlink.net')">
<dtml-var "y.welcome">
<dtml-var "y.user('kthangavelu')">
<dtml-var "y.pass_('ipc9@longbeach')">
<dtml-var "y.stat()">
</dtml-let>

and the response

+OK EarthLink POP server (version: EL_3_10 ) at pop04.earthlink.net. 
<21615.970119687@pop04.earthlink.net>
+OK Password required for kthangavelu.
+OK kthangavelu has 0 messages (0 octets).
(0, 0)

personally i thinks it better to just pass the module off to a python
method
and let it do the work. real coding in dtml is heinous, maybe even a
crime against humanity.

how it works.

it reads a module configuration from the FS. fmt is currently

module_name: class_name, class_name


it tries to import the module and than place the module in the namespace
dict.
	- usual python discovery rules apply (python_path, curdir)

it sets the magic key __allow__access_to_unprotected_subobjects=1 on all
imported modules.

it allows you to define classes in modules those you want to interact
with. defining those classes allows you to interact meaningfully with
instances otherwise zope's security policy will bump you because the
container doesn't define proper permissions or the magic key.

caveats:	

it hasn't had much testing, i did it as proof of concept and because i
had an itch.

	you can't accces any attributes of any object that doesn't define
	__allow__access_to_unprotected_subobjects=1

	an example where this is a pain, are getting filehandles back from
libraries,
	like urllib.urlopen (note. urllib is not ts).

	BE VERY VERY WARY OF THREAD SAFETY ISSUES

	it might be very much like holding a loaded gun to your server;)

possible improvements:
	allow for nested modules & classes
	?


if people think this is useful enough, i'll package it up as a product.

installation.

create a directory in your products folder (mines called ZModules)

drop in the attached file.

write a configuration file 

restart zope

sample config file.

poplib:POP3
mimetypes


Cheers

Kapil
--------------FF669FA7649E2DABDD3D5AA9
Content-Type: text/plain; charset=us-ascii;
 name="__init__.py"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="__init__.py"

# DISTRIBUTED UNDER THE ZPL 
# Author kthangavelu@earthlink.net
# made for zope, www.zope.org

import App
import App.Common
import Globals
import os
from DocumentTemplate import DT_Util
from DocumentTemplate.DT_Util import d
from string import strip,split,replace

config_path=os.path.join(App.Common.package_home(globals()), 'zmodules.config')
             
fh = open(config_path)
modules_list=fh.readlines()

#print 'read config file'

counter = 0

for module in modules_list:
    module = strip(module)
    module = replace(module, '\012', '')
    if len(module) <= 1:
        continue
    
    module_config = split(module, ':')
    
    if len(module_config) > 1:
        classes = split(module_config[1], ',')
    else : classes = []
        
    module = strip(module_config[0])
    module_name = module[:]

    try:
        exec('import %s'%(module))
    except:
        continue

    loaded_mod = eval(module)

    s_classes = classes[:]
    for class_name in classes:        
        class_name=strip(class_name)        
        if len(class_name) == 0: continue        
        try:
            getattr(loaded_mod, class_name).__allow_access_to_unprotected_subobjects__=1
        except:
            s_classes.pop(s.index(s_classes))

    d[module_name] = loaded_mod
    
    loaded_mod.__allow_access_to_unprotected_subobjects__=1

    print 'success: loaded module %s and classes %s'%(module_name, s_classes)

    

    

    
    

    




--------------FF669FA7649E2DABDD3D5AA9--