[Zope-Perl] Perl external methods

Gisle Aas gisle@ActiveState.com
05 Jun 2000 12:38:45 +0200


Let me just repost an "old" zope-2.1.6 patch that actually enable perl
external methods.  Jim has said that he wants to add more generic
hooks to Zope, but I have not heard anything more about that.  Does
somebody want to comment on what direction to take?

With this patch you have to add the complete signature of the perl
function into the "name"-field of of the ExternalMethod management
interface.  You also use the same interface to set up both python and
perl methods.  If a <module>.pm file is found we assume perl methods.

For instance if you have an external method defined as:

    Function name:  Foo::foo(self)
    Module name:    Foo

and then have the Foo.pm file placed under the Extentions directory,
then the perl function will be called with the $self object as the
only argument.

Regards,
Gisle


P.S. Next time I'll remember to cvs import with -ko so you don't have
to see the private $Id$ stuff :-)



Index: lib/python/App/Extensions.py
===================================================================
RCS file: /home/gisle/CVSROOT/zope/lib/python/App/Extensions.py,v
retrieving revision 1.1.1.1
retrieving revision 1.3
diff -u -r1.1.1.1 -r1.3
--- lib/python/App/Extensions.py	2000/05/10 18:11:45	1.1.1.1
+++ lib/python/App/Extensions.py	2000/05/10 20:11:22	1.3
@@ -86,10 +86,10 @@
 
 Extensions currently include external methods and pluggable brains.
 
-$Id: Extensions.py,v 1.1.1.1 2000/05/10 18:11:45 gisle Exp $'''
-__version__='$Revision: 1.1.1.1 $'[11:-2]
+$Id: Extensions.py,v 1.3 2000/05/10 20:11:22 gisle Exp $'''
+__version__='$Revision: 1.3 $'[11:-2]
 
-from string import find, split
+from string import find, split, replace
 import os, zlib, rotor
 path_split=os.path.split
 path_join=os.path.join
@@ -180,9 +180,10 @@
         old=None
 
     if module[-3:]=='.py': p=module[:-3]
+    elif module[-3:]=='.pm': p=module[:-3]
     elif module[-4:]=='.pyp': p=module[:-4]
     else: p=module
-    p=getPath('Extensions', p, suffixes=('','py','pyp'))
+    p=getPath('Extensions', p, suffixes=('','py','pyp','pm'))
     if p is None:
         raise "Module Error", (
             "The specified module, <em>%s</em>, couldn't be found." % module)
@@ -195,6 +196,38 @@
             rotor.newrotor(prod_id +' shshsh').decrypt(open(p,'rb').read())
             )
         execsrc=compile(data, module, 'exec')
+    elif p[-3:]=='.pm':
+	import perl
+  	if reload:
+	    loadop = "do"
+        else:
+            loadop = "require"
+	perl.eval(loadop + " '" + p + "';")
+        
+        tmp_list = split(name, "(", 2)
+        plain_name = tmp_list.pop(0);
+        args = ""
+        if tmp_list:
+            args = tmp_list[0];
+            args = replace(args, ")", "")
+
+	perldict = {'__perl': perl, '__plain_name': plain_name}
+
+        lcode = "lambda " + args + ": __perl.call(__plain_name"
+        if args:
+            lcode = lcode + ", " + args
+        lcode = lcode + ")"
+        #print "....", lcode
+        
+	proxy_func = eval(lcode, perldict)
+        
+	if old:
+	    old[name] = proxy_func
+	else:
+	    modules[module] = { name: proxy_func }
+
+	return proxy_func
+
     else:
         try: execsrc=open(p)
         except: raise "Module Error", (
Index: lib/python/Products/ExternalMethod/ExternalMethod.py
===================================================================
RCS file: /home/gisle/CVSROOT/zope/lib/python/Products/ExternalMethod/ExternalMethod.py,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -r1.1.1.1 -r1.2
--- lib/python/Products/ExternalMethod/ExternalMethod.py	2000/05/10 18:11:45	1.1.1.1
+++ lib/python/Products/ExternalMethod/ExternalMethod.py	2000/05/10 19:58:51	1.2
@@ -264,5 +264,5 @@
     def filepath(self):
         if not hasattr(self, '_v_filepath'):
             self._v_filepath=getPath('Extensions', self._module,
-                                     suffixes=('','py','pyc','pyp'))
+                                     suffixes=('','py','pyc','pyp','pm'))
         return self._v_filepath
Index: lib/python/Products/ExternalMethod/extmethod.gif
===================================================================
RCS file: /home/gisle/CVSROOT/zope/lib/python/Products/ExternalMethod/extmethod.gif,v
retrieving revision 1.1.1.1
retrieving revision 1.1
diff -u -r1.1.1.1 -r1.1
Index: lib/python/Products/ExternalMethod/methodAdd.dtml
===================================================================
RCS file: /home/gisle/CVSROOT/zope/lib/python/Products/ExternalMethod/methodAdd.dtml,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -r1.1.1.1 -r1.2
--- lib/python/Products/ExternalMethod/methodAdd.dtml	2000/05/10 18:11:45	1.1.1.1
+++ lib/python/Products/ExternalMethod/methodAdd.dtml	2000/05/10 20:00:29	1.2
@@ -33,7 +33,7 @@
 </TR>
 <tr>	<th ALIGN="LEFT">Function name</th>
 	<td ALIGN="LEFT"><input name="function" size="30"></td></tr>
-<tr>	<th ALIGN="LEFT">Python module file</th>
+<tr>	<th ALIGN="LEFT">Python/Perl module file</th>
 	<td ALIGN="LEFT"><input name="module" size="30"></td></tr>
 <TR>
 <TD></TD>
Index: lib/python/Products/ExternalMethod/methodEdit.dtml
===================================================================
RCS file: /home/gisle/CVSROOT/zope/lib/python/Products/ExternalMethod/methodEdit.dtml,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -r1.1.1.1 -r1.2
--- lib/python/Products/ExternalMethod/methodEdit.dtml	2000/05/10 18:11:45	1.1.1.1
+++ lib/python/Products/ExternalMethod/methodEdit.dtml	2000/05/10 20:03:25	1.2
@@ -23,7 +23,7 @@
 <tr>	<th ALIGN="LEFT">Function name</th>
 	<td ALIGN="LEFT"><input name="function" size="30"
              value="<dtml-var function html_quote>"></td></tr>
-<tr>	<th ALIGN="LEFT">Python module file (without suffix)</th>
+<tr>	<th ALIGN="LEFT">Python/Perl module file (without suffix)</th>
 	<td ALIGN="LEFT"><input name="module" size="30"
              value="<dtml-var module html_quote>"></td></tr>
 <TR>