[Zope] External methods and DTML ?
Tino Wildenhain
tino@wildenhain.de
Tue, 15 May 2001 19:41:13 +0200
Hi,
its quiet simple:
your External methods might look like so:
import string
files={'file1':'path/to/file','file2','path/to/file2'}
def ReadExternalFile(fileid):
# try to open file. If fileid is invalid, an exception occurs
file=open(files[fileid])
# you can do some processing as splitting up into lines:
return file.readlines()
def WriteExternalFile(fileid,lines):
# try to open file. If fileid is invalid, an exception occurs
file=open(files[fileid],'w')
file.writelines(lines)
You can then use these methods like this in your dtml-method:
<dtml-if myfile>
<dtml-call "WriteExternalFile(fileid='file1',lines=myfile)">
</dtml-if form was submitted>
<form method="post">
<dtml-with "ReadExternalFile(fileid='file1')">
<textarea name="myfile">
<dtml-var read htmlquote>
</textarea>
</dtml-with>
<input type="submit" name="foo" value="change">
Ok, this is very sketchy but you should get the point.
However, the above method does not protect the file
from modifying through concourrent requests.
If you want to protect yourself against this,
I would suggest writing a class in the external method:
import string
files={'file1':'path/to/file','file2','path/to/file2'}
class ProtectFileChange:
__allow_access_to_unprotected_subobjects_=1
def __init__(self,fileid):
# do something to protect the file, e.g. rename it
# or use locking on it (see posix)
self.file=open(files[fileid],'rw')
def read(self):
self.file.seek(0)
return(self.file.read())
def write(self,data):
self.file.seek(0)
self.file.write(data)
def __del__(self):
# do something to unlock the file
self.file.close() # cleenup.
def PFile(fileid):
return ProtectFileChange(fileid)
You can use this class as External method this way:
<dtml-with "PFile(fileid='file1')">
<dtml-if myfile>
<dtml-call "write(myfile)">
</dtml-if>
<form method=post>
<textarea name="myfile">
<dtml-var read html_quote>
</textarea>
<input type="submit" value="change">
</form>
</dtml-with>
The latter keeps the object with the file until the request ends.
This is untested code however, there might be a lot of typos :)
HTH
Tino Wildenhain
--On Montag, 14. Mai 2001 14:38 -0700 Gardner Pomper
<gardner@networknow.org> wrote:
> Well, the short answer would be that I don't know. Maybe you could give
> me a hint. For example..
>
> I put up a form, which must display the contents of a file on the Unix
> host. The user edits this form and presses the submit button. This should
> spawn a program, which will create an output file. I want to display the
> output file on another web page.
>
> Naturally I won't be displaying the files unchanged; I have to process
> them abit before displaying.
>
> I don't know how to call my external method by pressing the submit button
> and then display an output. If I make the action of the form run my
> external method, how do the results get displayed? If I have the action
> go to another DTML file, how do the form fields get passed to my external
> script?
>
> Thanks,
>
> - Gardner
>
> ---------- Original Message ----------------------------------
> From: Tino Wildenhain <tino@wildenhain.de>
> Date: Mon, 14 May 2001 23:16:04 +0200
>
>> Hi Gardner,
>>
>> why dont you just let your external methods handle data
>> in- and output and delegate rendering of html to
>> DTML-Methods in zope?
>>
>> Regards
>> Tino Wildenhain
>>
>> --On Montag, 14. Mai 2001 14:07 -0700 Gardner Pomper
>> <gardner@networknow.org> wrote:
>>
>>> Hi,
>>>
>>> I am using Zope to build a front end to a bunch of Unix processes. I
>>> need to use external methods to read/write files and kick off the jobs.
>>> In these external methods, I use DocumentTemplate.HTMLFile() to build
>>> the HTML forms for data entry.
>>>
>>> My problem is that I don't seem to be able to hook the
>>> DocumentTemplate.HTMLFile() to a DTML file created in Zope. Therefore I
>>> have to create the DTML files manually. This isn't too bad, but when I
>>> change the DTML files during development, Zope seems to have cached the
>>> old copy and I often end up having to kill off the Zope server and
>>> restart it just to try out the new version of the DTML file. This is
>>> annoying and time consuming.
>>>
>>> Is there some way to get the files reread each time? Failing that, is
>>> there a way for an external method to access a DTML file that Zope knows
>>> about (and will that help me with this refresh problem)?
>>>
>>> I am pretty new to Zope (a week or so). If there is an easier way to do
>>> all this, I would be happy to hear it.
>>>
>>> - Gardner
>>>
>>>
>>>
>>> _______________________________________________
>>> Zope maillist - Zope@zope.org
>>> http://lists.zope.org/mailman/listinfo/zope
>>> ** No cross posts or HTML encoding! **
>>> (Related lists -
>>> http://lists.zope.org/mailman/listinfo/zope-announce
>>> http://lists.zope.org/mailman/listinfo/zope-dev )
>>
>>
>>
>>
>>