[ZDP] BackTalk to Document The Zope Book (2.5 Edition)/Advanced Zope Scripting

webmaster@zope.org webmaster@zope.org
Thu, 26 Sep 2002 19:12:59 -0400

A comment to the paragraph below was recently added via http://www.zope.org/Documentation/Books/ZopeBook/current/ScriptingZope.stx#3-79


      One common use for scripts is to do string processing. Python has a
      number of standard modules for string processing. You cannot do regular
      expression processing from Python-based Scripts, but you do have access
      to the *string* module. You have access to the *string* module from
      DTML as well, but it is much easier to use from Python. Suppose you
      want to change all the occurrences of a given word in a DTML
      Document. Here's a script, *replaceWord*, that accepts two arguments,
      *word* and *replacement*.  This will change all the occurrences of a
      given word in a DTML Document::

        ## Script (Python) "replaceWord"
        ##parameters=word, replacement
        Replaces all the occurrences of a word with a
        replacement word in the source text of a DTML
        Document. Call this script on a DTML Document to use

        Note: you'll need permission to edit a document to
        call this script on the document.
        import string
        text=string.replace(text, word, replacement)
        context.manage_edit(text, context.title)

        % Anonymous User - Apr. 9, 2002 3:26 pm:
         Why no regular expressions? Perhaps this should be explained in greater detail. One can always use javascript
         if one needs regular expressions, but this is something that was quite surprising to me.

        % Anonymous User - Apr. 10, 2002 12:50 am:
         It's a security issue. Zope attaches attributes to objects in order to protect them via security assertions.
         The regex module is implemented in C (as opposed to pure Python), so it's hard to attach security attributes
         to the various objects that are used by the regex machinery. External methods can make full use of the regex
         module, but "through the web" code (like Python scripts) cannot due to the abovementioned security

        % Anonymous User - Aug. 20, 2002 10:05 am:
         context.manage_edit(text, context.title)

         Is not explained...

        % Anonymous User - Sep. 26, 2002 7:12 pm:
         of course the script should somehow verify, that context is a DTML document;
         but then manage_edit is explained in the Appendix A.