Mailing List Archive

Python Scripts
Hi everyone,

I am a newbie to Zope. I need to know where I can find information,
references and the like about python scripts. Do I need to know hardcore
python??

Thanks in advance.
Re: Python Scripts [ In reply to ]
Karl Munroe wrote:
>
> I am a newbie to Zope. I need to know where I can find information,
> references and the like about python scripts. Do I need to know hardcore
> python??

I wasn't aware there was such a thing...

Knowing python will help a lot, the following are your friends:

http://www.zope.org
http://www.python.org
http://zope.nipltd.com/public/lists.html

...and of course this list and the python list, but they respond better to
specific questions :-)

good luck,

Chris
RE: Python Scripts [ In reply to ]
Why not try a try/except statement, instead of an if/else... unless someone
knows of another way to do this...

Sean

-----Original Message-----
From: Peter Bengtsson [mailto:mail@peterbe.com]
Sent: Monday, May 28, 2001 3:15 AM
To: Tom Deprez; zope@zope.org
Subject: Re: [Zope] Python Scripts


What was wrong with
if hasattr(context,'coffeebreak'):
print "have coffee"
?

This is almost the equivalent of DTML's
<dtml-if coffeebreak>

Assuming your completely skip the REQUEST object out there.

It doesn't have to be 'context' like above or 'self' in External Method.
You can even do this one any Zope object.
if has(context.folder['document.html'], 'coffeebreak'):
print "had coffee"



----- Original Message -----
From: "Tom Deprez" <tom.deprez@uz.kuleuven.ac.be>
To: <zope@zope.org>
Sent: Monday, May 28, 2001 11:38 AM
Subject: [Zope] Python Scripts


> Hi,
>
> How can we in the python script check if the a certain variable exists in
> the namespace?
>
> ie. We can get the value of a variable by using context.myvar or
> context['myvar'], but both will return an error when the myvar doesn't
> exists. Now, how can you check if it exists?
>
> I tried several things, like context.hasattr('myvar') or
> context.has_key('myvar') , _hasattr(), _.hasattr() ... etc, but none seem
to
> work. Can somebody tell me if it even is possible? Thanks.
>
> Tom.
>
>
> _______________________________________________
> 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 )


_______________________________________________
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 )
Re: Python Scripts [ In reply to ]
> ----- Original Message -----
> From: "Tom Deprez" <tom.deprez@uz.kuleuven.ac.be>
> To: <zope@zope.org>
> Sent: Monday, May 28, 2001 11:38 AM
> Subject: [Zope] Python Scripts
>
> > Hi,
> >
> > How can we in the python script check if the a certain variable exists in
> > the namespace?
> >
> > ie. We can get the value of a variable by using context.myvar or
> > context['myvar'], but both will return an error when the myvar doesn't
> > exists. Now, how can you check if it exists?
> >
> > I tried several things, like context.hasattr('myvar') or
> > context.has_key('myvar') , _hasattr(), _.hasattr() ... etc, but none seem
> to
> > work. Can somebody tell me if it even is possible? Thanks.
> >
> > Tom.
> >
> >

perhaps try:

hasattr(context, 'myvar')
_.has_key('my_var')

--
| Casey Duncan
| Kaivo, Inc.
| cduncan@kaivo.com
`------------------>
Re: Python Scripts [ In reply to ]
Thanks Chris .... just what I needed. Now if I can only solve the equivalent
problem for ZDisccussions, a mixed product done as a ZClass with Python Base
Classes, I'll be totally happy!
Re: Python Scripts [ In reply to ]
In answer to my query,"Chris McDonough" <chrism@zope.com> wrote:

> This works:
>
> context.manage_addProduct['PythonScripts'].manage_addPythonScript('bar
> ')

and indeed it does. But there's another problem--I'm copying a Python script
and that requires special magic -- the source script needs to be deconstructed
and the destination constructed. I don't see a neat way to do that with te
API. Any hints?

-dra
Re: Python Scripts [ In reply to ]
There's a method named "read" on PythonScript objects. It returns the
source of the Python Script including the "##" header lines. It's
available to users who have a role that includes the "View management
screens" permissions. Note also that the constructor for a Python
Script (manage_addPythonScript) takes an argument "REQUEST". The
constructor looks for the "file" variable in REQUEST for the text to
the new script.

So let's say you want to add a parameter to the "new" Python Script.
If you create a Python Script with a proxy role of "Manager" that
includes the following, any user who runs it will create a new script
named 'new_script' with the text of 'old_script' with a new (fixed)
parameter added to the parameter list:

old_text = context.old_script.read()
new_lines = old_text.split('\n')
new_lines = []
for line in lines:
if line.startswith('##') and line.find('parameters=') != -1:
# this tacks foo=1 on to the parameter list
line = '%s,%s' % (line, 'foo=1')
new_lines.append(line)
new_text = '\n'.join(new_lines)
request = context.REQUEST
request.set('file', new_text)
context.manage_addPythonScript('new_script', REQUEST)
# defeat the redirect caused by using manage_addPythonScript
request.RESPONSE.setStatus(200)
request.RESPONSE.setHeader('Location', '')

Slice and dice as required. ;-)

HTH,

- C


----- Original Message -----
From: "Dennis Allison" <allison@sumeru.stanford.EDU>
To: <allison@sumeru.stanford.EDU>; <chrism@zope.com>; <zope@zope.org>
Sent: Saturday, July 27, 2002 11:04 PM
Subject: Re: [Zope] Python Scripts


> In answer to my query,"Chris McDonough" <chrism@zope.com> wrote:
>
> > This works:
> >
> >
context.manage_addProduct['PythonScripts'].manage_addPythonScript('bar
> > ')
>
> and indeed it does. But there's another problem--I'm copying a
Python script
> and that requires special magic -- the source script needs to be
deconstructed
> and the destination constructed. I don't see a neat way to do that
with te
> API. Any hints?
>
> -dra
>
>
Re: Python Scripts [ In reply to ]
Right. Thanks.... just the magic needed to solve the problem.

>
> There's a method named "read" on PythonScript objects. It returns the
> source of the Python Script including the "##" header lines. It's
> available to users who have a role that includes the "View management
> screens" permissions. Note also that the constructor for a Python
> Script (manage_addPythonScript) takes an argument "REQUEST". The
> constructor looks for the "file" variable in REQUEST for the text to
> the new script.
>
> So let's say you want to add a parameter to the "new" Python Script.
> If you create a Python Script with a proxy role of "Manager" that
> includes the following, any user who runs it will create a new script
> named 'new_script' with the text of 'old_script' with a new (fixed)
> parameter added to the parameter list:
>
> old_text = context.old_script.read()
> new_lines = old_text.split('\n')
> new_lines = []
> for line in lines:
> if line.startswith('##') and line.find('parameters=') != -1:
> # this tacks foo=1 on to the parameter list
> line = '%s,%s' % (line, 'foo=1')
> new_lines.append(line)
> new_text = '\n'.join(new_lines)
> request = context.REQUEST
> request.set('file', new_text)
> context.manage_addPythonScript('new_script', REQUEST)
> # defeat the redirect caused by using manage_addPythonScript
> request.RESPONSE.setStatus(200)
> request.RESPONSE.setHeader('Location', '')
>
> Slice and dice as required. ;-)
>
Re: Python Scripts [ In reply to ]
> We have been seeing a number of instances where python scripts fail
> due to an apparent "syntax error" but the syntax is correct and simply
> storing the method restores it to functionality. Anyone else seeing
> this?

How do you mean "fail"?

Often times, if you have an error, save, test, and then use the back
button, you'll see the old syntax error, even though the contents are
the new (and correct) version.

If you just revisit the script (click on the id in the breadcrumbs) the
message will go away.

--jcc
--
"Building Websites with Plone"
http://plonebook.packtpub.com
_______________________________________________
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 )
Re: Python Scripts [ In reply to ]
Yeah, I do see that every once in a while. I have a very simple
script, that looks perfect, but will always return "syntax error". I
copied the text, pasted into a text editor and checked all the indents
and tabs, then recreated the script. Problem went away.

What was interesting is that I didnt change the code. And even
thought the indents were the same, re-adding them might have solved
it. Dont know for sure, but I dont see this enough to cause me
problems.

Greg

On 6/16/05, J Cameron Cooper <zope-l@jcameroncooper.com> wrote:
> > We have been seeing a number of instances where python scripts fail
> > due to an apparent "syntax error" but the syntax is correct and simply
> > storing the method restores it to functionality. Anyone else seeing
> > this?
>
> How do you mean "fail"?
>
> Often times, if you have an error, save, test, and then use the back
> button, you'll see the old syntax error, even though the contents are
> the new (and correct) version.
>
> If you just revisit the script (click on the id in the breadcrumbs) the
> message will go away.
>
> --jcc
> --
> "Building Websites with Plone"
> http://plonebook.packtpub.com
> _______________________________________________
> 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 )
>


--
Greg Fischer
1st Byte Solutions
http://www.1stbyte.com
_______________________________________________
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 )
Re: Python Scripts [ In reply to ]
Dennis Allison wrote at 2005-6-16 09:06 -0700:
>We have been seeing a number of instances where python scripts fail due to
>an apparent "syntax error" but the syntax is correct and simply storing
>the method restores it to functionality. Anyone else seeing this?

I saw this today.

I expect DOS type line endings.

Your browser (or maybe the ":text" converter in ZPublisher) will remove
them. Therefore, they disappear when you "store" again.

--
Dieter
_______________________________________________
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 )
Re: Python Scripts [ In reply to ]
Dieter Maurer wrote:
>
> I saw this today.
>
> I expect DOS type line endings.
>
> Your browser (or maybe the ":text" converter in ZPublisher) will remove
> them. Therefore, they disappear when you "store" again.

Indeed, but should ZPT Python Scripts really be so sensitive to line
endings?

Chris

--
Simplistix - Content Management, Zope & Python Consulting
- http://www.simplistix.co.uk

_______________________________________________
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 )
Re: Python scripts [ In reply to ]
That works great, thanks. So there is no way to do this across, say, a
folder with hundreds of scripts in without duplicating the code in each
individually?


On 06/07/12 13:30, Laurence Rowe wrote:
> On 6 July 2012 14:09, Richard Harley<richard@scholarpack.com> wrote:
>> On Zope 2.10 is there a simple/universal way to only allow python scripts to
>> be called by DTML methods or other python scripts and not directly TTW?
> You can check that the script is not the published object with:
>
> if container.REQUEST['PUBLISHED'] is script:
> raise 'Forbidden'
>
> For newer versions of Zope raise an exception object:
>
> from zExceptions import Forbidden
> if container.REQUEST['PUBLISHED'] is script:
> raise Forbidden('Script may not be published.')
>
> Laurence
Re: Python scripts [ In reply to ]
On 6 July 2012 16:36, Richard Harley <richard@scholarpack.com> wrote:
> That works great, thanks. So there is no way to do this across, say, a
> folder with hundreds of scripts in without duplicating the code in each
> individually?

For one Plone hotfix we took the approach of blacklisting certain
scripts by monkey-patching Bindings._bindAndExec (Bindings is a
superclass of PythonScript):

from Shared.DC.Scripts.Bindings import Bindings
from zExceptions import Forbidden

DO_NOT_PUBLISH = [
'script_id',
...
]

def _patched_bindAndExec(self, args, kw, caller_namespace):
'''Prepares the bound information and calls _exec(), possibly
with a namespace.
'''
template_id = hasattr(self, 'getId') and self.getId() or ''
request = getattr(self, 'REQUEST', None)
if (template_id and request and template_id in DO_NOT_PUBLISH and
request.get('PUBLISHED') is self):
raise Forbidden('Script may not be published.')
return self._original_bindAndExec(args, kw, caller_namespace)

Bindings._original_bindAndExec = Bindings._bindAndExec
Bindings._bindAndExec = _patched_bindAndExec

You could create an unpublishable subclass of PythonScript using a
similar technique. Ideally PythonScripts would opt in to being
publishable based on some metadata option.

Laurence
_______________________________________________
Zope maillist - Zope@zope.org
https://mail.zope.org/mailman/listinfo/zope
** No cross posts or HTML encoding! **
(Related lists -
https://mail.zope.org/mailman/listinfo/zope-announce
https://mail.zope.org/mailman/listinfo/zope-dev )