Skip to content

Commit 943c590

Browse files
committed
Move JLD2.jl dependency to a package extension
1 parent 7200eef commit 943c590

File tree

5 files changed

+96
-68
lines changed

5 files changed

+96
-68
lines changed

Project.toml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
name = "MetaGraphs"
22
uuid = "626554b9-1ddb-594c-aa3c-2596fe9399a5"
3-
version = "0.8.1"
3+
version = "0.9.0"
44

55
[deps]
66
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
7-
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
87
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
98

9+
[weakdeps]
10+
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
11+
12+
[extensions]
13+
MetaGraphsJLD2Ext = ["Graphs", "JLD2"]
14+
1015
[compat]
1116
Graphs = "1.4"
1217
JLD2 = "0.1.11, 0.2, 0.3, 0.4, 0.5, 0.6"
1318
julia = "1"
1419

1520
[extras]
1621
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
22+
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
1723
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1824

1925
[targets]
20-
test = ["Test", "Base64"]
26+
test = ["Test", "Base64", "JLD2"]

ext/MetaGraphsJLD2Ext.jl

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
module MetaGraphsJLD2Ext
2+
3+
using Graphs
4+
using JLD2
5+
using MetaGraphs
6+
7+
function MetaGraphs.loadmg(fn::AbstractString)
8+
@load fn g
9+
return g
10+
end
11+
12+
function MetaGraphs.savemg(fn::AbstractString, g::AbstractMetaGraph)
13+
@save fn g
14+
return 1
15+
end
16+
17+
# escaping unescaped quotation marks
18+
# i.e. replacing `"`` with `\"` while leaving `\"` as is
19+
escape_quotes(s::AbstractString) = replace(s, r"([^\\])\"" => s"\1\\\\\"")
20+
21+
# According to the DOT language specification https://graphviz.org/doc/info/lang.html
22+
# we can quote everyhthing that's not an XML/HTML literal
23+
function quote_prop(p::AbstractString)
24+
if occursin(r"<+.*>+$", p)
25+
# The label is an HTML string, no additional quotes here.
26+
return p
27+
else
28+
return "\"" * escape_quotes(p) * "\""
29+
end
30+
end
31+
# if the property value is _not_ a string it cannot be XML/HTML literal, so just put it in quotes
32+
quote_prop(p::Any) = "\"" * escape_quotes(string(p)) * "\""
33+
# NOTE: down there I only quote property _values_. DOT allows quoting property _names_ too
34+
# I don't do that as long as names are Symbols and can't have spaces and commas and stuff.
35+
# That will break if someone uses a DOT keyword as a property name, as they must be quoted.
36+
37+
function MetaGraphs.savedot(io::IO, g::AbstractMetaGraph)
38+
if is_directed(g)
39+
write(io, "digraph G {\n")
40+
dash = "->"
41+
else
42+
write(io, "graph G {\n")
43+
dash = "--"
44+
end
45+
46+
for p in props(g)
47+
write(io, "$(p[1])=$(quote_prop(p[2]));\n")
48+
end
49+
50+
for v in vertices(g)
51+
write(io, "$v")
52+
if length(props(g, v)) > 0
53+
write(io, " [ ")
54+
55+
for p in props(g, v)
56+
write(io, "$(p[1])=$(quote_prop(p[2])), ")
57+
end
58+
59+
write(io, "];")
60+
end
61+
write(io, "\n")
62+
end
63+
64+
for e in edges(g)
65+
write(io, "$(src(e)) $dash $(dst(e)) [ ")
66+
for p in props(g,e)
67+
write(io, "$(p[1])=$(quote_prop(p[2])), ")
68+
end
69+
write(io, "]\n")
70+
end
71+
write(io, "}\n")
72+
end
73+
74+
end

src/MetaGraphs.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module MetaGraphs
22
using Graphs
3-
using JLD2
43

54
import Base:
65
eltype, show, ==, Pair,

src/persistence.jl

Lines changed: 11 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,76 +3,23 @@
33
struct MGFormat <: AbstractGraphFormat end
44
struct DOTFormat <: AbstractGraphFormat end
55

6-
function loadmg(fn::AbstractString)
7-
@load fn g
8-
return g
6+
function loadmg(args...)
7+
error("In order to load static graphs from binary files, you need to load the JLD2.jl \
8+
package")
99
end
1010

11-
function savemg(fn::AbstractString, g::AbstractMetaGraph)
12-
@save fn g
13-
return 1
11+
function savemg(args...)
12+
error("In order to save static graphs to binary files, you need to load the JLD2.jl \
13+
package")
1414
end
1515

16-
loadgraph(fn::AbstractString, gname::String, ::MGFormat) = loadmg(fn)
17-
savegraph(fn::AbstractString, g::AbstractMetaGraph) = savemg(fn, g)
18-
19-
# escaping unescaped quotation marks
20-
# i.e. replacing `"`` with `\"` while leaving `\"` as is
21-
escape_quotes(s::AbstractString) = replace(s, r"([^\\])\"" => s"\1\\\\\"")
22-
23-
# According to the DOT language specification https://graphviz.org/doc/info/lang.html
24-
# we can quote everyhthing that's not an XML/HTML literal
25-
function quote_prop(p::AbstractString)
26-
if occursin(r"<+.*>+$", p)
27-
# The label is an HTML string, no additional quotes here.
28-
return p
29-
else
30-
return "\"" * escape_quotes(p) * "\""
31-
end
16+
function savedot(args...)
17+
error("In order to save static graphs to binary files, you need to load the JLD2.jl \
18+
package")
3219
end
33-
# if the property value is _not_ a string it cannot be XML/HTML literal, so just put it in quotes
34-
quote_prop(p::Any) = "\"" * escape_quotes(string(p)) * "\""
35-
# NOTE: down there I only quote property _values_. DOT allows quoting property _names_ too
36-
# I don't do that as long as names are Symbols and can't have spaces and commas and stuff.
37-
# That will break if someone uses a DOT keyword as a property name, as they must be quoted.
38-
39-
function savedot(io::IO, g::AbstractMetaGraph)
40-
41-
if is_directed(g)
42-
write(io, "digraph G {\n")
43-
dash = "->"
44-
else
45-
write(io, "graph G {\n")
46-
dash = "--"
47-
end
48-
49-
for p in props(g)
50-
write(io, "$(p[1])=$(quote_prop(p[2]));\n")
51-
end
52-
53-
for v in vertices(g)
54-
write(io, "$v")
55-
if length(props(g, v)) > 0
56-
write(io, " [ ")
5720

58-
for p in props(g, v)
59-
write(io, "$(p[1])=$(quote_prop(p[2])), ")
60-
end
61-
62-
write(io, "];")
63-
end
64-
write(io, "\n")
65-
end
66-
67-
for e in edges(g)
68-
write(io, "$(src(e)) $dash $(dst(e)) [ ")
69-
for p in props(g,e)
70-
write(io, "$(p[1])=$(quote_prop(p[2])), ")
71-
end
72-
write(io, "]\n")
73-
end
74-
write(io, "}\n")
75-
end
21+
loadgraph(fn::AbstractString, ::String, ::MGFormat) = loadmg(fn)
22+
savegraph(fn::AbstractString, g::AbstractMetaGraph) = savemg(fn, g)
7623

7724
function savegraph(fn::AbstractString, g::AbstractMetaGraph, ::DOTFormat)
7825
open(fn, "w") do fp

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using Graphs
22
using MetaGraphs
3+
4+
using JLD2
35
using Test
46

57
import Graphs.SimpleGraphs: SimpleGraph, SimpleDiGraph

0 commit comments

Comments
 (0)