2

How to print in a .cpy file (python) ? I'm using Zope/Plone and I've just started with Python. I've tried this

import logging

logger = logging.getLogger()
logger.info("hello plone")

But it doesn't work.

Thank you for your answer

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user1499220
  • 419
  • 4
  • 12
  • 23

2 Answers2

2

"It doesnt' work" is awfully vague, but your problem is probably a violation of the security sandbox imposed on Python used in scripts that may be edited through the web. "Restricted Python" limits your imports to modules that have been audited to assure that they don't have nasty side effects -- like dumping noise into logs. See http://wiki.zope.org/zope2/PythonScripts for details on Restricted Python.

The general solution to this kind of problem is to build your functionality in unrestricted Python in a Python package. A Zope named utility is the usual mechanism for providing this kind of functionality, and you'll be able to reach the utility's operations from restricted Python by traversing to the named utility.

SteveM
  • 6,058
  • 1
  • 16
  • 20
  • Thank your for your answer. But I don't really understand what to do to solve the problem. Here is my python script : I should add some functionalities to this script (which isn't mine). And for this, I need to print to facilitate the debug. There are two files.log but I didn't find my "hello plone" in any of them . Thank you for your help – user1499220 Jul 19 '12 at 16:52
  • ## Script (Python) "pwreset_action.cpy" ##bind container=container ##bind context=context ##bind namespace= ##bind script=script ##bind subpath=traverse_subpath ##title=Reset a user's password ##parameters=randomstring, userid=None, password=None, password2=None from Products.CMFCore.utils import getToolByName from Products.PasswordResetTool.PasswordResetTool import InvalidRequestError, ExpiredRequestError import logging status = "success" – user1499220 Jul 19 '12 at 16:54
2

The answer above means that you cannot import any modules in RestrictedPython scripts which are through-the-web editable Plone scripts. These scripts have end-user permissions, so they are not allowed to run arbitrary Python code.

http://collective-docs.readthedocs.org/en/latest/security/sandboxing.html

You can use context.plone_log("mystring") style logging in restricted python scripts for logging purposes.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435