-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
On-demand loading of support for file formats? #7299
Comments
We could do something similar to how MIME types are handled, and have a default |
For reading, once #3656 is merged (and perhaps after the promised IO system On Tue, Jun 17, 2014 at 6:23 PM, Jeff Bezanson [email protected]
|
Simply detecting the extension sounds great already. Magic bytes could be used later to improve the detection (using something like XML definitions from http://www.freedesktop.org/wiki/Software/shared-mime-info/ ?). |
OK, I'll put this on my todo list. |
I was thinking about this a bit more today with the flurry of activity around jls/jld (I was prompted by Jeff's comment on adding Here's a sketch of how I'm thinking this might work: type FileType{Extension} <: String # Parameterized by a symbol for the extension
path::UTF8String
end
FileType(path::String) = (p = utf8(path); FileType{symbol(splitext(p)[2])}(p))
macro FileType_str(s) # For easily creating types
:(FileType{$(Expr(:quote, symbol(s)))})
end
# … and delegate UTF8/String methods to the path
# Then library authors could define
save(::FileType".jls", args...)
save(::FileType".jld", args...)
save(::FileType".h5", args...)
# And there could be a fallback in base that users would call:
save(p::String, args...) = save(FileType(p), args...) Loading would work similarly for path-based dispatch. Magic bytes are trickier; to do that I think you'd need a registration system similar to Images.jl given variable lengths and such. It could sit nicely on top of this system, however. If a filetype doesn't have a recognized extension, then Base could look through the registered magic bytes for a match, and then dispatch to the FileType registered to it. |
That looks quite elegant, @mbauman! |
Yes, there is a nice analogy to |
👍 |
I like it – that's turning into a standard Julia pattern. |
By the way, advanced my approach a little in FileIO. |
I haven't really showed how loading should work. load(::File, Mime{UniqueIdentifier})
safe(::T, Mime{UniqueIdentifier}) |
OK, I finally found time to deliver some "feedback": JuliaIO/FileIO.jl#15 My sense is that this doesn't need to live in base julia; every package that wants to use |
I agree! Would be odd to have a hook to every io package in Base, while
|
@mbauman had an interesting idea he proposed in JuliaIO/HDF5.jl#101 that may be worth discussing. He pointed out that there are conflicts for functions named
save
andload
. He suggested that one way to solve this is based on file extension, e.g.,save("mydata.jld", ...)
would invoke the HDF5/JLD code, whereassave("mydata.df", ...)
might invoke DataFrames routines, etc. (I don't know whether there's a standard extension for DataFrames, I just made that up.) This is especially important when the same datatype could be stored using two different on-disk formats.Images already has something that's a little bit like this, a framework for registering support for file formats based on extension and/or magic bytes, https://github.com/timholy/Images.jl/blob/master/doc/extendingIO.md#contributing-a-file-format-to-images. Should this registration framework be generalized and added to Julia proper? The idea is that all the support code would still live in packages, but that those packages would be loaded automatically depending on extension and/or magic bytes.
The text was updated successfully, but these errors were encountered: