Skip to content

Commit 6ce8245

Browse files
committed
include changes to make load(query(x)) work
- load(::File/Stream) dispatches now to the correct library - if file is unknown it throws a UnknownFormat error - moved all load/save functions in tests into module to give them their own scope ( better support the idea, that FileIO shouldn't define load/save functions)
1 parent 06856fe commit 6ce8245

File tree

5 files changed

+63
-42
lines changed

5 files changed

+63
-42
lines changed

src/FileIO.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ export DataFormat,
3838

3939
import Base.showerror
4040

41-
include("error_handling.jl")
4241
include("query.jl")
42+
include("error_handling.jl")
4343
include("loadsave.jl")
4444
include("registry.jl")
4545

src/error_handling.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ Base.showerror(io::IO, e::WriterError) = println(
2525
msg, "\n Will try next writer."
2626
)
2727

28-
2928
@doc """
3029
`NotInstalledError` should be thrown when a library is currently not installed.
3130
""" ->
@@ -35,6 +34,15 @@ immutable NotInstalledError <: Exception
3534
end
3635
Base.showerror(io::IO, e::NotInstalledError) = println(io, e.library, " is not installed.")
3736

37+
@doc """
38+
`UnknownFormat` gets thrown when FileIO can't recognize the format of a file.
39+
""" ->
40+
immutable UnknownFormat{T <: Formatted} <: Exception
41+
format::T
42+
end
43+
Base.showerror(io::IO, e::UnknownFormat) = println(io, e.format, " couldn't be recognized by FileIO.")
44+
45+
3846
@doc """
3947
Handles error as soon as they get thrown while doing IO
4048
""" ->

src/loadsave.jl

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,35 @@ the magic bytes are essential.
4040
- `load(File(format"PNG",filename))` specifies the format directly, and bypasses inference.
4141
- `load(f; options...)` passes keyword arguments on to the loader.
4242
""" ->
43-
function load(s::@compat(Union{AbstractString,IO}), args...; options...)
44-
q = query(s)
43+
load(s::@compat(Union{AbstractString,IO}), args...; options...) =
44+
load(query(s), args...; options...)
45+
46+
@doc """
47+
- `save(filename, data...)` saves the contents of a formatted file,
48+
trying to infer the format from `filename`.
49+
- `save(Stream(format"PNG",io), data...)` specifies the format directly, and bypasses inference.
50+
- `save(f, data...; options...)` passes keyword arguments on to the saver.
51+
""" ->
52+
save(s::@compat(Union{AbstractString,IO}), data...; options...) =
53+
save(query(s), data...; options...)
54+
55+
# Forced format
56+
function save{sym}(df::Type{DataFormat{sym}}, f::AbstractString, data...; options...)
57+
libraries = applicable_savers(df)
58+
checked_import(libraries[1])
59+
save(File(DataFormat{sym}, f), data...; options...)
60+
end
61+
62+
function save{sym}(df::Type{DataFormat{sym}}, s::IO, data...; options...)
63+
libraries = applicable_savers(df)
64+
checked_import(libraries[1])
65+
save(Stream(DataFormat{sym}, s), data...; options...)
66+
end
67+
68+
69+
# Fallbacks
70+
function load{F}(q::Formatted{F}, args...; options...)
71+
unknown(q) && throw(UnknownFormat(q))
4572
libraries = applicable_loaders(q)
4673
failures = Any[]
4774
for library in libraries
@@ -55,15 +82,8 @@ function load(s::@compat(Union{AbstractString,IO}), args...; options...)
5582
end
5683
handle_error(failures)
5784
end
58-
59-
@doc """
60-
- `save(filename, data...)` saves the contents of a formatted file,
61-
trying to infer the format from `filename`.
62-
- `save(Stream(format"PNG",io), data...)` specifies the format directly, and bypasses inference.
63-
- `save(f, data...; options...)` passes keyword arguments on to the saver.
64-
""" ->
65-
function save(s::@compat(Union{AbstractString,IO}), data...; options...)
66-
q = query(s)
85+
function save{F}(q::Formatted{F}, data...; options...)
86+
unknown(q) && throw(UnknownFormat(q))
6787
libraries = applicable_savers(q)
6888
failures = Any[]
6989
for library in libraries
@@ -77,21 +97,3 @@ function save(s::@compat(Union{AbstractString,IO}), data...; options...)
7797
end
7898
handle_error(failures)
7999
end
80-
81-
# Forced format
82-
function save{sym}(df::Type{DataFormat{sym}}, f::AbstractString, data...; options...)
83-
libraries = applicable_savers(df)
84-
checked_import(libraries[1])
85-
save(File(DataFormat{sym}, f), data...; options...)
86-
end
87-
88-
function save{sym}(df::Type{DataFormat{sym}}, s::IO, data...; options...)
89-
libraries = applicable_savers(df)
90-
checked_import(libraries[1])
91-
save(Stream(DataFormat{sym}, s), data...; options...)
92-
end
93-
94-
95-
# Fallbacks
96-
load{F}(f::Formatted{F}, args...; options...) = error("No load function defined for format ", F, " with filename ", filename(f))
97-
save{F}(f::Formatted{F}, data...; options...) = error("No save function defined for format ", F, " with filename ", filename(f))

test/loadsave.jl

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ using FactCheck
33

44
# Stub readers---these might bork any existing readers, so don't
55
# run these tests while doing other things!
6-
FileIO.load(file::File{format"PBMText"}) = "PBMText"
7-
FileIO.load(file::File{format"PBMBinary"}) = "PBMBinary"
8-
FileIO.load(file::File{format"HDF5"}) = "HDF5"
9-
FileIO.load(file::File{format"JLD"}) = "JLD"
6+
module TestLoadSave
7+
import FileIO: File, @format_str
8+
load(file::File{format"PBMText"}) = "PBMText"
9+
load(file::File{format"PBMBinary"}) = "PBMBinary"
10+
load(file::File{format"HDF5"}) = "HDF5"
11+
load(file::File{format"JLD"}) = "JLD"
12+
end
1013

1114
sym2loader = copy(FileIO.sym2loader)
1215
sym2saver = copy(FileIO.sym2saver)
@@ -16,6 +19,12 @@ try
1619
empty!(FileIO.sym2saver)
1720
file_dir = joinpath(dirname(@__FILE__), "files")
1821
context("Load") do
22+
23+
add_loader(format"PBMText", :TestLoadSave)
24+
add_loader(format"PBMBinary", :TestLoadSave)
25+
add_loader(format"HDF5", :TestLoadSave)
26+
add_loader(format"JLD", :TestLoadSave)
27+
1928
@fact load(joinpath(file_dir, "file1.pbm")) --> "PBMText"
2029
@fact load(joinpath(file_dir, "file2.pbm")) --> "PBMBinary"
2130
# Regular HDF5 file with magic bytes starting at position 0
@@ -118,30 +127,33 @@ del_format(format"DUMMY")
118127

119128
# PPM/PBM can be either binary or text. Test that the defaults work,
120129
# and that we can force a choice.
130+
module AmbigExt
131+
import FileIO: File, @format_str, Streamm, stream, skipmagic
121132

122-
FileIO.load(f::File{format"AmbigExt1"}) = open(f) do io
133+
load(f::File{format"AmbigExt1"}) = open(f) do io
123134
skipmagic(io)
124135
readall(stream(io))
125136
end
126-
FileIO.load(f::File{format"AmbigExt2"}) = open(f) do io
137+
load(f::File{format"AmbigExt2"}) = open(f) do io
127138
skipmagic(io)
128139
readall(stream(io))
129140
end
130141

131-
FileIO.save(f::File{format"AmbigExt1"}, testdata) = open(f, "w") do io
142+
save(f::File{format"AmbigExt1"}, testdata) = open(f, "w") do io
132143
s = stream(io)
133144
print(s, "ambigext1")
134145
print(s, testdata)
135146
end
136-
FileIO.save(f::File{format"AmbigExt2"}, testdata) = open(f, "w") do io
147+
save(f::File{format"AmbigExt2"}, testdata) = open(f, "w") do io
137148
s = stream(io)
138149
print(s, "ambigext2")
139150
print(s, testdata)
140151
end
152+
end
141153

142154
context("Ambiguous extension") do
143-
add_format(format"AmbigExt1", "ambigext1", ".aext")
144-
add_format(format"AmbigExt2", "ambigext2", ".aext")
155+
add_format(format"AmbigExt1", "ambigext1", ".aext", [:AmbigExt])
156+
add_format(format"AmbigExt2", "ambigext2", ".aext", [:AmbigExt])
145157
A = "this is a test"
146158
fn = string(tempname(), ".aext")
147159
# Test the forced version first: we wouldn't want some method in Netpbm

test/runtests.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ using FactCheck
33
facts("FileIO") do
44
include("query.jl")
55
include("loadsave.jl")
6-
76
include("error_handling.jl")
87
end
98

0 commit comments

Comments
 (0)