Skip to content

Commit 9d32f65

Browse files
dgleichgdalle
andauthored
Fix circular dependencies (#65)
* Fix circular dependencies * Remove old extension files --------- Co-authored-by: Guillaume Dalle <[email protected]>
1 parent 1e6ce43 commit 9d32f65

7 files changed

+139
-189
lines changed

Project.toml

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ EzXML = "8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615"
1313
ParserCombinator = "fae87a5f-d1ad-5cf0-8f61-c941e1580b46"
1414

1515
[extensions]
16-
GraphIODOTExt = "ParserCombinator"
17-
GraphIOGEXFExt = "EzXML"
18-
GraphIOGMLExt = "ParserCombinator"
19-
GraphIOGraphMLExt = "EzXML"
16+
GraphIOParserCombinator = "ParserCombinator"
17+
GraphIOXML = "EzXML"
2018
GraphIOLGCompressedExt = "CodecZlib"
2119

2220
[compat]

ext/GraphIODOTExt.jl

-95
This file was deleted.

ext/GraphIOGEXFExt.jl

-58
This file was deleted.

ext/GraphIOLGCompressedExt.jl

+3-9
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@ module GraphIOLGCompressedExt
33
using Graphs
44
import Graphs: loadgraph, loadgraphs, savegraph, LGFormat
55

6-
@static if isdefined(Base, :get_extension)
7-
using GraphIO
8-
using CodecZlib
9-
import GraphIO.LGCompressed.LGCompressedFormat
10-
else # not required for julia >= v1.9
11-
using ..GraphIO
12-
using ..CodecZlib
13-
import ..GraphIO.LGCompressed.LGCompressedFormat
14-
end
6+
using GraphIO
7+
using CodecZlib
8+
import GraphIO.LGCompressed.LGCompressedFormat
159

1610
function savegraph(
1711
fn::AbstractString, g::AbstractGraph, gname::AbstractString, format::LGCompressedFormat

ext/GraphIOGMLExt.jl renamed to ext/GraphIOParserCombinator.jl

+85-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,92 @@
1-
module GraphIOGMLExt
1+
module GraphIOParserCombinator
22

33
using Graphs
44
import Graphs: loadgraph, loadgraphs, savegraph
55

6-
@static if isdefined(Base, :get_extension)
7-
using GraphIO
8-
using ParserCombinator
9-
import GraphIO.GML.GMLFormat
10-
else # not required for julia >= v1.9
11-
using ..GraphIO
12-
using ..ParserCombinator
13-
import ..GraphIO.GML.GMLFormat
6+
using GraphIO
7+
using ParserCombinator
8+
import GraphIO.DOT.DOTFormat
9+
import GraphIO.GML.GMLFormat
10+
11+
function savedot(io::IO, g::AbstractGraph, gname::String="")
12+
isdir = is_directed(g)
13+
println(io, (isdir ? "digraph " : "graph ") * gname * " {")
14+
for i in vertices(g)
15+
println(io, "\t" * string(i))
16+
end
17+
if isdir
18+
for u in vertices(g)
19+
out_nbrs = outneighbors(g, u)
20+
length(out_nbrs) == 0 && continue
21+
println(io, "\t" * string(u) * " -> {" * join(out_nbrs, ',') * "}")
22+
end
23+
else
24+
for e in edges(g)
25+
source = string(src(e))
26+
dest = string(dst(e))
27+
println(io, "\t" * source * " -- " * dest)
28+
end
29+
end
30+
println(io, "}")
31+
return 1
32+
end
33+
34+
function savedot_mult(io::IO, graphs::Dict)
35+
ng = 0
36+
for (gname, g) in graphs
37+
ng += savedot(io, g, gname)
38+
end
39+
return ng
40+
end
41+
42+
function _dot_read_one_graph(pg::Parsers.DOT.Graph)
43+
isdir = pg.directed
44+
nvg = length(Parsers.DOT.nodes(pg))
45+
nodedict = Dict(zip(collect(Parsers.DOT.nodes(pg)), 1:nvg))
46+
if isdir
47+
g = DiGraph(nvg)
48+
else
49+
g = Graph(nvg)
50+
end
51+
for es in Parsers.DOT.edges(pg)
52+
s = nodedict[es[1]]
53+
d = nodedict[es[2]]
54+
add_edge!(g, s, d)
55+
end
56+
return g
57+
end
58+
59+
function _name(pg::Parsers.DOT.Graph)
60+
return if pg.id !== nothing
61+
pg.id.id
62+
else
63+
Parsers.DOT.StringID(pg.directed ? "digraph" : "graph")
64+
end
65+
end
66+
67+
function loaddot(io::IO, gname::String)
68+
p = Parsers.DOT.parse_dot(read(io, String))
69+
for pg in p
70+
_name(pg) == gname && return _dot_read_one_graph(pg)
71+
end
72+
return error("Graph $gname not found")
73+
end
74+
75+
function loaddot_mult(io::IO)
76+
p = Parsers.DOT.parse_dot(read(io, String))
77+
graphs = Dict{String,AbstractGraph}()
78+
79+
for pg in p
80+
graphs[_name(pg)] = _dot_read_one_graph(pg)
81+
end
82+
return graphs
1483
end
1584

85+
loadgraph(io::IO, gname::String, ::DOTFormat) = loaddot(io, gname)
86+
loadgraphs(io::IO, ::DOTFormat) = loaddot_mult(io)
87+
savegraph(io::IO, g::AbstractGraph, gname::String, ::DOTFormat) = savedot(io, g, gname)
88+
savegraph(io::IO, d::Dict, ::DOTFormat) = savedot_mult(io, d)
89+
1690
function _gml_read_one_graph(gs, dir)
1791
nodes = [x[:id] for x in gs[:node]]
1892
if dir
@@ -54,7 +128,7 @@ function loadgml_mult(io::IO)
54128
end
55129

56130
"""
57-
savegml(f, g, gname="graph")
131+
savegml(f, g, gname="graph")
58132
59133
Write a graph `g` with name `gname` to an IO stream `io` in the
60134
[GML](https://en.wikipedia.org/wiki/Graph_Modelling_Language) format. Return 1.
@@ -83,7 +157,7 @@ function savegml(io::IO, g::AbstractGraph, gname::String="")
83157
end
84158

85159
"""
86-
savegml_mult(io, graphs)
160+
savegml_mult(io, graphs)
87161
Write a dictionary of (name=>graph) to an IO stream `io` Return number of graphs written.
88162
"""
89163
function savegml_mult(io::IO, graphs::Dict)

ext/GraphIOGraphMLExt.jl renamed to ext/GraphIOXML.jl

+48-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,55 @@
1-
module GraphIOGraphMLExt
1+
module GraphIOXML
22

33
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 # not required for julia >= v1.9
11-
using ..GraphIO
12-
using ..EzXML
13-
import ..GraphIO.GraphML.GraphMLFormat
4+
import Graphs: loadgraph, loadgraphs, savegraph, AbstractGraph
5+
6+
using GraphIO
7+
using EzXML
8+
import GraphIO.GEXF.GEXFFormat
9+
import GraphIO.GraphML.GraphMLFormat
10+
11+
"""
12+
savegexf(f, g, gname)
13+
14+
Write a graph `g` with name `gname` to an IO stream `io` in the
15+
[Gexf](http://gexf.net/format/) format. Return 1 (number of graphs written).
16+
"""
17+
function savegexf(io::IO, g::AbstractGraph, gname::String)
18+
xdoc = XMLDocument()
19+
xroot = setroot!(xdoc, ElementNode("gexf"))
20+
xroot["xmlns"] = "http://www.gexf.net/1.2draft"
21+
xroot["version"] = "1.2"
22+
xroot["xmlns:xsi"] = "http://www.w3.org/2001/XMLSchema-instance"
23+
xroot["xsi:schemaLocation"] = "http://www.gexf.net/1.2draft/gexf.xsd"
24+
25+
xmeta = addelement!(xroot, "meta")
26+
addelement!(xmeta, "description", gname)
27+
xg = addelement!(xroot, "graph")
28+
strdir = is_directed(g) ? "directed" : "undirected"
29+
xg["defaultedgetype"] = strdir
30+
31+
xnodes = addelement!(xg, "nodes")
32+
for i in 1:nv(g)
33+
xv = addelement!(xnodes, "node")
34+
xv["id"] = "$(i-1)"
35+
end
36+
37+
xedges = addelement!(xg, "edges")
38+
m = 0
39+
for e in edges(g)
40+
xe = addelement!(xedges, "edge")
41+
xe["id"] = "$m"
42+
xe["source"] = "$(src(e)-1)"
43+
xe["target"] = "$(dst(e)-1)"
44+
m += 1
45+
end
46+
47+
prettyprint(io, xdoc)
48+
return 1
1449
end
1550

51+
savegraph(io::IO, g::AbstractGraph, gname::String, ::GEXFFormat) = savegexf(io, g, gname)
52+
1653
function _graphml_read_one_graph(reader::EzXML.StreamReader, isdirected::Bool)
1754
nodes = Dict{String,Int}()
1855
xedges = Vector{Edge}()

test/runtests.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ include("graphio.jl")
1818
Aqua.test_all(GraphIO)
1919
end
2020
@testset "Code formatting" begin
21-
@test JuliaFormatter.format(GraphIO; verbose=false, overwrite=false)
21+
@test JuliaFormatter.format(GraphIO; verbose=true, overwrite=false)
2222
end
2323
for name in modules
2424
path = joinpath(testdir, name, "runtests.jl")

0 commit comments

Comments
 (0)