How to access Zope root in Script (Python)
How do I refer to the root Zope directory within a Python script? For example, to access the "price" object in a Page Template, (when price in a sibling or parent folder) I just say "root/service/price", but a Py script doesn't recognize "root", so what do I use? Thanks in advance, Jon Detroit, Michigan USA
Hi Jon,
How do I refer to the root Zope directory within a Python script?
From file system level code one could just say "context.getPhysicalRoot()", but for some reason this is not allowed inside a Script. Maybe this is due to security restrictions this people cannot mess with the Zope root if they do only have permissions for some nested folders. You could try: 1. use an External Method as a helper, which calls getPhysicalRoot() on the file system level. 2. use 'restrictedTraverse': root = container.restrictedTraverse('/') The second one is possibly the simpler way to do it. cheers, clemens
Clemens Robbenhaar wrote:
Hi Jon,
How do I refer to the root Zope directory within a Python script?
From file system level code one could just say "context.getPhysicalRoot()", but for some reason this is not allowed inside a Script. Maybe this is due to security restrictions this people cannot mess with the Zope root if they do only have permissions for some nested folders.
You could try:
1. use an External Method as a helper, which calls getPhysicalRoot() on the file system level.
2. use 'restrictedTraverse':
root = container.restrictedTraverse('/')
The second one is possibly the simpler way to do it.
And it doesn't work, because restrictedTraverse traverses the path down from the ObjectManager it's called on, i.e. its argument is the path relative to container in your example. cheers, oliver
root = container.restrictedTraverse('/')
The second one is possibly the simpler way to do it.
And it doesn't work, because restrictedTraverse traverses the path down from the ObjectManager it's called on, i.e. its argument is the path relative to container in your example.
Oh yes it does, works fine for me in Zope 2.6. In OFS/Traversable.py line 95 the it calls getPhysicalRoot() if the path starts with a '/'. -- Andy McKay www.agmweb.ca
Andy McKay wrote:
root = container.restrictedTraverse('/')
The second one is possibly the simpler way to do it.
And it doesn't work, because restrictedTraverse traverses the path down from the ObjectManager it's called on, i.e. its argument is the path relative to container in your example.
Oh yes it does, works fine for me in Zope 2.6.
In OFS/Traversable.py line 95 the it calls getPhysicalRoot() if the path starts with a '/'.
Oh, sorry then, this must have got changed in some zope version. Ok, I learned something. cheers, oliver
Oliver Bleutgen wrote:
Oh yes it does, works fine for me in Zope 2.6.
In OFS/Traversable.py line 95 the it calls getPhysicalRoot() if the path starts with a '/'.
Oh, sorry then, this must have got changed in some zope version. Ok, I learned something.
Nope, it's always worked like that ;-) Chris
Chris Withers wrote:
Oliver Bleutgen wrote:
Oh yes it does, works fine for me in Zope 2.6.
In OFS/Traversable.py line 95 the it calls getPhysicalRoot() if the path starts with a '/'.
Oh, sorry then, this must have got changed in some zope version. Ok, I learned something.
Nope, it's always worked like that ;-)
Dang, you're right, it also works in Zope 2.3.3 - well sort of, restrictedTraverse('/') gives an error about "string index out of range" in Traversable.py. Guess noone is interested in a bug report for 2.3.3 ;-). But maybe the online help should be fixed, atleast I was misguided since I first learned about restrictedTraverse: """Return the object obtained by traversing the given path from the object on which the method was called, performing security checks along the way. If an object is not found then the default argument will be returned.""" I don't have 2.6, if someone can tell me if the text there is the same, I might put up something in the collector for a small cosmetic fix. cheers, oliver
Oliver Bleutgen wrote:
"""Return the object obtained by traversing the given path from the object on which the method was called, performing security checks along the way.
If an object is not found then the default argument will be returned."""
...but that's perfectly accurate, think of it in terms of directories c:\zope\folder> cd \ root.zope.folder.restrictedTraverse('/') Traversing from something doesn't only mean going further down the tree ;-) cheers, Chris
Chris Withers wrote:
Oliver Bleutgen wrote:
"""Return the object obtained by traversing the given path from the object on which the method was called, performing security checks along the way.
If an object is not found then the default argument will be returned."""
...but that's perfectly accurate, think of it in terms of directories
c:\zope\folder> cd \ root.zope.folder.restrictedTraverse('/')
Traversing from something doesn't only mean going further down the tree ;-)
Yes, but I don't know of an analogous concept for zope's or python's object trees (ignoring restrictedTraverse). I always think of them as doubly linked list, without an easy method to get to the "root". I thought of restrictedTraverse always just as a utility for the translation of paths into objects. Why do we have an aq_parent, when there is also a restrictedTraverse('..'), or, conversly, why is getPhysicalRoot() a python (product) only method while restrictedTraverse('/') does the same and is unrestricted? cheers, oliver
Oliver Bleutgen wrote:
Yes, but I don't know of an analogous concept for zope's or python's object trees (ignoring restrictedTraverse).
Why are you ignoring restrictedTraverse? That's exactly what the Traversable interface (of which it is a part) was built for...
I thought of restrictedTraverse always just as a utility for the translation of paths into objects.
It is.
Why do we have an aq_parent, when there is also a restrictedTraverse('..'),
aq_parent is very much an implementation detail, whereas restrictedTraverse is an interface who's implementation may or may not need aq_parent to work. Squishdot and its postings implement the Traversable interface but do their aquisition wrapping differently...
or, conversly, why is getPhysicalRoot() a python (product) only method while restrictedTraverse('/') does the same and is unrestricted?
That, I suspect, is purely by chance. I see now reason why getPhysicalRoot isn't protected identically to restrictedTraverse('/'). cheers, Chris
participants (5)
-
Andy McKay -
Chris Withers -
Clemens Robbenhaar -
Jon Whitener -
Oliver Bleutgen