RE: [Zope-dev] Accessing root object
From: Robert Sander [mailto:zope-dev@beteigeuze.cs.tu-berlin.de]
On Thu, Feb 10, 2000 at 03:40:38PM -0000, Phil Harris wrote:
I think there's something called app?
It's declared in <zopedir>/lib/python/Main.py.
That may be what your looking for??
I don't know, the object app is not in the global namespace when __init__.py of my Product is called.
It may be the same problem with the discussed scheduler product when creating another thread, b ut I don't know.
Here's what I'd do (in __init__.py): def initialize(context): app = context._ProductContext__app # Don't you love private name mangling if not hasattr(app, #IdForObject#): object = Object() app._setObject(#IdForObject#, object) get_transaction().note('Added Object') get_transaction().commit() context._ProductContext__app is the root object. #IdForObject# is a string ID for the object. -- Martijn Pieters, Software Engineer | Digital Creations http://www.digicool.com | Creators of Zope http://www.zope.org | mailto:mj@digicool.com ICQ: 4532236 | PGP: http://wwwkeys.nl.pgp.net:11371/pks/lookup?op=get&search=0xA8A32149 -------------------------------------------
I can't figure out the opposite angle on this. In __init__.py, my product creates a root-level object named 'S' with Martijn's method below. Now another module 'D' needs to look up 'S' and get a reference to the object so it can call one of its methods "S.next". But I don't see how to look up 'S'. I'm sure I'm just being dense. Can somebody help? -- Loren
From: Robert Sander [mailto:zope-dev@beteigeuze.cs.tu-berlin.de]
On Thu, Feb 10, 2000 at 03:40:38PM -0000, Phil Harris wrote:
I think there's something called app?
It's declared in <zopedir>/lib/python/Main.py.
That may be what your looking for??
I don't know, the object app is not in the global namespace when __init__.py of my Product is called.
From: Martijn Pieters <mj@digicool.com>
Here's what I'd do (in __init__.py):
def initialize(context): app = context._ProductContext__app # Don't you love private name mangling if not hasattr(app, #IdForObject#): object = Object() app._setObject(#IdForObject#, object) get_transaction().note('Added Object') get_transaction().commit()
context._ProductContext__app is the root object. #IdForObject# is a string ID for the object.
-- Martijn Pieters, Software Engineer | Digital Creations http://www.digicool.com | Creators of Zope http://www.zope.org | mailto:mj@digicool.com ICQ: 4532236 | PGP: http://wwwkeys.nl.pgp.net:11371/pks/lookup?op=get&search=0xA8A32149 -------------------------------------------
_______________________________________________ Zope-Dev maillist - Zope-Dev@zope.org http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
----- Original Message ----- From: Loren Stafford <lstaffor@dynalogic.com>
I can't figure out the opposite angle on this. In __init__.py, my product creates a root-level object named 'S' with Martijn's method below. Now another module 'D' needs to look up 'S' and get a reference to the object so it can call one of its methods "S.next". But I don't see how to look up 'S'. I'm sure I'm just being dense. Can somebody help?
Sure! There are a few ways to do it, but one is to add a pair of lines here...
def initialize(context): app = context._ProductContext__app # Don't you love private name mangling global object object = getattr(app, #IdForObject#, None) if object is None: object = Object() app._setObject(#IdForObject#, object) get_transaction().note('Added Object') get_transaction().commit()
...and in your other Product(s) do... from Products.#NameOfFirstProduct# import object object.next() Cheers, Evan @ 4-am & digicool
On Fri, Feb 18, 2000 at 12:55:49AM -0600, Evan Simpson wrote:
from Products.#NameOfFirstProduct# import object object.next()
OK, this goes for product global and unique objects. What if I am instanziating a product and want to access this object from subfolders of the object. I mean something like this: / + | +- ProdInst1 | | | +-Folder1.1 | +-Folder1.2 | +- ProdInst2 | +-Folder2.1 | +-Folder2.1.1 The product has some dtml-methods as attributes set with manage_addDTMLMethod at instanziation. How do I access this DTML methods from Folder2.1.1? They are to be edited differently between ProdInst1 and ProdInst2 so that I cannot use Product global objects. I feel like I am missing some basics here... Greetings -- Robert Sander www.gurubert.de
On Fri, Feb 18, 2000 at 11:53:10AM +0100, Robert Sander wrote:
I feel like I am missing some basics here...
I think I was missing the aq_parent attribute of every ZOPE object. I have this from the source in SimpleItem.py, is it documented somewhere else? Greetings -- Robert Sander www.gurubert.de
Hi! OK, I learned that I can use self.aq_parent to get the parent object of self. I defined the following: from Acquisition import Implicit from AccessControl.Role import RoleManager from OFS.Folder import Folder from OFS.SimpleItem import Item from Globals import Persistent class One(Folder, Persistent, Implicit, RoleManager): def uno(self): return self attribute = "some text for example" class Two(Folder, Persistent, Implicit, RoleManager): def uno(self): return self.aq_parent.uno() class Three(Implicit, Persistent, RoleManager, Item): def uno(self): return self.aq_parent.uno() def output(self): print self.uno().attribute If I have a treelike-structure I should now be able to access my "toplevel" object which is of the class One from any objects of the class Three that are contained in objects of class Two, which are contained in an object of class One. But the call to Two.uno() raises an AttributeError, aq_parent is not know. In objects of class Three self.aq_parent raises no error. Is the Folder class the problem here? This is the only difference between Three and Two. Any help is appreciated. Greetings -- Robert Sander www.gurubert.de
Hi ! I am solving the problems for myself ;-) The object of class Two was already instanziated but I haven't set it to be a child object of my object of class One, so no acquisition was possible. It seems that this does not work, too. The object from class One can acquire its parents, I can _setObject an object two of class Two in it, but when I want to acquire anything from this object two, there is nothing ... Greetings -- Robert Sander www.gurubert.de
On Fri, Feb 18, 2000 at 04:19:22PM +0100, Robert Sander wrote:
It seems that this does not work, too. The object from class One can acquire its parents, I can _setObject an object two of class Two in it, but when I want to acquire anything from this object two, there is nothing ...
I created a workaround with: one._setObject(id, two) two.aq_parent = one But I do not think that this is usual method in ZOPE. Greetings -- Robert Sander www.gurubert.de
Hi! Ok, in a recent message I saw self.aq_acquire("some object"), that is extactly what I meant and needed. Why are the Acquisition classes so badly documented? Acquisition is a .so loadable module (for performance reasons, I presume) and I do not want to look into its C code to imagine what is done in Python ... Searching www.zope.org for "Acquisition" did not reveal anything better. OK, after disclaiming a little bit, I still have a problem: from Acquisition import Implicit from AccessControl.Role import RoleManager from OFS.Folder import Folder from Globals import Persistent class One(Implicit, Persistent, RoleManager, Folder): attribute = "Some text or something else" def somemethod(self): ob = Two() self._setObject("id", ob) ob.method() class Two(Implicit, Persistent, RoleManager, Folder): def method(self): print self.aq_acquire("attribute") one = One() one.somemethod() Leads toward an AttributeError because aq_acquire was not found in Two.method() Any help is very appreciated. Greetings -- Robert Sander www.gurubert.de
----- Original Message ----- From: Robert Sander <gurubert@gurubert.de>
class One(Implicit, Persistent, RoleManager, Folder): attribute = "Some text or something else" def somemethod(self): ob = Two() self._setObject("id", ob) ob.method()
class Two(Implicit, Persistent, RoleManager, Folder): def method(self): print self.aq_acquire("attribute")
one = One() one.somemethod()
Leads toward an AttributeError because aq_acquire was not found in Two.method()
I'm afraid this isn't going to improve your opinion of the documentation <wink>. Your Two instance doesn't have aq_acquire because it's not in an acquisition wrapper; It would only get one by being acquired, or by adding... ob = ob.__of__(self) ...before the 'ob.method()' line. Searching www.zope.org on Acquisition gets me quite a few documents, including Jim Fulton's http://www.zope.org/Members/jim/Info/IPC8/AqAlgNews Admittedly, none of them seems to talk about the aq_* methods. You don't have to hit the C source, though; See lib/Components/ExtensionClass/*.stx (docs in structured text). Cheers, Evan @ 4-am & digicool
On Thu, Feb 24, 2000 at 07:38:59AM -0600, Evan Simpson wrote:
class One(Implicit, Persistent, RoleManager, Folder): attribute = "Some text or something else" def somemethod(self): ob = Two() self._setObject("id", ob) ob.method()
class Two(Implicit, Persistent, RoleManager, Folder): def method(self): print self.aq_acquire("attribute")
one = One() one.somemethod()
Leads toward an AttributeError because aq_acquire was not found in Two.method()
I'm afraid this isn't going to improve your opinion of the documentation <wink>. Your Two instance doesn't have aq_acquire because it's not in an acquisition wrapper; It would only get one by being acquired, or by adding...
ob = ob.__of__(self)
...before the 'ob.method()' line.
That works now, and it improves my opinion about this mailing list and its involved members ;-)
Searching www.zope.org on Acquisition gets me quite a few documents, including Jim Fulton's http://www.zope.org/Members/jim/Info/IPC8/AqAlgNews
I found these documents already, but they do not help very much.
Admittedly, none of them seems to talk about the aq_* methods. You don't have to hit the C source, though; See lib/Components/ExtensionClass/*.stx (docs in structured text).
Ah, some docs! ;-) I have them in ./lib/python/StructuredText/regressions/Acquisition.stx and did not know anything about them ... Now I have my classes subclassed from Acquistion.Implicit, therefore I do not need to aq_acquire, which is explicit, I think. The part where I begin to wonder is my third class: class Three(Implicit, Persistent, RoleManager, OFS.SimpleItem.Item): def method(self): print self.aq_acquire("something") whose objects are children to objects of Two. I do not get AttributeErrors here, even if I do not do "three = three.__of__(two)" ... Greetings -- Robert Sander www.gurubert.de
----- Original Message ----- From: Robert Sander <gurubert@gurubert.de>
Now I have my classes subclassed from Acquistion.Implicit, therefore I do not need to aq_acquire, which is explicit, I think.
Yep. In Zope, at least, it's still handy when the thing you are acquiring has a non-identifier name.
The part where I begin to wonder is my third class:
class Three(Implicit, Persistent, RoleManager, OFS.SimpleItem.Item): def method(self): print self.aq_acquire("something")
whose objects are children to objects of Two. I do not get AttributeErrors here, even if I do not do "three = three.__of__(two)" ...
When you say "are children", do you mean that you are doing something like "aTwo.aThree.method()"? If so, the process of fetching 'aThree' from 'aTwo' gives it an acquisition wrapper, making explicit wrapping unnecessary. Cheers, Evan @ 4-am & digicool
On Thu, Feb 24, 2000 at 08:34:26AM -0600, Evan Simpson wrote:
When you say "are children", do you mean that you are doing something like "aTwo.aThree.method()"? If so, the process of fetching 'aThree' from 'aTwo' gives it an acquisition wrapper, making explicit wrapping unnecessary.
No, I do it like in the example with one and two. I do let a two instance a three and make a two._setObject("id", three) and then in three I acquire an attribute of an one, which is parent of two. And I do not see the difference here... Greetings -- Robert Sander www.gurubert.de
Robert Sander wrote:
Hi!
Ok, in a recent message I saw self.aq_acquire("some object"), that is extactly what I meant and needed. Why are the Acquisition classes so badly documented?
There's documentation in lib/python/StructuredText/regressions/ on Acquisition and ExtensionClasses. -- -= This is NOT a pyramid scheme =- The SNAFU Principle: True communication is possible only between equals Itamar S.T. itamars@ibm.net
participants (6)
-
Evan Simpson -
Evan Simpson -
Itamar Shtull-Trauring -
Loren Stafford -
Martijn Pieters -
Robert Sander