Making a self written module available to Zope
If I have a self-written module/package which contains some module level methods, how can I make it available for use in the rest of Zope (e.g. in page templates, python script objects etc.)? Because it is just some utility functions, like the zope built-in sequence module, I don't want to register them as a zope class with all the manage* methods and create zope object from that. And I want the module to be initialized once, upon the start of zope or refresh of product maybe, to read some configuration file on the file system. What's the simplest way to achieve this? Greetings, Hong Yuan
look at lib/python/Products/PythonScripts/README.txt or learn about using ModuleSecurityInfo(). -aj --On Samstag, 22. Januar 2005 0:19 Uhr +0800 Hong Yaun <hongyuan@homemaster.cn> wrote:
If I have a self-written module/package which contains some module level methods, how can I make it available for use in the rest of Zope (e.g. in page templates, python script objects etc.)?
Because it is just some utility functions, like the zope built-in sequence module, I don't want to register them as a zope class with all the manage* methods and create zope object from that. And I want the module to be initialized once, upon the start of zope or refresh of product maybe, to read some configuration file on the file system. What's the simplest way to achieve this?
Greetings, Hong Yuan
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
On Sat, Jan 22, 2005 at 12:19:11AM +0800, Hong Yaun wrote:
If I have a self-written module/package which contains some module level methods, how can I make it available for use in the rest of Zope (e.g. in page templates, python script objects etc.)?
Because it is just some utility functions, like the zope built-in sequence module, I don't want to register them as a zope class with all the manage* methods and create zope object from that. And I want the module to be initialized once, upon the start of zope or refresh of product maybe, to read some configuration file on the file system. What's the simplest way to achieve this?
a trivial example: # this is Products/MyFunctions/__init__.py from AccessControl import ModuleSecurityInfo def trivialFunc(): return True def anotherFunc(): return False ModuleSecurityInfo('Products.MyFunctions').declarePublic( 'trivialFunc', 'anotherFunc' ) # EOF Note that you cannot declare module-level functions protected by a particular permission; you can only declare them public. See lib/python/AccessControl/SecurityInfo.py. You may also find AccessControl.allow_module() useful. It's defined in that same file. -- Paul Winkler http://www.slinkp.com
Paul Winkler wrote:
a trivial example:
# this is Products/MyFunctions/__init__.py
from AccessControl import ModuleSecurityInfo
def trivialFunc(): return True
def anotherFunc(): return False
ModuleSecurityInfo('Products.MyFunctions').declarePublic( 'trivialFunc', 'anotherFunc' )
# EOF
Thanks for the sample and now I can access trivialFunc() from within a template file using: <p tal:content="python:modules['Products.MyFunctions'].trivialFunc()"></p> It works fine, just somewhat cumbersome. One way to simplify its usage is to use tal:define, like: <p tal:define="func python:modules['Products.MyFunctions'].trivialFunc" tal:content="func()"></p> But this would require using tal:define in every template file that could access the function. Is there any other way to simplify the access to the function? Best Regards Hong Yuan
Am Montag, den 24.01.2005, 00:20 +0800 schrieb Hong Yaun:
Paul Winkler wrote:
a trivial example:
# this is Products/MyFunctions/__init__.py
from AccessControl import ModuleSecurityInfo
def trivialFunc(): return True
def anotherFunc(): return False
ModuleSecurityInfo('Products.MyFunctions').declarePublic( 'trivialFunc', 'anotherFunc' )
# EOF
Thanks for the sample and now I can access trivialFunc() from within a template file using:
<p tal:content="python:modules['Products.MyFunctions'].trivialFunc()"></p>
It works fine, just somewhat cumbersome. One way to simplify its usage is to use tal:define, like:
<p tal:define="func python:modules['Products.MyFunctions'].trivialFunc" tal:content="func()"></p>
But this would require using tal:define in every template file that could access the function.
Is there any other way to simplify the access to the function?
Yes, simply dont write application code in pagetemplates :-) You can either stuff your complete API in context by defining useful classes and methods or do the support scripting in one or some python scripts. Regards Tino
participants (4)
-
Andreas Jung -
Hong Yaun -
Paul Winkler -
Tino Wildenhain