[Zope] Script to do various 'manage_' operations

Tim Hicks Tim Hicks" <tim@sitefusion.co.uk
Sun, 28 Jan 2001 00:59:47 -0000


I have been trying to get hold of a script that can automatically convert
various dtml methods to custom zclass instances.  I found a script on the
zope mailing list (see bottom)

I put my file (see below) in the Extensions directory and added an external
method from within zope.  This all appeared to go fine.  To execute the
script, I made a method that simply had <dtml-call conscript()> and then
standard_h_h etc.  The problem is, nothing seems to happen.  I can view the
method that was supposed to run the script, but the call doesn't seem to
work.  Within the filesystem, there is no file called tempo (as a kind of
log of what's been done), and the methods with extension .shtml certainly do
not get changed to dtml documents.

Has anyone done this before?  Can you shed any light on what I'm doing
wrong?

Much obliged.

tim


Script from zope@zope.org
-------

import re

def convert_dtml(self):
     """Convert DTML Methods and DTML Documents from old syntax to
        new syntax.
        Warning: recursive!
        This assumes that DTML Method and DTML Document haven't been
        subclassed.
     """
     print 'convert_dtml: id=%s' % self.title_and_id()
     if hasattr(self, 'meta_type') and \
           (self.meta_type == 'DTML Method' or \
            self.meta_type == 'DTML Document'):
         convert(self)

     # should this be "isPrincipiaFolderish"?
     if hasattr(self, 'isAnObjectManager') and self.isAnObjectManager:
         for v in self.objectValues():
             v.convert_dtml()

_convert_regex =
re.compile('''<!--#(/?)(([^"-]+?|"[^"]*?"|'[^']*?'|-[^-])+?)-->''')

def convert(dtml_item):
     print 'converting...'
     title = dtml_item.title
            # like document_src, but doesn't require RESPONSE
     data = dtml_item.PrincipiaSearchSource()
     print '----data----'
     print data
     newdata = _convert_regex.sub('<\g<1>dtml-\g<2>>', data)
     print '----newdata----'
     print newdata
     print '----end----'
     dtml_item.manage_edit(newdata, title)


------
And here is how I adjusted it for my purposes:
------

import re

get_shtml = re.compile('\w+\.shtml')
f = open("tempo", "w+")

def convert_dtml(self):
     """Convert DTML Methods and DTML Documents from old syntax to
        new syntax.
        Warning: recursive!
        This assumes that DTML Method and DTML Document haven't been
        subclassed.
     """
     if hasattr(self, 'meta_type') and \
             get_shtml.match(self.id):
         convert(self)

     # should this be "isPrincipiaFolderish"?
     if hasattr(self, 'isAnObjectManager') and self.isAnObjectManager:
         for v in self.objectValues():
             v.convert_dtml()

def convert(dtml_item):
     id = dtml_item.id+'2'
     title = dtml_item.title
     # like document_src, but doesn't require RESPONSE
     f.write(id /n)
     data = dtml_item.PrincipiaSearchSource()
     PARENTS[0].manage_addDTMLDocument(id, title)
     id.manage_edit(data, title)