[Zope] Extracting just the string of the zope file or dtml object

Andy Bulka abulka@netspace.net.au
Mon, 17 Sep 2001 13:23:50 +1000


It seems that there are a couple of different methods of extracting a string
from a zope object, in the external method side of things.  Here is a rough
utility method that takes care of the differences:

def _convertToStr(s):
    atype = safetest(s)
    if atype == 'string':
        return s
    elif atype == 'DTML Document':
        assert hasattr(s,'read')
        s = s.read() # now "s" is a string
        assert safetest(s) == 'string'
        return s
    elif atype == 'File':
        s = str(s.data)
        assert safetest(s) == 'string'
        return s

this uses the safetest() function that I posted earlier.

Thanks to Dieter and Adam Warner for their contributions.
-Andy Bulka
www.atug.com/andypatterns

-----Original Message-----
From: Dieter Maurer [mailto:dieter@handshake.de]
Sent: Tuesday, 11 September 2001 6:35 AM
To: Andy Bulka
Cc: Zope egroups List
Subject: Re: [Zope] Extracting just the string of the zope file or dtml
object


Andy Bulka writes:
 > How do I ensure that the string *contents* of my Zope object is passed to
an
 > external method, rather than passing the zope object itself?
 >
 > E.g. if
 >   mydtmldoc  is a DTML document with plain text inside it
 >   myFile     is a file object with content-type plain text
 > then when I call
 >
 > <dtml-call "ProcessAstring1('a literal string')">
 > <dtml-call "ProcessAstring1(mydtmldoc)">
 > <dtml-call "ProcessAstring1(myFile)">
The easiest way is to work in your external method
and let DTML pass what it likes:

  def extMethod(string_or_object):
    s= string_or_object
    if hasattr(s,'read'): s= s.read() # now "s" is a string

In DTML, it is much more difficult, as "read" is private
for DTML objects. However,  "PrincipiaSearchSource" is
an accessible synonym for the "read" of DTML objects.

I did not check the permissions of "OFS.File.read".
You can use my DocFinder product

  <http://www.dieter.handshake.de/pyprojects/zope/DocFinder.html>

to find out about it.



Dieter