[Zope] Converting Python Methods to Python Scripts

Itai Tavor itai@optusnet.com.au
Fri, 15 Dec 2000 17:13:43 +1100


Hi,

I was wondering if anyone is planning to add automated migration of 
Python Methods to Python Scripts (maybe with Zope 2.3 final)? Or 
maybe there's something like this in there already that I missed?

Anyway, for now I created a simple hack to do this - it's not pretty, 
but it works. In case anyone's interested, this is how to do it:

* Create the following External Method at the top level of Zope, named M2S:

from Products.PythonScripts.PythonScript import PythonScript
from string import strip

def M2S(self, ids, REQUEST):
     method_id = type(ids) == type('') and ids or ids[0]
     method = self._getOb(method_id)

     if method.meta_type != 'Python Method':
         raise 'HackError', 'Selected object is not a Python Method'

     title = method.title
     params = method._params
     if not type(method._body) == type(''):
         body = method._body.read()
     else:
         body = method._body

     self.manage_renameObject(method_id, method_id+'X')

     id = self._setObject(method_id, PythonScript(method_id))

     script = self._getOb(method_id)
     script.ZPythonScript_setTitle(title)
     script.ZPythonScript_edit(params, body)

     return REQUEST.RESPONSE.redirect('%s/%s/manage' % 
(REQUEST['URL1'], method_i
d))


* Edit lib/python/OFS/main.dtml. Find these lines:

   <dtml-if "AUTHENTICATED_USER.has_permission('Import/Export 
objects', this())">
   <INPUT TYPE="SUBMIT" 
NAME="&dtml-id;/manage_importExportForm:method" VALUE="Ex
port...">
   </dtml-if>

After them add this line:

   <INPUT TYPE="SUBMIT" NAME="M2S:method" VALUE="Convert Python Method 
to Script"


* Repeat the last step for lib/python/Products/PlugIns/www/main.dtml

* To use, check a Python Method in the management interface and click 
"Convert Python Method To Script". If you check more than one method, 
only the first one will be converted.

* A new Python Script will be created, using the same id of the 
original Python Method. The old method will remain, with an 'X' added 
to its id. After the conversion you'll be redirected to the editing 
interface of the new script.


Hope this is useful to anyone. It could probably be made much nicer, 
but why bother.

Itai
-- 
Itai Tavor                    "Je sautille, donc je suis."
C3Works    itai@c3works.com              - Kermit the Frog

"If you haven't got your health, you haven't got anything"