You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, the as_file implementation only supports a single file and not a tree of files. To support use-cases like those of cssutils, as_file should support extracting a tree of traversables.
I think you meant to open this in importlib_resources, you should be able to transfer the issue there.
Would expanding as_file work in the case you provided? as_file is a context manager and the file is only guaranteed to be on the file system for the duration of it, so you can't return the path as you are doing in get_sheet_filename. I think you need to either change the function to return a traversable, or if you really want a file system path, the __fspath__ proposal would work perfectly here 😁.
as_file is a context manager and the file is only guaranteed to be on the file system for the duration of it, so you can't return the path as you are doing in get_sheet_filename.
Right. And that's the core challenge with these file-based providers. If the file (or directory of files) doesn't already exist on the disk, something needs to create them on disk and something needs to later clean up that creation. Hence, the as_file presents a context manager, forcing the consumer to define when and how the context is cleaned up.
That doesn't mean the files can't persist for longer. For example, see how certifi/python-certifi#147 proposes to use as_file to create a context manager and have that context manager cleaned up "at exit".
I fail to see how __fspath__ or os.PathLike helps here. PathLike is simply a protocol that says the object can return a string-representation of the object on the file system. If the object doesn't already exist on the file system, a call to __fspath__ would have to create one and would have no opportunity to clean it up.
Maybe it would help if you could describe how a ZipReader could present something with __fspath__ to address this issue (and maybe others)?
You know that test/sheets is not a zip module, you know it is on the file-system, unless you are considering the case of user running the tests. os.fspath works when you know this.
Not every use-case guarantees this, for eg. the certifi one doesn't. __fspath__ would simply not work in those cases, but that does not negate its usefulness in the cases where is does work.
I've had use for it a few times.
Maybe it would help if you could describe how a ZipReader could present something with __fspath__ to address this issue (and maybe others)?
It can't, you'd have to resort as_file or something similar, though I would guess in most cases you should just be able to use a traversable to do what you want instead of having to deal with a fs path.
Just an idea, instead of having as_file support a tree of traversables, what about adding a as_file helper directly to Traversable?
But that puts some extra burden on implementations using traversables that do not inherit from importlib.abc.Traversable, so it's probably not worth it.
Not every use-case guarantees [that a resource is on the file system]
Right. And what I want is a solution that works for resources on the file system and resources by other providers, where if a directory of files is needed, it can be extracted to the file system for the duration of a context.
Activity
FFY00 commentedon May 24, 2021
I think you meant to open this in
importlib_resources, you should be able to transfer the issue there.Would expanding
as_filework in the case you provided?as_fileis a context manager and the file is only guaranteed to be on the file system for the duration of it, so you can't return the path as you are doing inget_sheet_filename. I think you need to either change the function to return a traversable, or if you really want a file system path, the__fspath__proposal would work perfectly here 😁.jaraco commentedon May 24, 2021
Yep. I'm often getting these mixed up.
jaraco commentedon May 24, 2021
Right. And that's the core challenge with these file-based providers. If the file (or directory of files) doesn't already exist on the disk, something needs to create them on disk and something needs to later clean up that creation. Hence, the
as_filepresents a context manager, forcing the consumer to define when and how the context is cleaned up.That doesn't mean the files can't persist for longer. For example, see how certifi/python-certifi#147 proposes to use
as_fileto create a context manager and have that context manager cleaned up "at exit".I fail to see how
__fspath__oros.PathLikehelps here. PathLike is simply a protocol that says the object can return a string-representation of the object on the file system. If the object doesn't already exist on the file system, a call to__fspath__would have to create one and would have no opportunity to clean it up.Maybe it would help if you could describe how a ZipReader could present something with
__fspath__to address this issue (and maybe others)?FFY00 commentedon May 24, 2021
You know that
test/sheetsis not a zip module, you know it is on the file-system, unless you are considering the case of user running the tests.os.fspathworks when you know this.Not every use-case guarantees this, for eg. the certifi one doesn't.
__fspath__would simply not work in those cases, but that does not negate its usefulness in the cases where is does work.I've had use for it a few times.
It can't, you'd have to resort
as_fileor something similar, though I would guess in most cases you should just be able to use a traversable to do what you want instead of having to deal with a fs path.Just an idea, instead of having
as_filesupport a tree of traversables, what about adding aas_filehelper directly toTraversable?But that puts some extra burden on implementations using traversables that do not inherit from
importlib.abc.Traversable, so it's probably not worth it.jaraco commentedon Jul 3, 2022
Right. And what I want is a solution that works for resources on the file system and resources by other providers, where if a directory of files is needed, it can be extracted to the file system for the duration of a context.
I have an implementation drafted.
jaraco commentedon Jul 3, 2022
Here's a demo of what I have in mind: