Skip to content

Commit

Permalink
Move FileIO support into extension (#1166)
Browse files Browse the repository at this point in the history
Co-authored-by: Mark Kittisopikul <[email protected]>
  • Loading branch information
topolarity and mkitti authored Nov 14, 2024
1 parent 08d030f commit 038f96c
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 @@ -33,7 +33,9 @@ BloscExt = "Blosc"
CodecBzip2Ext = "CodecBzip2"
CodecLz4Ext = "CodecLz4"
CodecZstdExt = "CodecZstd"
FileIOExt = "FileIO"
MPIExt = "MPI"
OrderedCollectionsFileIOExt = ["FileIO", "OrderedCollections"]
bitshuffle_jll_ext = "bitshuffle_jll"

[extras]
Expand Down Expand Up @@ -63,5 +65,7 @@ Blosc = "a74b3585-a348-5f62-a45c-50e91977d574"
CodecBzip2 = "523fee87-0ab8-5b00-afb7-3ecf72e48cfd"
CodecLz4 = "5ba52731-8f18-5e0d-9241-30f10d1ec561"
CodecZstd = "6b39b394-51ab-5f42-8807-6242bab2b4c2"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
MPI = "da04e1cc-30fd-572f-bb4f-1f8673147195"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
bitshuffle_jll = "228fe19c-1b83-5282-a626-13744502a320"
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 038f96c

Please sign in to comment.