From 20979b20b86b65a6712bc2ada84f7102145cc11e Mon Sep 17 00:00:00 2001 From: Simone Carlo Surace Date: Wed, 28 Jan 2026 15:08:18 +0100 Subject: [PATCH] Move JLD2 dependency to package extension --- .github/workflows/test.yml | 2 +- Project.toml | 11 ++++++++--- docs/Project.toml | 2 ++ ext/MetaGraphsNextJLD2Ext.jl | 17 +++++++++++++++++ src/MetaGraphsNext.jl | 22 +++++++++++++++++++++- src/persistence.jl | 21 ++++++--------------- test/Project.toml | 1 + test/runtests.jl | 1 + test/tutorial/3_files.jl | 4 +++- 9 files changed, 60 insertions(+), 21 deletions(-) create mode 100644 ext/MetaGraphsNextJLD2Ext.jl diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 05116bf..b35df7e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,7 +11,7 @@ jobs: strategy: matrix: version: - - '1.6' + - '1.10' - '1' steps: - uses: actions/checkout@v6 diff --git a/Project.toml b/Project.toml index fa0555b..5d38745 100644 --- a/Project.toml +++ b/Project.toml @@ -1,14 +1,19 @@ name = "MetaGraphsNext" uuid = "fa8bd995-216d-47f1-8a91-f3b68fbeb377" -version = "0.7.5" +version = "0.8.0" [deps] Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" -JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" SimpleTraits = "699a6c99-e7fa-54fc-8d76-47d257e15c1d" +[weakdeps] +JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" + +[extensions] +MetaGraphsNextJLD2Ext = ["Graphs", "JLD2"] + [compat] Graphs = "1.4.1" JLD2 = "0.1.11, 0.2, 0.3, 0.4, 0.5, 0.6" SimpleTraits = "0.9" -julia = "1.6" +julia = "1.10" diff --git a/docs/Project.toml b/docs/Project.toml index 4151723..4ee2f1b 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -3,6 +3,7 @@ BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" +JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306" MetaGraphs = "626554b9-1ddb-594c-aa3c-2596fe9399a5" MetaGraphsNext = "fa8bd995-216d-47f1-8a91-f3b68fbeb377" @@ -10,3 +11,4 @@ MetaGraphsNext = "fa8bd995-216d-47f1-8a91-f3b68fbeb377" [compat] Documenter = "1" Graphs = "1.13.3" +JLD2 = "0.6" diff --git a/ext/MetaGraphsNextJLD2Ext.jl b/ext/MetaGraphsNextJLD2Ext.jl new file mode 100644 index 0000000..5cc54d2 --- /dev/null +++ b/ext/MetaGraphsNextJLD2Ext.jl @@ -0,0 +1,17 @@ +module MetaGraphsNextJLD2Ext + +using Graphs +using JLD2 +using MetaGraphsNext + +function MetaGraphsNext.loadmg(file::AbstractString) + @load file meta_graph + return meta_graph +end + +function MetaGraphsNext.savemg(file::AbstractString, meta_graph::MetaGraph) + @save file meta_graph + return 1 +end + +end diff --git a/src/MetaGraphsNext.jl b/src/MetaGraphsNext.jl index 963b04a..afdd8b3 100644 --- a/src/MetaGraphsNext.jl +++ b/src/MetaGraphsNext.jl @@ -5,7 +5,6 @@ A package for graphs with vertex labels and metadata in Julia. Its main export i """ module MetaGraphsNext -using JLD2 using Graphs using SimpleTraits @@ -24,4 +23,25 @@ include("dict_utils.jl") include("weights.jl") include("persistence.jl") +function __init__() + # Register error hint for the `loadmg` and `savemg` + if isdefined(Base.Experimental, :register_error_hint) + Base.Experimental.register_error_hint(MethodError) do io, exc, _, _ + if exc.f === loadmg + print( + io, + "\n\nIn order to load meta graphs from binary files, you need to load \ + the JLD2.jl package.", + ) + elseif exc.f === savemg + print( + io, + "\n\nIn order to save meta graphs to binary files, you need to load \ + the JLD2.jl package.", + ) + end + end + end +end + end # module diff --git a/src/persistence.jl b/src/persistence.jl index cf7fd8e..81aa860 100644 --- a/src/persistence.jl +++ b/src/persistence.jl @@ -1,5 +1,3 @@ -# Metagraphs files are simply JLD2 files. - """ struct MGFormat <: AbstractGraphFormat end @@ -7,6 +5,12 @@ You can save `MetaGraph`s in a `MGFormat`, currently based on `JLD2`. """ struct MGFormat <: Graphs.AbstractGraphFormat end +function loadmg end +function savemg end + +Graphs.loadgraph(file::AbstractString, ::String, ::MGFormat) = loadmg(file) +Graphs.savegraph(file::AbstractString, meta_graph::MetaGraph) = savemg(file, meta_graph) + """ struct DOTFormat <: AbstractGraphFormat end @@ -14,19 +18,6 @@ If all metadata types support `pairs` or are `Nothing`, you can save `MetaGraph` """ struct DOTFormat <: Graphs.AbstractGraphFormat end -function loadmg(file::AbstractString) - @load file meta_graph - return meta_graph -end - -function savemg(file::AbstractString, meta_graph::MetaGraph) - @save file meta_graph - return 1 -end - -Graphs.loadgraph(file::AbstractString, ::String, ::MGFormat) = loadmg(file) -Graphs.savegraph(file::AbstractString, meta_graph::MetaGraph) = savemg(file, meta_graph) - function show_meta_list(io::IO, meta) if meta !== nothing && length(meta) > 0 next = false diff --git a/test/Project.toml b/test/Project.toml index 5b04b0e..f46087c 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -4,6 +4,7 @@ BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" +JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" MetaGraphs = "626554b9-1ddb-594c-aa3c-2596fe9399a5" SimpleTraits = "699a6c99-e7fa-54fc-8d76-47d257e15c1d" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/test/runtests.jl b/test/runtests.jl index ac55636..2046279 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,6 +1,7 @@ using Aqua using Documenter using Graphs +using JLD2 using MetaGraphsNext using Test diff --git a/test/tutorial/3_files.jl b/test/tutorial/3_files.jl index f4442f7..16d76a0 100644 --- a/test/tutorial/3_files.jl +++ b/test/tutorial/3_files.jl @@ -7,7 +7,9 @@ using Test #src # ## MGFormat # MetaGraphsNext.jl overloads `Graphs.savegraph` to write graphs in a custom format called `MGFormat`, which is based on JLD2. -# It is not very readable, but it does give the right result when we load it back. +# It is not very readable, but it does give the right result when we load it back. This requires JLD2 to be loaded as well. + +using JLD2 example = MetaGraph(Graph(), Symbol);