-
-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow resources to be in subdirectories #58
Comments
In GitLab by @warsaw on May 16, 2018, 16:28 This was a deliberate choice, but I think you have a valid use case. @brettcannon what do you think? And if we allow this, should we make sure it gets into Python 3.7? I'm hesitant because I don't know how much work it will take or what the semantic and implementation implications are. |
In GitLab by @brettcannon on May 16, 2018, 16:33 I get the desire to use this, but I'm personally not convinced it's worth the headache of having to manage paths separate from the import system since the abstraction handles a lot of subtle details for us. IOW I'm with Barry and I'm not comfortable in doing this until someone produces a PR that implements this to see what kind of complications this would introduce as for me some empty files is not a huge cost for cross-platform file loading when that's needed (I get why people who are controlling their deployment and not doing this in a library feel like not bothering). |
In GitLab by @warsaw on May 21, 2018, 15:27 I actually have another possible use case. Over in importlib_metadata it occurs to me that I'd really like to use I have been considering writing a custom loader for that purpose, so we could use the |
In GitLab by @warsaw on Sep 11, 2018, 19:31 Note that we are using finders, not loaders for |
In GitLab by @jaraco on Oct 31, 2018, 17:27 I recently encountered another project where I adapted the implementation to use importlib_resources. This particular project had a jarfile in a subdirectory of the package. Package was You know what might be better? I know it's a little late to re-envision the design, but what if instead of returning a context that when entered ensures there's a file on the file system, it instead returns a first-class object which is traversable. So the OP could implement his application thus:
Such a solution would be much more elegant in that :
Such an approach would require some functionality that doesn't exist (pathlib-compatible wrappers for zip files and others). |
In GitLab by @warsaw on Nov 1, 2018, 13:30 I'd love to see zipfile compatible pathlib-like API. As @jaraco knows, this would also come in handy for importlib_metadata. I wouldn't be opposed to a traversable object, but also remember that a motivating use case for Can you think of a way to support the existing |
In GitLab by @jaraco on Dec 17, 2018, 15:19 I've just stumbled on another project that stores "package data" in a subdirectory of the package. And as it turns out, that's what distutils indicates to do. I think it's inconsistent for importlib.resources to declare this an unsupported case but distutils (as deprecated as it is) to still be recommending this for packaging.
At first blush, it feels incompatible with the implementation I have in mind. I can draft what I have in mind as a separate function and then we can analyze if there's a way to combine these into a unified implementation. |
In GitLab by @gregory.szorc on Feb 26, 2019, 23:59 I was told to comment here after filing https://bugs.python.org/issue36128. I can see both sides of the argument for whether the On one hand, preventing path separators keeps things simple and provides only 1 way to access a specific resource. On the other, requiring resources exist within Python packages can be rather annoying (as the original reporter has stated). Just today I was refactoring Mercurial to convert various directories into Python packages and it was somewhat annoying :/ It is much more convenient to place a bunch of resource files in a friendly directory tree and access them using hierarchical addressing with path separators as the delimiter. And this conveniently maps to filesystem paths. From my perspective as someone who has hacked together Python module importing using zero-copy (https://gregoryszorc.com/blog/2018/12/28/faster-in-memory-python-module-importing/), I love the For starters, the implementation in I can make the argument that resource names should be nearly anything and it is up to the That being said, what is the harm for |
In GitLab by @warsaw on Feb 27, 2019, 14:09 @gregory.szorc I'm a little strapped for time right now, but I just want to thank you for your very valuable feedback. It's always great to have real-world use cases to base decisions on, and I think you give us a really important data point (and one that validates the basic idea, even with its warts). I'll follow up again once I have time to digest your comment and think about its implications. |
In GitLab by @jaraco on May 6, 2019, 15:55 mentioned in merge request !76 |
In GitLab by @warsaw on Jun 2, 2019, 12:57 I really wanted to get to this in time for Python 3.8, but I just couldn't get far enough with working code. Contributions are welcome. |
In GitLab by @davidism on Oct 18, 2019, 10:35 I was removing the dependency on pkg_resources from Werkzeug's
Due to backwards compatibility as well as ease of use, it wasn't possible for me to require users to add init files to every folder and subfolder. Additionally, even if init files were added, there didn't appear to be a way to access subdirectories, or even determine that a name was a directory. There were secondary requirements such as being able to get the mtime of a resource if possible as well. I'm hesitant to bring this up, since it may have been a convenient oversight, but It would be great to see support for nested resources without adding init files. |
In GitLab by @wimglenn on Nov 1, 2019, 16:23 Hey Barry - probably worth mentioning, this design makes This works (pytz 2019.3):
This doesn't:
So it looks like one can't use |
In GitLab by @warsaw on Nov 1, 2019, 17:07 Contributions welcome :) |
In GitLab by @wimglenn on Nov 1, 2019, 18:32 OK, should I take that to mean "it's not an intentional opinion that |
In GitLab by @warsaw on Nov 4, 2019, 21:18 When we originally wrote |
In GitLab by @Mekk on Nov 11, 2019, 09:35 Let me just mention my „favorite” use case on old APIs: pypa/setuptools#1635 New APIs do fine via importlib, less so via raw reader interface (code below tried on 3.7.3). So let's be careful not to allow too much:
(this is fine) but:
(this is somewhat dangerous) Context: the idea of mapping urls to resources (and relying on resource api for validation) seems appealing to some people. |
In GitLab by @ztane on Nov 19, 2019, 09:24 @Mekk well ... of course for as long as the accepted Stack Overflow answer uses
what could go wrong ;) |
In GitLab by @brettcannon on Dec 6, 2019, 17:01 So I can think of two issues here: one is security-related and one is API-related. The security one is directory traversal. What if we just flat-out banned The API-related one is I say if you want something in a subdirectory you are expected to pass in a What do you people think about that in terms of design constraints to solve the key issues that led us to punt on this decision? BTW I think all of this is backwards-compatible. |
In GitLab by @jaraco on Jan 18, 2020, 11:04 In the feature/traversable branch (!76), I have what I believe is a viable approach to this issue. Here it is in action:
As of this writing, the |
In GitLab by @warsaw on Jan 19, 2020, 16:05 @jaraco This is really interesting. Let me see if I understand what's going on here.
If that's how it's supposed to work, then I think this is a fantastic solution to the problem. We'd need to be clear about what the Is Unfortunately What do you think about |
In GitLab by @brettcannon on Jan 20, 2020, 16:02 I also agree that returning an Perhaps there should be something that takes a module as an anchor and returns that location and then you construct everything from there using Am I mistaken into thinking that if we adopted this approach it basically would negate most of the custom API we have provided (which I don't view as a bad thing)? |
In GitLab by @jaraco on Feb 9, 2020, 10:32
Actually, there's no protection against this, but I'm not sure it's important that there is. After all, you can get to
Agreed, this is a new, replacement API that's more aligned to Pathlib conventions than the former conventions.
No, and Yes. It's not a traditional package with a
It seems that the directory is importable due to the PEP 420 namespace package implementation. I thought child namespaces of parent simple packages was not allowed, but this behavior indicates otherwise. Regardless, the implementation is not relying on this behavior, demonstrated by it working on Python 2.7 where
I agree
(or some variation) Another consideration I had was perhaps the
If we use this approach, then I'm liking the function
Well, gee. I thought it was my idea, but now that I'm reading through Brett's response, he's made the same suggestion (that surely I read earlier). Sounds like we have some convergence. What do you think of
It does provide a mechanism to completely bypass most if not all of the remaining API, yes, although I imagine retaining it for compatibility. I think it would be interesting to see what the implementation looks like without those functions (for curiosity). |
In GitLab by @jaraco on Feb 9, 2020, 10:33
Oh! If |
In GitLab by @jaraco on Feb 9, 2020, 10:42 The latest implementation implements |
In GitLab by @jaraco on Feb 17, 2020, 10:10
I've started stripping out this functionality in the feature/drop-legacy-api branch. Progress was good, but I got stumped on |
In GitLab by @jaraco on Feb 29, 2020, 17:52 closed |
In GitLab by @brettcannon on Apr 22, 2020, 15:11 @ankostis because |
In GitLab by @hinakuroori on Jun 22, 2020, 14:33 mentioned in commit hinakuroori/ament_package@0c80a10a6f7de541cc8b09c16846df5ffc9ab82f |
In GitLab by @hinakuroori on Jun 22, 2020, 15:35 mentioned in commit hinakuroori/sros2@f1173fa1914b7ad59d1d2b46b854a86c7bba81c0 |
In GitLab by @hinakuroori on Jun 23, 2020, 13:33 mentioned in commit hinakuroori/sros2@dbec91d1c9efafadb9b7eeb183cc5f2663feb136 |
In GitLab by @hinakuroori on Jun 23, 2020, 14:01 mentioned in commit hinakuroori/ament_package@4db28d437e28dfdc0972fa65d92098647691bd9a |
In GitLab by @brettcannon on Jun 25, 2020, 14:10 marked this issue as related to #104 |
In GitLab by @adam.hendry on Aug 19, 2020, 22:53 @brettcannon @warsaw I'm of the opinion we should keep Benefits:
Cons:
Honestly, the cons above aren't really cons to me. I can't see any benefits of |
In GitLab by @adam.hendry on Aug 22, 2020, 19:37 @brettcannon and @warsaw I'm actually changing my answer. |
In GitLab by @ankostis on Aug 23, 2020, 05:22 In my view the greatest benefit of |
In GitLab by @warsaw on Aug 23, 2020, 17:51 Please keep in mind that we all want to support data files in non-package subdirectories, it's just that none of us has had the time to implement it. In the fine tradition of open source, "Contributions Welcome!" |
In GitLab by @wimglenn on Aug 26, 2020, 13:20 @warsaw Seems to be already implemented! The issue is closed, and Jason says the feature is complete. I've had a look on v3.0.0, and got pathlib instances to site-packages - when the package was zipped I got Example:
I've updated https://github.com/wimglenn/resources-example to demonstrate Looks pretty good now, just a waiting game for the new APIs to appear in stdlib. |
In GitLab by @jaraco on Aug 30, 2020, 21:06 That's right. The functionality is present in importlib_resources 1.3 and the same functionality is available in Python 3.9. What remains is to add support for namespace packages and work toward an API for alternate loaders to supply resources. |
In GitLab by @rob.speer on May 16, 2018, 01:23
Suppose I have a Web application,
myapp
, that needs to serve a static file, which by convention needs to be in the pathmyapp/static/ld/context.ld.json
. Suppose I also want to be able to access that file from Python code, because its contents are used in a test.As importlib_resources is currently defined, I would need to rewrite the path as if it were a Python submodule, even though it does not contain actual Python code:
path(myapp.static.ld, "context.ld.json")
. I would also need to create empty files namedmyapp/static/__init__.py
andmyapp/static/ld/__init__.py
, and hopefully exclude them from being served as static files.That would be enough for me to give up and use paths relative to
__file__
instead. In general, I would heartily recommend importlib if I could reasonably promise that it was an improvement over using__file__
or over existing uses ofpkg_resources
, which it wouldn't be if it doesn't support subdirectories.The call I would like to be able to make in this situation is
path(myapp, "static/ld/context.ld.json")
.The text was updated successfully, but these errors were encountered: