Interfaces to imported python modules
I seem to be having a problem accessing attributes and methods of objects created inside a custom Zope product from python scripts and ZPTs. I am not sure whether this is a misunderstanding of the security access rules described in the Zope Developer's Guide, or perhaps just the wrong way to think about exposing python objects to python scripts and ZPTs. The basic scenario in my product module is this from SomeWhereElse import ObjectA Class MyProduct( Folder.Folder, Persistent, Implicit ) def __init__( self ): self.dataObjects = [] def createAndAddDataObject( self ): self.dataObjects.append( Data( ) ) def getDataObjects( self ): """ return the dara """ return self.dataObjects Class Data: def __init__( self ): self.data = ObjectA() ---------- So not very interesting, and I have skipped all the usual product and _p_changed bits, I'm simply demonstrating the relationship between product class, a custom data class, and classes from SomeWhereElse. in SomeWhereElse, ObjectA creates and holds references to many other Objects of many different classes. The problem is that I can call a method such as getDataObjects from a python script and get back a list of objects of type Data. However I cannot access the attributes or methods of the ".data" attributes of these Data objects. No amount of setting the following in either the Product class or the Data class helps. __allow_access_to_unprotected_subobjects__=1 security = ClassSecurityInfo() security.setDefaultAccess("allow") The only option I feel I have is to explicitly define in Data or MyProduct getters/callers for all the methods and attributes of the classes defined in SomeWhereElse using proxy classes with the appropriate security settings set. This seems a bit cumbersome. Any pointers would be much appreciated. regards Matt
Matt wrote at 2003-7-23 18:32 +1200:
... I seem to be having a problem accessing attributes and methods of objects created inside a custom Zope product from python scripts and ZPTs.
Whenever you have authorization problems, Shane's "VerboseSecurity" may help you.
... def createAndAddDataObject( self ): self.dataObjects.append( Data( ) )
def getDataObjects( self ): """ return the dara """ return self.dataObjects
Class Data:
def __init__( self ): self.data = ObjectA() ... The problem is that I can call a method such as getDataObjects from a python script and get back a list of objects of type Data. However I cannot access the attributes or methods of the ".data" attributes of these Data objects.
Your "ObjectA" instances must have security declarations. Unless they derive from "Acquisition.[Im|Ex]plicite", they must be public. The ZDG should provide the details. Dieter
So after some experimentation I found I need to create access controlled proxy objects to all object definitions in a module I want to use which is in site-packages of the python distribution. It was lucky that I used a factory in that external module. But perhaps there is a simpler way. In the example below I had Class Data creating an ObjectA() and assigning it to self.data. This object actually references many other objects of different classes which in turn reference many other types of objects. If I had an instance of Data available to a python script, say called data, and I wanted to go data.objectRefB.objectRefC.someMethod() where objectRefC is an attribute of the object that is the value of objectRefB, then I also need to proxy that second object class so that it is available to the python script in Zope. Is there any way to proxy only ObjectA and tell it to make public all interfaces of any object that can be referenced through it? I can't seem to find information on the ZDG that suggests I can. regards Matt Dieter Maurer wrote:
Matt wrote at 2003-7-23 18:32 +1200:
... I seem to be having a problem accessing attributes and methods of objects created inside a custom Zope product from python scripts and ZPTs.
Whenever you have authorization problems, Shane's "VerboseSecurity" may help you.
... def createAndAddDataObject( self ): self.dataObjects.append( Data( ) )
def getDataObjects( self ): """ return the dara """ return self.dataObjects
Class Data:
def __init__( self ): self.data = ObjectA() ... The problem is that I can call a method such as getDataObjects from a python script and get back a list of objects of type Data. However I cannot access the attributes or methods of the ".data" attributes of these Data objects.
Your "ObjectA" instances must have security declarations.
Unless they derive from "Acquisition.[Im|Ex]plicite", they must be public.
The ZDG should provide the details.
Dieter
Matt wrote at 2003-7-24 17:04 +1200:
... making objects accessible for TTW code ... ... If I had an instance of Data available to a python script, say called data, and I wanted to go
data.objectRefB.objectRefC.someMethod()
where objectRefC is an attribute of the object that is the value of objectRefB,
then I also need to proxy that second object class so that it is available to the python script in Zope.
Provided your classes are true Python classes and not types (implemented in "C"), then you do not need proxying but can use a so called "monkey patch" to add the security declarations at startup time to your classes. Search "zope.org" for "monkey patch".
Is there any way to proxy only ObjectA and tell it to make public all interfaces of any object that can be referenced through it? I can't seem to find information on the ZDG that suggests I can.
You could use dynamic proxying (something what "ExtensionClass" and "Acquisition" does) but you probably do not want to do this. Dieter
Matt wrote:
available to the python script in Zope. Is there any way to proxy only ObjectA and tell it to make public all interfaces of any object that can be referenced through it?
Don't think so. That wouldn't be very secure would it? How about actually giving your other classes security definitions rather than creating all these proxies? cheers, Chris
participants (3)
-
Chris Withers -
Dieter Maurer -
Matt