You are not logged in Log in Join
You are here: Home » Members » Jim Washington » Converting Python Methods to Python Scripts

Log in
Name

Password

 

Converting Python Methods to Python Scripts

Python Scripts are very similar to Python Methods.

The important differences in my experience for converting Python Methods to Python Scripts are:

  1. You need to import to get modules you need, like string
  2. "self" is not available and in its place you have to choose context or container in its place as appropriate
  3. Python Scripts participate more fully in the security model. You could get away with some things in Methods you can no longer do in Scripts. Proxy roles become important.

Why Convert?

The main reason I can think of is that retaining Python Methods adds another dependency to your work, where Python Scripts are part of the distribution in Zope 2.3.0+.

I have written a Python Script that does some of the work for converting (a folder of) your existing Python Methods to Python Scripts. It's not perfect, but it mostly worked for me.

Just put it in a folder that has Python Methods, and press the "try" management tab

Please do understand what it does before using it.

import string
ids = container.objectIds('Python Method')
oldscriptsuffix='.old'
#replace all self. with the following:
#change to context if needed
newself = 'container'
#for future programmatic use just to be certain no extra dots
if string.find(newself,'.') >= 0:
  newself = string.replace(newself,'.','')
for method_id in ids:
  method_id = string.strip(method_id)
#get the Method and its title
  method = container[method_id]
  title = method.title
#FTPget does not require external method!
  thebody = method.manage_FTPget()
#get the params
  eop = string.find(thebody,'/params')-1
  params = thebody[8:eop]
  params = string.replace(params,' ','')
  params = string.replace(params,'self,','')
  params = string.rstrip(string.replace(params,'self',''))
  body = thebody[eop+10:]
#get the body
  newbodylist = []
  splitbody = string.split(body,'\n')
#do imports as needed
#bug: random will be imported if you use whrandom
  for animport in ['string','whrandom','random','math']:
    if string.find(body,animport+'.') >= 0:
      newbodylist.append('import ' + animport)
  for k in splitbody:
    #this would be a good place for re
    newstring = string.replace(k,'self.',newself + '.')
#bug: might miss 'self [' wish re were available
    newstring = string.replace(newstring,'self[', newself + '[')
    newbodylist.append(string.rstrip(newstring))
  body = string.join(newbodylist,'\n')
# uncomment to see the raw data
#  print 'params = "%s"' % params
#  print 'body is:\n"%s"' % body
  
#rename the old and create the new.  don't do more than once.
  if method_id [-len(oldscriptsuffix):] <> oldscriptsuffix:
    container.manage_renameObject(method_id,method_id+oldscriptsuffix)
    container.manage_addProduct['PythonScripts'].manage_addPythonScript(method_id)
    newscript = container[method_id]
    newscript.ZPythonScript_setTitle(title)
    newscript.ZPythonScript_edit(params, body)
    print 'converted: \t%s' % method_id

if len(printed) < 9:
  print "No methods to convert"
return printed