Skip to content

Commit 9683ed1

Browse files
authored
Merge branch 'master' into fits-format
2 parents 635eba6 + 1285fc8 commit 9683ed1

21 files changed

+1176
-92
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test/files/minimal_ascii.rda eol=lf

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ os:
44
- osx
55
julia:
66
- 0.5
7+
- 0.6
78
- nightly
89
notifications:
910
email: false

README.md

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,6 @@ Users are encouraged to contribute these definitions to the
9393
`registry.jl` file of this package, so that information about file
9494
formats exists in a centralized location.
9595

96-
Handling MIME outputs is similar, except that one also provides the
97-
type of the object to be written:
98-
```jl
99-
mimewritable(::MIME"image/png", img::AbstractArray) = ndims(img) == 2
100-
add_mime(MIME("image/png"), AbstractArray, :ImageMagick)
101-
```
102-
103-
In cases where the type is defined in Base julia, such declarations
104-
can by included in FileIO's `registry` file. In contrast, when the
105-
type is defined in a package, that package should call them. Note that
106-
`add_mime` should be called from the package's `__init__` function.
107-
10896
## Implementing loaders/savers
10997

11098
In your package, write code like the following:
@@ -144,19 +132,6 @@ automatically even if the code inside the `do` scope throws an error.)
144132
Conversely, `load(::Stream)` and `save(::Stream)` should not close the
145133
input stream.
146134

147-
For MIME output, you would implement a method like this:
148-
```jl
149-
function Base.writemime(s::Stream{format"ImageMagick"}, ::MIME"image/png", x)
150-
io = stream(s)
151-
# Do the stuff needed to create the output
152-
end
153-
```
154-
155-
It's perfectly acceptable to also create a `Base.writemime(s::IO,
156-
::MIME"image/png", x)` method. Such methods will generally take
157-
precedence over FileIO's generic fallback `writemime` function, and
158-
therefore in some cases might improve performance.
159-
160135
## Help
161136

162137
You can get an API overview by typing `?FileIO` at the REPL prompt.

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
julia 0.5
2-
Compat 0.17.0
2+
Compat 0.27.0

appveyor.yml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
environment:
22
matrix:
3-
- JULIAVERSION: "julialang/bin/winnt/x86/0.5/julia-0.5-latest-win32.exe"
4-
- JULIAVERSION: "julialang/bin/winnt/x64/0.5/julia-0.5-latest-win64.exe"
5-
- JULIAVERSION: "julianightlies/bin/winnt/x86/julia-latest-win32.exe"
6-
- JULIAVERSION: "julianightlies/bin/winnt/x64/julia-latest-win64.exe"
3+
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.5/julia-0.5-latest-win32.exe"
4+
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.5/julia-0.5-latest-win64.exe"
5+
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
6+
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
7+
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
8+
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"
79

810
branches:
911
only:
1012
- master
1113
- /release-.*/
1214
install:
15+
- ps: "[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12"
1316
# Download most recent Julia Windows binary
1417
- ps: (new-object net.webclient).DownloadFile(
15-
$("http://s3.amazonaws.com/"+$env:JULIAVERSION),
18+
$env:JULIA_URL,
1619
"C:\projects\julia-binary.exe")
1720
# Run installer silently, output to C:\projects\julia
1821
- C:\projects\julia-binary.exe /S /D=C:\projects\julia

docs/make_docs.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using FileIO, Compat
2-
import Compat.String
1+
using FileIO
32
import FileIO: LOAD, SAVE, OSX, OS
43
const fs = open(Pkg.dir("FileIO", "docs", "registry.md"), "w")
54

@@ -44,7 +43,7 @@ function add_format{Sym}(::Type{DataFormat{Sym}}, magic, extension, io_libs...)
4443
end
4544

4645

47-
function add_format{sym}(fmt::Type{DataFormat{sym}}, magic::@compat(Union{Tuple,AbstractVector,String}), extension)
46+
function add_format{sym}(fmt::Type{DataFormat{sym}}, magic::Union{Tuple,AbstractVector,String}, extension)
4847
println(sym)
4948
end
5049

src/FileIO.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ __precompile__()
33
module FileIO
44

55
using Compat
6-
using Compat.String
76

87
export DataFormat,
98
File,
@@ -13,7 +12,6 @@ export DataFormat,
1312
add_format,
1413
del_format,
1514
add_loader,
16-
add_mime,
1715
add_saver,
1816
filename,
1917
file_extension,
@@ -31,6 +29,7 @@ import Base.showerror
3129
include("query.jl")
3230
include("error_handling.jl")
3331
include("loadsave.jl")
32+
include("mimesave.jl")
3433
include("registry.jl")
3534

3635
"""

src/error_handling.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ be given the chance to recover from the error. Reports the library name and an
44
LoaderError("ImageMagick", "Foo not available")
55
"""
66
immutable LoaderError <: Exception
7-
library::Compat.UTF8String
8-
msg::Compat.UTF8String
7+
library::String
8+
msg::String
99
end
1010
Base.showerror(io::IO, e::LoaderError) = println(io, e.library, " load error: ",
1111
e.msg, "\n Will try next loader.")
@@ -16,8 +16,8 @@ be given the chance to recover from the error. Reports the library name and an
1616
WriterError("ImageMagick", "Foo not available")
1717
"""
1818
immutable WriterError <: Exception
19-
library::Compat.UTF8String
20-
msg::Compat.UTF8String
19+
library::String
20+
msg::String
2121
end
2222
Base.showerror(io::IO, e::WriterError) = println(
2323
io, e.library, " writer error: ",
@@ -29,7 +29,7 @@ Base.showerror(io::IO, e::WriterError) = println(
2929
"""
3030
immutable NotInstalledError <: Exception
3131
library::Symbol
32-
message::Compat.UTF8String
32+
message::String
3333
end
3434
Base.showerror(io::IO, e::NotInstalledError) = println(io, e.library, " is not installed.")
3535

@@ -60,7 +60,7 @@ Handles a list of thrown errors after no IO library was found working
6060
function handle_exceptions(exceptions::Vector, action)
6161
# first show all errors when there are more then one
6262
multiple = length(exceptions) > 1
63-
println(STDERR, "Error$(multiple?"s" : "") encountered while $action.")
63+
println(STDERR, "Error$(multiple ? "s" : "") encountered while $action.")
6464
if multiple
6565
println("All errors:")
6666
for (err, file) in exceptions

src/loadsave.jl

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,31 @@ const sym2loader = Dict{Symbol,Vector{Symbol}}()
22
const sym2saver = Dict{Symbol,Vector{Symbol}}()
33

44
function is_installed(pkg::Symbol)
5-
isdefined(pkg) && isa(@compat(getfield(Main, pkg)), Module) && return true # is a module defined in Main scope
5+
isdefined(Main, pkg) && isa(getfield(Main, pkg), Module) && return true # is a module defined in Main scope
66
path = Base.find_in_path(string(pkg)) # hacky way to determine if a Package is installed
77
path == nothing && return false
88
return isfile(path)
99
end
1010

1111
function checked_import(pkg::Symbol)
12-
!is_installed(pkg) && throw(NotInstalledError(pkg, ""))
13-
!isdefined(Main, pkg) && eval(Main, Expr(:import, pkg))
14-
return @compat(getfield(Main, pkg))
12+
isdefined(Main, pkg) && return getfield(Main, pkg)
13+
isdefined(FileIO, pkg) && return getfield(FileIO, pkg)
14+
!is_installed(pkg) && throw(NotInstalledError(pkg, ""))
15+
!isdefined(Main, pkg) && eval(Main, Expr(:import, pkg))
16+
return getfield(Main, pkg)
1517
end
1618

1719

1820
for (applicable_, add_, dict_) in (
1921
(:applicable_loaders, :add_loader, :sym2loader),
2022
(:applicable_savers, :add_saver, :sym2saver))
2123
@eval begin
22-
$applicable_{sym}(::@compat(Union{Type{DataFormat{sym}}, Formatted{DataFormat{sym}}})) = get($dict_, sym, [:FileIO]) # if no loader is declared, fallback to FileIO
24+
function $applicable_{sym}(::Union{Type{DataFormat{sym}}, Formatted{DataFormat{sym}}})
25+
if haskey($dict_, sym)
26+
return $dict_[sym]
27+
end
28+
error("No $($applicable_) found for $(sym)")
29+
end
2330
function $add_{sym}(::Type{DataFormat{sym}}, pkg::Symbol)
2431
list = get($dict_, sym, Symbol[])
2532
$dict_[sym] = push!(list, pkg)
@@ -42,7 +49,7 @@ the magic bytes are essential.
4249
- `load(File(format"PNG",filename))` specifies the format directly, and bypasses inference.
4350
- `load(f; options...)` passes keyword arguments on to the loader.
4451
"""
45-
load(s::@compat(Union{AbstractString,IO}), args...; options...) =
52+
load(s::Union{AbstractString,IO}, args...; options...) =
4653
load(query(s), args...; options...)
4754

4855
"""
@@ -51,9 +58,13 @@ trying to infer the format from `filename`.
5158
- `save(Stream(format"PNG",io), data...)` specifies the format directly, and bypasses inference.
5259
- `save(f, data...; options...)` passes keyword arguments on to the saver.
5360
"""
54-
save(s::@compat(Union{AbstractString,IO}), data...; options...) =
61+
save(s::Union{AbstractString,IO}, data...; options...) =
5562
save(query(s), data...; options...)
5663

64+
function save(s::Union{AbstractString,IO}; options...)
65+
data -> save(s, data; options...)
66+
end
67+
5768
# Forced format
5869
function save{sym}(df::Type{DataFormat{sym}}, f::AbstractString, data...; options...)
5970
libraries = applicable_savers(df)

src/mimesave.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module MimeWriter
2+
3+
using ..FileIO: File, @format_str
4+
5+
function save(file::File{format"PNG"}, data)
6+
if mimewritable("image/png", data)
7+
open(file.filename, "w") do s
8+
show(IOContext(s, :full_fidelity=>true), "image/png", data)
9+
end
10+
else
11+
throw(ArgumentError("Argument does not support conversion to png."))
12+
end
13+
end
14+
15+
function save(file::File{format"SVG"}, data)
16+
if mimewritable("image/svg+xml", data)
17+
open(file.filename, "w") do s
18+
show(IOContext(s, :full_fidelity=>true), "image/svg+xml", data)
19+
end
20+
else
21+
throw(ArgumentError("Argument does not support conversion to svg."))
22+
end
23+
end
24+
25+
function save(file::File{format"PDF"}, data)
26+
if mimewritable("application/pdf", data)
27+
open(file.filename, "w") do s
28+
show(IOContext(s, :full_fidelity=>true), "application/pdf", data)
29+
end
30+
else
31+
throw(ArgumentError("Argument does not support conversion to pdf."))
32+
end
33+
end
34+
35+
function save(file::File{format"EPS"}, data)
36+
if mimewritable("application/eps", data)
37+
open(file.filename, "w") do s
38+
show(IOContext(s, :full_fidelity=>true), "application/eps", data)
39+
end
40+
else
41+
throw(ArgumentError("Argument does not support conversion to eps."))
42+
end
43+
end
44+
45+
end

0 commit comments

Comments
 (0)