Log in |
Creating a Script class*This is version 1.0 (stable) of this How-To. For the "development version", check out http://www.zope.org/Members/lalo/scriptclass-dev - with space for comments, even.* What Scripts (found in This How-To aims to tutor a Python/Zope developer who can already develop Python-based Products and classes, to use the Script API. Why and WhenBecause it's fun? ;-) Seriously, why would you want to use this new API?
If you scored one point, you should think seriously. If you scored more than one, then this is definitely the API for you. HowOkay, so you're still reading. Let's get our hands dirty. Reference code In case of doubt, you can read the sources to What to subclass Your class has to be a subclass of Manage optionsIt is conventional that the first tab edits the script, and the second one tests it. Instead of redefining the others, you may want to simply add them, like in this example (taken from manage_options = ( {'label':'Edit', 'action':'ZPythonScriptHTML_editForm', 'help': ('PythonScripts', 'PythonScript_edit.stx')}, ) + BindingsUI.manage_options + ( {'label':'Test', 'action':'ZScriptHTML_tryForm', 'help': ('PythonScripts', 'PythonScript_test.stx')}, {'label':'Proxy', 'action':'manage_proxyForm', 'help': ('OFSP','DTML-DocumentOrMethod_Proxy.stx')}, ) + Historical.manage_options + SimpleItem.manage_options + \ Cacheable.manage_options The "Try" tab You can use the provided For this to work, you have to define a method with the signature 'ZScriptHTML_tryParams(self)'; this method should return a list of strings, that will be used to input the parameters (arguments). If type matters, you can use form variable type conversions in these strings. The user can also edit the "Parameter" inputs, to add these conversions. Just return something like Initializing the instanceBefore you first call your object, you must initialize the bindings (if you don't know what they are, read the help for PythonScript and then come back). You'll probably be surprised to know that there aren't any defaults for the bindings, so you'll have to set them up yourself. You do that by calling Well, actuall there is a set of standard bindings; they simply are not enforced by the code, you have to bind them yourself. They live at defaultBindings = {'name_context': 'context', 'name_container': 'container', 'name_m_self': 'script', 'name_ns': '', 'name_subpath': 'traverse_subpath'} Both PythonScript and ZopePageTemplate store a "cooked" (intermediate code) version of the script in a volatile attribute, for performance reasons. It is probably a good idea to follow this pattern. In this case, it is advisable to "pre-cook" this representation in the initialization, too. What to override Of course, it's usually a good idea to override However, for the Script API itself, the only method you need to override is def _exec(self, bound_names, args, kw): Here, It is advisable to take any additional security measures you conceive here. For examples, look (again) at the source. The Optional hooks
How to use your object from Python code After all this work, your object will be treated just like a Python function. You can call it from Python Scripts or Page Templates or even DTML So, if your instance runs with no parameters, you can do just |