From 16a1d73e8f194fe3ecf5c9bcabfc40b732937de2 Mon Sep 17 00:00:00 2001 From: Cody Tapscott Date: Wed, 9 Oct 2024 17:19:10 -0400 Subject: [PATCH] Move FileIO support into extension --- Project.toml | 4 ++++ src/fileio.jl => ext/FileIOExt.jl | 20 +++++++++++++++----- ext/OrderedCollectionsFileIOExt.jl | 16 ++++++++++++++++ src/HDF5.jl | 11 ++++++++++- 4 files changed, 45 insertions(+), 6 deletions(-) rename src/fileio.jl => ext/FileIOExt.jl (82%) create mode 100644 ext/OrderedCollectionsFileIOExt.jl diff --git a/Project.toml b/Project.toml index d27dbd6b4..5cd103edd 100644 --- a/Project.toml +++ b/Project.toml @@ -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] diff --git a/src/fileio.jl b/ext/FileIOExt.jl similarity index 82% rename from src/fileio.jl rename to ext/FileIOExt.jl index 468ba19cb..f4d40b7d4 100644 --- a/src/fileio.jl +++ b/ext/FileIOExt.jl @@ -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) @@ -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 @@ -75,3 +83,5 @@ function fileio_save( end end end + +end diff --git a/ext/OrderedCollectionsFileIOExt.jl b/ext/OrderedCollectionsFileIOExt.jl new file mode 100644 index 000000000..29cbbd687 --- /dev/null +++ b/ext/OrderedCollectionsFileIOExt.jl @@ -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 diff --git a/src/HDF5.jl b/src/HDF5.jl index b533d84a0..977b01ab8 100644 --- a/src/HDF5.jl +++ b/src/HDF5.jl @@ -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 # @@ -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) =