Zope
Zope copied to clipboard
Idea: allow traversing with pathlib semantics
In Zope/Plone we use traversal for almost everything:
root = somehow_get_root()
content = root.restrictedTraverse('and/now/get/some/content')
Wouldn't it be cool to be able to do instead:
root = somehow_get_root()
content = root / 'and' / 'now' / 'get' / 'some' / 'content'
✨
How is that better than root['and']['now']['get']['some']['content']
? It's for sure less obvious.
Cool :)
BTW it will be more convenient to do it like this:
content = root / "and/now/get/some/content"
See what we have in pathlib:
>>> from pathlib import Path
>>> Path("foo") / "bar/baz"
PosixPath('foo/bar/baz')
However, using restrictedTraverse
and unrestrictedTraverse
makes it clear the security implications.
Will the /
operator traverse the object in a restricted or unrestricted way?
Theoretically, you could do:
content_path = root / "and/now/get/some/content"
content_path.restricted_get()
content_path.unrestricted_get()
With this it already starts to be less cool :D
@davisagli less obvious is you already know that you can do dictionary-like traversing, but with all the rage of using pathlib everywhere (maybe I'm biased here 😓 ) and that Zope/Plone always use the metaphor of storing objects like folders and files, well, it makes it easier for onboarding IMHO
@ale-rt good point on the (un)restricted that's not obvious at first sight... but maybe it could be a method of the object itself, i.e.:
obj_path = root / 'my/deep/nested/obj'
obj = obj_path.resolve()
# vs
obj = obj_path.unrestricted_resolve()
So one thing could be constructing the path to the object, and once you have that you need to decide how you want to use it.
Overriding the resolve
method from upstream Python.
If I keep dreaming about this, Pathlib
already has the idea of pure paths (objects with everything but I/O operations) and specific paths that have the I/O operations.
So, it would be a """"""matter"""""" of creating one such specific path meant for traversing a ZODB storage ✨
I don't have any specific need to implement it myself, but I thought that as an idea it would be worth sharing it here 😊
Gil Forcada Codinachs wrote at 2024-3-14 04:53 -0700:
...
obj_path = root / 'my/deep/nested/obj' obj = obj_path.resolve() # vs obj = obj_path.unrestricted_resolve()
What about support of Pathlib
objects by [un]restrictedTraverse
?
This would give:
path =
Very similar to the above and does not need an extra special method.
I like @d-maurer's idea best of the ones listed here, but I'm not sure that I would actually use it much. It's already pretty convenient to construct string paths using f-strings.
Different topic, but something that I would use is an easier way to get the string path of a Zope object, instead of doing "/".join(obj.getPhysicalPath())
David Glick wrote at 2024-3-14 09:41 -0700:
... Different topic, but something that I would use is an easier way to get the string path of a Zope object, instead of doing
"/".join(obj.getPhysicalPath())
Why do you need the path as a string?
Can you use absolute_url_path
or absolute_url(relative=1)
in those cases?
@d-maurer Yep, absolute_url_path
looks like what I want. I either wasn't aware of it or forgot. Thanks.