Skip to content

Commit

Permalink
Move FileIO support into extension
Browse files Browse the repository at this point in the history
  • Loading branch information
topolarity committed Oct 9, 2024
1 parent fbcd95d commit 16a1d73
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
MPIPreferences = "3da0fdf6-3ccc-4f1b-acd9-58baa6c99267"

[weakdeps]
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
MPI = "da04e1cc-30fd-572f-bb4f-1f8673147195"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"

[extensions]
FileIOExt = "FileIO"
OrderedCollectionsFileIOExt = ["FileIO", "OrderedCollections"]
MPIExt = "MPI"

[compat]
Expand Down
20 changes: 15 additions & 5 deletions src/fileio.jl → ext/FileIOExt.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import .FileIO
module FileIOExt

import HDF5: File, Group, h5open, fileio_save, fileio_load, _infer_track_order
@static if isdefined(Base, :get_extension)
import FileIO
else
import ..FileIO
import Requires: @require
end

function loadtodict!(d::AbstractDict, g::Union{File,Group}, prefix::String="")
for k in keys(g)
Expand All @@ -14,10 +22,10 @@ end
_infer_track_order(track_order::Union{Nothing,Bool}, dict::AbstractDict) =
something(track_order, false)

@require OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" begin
_infer_track_order(
track_order::Union{Nothing,Bool}, dict::OrderedCollections.OrderedDict
) = something(track_order, true)
@static if !isdefined(Base, :get_extension)
@require OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" include(
"OrderedCollectionsFileIOExt.jl"
)
end

# load with just a filename returns a flat dictionary containing all the variables
Expand Down Expand Up @@ -75,3 +83,5 @@ function fileio_save(
end
end
end

end
16 changes: 16 additions & 0 deletions ext/OrderedCollectionsFileIOExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module OrderedCollectionsFileIOExt

import HDF5: _infer_track_order
@static if isdefined(Base, :get_extension)
import OrderedCollections
else
import ..OrderedCollections
end

function _infer_track_order(
track_order::Union{Nothing,Bool}, dict::OrderedCollections.OrderedDict
)
return something(track_order, true)
end

end
11 changes: 10 additions & 1 deletion src/HDF5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ Returns `true` if the HDF5 libraries were compiled with ros3 support
"""
has_ros3() = HAS_ROS3[]

# Functions implemented by extensions
function _infer_track_order end
function fileio_save end
function fileio_load end

function __init__()
# HDF5.API.__init__() is run first
#
Expand All @@ -118,7 +123,11 @@ function __init__()
ASCII_ATTRIBUTE_PROPERTIES.char_encoding = :ascii
UTF8_ATTRIBUTE_PROPERTIES.char_encoding = :utf8

@require FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549" include("fileio.jl")
@static if !isdefined(Base, :get_extension)
@require FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549" include(
"../ext/FileIOExt.jl"
)
end

@require H5Zblosc = "c8ec2601-a99c-407f-b158-e79c03c2f5f7" begin
set_blosc!(p::Properties, val::Bool) =
Expand Down

0 comments on commit 16a1d73

Please sign in to comment.