graphml2json 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +17 -0
- data/LICENSE.txt +1 -1
- data/README.md +82 -11
- data/bin/graphml2json +5 -0
- data/graphml2json.gemspec +10 -10
- data/lib/graphml2json.rb +24 -7
- data/lib/graphml2json/version.rb +1 -1
- data/spec/fixtures/attributes.graphml +39 -0
- data/spec/graphml2json_spec.rb +14 -7
- data/spec/spec_helper.rb +1 -0
- metadata +13 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 90e038ecdb0e2038bb4f4b5a274e2d30c112c29f6f47fb0d8ba07f5436e9aece
|
4
|
+
data.tar.gz: b30c4012708853363a099c210818a9614c283e4ad71e89cedc99175ad8cce775
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d365932f68e60f125c41db42e62b8d50c86ec5660d7a110951631abac8a5428a17166f57dd69f807ec947dc057c6df732775a4c463966a4f06cb557bd257cf9f
|
7
|
+
data.tar.gz: 93d9b6fd54733783286915763e6502d7611507004a03fdd37cf6b1f7c98bc7e2a13546f1c792696ff1a63160b5e606e7613e74e09c1baab524cb14304084007b
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# 0.1.0 / 2018-12-30
|
2
|
+
|
3
|
+
* Add command line excutable helper
|
4
|
+
* Add data nodes to attributes
|
5
|
+
|
6
|
+
# 0.0.3 / 2018-12-28
|
7
|
+
|
8
|
+
* Update Nokogiri dependency
|
9
|
+
|
10
|
+
# 0.0.2 / 2013-11-14
|
11
|
+
|
12
|
+
* make work with namespacing
|
13
|
+
* add specs
|
14
|
+
|
15
|
+
# 0.0.1 / 2013-08-05
|
16
|
+
|
17
|
+
* initial release
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Graphml2json
|
2
2
|
|
3
|
-
|
3
|
+
This utility takes graphml and converts it to json suitable for use in D3
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,20 +18,91 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
+
### From ruby
|
21
22
|
```
|
22
|
-
|
23
|
-
>> file = File.open('simple.graphml', 'rb')
|
24
|
-
=> #<File:simple.graphml>
|
25
|
-
>> graphml = file.read
|
26
|
-
=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" \n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns\n http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">\n <graph id=\"G\" edgedefault=\"undirected\">\n <node id=\"n0\"/>\n <node id=\"n1\"/>\n <node id=\"n2\"/>\n <node id=\"n3\"/>\n <node id=\"n4\"/>\n <node id=\"n5\"/>\n <node id=\"n6\"/>\n <node id=\"n7\"/>\n <node id=\"n8\"/>\n <node id=\"n9\"/>\n <node id=\"n10\"/>\n <edge source=\"n0\" target=\"n2\"/>\n <edge source=\"n1\" target=\"n2\"/>\n <edge source=\"n2\" target=\"n3\"/>\n <edge source=\"n3\" target=\"n5\"/>\n <edge source=\"n3\" target=\"n4\"/>\n <edge source=\"n4\" target=\"n6\"/>\n <edge source=\"n6\" target=\"n5\"/>\n <edge source=\"n5\" target=\"n7\"/>\n <edge source=\"n6\" target=\"n8\"/>\n <edge source=\"n8\" target=\"n7\"/>\n <edge source=\"n8\" target=\"n9\"/>\n <edge source=\"n8\" target=\"n10\"/>\n </graph>\n</graphml>\n"
|
23
|
+
>> require 'open-uri'
|
27
24
|
>> require 'graphml2json'
|
28
|
-
|
29
|
-
>>
|
30
|
-
=> "
|
25
|
+
|
26
|
+
>> data = open('http://graphml.graphdrawing.org/primer/attributes.graphml').read
|
27
|
+
=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!-- This file was written by the JAVA GraphML Library.-->\n<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\nxsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">\n <key id=\"d0\" for=\"node\" attr.name=\"color\" attr.type=\"string\">\n <default>yellow</default>\n </key>\n <key id=\"d1\" for=\"edge\" attr.name=\"weight\" attr.type=\"double\"/>\n <graph id=\"G\" edgedefault=\"undirected\">\n <node id=\"n0\">\n <data key=\"d0\">green</data>\n </node>\n <node id=\"n1\"/>\n <node id=\"n2\">\n <data key=\"d0\">blue</data>\n </node>\n <node id=\"n3\">\n <data key=\"d0\">red</data>\n </node>\n <node id=\"n4\"/>\n <node id=\"n5\">\n <data key=\"d0\">turquoise</data>\n </node>\n <edge id=\"e0\" source=\"n0\" target=\"n2\">\n <data key=\"d1\">1.0</data>\n </edge>\n <edge id=\"e1\" source=\"n0\" target=\"n1\">\n <data key=\"d1\">1.0</data>\n </edge>\n <edge id=\"e2\" source=\"n1\" target=\"n3\">\n <data key=\"d1\">2.0</data>\n </edge>\n <edge id=\"e3\" source=\"n3\" target=\"n2\"/>\n <edge id=\"e4\" source=\"n2\" target=\"n4\"/>\n <edge id=\"e5\" source=\"n3\" target=\"n5\"/>\n <edge id=\"e6\" source=\"n5\" target=\"n4\">\n <data key=\"d1\">1.1</data>\n </edge>\n </graph>\n</graphml>\n"
|
28
|
+
|
29
|
+
>> Graphml2Json.generate(data)
|
30
|
+
|
31
|
+
=> "{\"nodes\":[{\"name\":0,\"d0\":\"green\"},{\"name\":1},{\"name\":2,\"d0\":\"blue\"},{\"name\":3,\"d0\":\"red\"},{\"name\":4},{\"name\":5,\"d0\":\"turquoise\"}],\"edges\":[{\"source\":0,\"target\":2,\"d1\":\"1.0\"},{\"source\":0,\"target\":1,\"d1\":\"1.0\"},{\"source\":1,\"target\":3,\"d1\":\"2.0\"},{\"source\":3,\"target\":2},{\"source\":2,\"target\":4},{\"source\":3,\"target\":5},{\"source\":5,\"target\":4,\"d1\":\"1.1\"}]}"
|
32
|
+
```
|
33
|
+
|
34
|
+
### From a terminal
|
35
|
+
|
36
|
+
You can also invoke the `graphml2json` utility from the command line. It takes stdin as input.
|
37
|
+
|
31
38
|
```
|
39
|
+
$ curl http://graphml.graphdrawing.org/primer/attributes.graphml | graphml2json | json_pp
|
40
|
+
{
|
41
|
+
"nodes" : [
|
42
|
+
{
|
43
|
+
"d0" : "green",
|
44
|
+
"name" : 0
|
45
|
+
},
|
46
|
+
{
|
47
|
+
"name" : 1
|
48
|
+
},
|
49
|
+
{
|
50
|
+
"name" : 2,
|
51
|
+
"d0" : "blue"
|
52
|
+
},
|
53
|
+
{
|
54
|
+
"name" : 3,
|
55
|
+
"d0" : "red"
|
56
|
+
},
|
57
|
+
{
|
58
|
+
"name" : 4
|
59
|
+
},
|
60
|
+
{
|
61
|
+
"d0" : "turquoise",
|
62
|
+
"name" : 5
|
63
|
+
}
|
64
|
+
],
|
65
|
+
"edges" : [
|
66
|
+
{
|
67
|
+
"d1" : "1.0",
|
68
|
+
"source" : 0,
|
69
|
+
"target" : 2
|
70
|
+
},
|
71
|
+
{
|
72
|
+
"target" : 1,
|
73
|
+
"d1" : "1.0",
|
74
|
+
"source" : 0
|
75
|
+
},
|
76
|
+
{
|
77
|
+
"source" : 1,
|
78
|
+
"d1" : "2.0",
|
79
|
+
"target" : 3
|
80
|
+
},
|
81
|
+
{
|
82
|
+
"target" : 2,
|
83
|
+
"source" : 3
|
84
|
+
},
|
85
|
+
{
|
86
|
+
"source" : 2,
|
87
|
+
"target" : 4
|
88
|
+
},
|
89
|
+
{
|
90
|
+
"source" : 3,
|
91
|
+
"target" : 5
|
92
|
+
},
|
93
|
+
{
|
94
|
+
"d1" : "1.1",
|
95
|
+
"source" : 5,
|
96
|
+
"target" : 4
|
97
|
+
}
|
98
|
+
]
|
99
|
+
}
|
100
|
+
|
101
|
+
```
|
102
|
+
|
103
|
+
## Changelog
|
32
104
|
|
33
|
-
|
34
|
-
http://graphml.graphdrawing.org/primer/graphml-primer.html
|
105
|
+
* [changelog](https://github.com/gregors/graphml2json/blob/master/CHANGELOG.md)
|
35
106
|
|
36
107
|
## Contributing
|
37
108
|
|
data/bin/graphml2json
ADDED
data/graphml2json.gemspec
CHANGED
@@ -4,22 +4,22 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'graphml2json/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'graphml2json'
|
8
8
|
spec.version = Graphml2Json::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
spec.description = %q{Convert graphml to json}
|
12
|
-
spec.summary = %q{Convert graphml to json}
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
9
|
+
spec.authors = ['Gregory Ostermayr']
|
10
|
+
spec.email = ['<gregory.ostermayr@gmail.com>']
|
11
|
+
spec.description = %q{Convert graphml to json for D3 use}
|
12
|
+
spec.summary = %q{Convert graphml to json for D3 use}
|
13
|
+
spec.homepage = 'https://github.com/gregors/graphml2json'
|
14
|
+
spec.license = 'MIT'
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
17
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
-
spec.require_paths = [
|
19
|
+
spec.require_paths = ['lib']
|
20
20
|
|
21
21
|
spec.add_dependency 'nokogiri', '>= 1.8.2'
|
22
|
-
spec.add_development_dependency
|
23
|
-
spec.add_development_dependency "rspec",
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
23
|
+
spec.add_development_dependency "rspec", '~> 2.14.1'
|
24
24
|
spec.add_development_dependency "rake"
|
25
25
|
end
|
data/lib/graphml2json.rb
CHANGED
@@ -3,7 +3,7 @@ require 'json'
|
|
3
3
|
|
4
4
|
module Graphml2Json
|
5
5
|
|
6
|
-
def self.generate(xml_string)
|
6
|
+
def self.generate(xml_string)
|
7
7
|
doc = Nokogiri::XML(xml_string)
|
8
8
|
doc.remove_namespaces!
|
9
9
|
graph = doc.xpath('//graph')
|
@@ -14,21 +14,38 @@ module Graphml2Json
|
|
14
14
|
@json_nodes = []
|
15
15
|
|
16
16
|
@graph = {}
|
17
|
-
@graph[:nodes] = []
|
18
|
-
@graph[:edges] = []
|
17
|
+
@graph[:nodes] = []
|
18
|
+
@graph[:edges] = []
|
19
19
|
|
20
20
|
nodes.each_with_index do |n, index|
|
21
|
-
|
22
|
-
|
21
|
+
|
22
|
+
attributes = data_attributes(n)
|
23
|
+
@mapping[n[:id]] = index
|
24
|
+
new_node = {:name => index}.merge(attributes)
|
25
|
+
@graph[:nodes] << new_node
|
23
26
|
end
|
24
27
|
|
25
|
-
@json_edges = []
|
28
|
+
@json_edges = []
|
26
29
|
edges.each do |e|
|
27
30
|
source = @mapping[e[:source]]
|
28
31
|
target = @mapping[e[:target]]
|
29
|
-
|
32
|
+
attributes = data_attributes(e)
|
33
|
+
new_edge = { :source => source, :target => target }.merge(attributes)
|
34
|
+
@graph[:edges] << new_edge
|
30
35
|
end
|
31
36
|
|
32
37
|
JSON.generate(@graph)
|
33
38
|
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def self.data_attributes(node)
|
43
|
+
data_nodes = node.xpath('./data')
|
44
|
+
|
45
|
+
attributes = data_nodes.map do |d|
|
46
|
+
{d[:key] => d.text}
|
47
|
+
end.inject(&:merge)
|
48
|
+
|
49
|
+
attributes || {}
|
50
|
+
end
|
34
51
|
end
|
data/lib/graphml2json/version.rb
CHANGED
@@ -0,0 +1,39 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
3
|
+
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
|
4
|
+
<key id="d0" for="node" attr.name="color" attr.type="string">
|
5
|
+
<default>yellow</default>
|
6
|
+
</key>
|
7
|
+
<key id="d1" for="edge" attr.name="weight" attr.type="double"/>
|
8
|
+
<graph id="G" edgedefault="undirected">
|
9
|
+
<node id="n0">
|
10
|
+
<data key="color">green</data>
|
11
|
+
</node>
|
12
|
+
<node id="n1"/>
|
13
|
+
<node id="n2">
|
14
|
+
<data key="color">blue</data>
|
15
|
+
</node>
|
16
|
+
<node id="n3">
|
17
|
+
<data key="color">red</data>
|
18
|
+
</node>
|
19
|
+
<node id="n4"/>
|
20
|
+
<node id="n5">
|
21
|
+
<data key="color">turquoise</data>
|
22
|
+
</node>
|
23
|
+
<edge id="e0" source="n0" target="n2">
|
24
|
+
<data key="size">1.0</data>
|
25
|
+
</edge>
|
26
|
+
<edge id="e1" source="n0" target="n1">
|
27
|
+
<data key="size">1.0</data>
|
28
|
+
</edge>
|
29
|
+
<edge id="e2" source="n1" target="n3">
|
30
|
+
<data key="size">2.0</data>
|
31
|
+
</edge>
|
32
|
+
<edge id="e3" source="n3" target="n2"/>
|
33
|
+
<edge id="e4" source="n2" target="n4"/>
|
34
|
+
<edge id="e5" source="n3" target="n5"/>
|
35
|
+
<edge id="e6" source="n5" target="n4">
|
36
|
+
<data key="size">1.1</data>
|
37
|
+
</edge>
|
38
|
+
</graph>
|
39
|
+
</graphml>
|
data/spec/graphml2json_spec.rb
CHANGED
@@ -1,23 +1,30 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
require 'graphml2json'
|
3
2
|
|
4
3
|
describe Graphml2Json do
|
5
4
|
let(:json) {'{"nodes":[{"name":0},{"name":1},{"name":2},{"name":3}],"edges":[{"source":0,"target":1},{"source":1,"target":2},{"source":2,"target":3},{"source":3,"target":null}]}'}
|
6
5
|
|
7
|
-
describe
|
8
|
-
context
|
6
|
+
describe '.generate' do
|
7
|
+
context 'missing proper namespace' do
|
9
8
|
it 'returns valid json' do
|
10
|
-
graphml = File.read(
|
9
|
+
graphml = File.read('spec/fixtures/graphml_without_namespacing.txt')
|
11
10
|
Graphml2Json::generate(graphml).should == json
|
12
11
|
end
|
13
12
|
end
|
14
13
|
|
15
|
-
context
|
14
|
+
context 'with namespace' do
|
16
15
|
it 'returns valid json' do
|
17
|
-
graphml = File.read(
|
16
|
+
graphml = File.read('spec/fixtures/graphml_with_namespacing.txt')
|
17
|
+
Graphml2Json::generate(graphml).should == json
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'with nodes' do
|
22
|
+
let(:json){'{"nodes":[{"name":0,"color":"green"},{"name":1},{"name":2,"color":"blue"},{"name":3,"color":"red"},{"name":4},{"name":5,"color":"turquoise"}],"edges":[{"source":0,"target":2,"size":"1.0"},{"source":0,"target":1,"size":"1.0"},{"source":1,"target":3,"size":"2.0"},{"source":3,"target":2},{"source":2,"target":4},{"source":3,"target":5},{"source":5,"target":4,"size":"1.1"}]}'}
|
23
|
+
|
24
|
+
it 'returns data as attributes' do
|
25
|
+
graphml = File.read('spec/fixtures/attributes.graphml')
|
18
26
|
Graphml2Json::generate(graphml).should == json
|
19
27
|
end
|
20
28
|
end
|
21
29
|
end
|
22
30
|
end
|
23
|
-
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphml2json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gregory Ostermayr
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-12-
|
11
|
+
date: 2018-12-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '1.
|
33
|
+
version: '1.6'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '1.
|
40
|
+
version: '1.6'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,22 +66,26 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
-
description: Convert graphml to json
|
69
|
+
description: Convert graphml to json for D3 use
|
70
70
|
email:
|
71
71
|
- "<gregory.ostermayr@gmail.com>"
|
72
|
-
executables:
|
72
|
+
executables:
|
73
|
+
- graphml2json
|
73
74
|
extensions: []
|
74
75
|
extra_rdoc_files: []
|
75
76
|
files:
|
76
77
|
- ".gitignore"
|
77
78
|
- ".rspec"
|
79
|
+
- CHANGELOG.md
|
78
80
|
- Gemfile
|
79
81
|
- LICENSE.txt
|
80
82
|
- README.md
|
81
83
|
- Rakefile
|
84
|
+
- bin/graphml2json
|
82
85
|
- graphml2json.gemspec
|
83
86
|
- lib/graphml2json.rb
|
84
87
|
- lib/graphml2json/version.rb
|
88
|
+
- spec/fixtures/attributes.graphml
|
85
89
|
- spec/fixtures/graphml_with_namespacing.txt
|
86
90
|
- spec/fixtures/graphml_without_namespacing.txt
|
87
91
|
- spec/graphml2json_spec.rb
|
@@ -106,11 +110,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
106
110
|
version: '0'
|
107
111
|
requirements: []
|
108
112
|
rubyforge_project:
|
109
|
-
rubygems_version: 2.7.
|
113
|
+
rubygems_version: 2.7.7
|
110
114
|
signing_key:
|
111
115
|
specification_version: 4
|
112
|
-
summary: Convert graphml to json
|
116
|
+
summary: Convert graphml to json for D3 use
|
113
117
|
test_files:
|
118
|
+
- spec/fixtures/attributes.graphml
|
114
119
|
- spec/fixtures/graphml_with_namespacing.txt
|
115
120
|
- spec/fixtures/graphml_without_namespacing.txt
|
116
121
|
- spec/graphml2json_spec.rb
|