Extracting just the string of the zope file or dtml object
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)"> I get Got string Got DTML Document Got File respectively in my external method. I really just want the string contents of these zope objects. I want to be able to do all sortd of string operations on them - but this is not working since python complains they are not strings. My test external method is simply def ProcessAstring(astr): print 'Got', safetest(astr) # safetest I got off this mailing list, and is listed below How do I just get at the string being passed? Or how do I just pass the raw string? thanks Andy Bulka ----------------------------- def safetest(object): if object is None: return 'None' s = str(object) if object==s: return 'string' try: t = object.meta_type return t except: pass try: l = len(object) if s[0]=='[': return 'list' if s[0]=='(': return 'tuple' if s[0]=='{': return 'dict' return 'sequence' except: pass try: t = object.timeTime() return 'DateTime' except: pass try: val = int(object) if val is object: return 'integer' return 'number' except: return 'unknown'
On Mon, 2001-09-10 at 23:48, Andy Bulka wrote:
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?
Andy, you will know a lot more than me since I've only been using external methods for a couple of days. I do know you could approach it this way: In the external python script try this: s = str(objectname.data) Which will extract the data stream from the object and turn it into a string. Your def's should also have "self" in them. That is: def safetest(self, object): I understand the self is all that is needed for you to be able to access the zope database in the external method. Regards, Adam
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
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
participants (3)
-
Adam Warner -
Andy Bulka -
Dieter Maurer