Idea: allow traversing with pathlib semantics #1200
Description
Activity
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
restrictedTraverseandunrestrictedTraversemakes 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
resolvemethod from upstream Python.If I keep dreaming about this,
Pathlibalready 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:... ```python 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 = <construct in whatever way you want> robj = obj.[un]restrictedTraverse](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())Reacted by Gil Forcada Codinachs- 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?Reacted by Yuri
@d-maurer Yep,
absolute_url_pathlooks like what I want. I either wasn't aware of it or forgot. Thanks.As I got bitten by it:
getPhysicalPath()returns the path with the Plone idabsolute_url_path()returns the path without the Plone id
So mind the difference if you want to compare paths to avoid duplicates as it happened to us 🤦🏾 😅
In Zope/Plone we use traversal for almost everything:
Wouldn't it be cool to be able to do instead:
✨