HowTo Render txt-Files with LocalFS in PythonScript?
Dear List, with the following PythonScript I try to render the content of txt-Files with LocalFS according to the howto of LocalFS (...snip..." To insert the text of the file after evaluating it as a DTML template in the current namespace, you would use: <dtml-var "test['foo.dtml'](_.None, _)"> .....snip ..."). My Script: ## Script (Python) "getContentFiles" ##bind container=container ##bind context=context ##bind namespace=_ ##bind script=script ##bind subpath=traverse_subpath ##parameters=REQUEST=None,myLFSInstance ##title= myLFS = getattr(context, myLFSInstance) print myLFS['textfile.txt'](_.None,_) but I get the below Error-Message. If I try: print myLFS['textfile.txt'].data the content is returned but without rendering (as expected). Does anybody know how to get the rendering done? Thanks a lot for your help, Elena Error-Message: Error Type: AttributeError Error Value: __call__: Traceback (innermost last): File J:\Zope\ZopeSite\lib\python\ZPublisher\Publish.py, line 223, in publish_module File J:\Zope\ZopeSite\lib\python\ZPublisher\Publish.py, line 187, in publish File J:\Zope\ZopeSite\lib\python\Zope\__init__.py, line 226, in zpublisher_exception_hook (Object: jobs) File J:\Zope\ZopeSite\lib\python\ZPublisher\Publish.py, line 171, in publish File J:\Zope\ZopeSite\lib\python\ZPublisher\mapply.py, line 160, in mapply (Object: index.html) File J:\Zope\ZopeSite\lib\python\ZPublisher\Publish.py, line 112, in call_object (Object: index.html) File J:\Zope\ZopeSite\lib\python\OFS\DTMLMethod.py, line 199, in __call__ (Object: index.html) File J:\Zope\ZopeSite\lib\python\DocumentTemplate\DT_String.py, line 546, in __call__ (Object: index.html) File J:\Zope\ZopeSite\lib\python\OFS\DTMLMethod.py, line 192, in __call__ (Object: inhalt) File J:\Zope\ZopeSite\lib\python\DocumentTemplate\DT_String.py, line 546, in __call__ (Object: inhalt) File J:\Zope\ZopeSite\lib\python\DocumentTemplate\DT_Util.py, line 231, in eval (Object: getContentFiles(_.None,sourceFolder,contentFiles)) (Info: getContentFiles) File <string>, line 2, in f (Object: guarded_getattr) File J:\Zope\ZopeSite\lib\python\Shared\DC\Scripts\Bindings.py, line 324, in __call__ (Object: getContentFiles) File J:\Zope\ZopeSite\lib\python\Shared\DC\Scripts\Bindings.py, line 354, in _bindAndExec (Object: getContentFiles) File J:\Zope\ZopeSite\lib\python\Products\PythonScripts\PythonScript.py, line 363, in _exec (Object: getContentFiles) (Info: ({'script': <PythonScript instance at 01F468C0>, 'context': <Folder instance at 01F4D430>, 'container': <Folder instance at 01EF0530>, '_': <TemplateDict object at 01EB1A30>, 'traverse_subpath': []}, (None, 'stxTexte', ['jobs.txt']), {}, (None, None))) File Script (Python), line 16, in getContentFiles AttributeError: (see above)
Hi, Just to clarify, you have a .txt file and you want it to be rendered as if it was a DTML file with LocalFS. If so, I don' think that is going to work. See, the LocalFS product does not know the types of the files it serves, so it tries to guess them and then creates a wrapper object around the file to expose the functionality of the zope object normally associated with the object. Anyway, if it sees a .txt file, it will produce a file object (print the meta_type), if it would serve a .dtml file, it would serve a dtml object. So, in your case it serves a file object, which does not support the render method __call__ You could solve this by either renaming your file to .dtml, or you might have a try and change the type_map property of your localFS instance. associating .txt files with DTMLMethod/Document should make localFS produce the right kind of objects. Douwe
-----Original Message----- From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of Elena Schulz Sent: Monday, August 05, 2002 12:01 PM To: zope@zope.org Subject: [Zope] HowTo Render txt-Files with LocalFS in PythonScript?
Dear List,
with the following PythonScript I try to render the content of txt-Files with LocalFS according to the howto of LocalFS (...snip..." To insert the text of the file after evaluating it as a DTML template in the current namespace, you would use: <dtml-var "test['foo.dtml'](_.None, _)"> .....snip ...").
My Script:
## Script (Python) "getContentFiles" ##bind container=container ##bind context=context ##bind namespace=_ ##bind script=script ##bind subpath=traverse_subpath ##parameters=REQUEST=None,myLFSInstance ##title=
myLFS = getattr(context, myLFSInstance) print myLFS['textfile.txt'](_.None,_)
but I get the below Error-Message. If I try:
print myLFS['textfile.txt'].data
the content is returned but without rendering (as expected). Does anybody know how to get the rendering done?
Thanks a lot for your help,
Elena
Error-Message:
Error Type: AttributeError Error Value: __call__:
Traceback (innermost last): File J:\Zope\ZopeSite\lib\python\ZPublisher\Publish.py, line 223, in publish_module File J:\Zope\ZopeSite\lib\python\ZPublisher\Publish.py, line 187, in publish File J:\Zope\ZopeSite\lib\python\Zope\__init__.py, line 226, in zpublisher_exception_hook (Object: jobs) File J:\Zope\ZopeSite\lib\python\ZPublisher\Publish.py, line 171, in publish File J:\Zope\ZopeSite\lib\python\ZPublisher\mapply.py, line 160, in mapply (Object: index.html) File J:\Zope\ZopeSite\lib\python\ZPublisher\Publish.py, line 112, in call_object (Object: index.html) File J:\Zope\ZopeSite\lib\python\OFS\DTMLMethod.py, line 199, in __call__ (Object: index.html) File J:\Zope\ZopeSite\lib\python\DocumentTemplate\DT_String.py, line 546, in __call__ (Object: index.html) File J:\Zope\ZopeSite\lib\python\OFS\DTMLMethod.py, line 192, in __call__ (Object: inhalt) File J:\Zope\ZopeSite\lib\python\DocumentTemplate\DT_String.py, line 546, in __call__ (Object: inhalt) File J:\Zope\ZopeSite\lib\python\DocumentTemplate\DT_Util.py, line 231, in eval (Object: getContentFiles(_.None,sourceFolder,contentFiles)) (Info: getContentFiles) File <string>, line 2, in f (Object: guarded_getattr) File J:\Zope\ZopeSite\lib\python\Shared\DC\Scripts\Bindings.py, line 324, in __call__ (Object: getContentFiles) File J:\Zope\ZopeSite\lib\python\Shared\DC\Scripts\Bindings.py, line 354, in _bindAndExec (Object: getContentFiles) File J:\Zope\ZopeSite\lib\python\Products\PythonScripts\PythonScript.py, line 363, in _exec (Object: getContentFiles) (Info: ({'script': <PythonScript instance at 01F468C0>, 'context': <Folder instance at 01F4D430>, 'container': <Folder instance at 01EF0530>, '_': <TemplateDict object at 01EB1A30>, 'traverse_subpath': []}, (None, 'stxTexte', ['jobs.txt']), {}, (None, None))) File Script (Python), line 16, in getContentFiles AttributeError: (see above)
_______________________________________________ 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 )
Subject: [Zope] HowTo Render txt-Files with LocalFS in PythonScript?
the content is returned but without rendering (as expected). Does anybody know how to get the rendering done?
in globals which import HTMLFile you can also import HTML which takes a string as an argument. It can then render the text as if it was a normal HTMLFile. like:: # the normal way index_html = HTMLFile('path/to/file', globals()) # the other way aText = open('path/to/file', 'r').read() index_html = HTML(aText) I hope this is of some help. if you want to render an arbitrary textfile selected dynamically, this would probably work (untested): def fileRender(self, filename): aText = open(filename', 'r').read() # get the text theDtmlFile = HTML(aText) # create the page return theDtmlFile(self, self.REQUEST) # render it But my guess is that it can be pretty expensive to create a "HTML" object as the dtml will probably be parsed every time. So perhaps you should make some kind of dict to put the pages into. Like:: class HtmlTxtFile: def updateFiles(self): pages = {} for file in textFiles: f = open(file, 'r') aText = f.read() key = file[file.rfind('/')+1:-len('.txt')] pages[key] = HTML(aText) f.close() self._pages = pages def __getattr__(self, attr): return self._pages.get(attr)(self, self.REQUEST) regards max M -- "Sorry I would Really Like To Help More On This Project, But Am To Busy Doing Paid Work On A Tight Deadline" Max M
Hi Douwe, thanks for your help, I didn't know that LocalFS checks the ending and behaves accordingly. Now using print myLFS['textfile.dtml'](_.None, _) works well but another problem came up: if there is a <dtml-var id> in that textfile.dtml I get the following error. But an id-Property should be everywhere isn't it? So what to do so that the names in the context are found? Thanks again for your anwers and your patience, Elena Error Type: NameError Error Value: global name 'id' is not defined Traceback (innermost last): File J:\Zope\ZopeSite\lib\python\ZPublisher\Publish.py, line 223, in publish_module File J:\Zope\ZopeSite\lib\python\ZPublisher\Publish.py, line 187, in publish File J:\Zope\ZopeSite\lib\python\Zope\__init__.py, line 226, in zpublisher_exception_hook (Object: jobs) File J:\Zope\ZopeSite\lib\python\ZPublisher\Publish.py, line 171, in publish File J:\Zope\ZopeSite\lib\python\ZPublisher\mapply.py, line 160, in mapply (Object: index.html) File J:\Zope\ZopeSite\lib\python\ZPublisher\Publish.py, line 112, in call_object (Object: index.html) File J:\Zope\ZopeSite\lib\python\OFS\DTMLMethod.py, line 199, in __call__ (Object: index.html) File J:\Zope\ZopeSite\lib\python\DocumentTemplate\DT_String.py, line 546, in __call__ (Object: index.html) File J:\Zope\ZopeSite\lib\python\OFS\DTMLMethod.py, line 192, in __call__ (Object: inhalt) File J:\Zope\ZopeSite\lib\python\DocumentTemplate\DT_String.py, line 546, in __call__ (Object: inhalt) File J:\Zope\ZopeSite\lib\python\DocumentTemplate\DT_Util.py, line 231, in eval (Object: getContentFiles(_.None,sourceFolder,contentFiles)) (Info: getContentFiles) File <string>, line 2, in f (Object: guarded_getattr) File J:\Zope\ZopeSite\lib\python\Shared\DC\Scripts\Bindings.py, line 324, in __call__ (Object: getContentFiles) File J:\Zope\ZopeSite\lib\python\Shared\DC\Scripts\Bindings.py, line 354, in _bindAndExec (Object: getContentFiles) File J:\Zope\ZopeSite\lib\python\Products\PythonScripts\PythonScript.py, line 363, in _exec (Object: getContentFiles) (Info: ({'script': <PythonScript instance at 018BF5A0>, 'context': <Folder instance at 018DFE70>, 'container': <Folder instance at 01847B50>, '_': <TemplateDict object at 01F4B480>, 'traverse_subpath': []}, (None, 'stxTexte', ['jobs.dtml']), {}, (None, None))) File Script (Python), line 16, in getContentFiles File J:\Zope\ZopeSite\lib\python\OFS\DTMLMethod.py, line 192, in __call__ (Object: jobs.dtml) File J:\Zope\ZopeSite\lib\python\DocumentTemplate\DT_String.py, line 546, in __call__ (Object: jobs.dtml) File J:\Zope\ZopeSite\lib\python\DocumentTemplate\DT_Util.py, line 231, in eval (Object: id) (Info: id) File <string>, line 2, in f NameError: (see above) ----- Original Message ----- From: <douwe@oberon.nl> To: "Elena Schulz" <elena.schulz@gmx.net>; <zope@zope.org> Sent: Monday, August 05, 2002 12:44 PM Subject: RE: [Zope] HowTo Render txt-Files with LocalFS in PythonScript?
Hi,
Just to clarify, you have a .txt file and you want it to be rendered as if it was a DTML file with LocalFS.
If so, I don' think that is going to work. See, the LocalFS product does not know the types of the files it serves, so it tries to guess them and then creates a wrapper object around the file to expose the functionality of the zope object normally associated with the object. Anyway, if it sees a .txt file, it will produce a file object (print the meta_type), if it would serve a .dtml file, it would serve a dtml object. So, in your case it serves a file object, which does not support the render method __call__
You could solve this by either renaming your file to .dtml, or you might have a try and change the type_map property of your localFS instance. associating .txt files with DTMLMethod/Document should make localFS produce the right kind of objects.
Douwe
-----Original Message----- From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of Elena Schulz Sent: Monday, August 05, 2002 12:01 PM To: zope@zope.org Subject: [Zope] HowTo Render txt-Files with LocalFS in PythonScript?
Dear List,
with the following PythonScript I try to render the content of txt-Files with LocalFS according to the howto of LocalFS (...snip..." To insert the text of the file after evaluating it as a DTML template in the current namespace, you would use: <dtml-var "test['foo.dtml'](_.None, _)"> .....snip ...").
My Script:
## Script (Python) "getContentFiles" ##bind container=container ##bind context=context ##bind namespace=_ ##bind script=script ##bind subpath=traverse_subpath ##parameters=REQUEST=None,myLFSInstance ##title=
myLFS = getattr(context, myLFSInstance) print myLFS['textfile.txt'](_.None,_)
but I get the below Error-Message. If I try:
print myLFS['textfile.txt'].data
the content is returned but without rendering (as expected). Does anybody know how to get the rendering done?
Thanks a lot for your help,
Elena
Elena Schulz wrote:
Hi Douwe,
thanks for your help, I didn't know that LocalFS checks the ending and behaves accordingly. Now using
print myLFS['textfile.dtml'](_.None, _)
works well but another problem came up: if there is a <dtml-var id> in that textfile.dtml I get the following error. But an id-Property should be everywhere isn't it? So what to do so that the names in the context are found?
Thanks again for your anwers and your patience,
The attributes passed to a dtml page are: aDtmlFile(aClass, aDict, **keywordArguments) when you pass _.None as the object it cannot look up an id, as None has no attributes. If you are calling it from a Python script you should probably pass:: aDtmlFile(context, REQUEST) regards Max M -- "Sorry I would Really Like To Help More On This Project, But Am To Busy Doing Paid Work On A Tight Deadline" Max M
Hi Elena,
The attributes passed to a dtml page are:
aDtmlFile(aClass, aDict, **keywordArguments)
when you pass _.None as the object it cannot look up an id, as None has no attributes.
If you are calling it from a Python script you should probably pass::
aDtmlFile(context, REQUEST) That's correct. From DTML an earlier post recommends: <dtml-var "dtmlMethod(_.None,_, keyword_parameters_as_needed)">
Douwe
say youve got a structured-text file named 'textfile.txt' solution 1, dtml-only: <dtml-var "myLFS['textfile.txt'].data" fmt=structured-text> solution 2, pyscript and dtml: pyscript "test_py": ## Script (Python) "test_py" ##bind container=container ##bind context=context ##bind namespace= ##bind script=script ##bind subpath=traverse_subpath ##parameters= ##title= ## lfs = context.lfs test_dtml = context.test_dtml f = lfs['textfile.txt'] data = f.data print test_dtml(client=context, REQUEST=context.REQUEST, data=data) return printed dtml method "test_dtml": <dtml-var data fmt=structured-text> you can replace the string 'textfile.txt' with a variable of course. cheers, peter. Elena Schulz wrote:
Dear List,
with the following PythonScript I try to render the content of txt-Files with LocalFS according to the howto of LocalFS (...snip..." To insert the text of the file after evaluating it as a DTML template in the current namespace, you would use: <dtml-var "test['foo.dtml'](_.None, _)"> .....snip ...").
My Script:
## Script (Python) "getContentFiles" ##bind container=container ##bind context=context ##bind namespace=_ ##bind script=script ##bind subpath=traverse_subpath ##parameters=REQUEST=None,myLFSInstance ##title=
myLFS = getattr(context, myLFSInstance) print myLFS['textfile.txt'](_.None,_)
but I get the below Error-Message. If I try:
print myLFS['textfile.txt'].data
the content is returned but without rendering (as expected). Does anybody know how to get the rendering done?
Thanks a lot for your help,
Elena
Error-Message:
Error Type: AttributeError Error Value: __call__:
Traceback (innermost last): File J:\Zope\ZopeSite\lib\python\ZPublisher\Publish.py, line 223, in publish_module File J:\Zope\ZopeSite\lib\python\ZPublisher\Publish.py, line 187, in publish File J:\Zope\ZopeSite\lib\python\Zope\__init__.py, line 226, in zpublisher_exception_hook (Object: jobs) File J:\Zope\ZopeSite\lib\python\ZPublisher\Publish.py, line 171, in publish File J:\Zope\ZopeSite\lib\python\ZPublisher\mapply.py, line 160, in mapply (Object: index.html) File J:\Zope\ZopeSite\lib\python\ZPublisher\Publish.py, line 112, in call_object (Object: index.html) File J:\Zope\ZopeSite\lib\python\OFS\DTMLMethod.py, line 199, in __call__ (Object: index.html) File J:\Zope\ZopeSite\lib\python\DocumentTemplate\DT_String.py, line 546, in __call__ (Object: index.html) File J:\Zope\ZopeSite\lib\python\OFS\DTMLMethod.py, line 192, in __call__ (Object: inhalt) File J:\Zope\ZopeSite\lib\python\DocumentTemplate\DT_String.py, line 546, in __call__ (Object: inhalt) File J:\Zope\ZopeSite\lib\python\DocumentTemplate\DT_Util.py, line 231, in eval (Object: getContentFiles(_.None,sourceFolder,contentFiles)) (Info: getContentFiles) File <string>, line 2, in f (Object: guarded_getattr) File J:\Zope\ZopeSite\lib\python\Shared\DC\Scripts\Bindings.py, line 324, in __call__ (Object: getContentFiles) File J:\Zope\ZopeSite\lib\python\Shared\DC\Scripts\Bindings.py, line 354, in _bindAndExec (Object: getContentFiles) File J:\Zope\ZopeSite\lib\python\Products\PythonScripts\PythonScript.py, line 363, in _exec (Object: getContentFiles) (Info: ({'script': <PythonScript instance at 01F468C0>, 'context': <Folder instance at 01F4D430>, 'container': <Folder instance at 01EF0530>, '_': <TemplateDict object at 01EB1A30>, 'traverse_subpath': []}, (None, 'stxTexte', ['jobs.txt']), {}, (None, None))) File Script (Python), line 16, in getContentFiles AttributeError: (see above)
_______________________________________________ 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 )
participants (4)
-
douwe@oberon.nl -
Elena Schulz -
Max M -
peter sabaini