Skip to content

Commit

Permalink
Merge pull request #179 from davidanthoff/gen2-names
Browse files Browse the repository at this point in the history
Add support for fileio_* names
  • Loading branch information
SimonDanisch authored May 22, 2018
2 parents b81b089 + adc8577 commit 1a1d5e5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ Consequently, **packages should define "private" `load` and `save` methods (also
`loadstreaming` and `savestreaming` if you implement them), and not extend
(import) FileIO's**.

If you run into a naming conflict with the `load` and `save` functions
(for example, you already have another function in your package that has
one of these names), you can instead name your loaders `fileio_load`,
`fileio_save` etc. Note that you cannot mix and match these styles: either
all your loaders have to be named `load`, or all of them should be called
`fileio_load`, but you cannot use both conventions in one module.

`load(::File)` and `save(::File)` should close any streams
they open. (If you use the `do` syntax, this happens for you
automatically even if the code inside the `do` scope throws an error.)
Expand Down
8 changes: 8 additions & 0 deletions src/loadsave.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ for fn in (:load, :loadstreaming, :metadata)
for library in libraries
try
Library = checked_import(library)
gen2_func_name = Symbol("fileio_" * $(string(fn)))
if isdefined(Library, gen2_func_name)
return eval(Library, :($gen2_func_name($q, $args...; $options...)))
end
if !has_method_from(methods(Library.$fn), Library)
throw(LoaderError(string(library), "$($fn) not defined"))
end
Expand All @@ -193,6 +197,10 @@ for fn in (:save, :savestreaming)
for library in libraries
try
Library = checked_import(library)
gen2_func_name = Symbol("fileio_" * $(string(fn)))
if isdefined(Library, gen2_func_name)
return eval(Library, :($gen2_func_name($q, $data...; $options...)))
end
if !has_method_from(methods(Library.$fn), Library)
throw(WriterError(string(library), "$($fn) not defined"))
end
Expand Down
11 changes: 7 additions & 4 deletions test/loadsave.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ module TestLoadSave
import FileIO: File, @format_str
load(file::File{format"PBMText"}) = "PBMText"
load(file::File{format"PBMBinary"}) = "PBMBinary"
load(file::File{format"HDF5"}) = "HDF5"
load(file::File{format"JLD"}) = "JLD"
load(file::File{format"GZIP"}) = "GZIP"
end
module TestLoadSave2
import FileIO: File, @format_str
fileio_load(file::File{format"HDF5"}) = "HDF5"
end

sym2loader = copy(FileIO.sym2loader)
sym2saver = copy(FileIO.sym2saver)
Expand All @@ -23,7 +26,7 @@ try

add_loader(format"PBMText", :TestLoadSave)
add_loader(format"PBMBinary", :TestLoadSave)
add_loader(format"HDF5", :TestLoadSave)
add_loader(format"HDF5", :TestLoadSave2)
add_loader(format"JLD", :TestLoadSave)
add_loader(format"GZIP", :TestLoadSave)

Expand Down Expand Up @@ -285,12 +288,12 @@ load(f::File{format"AmbigExt2"}) = open(f) do io
read(stream(io), String)
end

save(f::File{format"AmbigExt1"}, testdata) = open(f, "w") do io
fileio_save(f::File{format"AmbigExt1"}, testdata) = open(f, "w") do io
s = stream(io)
print(s, "ambigext1")
print(s, testdata)
end
save(f::File{format"AmbigExt2"}, testdata) = open(f, "w") do io
fileio_save(f::File{format"AmbigExt2"}, testdata) = open(f, "w") do io
s = stream(io)
print(s, "ambigext2")
print(s, testdata)
Expand Down

0 comments on commit 1a1d5e5

Please sign in to comment.