Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f21671f

Browse files
committedJun 13, 2023
Use package extensions
1 parent c655409 commit f21671f

13 files changed

+152
-175
lines changed
 

‎Project.toml

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "GraphIO"
22
uuid = "aa1b3936-2fda-51b9-ab35-c553d3a640a2"
3-
version = "0.6.0"
3+
version = "0.7.0"
44

55
[deps]
66
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
@@ -10,12 +10,19 @@ SimpleTraits = "699a6c99-e7fa-54fc-8d76-47d257e15c1d"
1010

1111
[compat]
1212
EzXML = "1"
13-
Graphs = "1.4"
13+
Graphs = "1"
1414
ParserCombinator = "2.1"
1515
Requires = "1"
1616
SimpleTraits = "0.9"
1717
julia = "1"
1818

19+
[extensions]
20+
GraphIODOTExt = "ParserCombinator"
21+
GraphIOGEXFExt = "EzXML"
22+
GraphIOGMLExt = "ParserCombinator"
23+
GraphIOGraphMLExt = "EzXML"
24+
GraphIOLGCompressedExt = "CodecZlib"
25+
1926
[extras]
2027
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
2128
EzXML = "8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615"
@@ -25,3 +32,8 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2532

2633
[targets]
2734
test = ["CodecZlib", "Graphs", "EzXML", "ParserCombinator", "Test"]
35+
36+
[weakdeps]
37+
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
38+
EzXML = "8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615"
39+
ParserCombinator = "fae87a5f-d1ad-5cf0-8f61-c941e1580b46"

‎src/DOT/Dot_conditional.jl ‎ext/GraphIODOTExt.jl

+27-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
1-
using .ParserCombinator.Parsers
1+
module GraphIODOTExt
22

3-
function savedot(io::IO, g::Graphs.AbstractGraph, gname::String = "")
4-
isdir = Graphs.is_directed(g)
3+
using Graphs
4+
import Graphs: loadgraph, loadgraphs, savegraph
5+
6+
@static if isdefined(Base, :get_extension)
7+
using GraphIO
8+
using ParserCombinator
9+
import GraphIO.DOT.DOTFormat
10+
else
11+
using ..GraphIO
12+
using ..ParserCombinator
13+
import ..GraphIO.DOT.DOTFormat
14+
end
15+
16+
17+
function savedot(io::IO, g::AbstractGraph, gname::String = "")
18+
isdir = is_directed(g)
519
println(io,(isdir ? "digraph " : "graph ") * gname * " {")
6-
for i in Graphs.vertices(g)
20+
for i in vertices(g)
721
println(io,"\t" * string(i))
822
end
923
if isdir
10-
for u in Graphs.vertices(g)
11-
out_nbrs = Graphs.outneighbors(g, u)
24+
for u in vertices(g)
25+
out_nbrs = outneighbors(g, u)
1226
length(out_nbrs) == 0 && continue
1327
println(io, "\t" * string(u) * " -> {" * join(out_nbrs,',') * "}")
1428
end
1529
else
16-
for e in Graphs.edges(g)
17-
source = string(Graphs.src(e))
18-
dest = string(Graphs.dst(e))
30+
for e in edges(g)
31+
source = string(src(e))
32+
dest = string(dst(e))
1933
println(io, "\t" * source * " -- " * dest)
2034
end
2135
end
@@ -36,9 +50,9 @@ function _dot_read_one_graph(pg::Parsers.DOT.Graph)
3650
nvg = length(Parsers.DOT.nodes(pg))
3751
nodedict = Dict(zip(collect(Parsers.DOT.nodes(pg)), 1:nvg))
3852
if isdir
39-
g = Graphs.DiGraph(nvg)
53+
g = DiGraph(nvg)
4054
else
41-
g = Graphs.Graph(nvg)
55+
g = Graph(nvg)
4256
end
4357
for es in Parsers.DOT.edges(pg)
4458
s = nodedict[es[1]]
@@ -74,3 +88,5 @@ loadgraph(io::IO, gname::String, ::DOTFormat) = loaddot(io, gname)
7488
loadgraphs(io::IO, ::DOTFormat) = loaddot_mult(io)
7589
savegraph(io::IO, g::AbstractGraph, gname::String, ::DOTFormat) = savedot(io, g, gname)
7690
savegraph(io::IO, d::Dict, ::DOTFormat) = savedot_mult(io, d)
91+
92+
end

‎src/GEXF/Gexf_conditional.jl ‎ext/GraphIOGEXFExt.jl

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1-
using .EzXML
1+
module GraphIOGEXFExt
2+
3+
using Graphs
4+
import Graphs: loadgraph, loadgraphs, savegraph, AbstractGraph
5+
6+
@static if isdefined(Base, :get_extension)
7+
using GraphIO
8+
using EzXML
9+
import GraphIO.GEXF.GEXFFormat
10+
else
11+
using ..GraphIO
12+
using ..EzXML
13+
import ..GraphIO.GEXF.GEXFFormat
14+
end
15+
216

317
"""
418
savegexf(f, g, gname)
519
620
Write a graph `g` with name `gname` to an IO stream `io` in the
721
[Gexf](http://gexf.net/format/) format. Return 1 (number of graphs written).
822
"""
9-
function savegexf(io::IO, g::Graphs.AbstractGraph, gname::String)
23+
function savegexf(io::IO, g::AbstractGraph, gname::String)
1024
xdoc = XMLDocument()
1125
xroot = setroot!(xdoc, ElementNode("gexf"))
1226
xroot["xmlns"] = "http://www.gexf.net/1.2draft"
@@ -28,7 +42,7 @@ function savegexf(io::IO, g::Graphs.AbstractGraph, gname::String)
2842

2943
xedges = addelement!(xg, "edges")
3044
m = 0
31-
for e in Graphs.edges(g)
45+
for e in edges(g)
3246
xe = addelement!(xedges, "edge")
3347
xe["id"] = "$m"
3448
xe["source"] = "$(src(e)-1)"
@@ -41,3 +55,5 @@ function savegexf(io::IO, g::Graphs.AbstractGraph, gname::String)
4155
end
4256

4357
savegraph(io::IO, g::AbstractGraph, gname::String, ::GEXFFormat) = savegexf(io, g, gname)
58+
59+
end

‎src/GML/Gml_conditional.jl ‎ext/GraphIOGMLExt.jl

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1-
using .ParserCombinator.Parsers
1+
module GraphIOGMLExt
2+
3+
using Graphs
4+
import Graphs: loadgraph, loadgraphs, savegraph
5+
6+
@static if isdefined(Base, :get_extension)
7+
using GraphIO
8+
using ParserCombinator
9+
import GraphIO.GML.GMLFormat
10+
else
11+
using ..GraphIO
12+
using ..ParserCombinator
13+
import ..GraphIO.GML.GMLFormat
14+
end
15+
216

317
function _gml_read_one_graph(gs, dir)
418
nodes = [x[:id] for x in gs[:node]]
519
if dir
6-
g = Graphs.DiGraph(length(nodes))
20+
g = DiGraph(length(nodes))
721
else
8-
g = Graphs.Graph(length(nodes))
22+
g = Graph(length(nodes))
923
end
1024
mapping = Dict{Int,Int}()
1125
for (i, n) in enumerate(nodes)
@@ -31,7 +45,7 @@ end
3145

3246
function loadgml_mult(io::IO)
3347
p = Parsers.GML.parse_dict(read(io, String))
34-
graphs = Dict{String,Graphs.AbstractGraph}()
48+
graphs = Dict{String,AbstractGraph}()
3549
for gs in p[:graph]
3650
dir = Bool(get(gs, :directed, 0))
3751
graphname = get(gs, :label, dir ? "digraph" : "graph")
@@ -46,7 +60,7 @@ end
4660
Write a graph `g` with name `gname` to an IO stream `io` in the
4761
[GML](https://en.wikipedia.org/wiki/Graph_Modelling_Language) format. Return 1.
4862
"""
49-
function savegml(io::IO, g::Graphs.AbstractGraph, gname::String = "")
63+
function savegml(io::IO, g::AbstractGraph, gname::String = "")
5064
println(io, "graph")
5165
println(io, "[")
5266
length(gname) > 0 && println(io, "label \"$gname\"")
@@ -57,7 +71,7 @@ function savegml(io::IO, g::Graphs.AbstractGraph, gname::String = "")
5771
println(io, "\t\tid $i")
5872
println(io, "\t]")
5973
end
60-
for e in Graphs.edges(g)
74+
for e in edges(g)
6175
s, t = Tuple(e)
6276
println(io, "\tedge")
6377
println(io, "\t[")
@@ -85,3 +99,5 @@ loadgraph(io::IO, gname::String, ::GMLFormat) = loadgml(io, gname)
8599
loadgraphs(io::IO, ::GMLFormat) = loadgml_mult(io)
86100
savegraph(io::IO, g::AbstractGraph, gname::String, ::GMLFormat) = savegml(io, g, gname)
87101
savegraph(io::IO, d::Dict, ::GMLFormat) = savegml_mult(io, d)
102+
103+
end

‎src/GraphML/GraphML_conditional.jl ‎ext/GraphIOGraphMLExt.jl

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1-
using .EzXML
1+
module GraphIOGraphMLExt
2+
3+
using Graphs
4+
import Graphs: loadgraph, loadgraphs, savegraph
5+
6+
@static if isdefined(Base, :get_extension)
7+
using GraphIO
8+
using EzXML
9+
import GraphIO.GraphML.GraphMLFormat
10+
else
11+
using ..GraphIO
12+
using ..EzXML
13+
import ..GraphIO.GraphML.GraphMLFormat
14+
end
15+
216

317
function _graphml_read_one_graph(reader::EzXML.StreamReader, isdirected::Bool)
418
nodes = Dict{String,Int}()
5-
xedges = Vector{Graphs.Edge}()
19+
xedges = Vector{Edge}()
620
nodeid = 1
721
for typ in reader
822
if typ == EzXML.READER_ELEMENT
@@ -13,13 +27,13 @@ function _graphml_read_one_graph(reader::EzXML.StreamReader, isdirected::Bool)
1327
elseif elname == "edge"
1428
src = reader["source"]
1529
tar = reader["target"]
16-
push!(xedges, Graphs.Edge(nodes[src], nodes[tar]))
30+
push!(xedges, Edge(nodes[src], nodes[tar]))
1731
else
1832
@warn "Skipping unknown node '$(elname)' - further warnings will be suppressed" maxlog=1 _id=:unknode
1933
end
2034
end
2135
end
22-
g = (isdirected ? Graphs.DiGraph : Graphs.Graph)(length(nodes))
36+
g = (isdirected ? DiGraph : Graph)(length(nodes))
2337
for edge in xedges
2438
add_edge!(g, edge)
2539
end
@@ -58,7 +72,7 @@ end
5872

5973
function loadgraphml_mult(io::IO)
6074
reader = EzXML.StreamReader(io)
61-
graphs = Dict{String,Graphs.AbstractGraph}()
75+
graphs = Dict{String,AbstractGraph}()
6276
for typ in reader
6377
if typ == EzXML.READER_ELEMENT
6478
elname = EzXML.nodename(reader)
@@ -101,7 +115,7 @@ function savegraphml_mult(io::IO, graphs::Dict)
101115
end
102116

103117
m = 0
104-
for e in Graphs.edges(g)
118+
for e in edges(g)
105119
xe = addelement!(xg, "edge")
106120
xe["id"] = "e$m"
107121
xe["source"] = "n$(src(e)-1)"
@@ -113,11 +127,13 @@ function savegraphml_mult(io::IO, graphs::Dict)
113127
return length(graphs)
114128
end
115129

116-
savegraphml(io::IO, g::Graphs.AbstractGraph, gname::String) =
130+
savegraphml(io::IO, g::AbstractGraph, gname::String) =
117131
savegraphml_mult(io, Dict(gname => g))
118132

119133

120134
loadgraph(io::IO, gname::String, ::GraphMLFormat) = loadgraphml(io, gname)
121135
loadgraphs(io::IO, ::GraphMLFormat) = loadgraphml_mult(io)
122136
savegraph(io::IO, g::AbstractGraph, gname::String, ::GraphMLFormat) = savegraphml(io, g, gname)
123137
savegraph(io::IO, d::Dict, ::GraphMLFormat) = savegraphml_mult(io, d)
138+
139+
end

‎src/LGCompressed/LGCompressed_conditional.jl ‎ext/GraphIOLGCompressedExt.jl

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1-
using .CodecZlib
1+
module GraphIOLGCompressedExt
2+
3+
using Graphs
4+
import Graphs: loadgraph, loadgraphs, savegraph, LGFormat
5+
6+
@static if isdefined(Base, :get_extension)
7+
using GraphIO
8+
using CodecZlib
9+
import GraphIO.LGCompressed.LGCompressedFormat
10+
else
11+
using ..GraphIO
12+
using ..CodecZlib
13+
import ..GraphIO.LGCompressed.LGCompressedFormat
14+
end
15+
216

317
function savegraph(fn::AbstractString, g::AbstractGraph, gname::AbstractString,
418
format::LGCompressedFormat)
519
io = open(fn, "w")
620
try
721
io = GzipCompressorStream(io)
8-
return savegraph(io, g, gname, Graphs.LGFormat())
22+
return savegraph(io, g, gname, LGFormat())
923
catch
1024
rethrow()
1125
finally
@@ -21,7 +35,7 @@ function savegraph(fn::AbstractString, d::Dict{T,U},
2135
io = open(fn, "w")
2236
try
2337
io = GzipCompressorStream(io)
24-
return savegraph(io, d, Graphs.LGFormat())
38+
return savegraph(io, d, LGFormat())
2539
catch
2640
rethrow()
2741
finally
@@ -32,8 +46,10 @@ end
3246
# savegraph(fn::AbstractString, d::Dict; compress) = savegraph(fn, d, LGCompressedFormat())
3347

3448
loadgraph(fn::AbstractString, gname::AbstractString, format::LGCompressedFormat) =
35-
loadgraph(fn, gname, Graphs.LGFormat())
49+
loadgraph(fn, gname, LGFormat())
3650

37-
loadgraph(fn::AbstractString, format::LGCompressedFormat) = loadgraph(fn, "graph", Graphs.LGFormat())
51+
loadgraph(fn::AbstractString, format::LGCompressedFormat) = loadgraph(fn, "graph", LGFormat())
3852

3953
loadgraphs(fn::AbstractString, format::LGCompressedFormat) = loadgraphs(fn, LGFormat())
54+
55+
end

‎src/DOT/Dot.jl

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11
module DOT
22

3-
using Requires
4-
using Graphs
5-
using Graphs: AbstractGraphFormat
6-
7-
import Graphs: loadgraph, loadgraphs, savegraph
3+
import Graphs: AbstractGraphFormat
84

95
export DOTFormat
106

117
struct DOTFormat <: AbstractGraphFormat end
128

13-
function __init__()
14-
@require ParserCombinator="fae87a5f-d1ad-5cf0-8f61-c941e1580b46" begin
15-
include("Dot_conditional.jl")
16-
end
17-
end
18-
199
end #module

‎src/GEXF/Gexf.jl

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
module GEXF
22

3-
using Requires
4-
using Graphs
5-
using Graphs: AbstractGraph, AbstractGraphFormat
6-
7-
import Graphs: savegraph
3+
import Graphs: AbstractGraphFormat
84

95
export GEXFFormat
106

117
# TODO: implement readgexf
128
struct GEXFFormat <: AbstractGraphFormat end
139

14-
function __init__()
15-
@require EzXML="8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615" begin
16-
include("Gexf_conditional.jl")
17-
end
18-
end
19-
2010
end #module

‎src/GML/Gml.jl

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11
module GML
22

3-
using Requires
4-
using Graphs
5-
using Graphs: AbstractGraphFormat
6-
7-
import Graphs: loadgraph, loadgraphs, savegraph
3+
import Graphs: AbstractGraphFormat
84

95
export GMLFormat
106

117
struct GMLFormat <: AbstractGraphFormat end
128

13-
function __init__()
14-
@require ParserCombinator="fae87a5f-d1ad-5cf0-8f61-c941e1580b46" begin
15-
include("Gml_conditional.jl")
16-
end
17-
end
18-
199
end # module

‎src/GraphIO.jl

+21-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
module GraphIO
22

3-
#=
4-
NOTE: Requires.jl is a temporary fix until we can have multiple sub-packages with their own
5-
requirements in a single repository.
6-
=#
3+
@static if !isdefined(Base, :get_extension)
4+
using Requires
5+
end
6+
7+
8+
@static if !isdefined(Base, :get_extension)
9+
function __init__()
10+
@require CodecZlib="944b1d66-785c-5afd-91f1-9de20f533193" begin
11+
include("../ext/GraphIOLGCompressedExt.jl")
12+
end
13+
@require EzXML="8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615" begin
14+
include("../ext/GraphIOGEXFExt.jl")
15+
include("../ext/GraphIOGraphMLExt.jl")
16+
end
17+
@require ParserCombinator="fae87a5f-d1ad-5cf0-8f61-c941e1580b46" begin
18+
include("../ext/GraphIODOTExt.jl")
19+
include("../ext/GraphIOGMLExt.jl")
20+
end
21+
end
22+
end
723

824
include("LGCompressed/LGCompressed.jl")
925
include("GEXF/Gexf.jl")
@@ -15,6 +31,4 @@ include("NET/Net.jl")
1531
include("Edgelist/Edgelist.jl")
1632
include("CDF/Cdf.jl")
1733

18-
include("deprecations.jl")
19-
20-
end # module
34+
end

‎src/GraphML/GraphML.jl

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11
module GraphML
22

3-
using Requires
4-
using Graphs
5-
using Graphs: AbstractGraphFormat
6-
7-
import Graphs: loadgraph, loadgraphs, savegraph
3+
import Graphs: AbstractGraphFormat
84

95
export GraphMLFormat
106

11-
# TODO: implement writing a dict of graphs
12-
137
struct GraphMLFormat <: AbstractGraphFormat end
148

15-
function __init__()
16-
@require EzXML="8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615" begin
17-
include("GraphML_conditional.jl")
18-
end
19-
end
20-
219
end # module

‎src/LGCompressed/LGCompressed.jl

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11
module LGCompressed
22

3-
using Requires
4-
using Graphs
5-
using Graphs: AbstractGraphFormat
6-
7-
import Graphs: loadgraph, loadgraphs, savegraph
3+
import Graphs: AbstractGraphFormat
84

95
export LGCompressedFormat
106

117
struct LGCompressedFormat <: AbstractGraphFormat end
128

13-
function __init__()
14-
@require CodecZlib="944b1d66-785c-5afd-91f1-9de20f533193" begin
15-
include("LGCompressed_conditional.jl")
16-
end
17-
end
18-
199
end # module

‎src/deprecations.jl

-77
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.