-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathclient.py
More file actions
106 lines (85 loc) · 3.28 KB
/
client.py
File metadata and controls
106 lines (85 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/python
# coding: utf-8
#
# Copyright (C) 2012 André Panisson
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Allow a Python script to communicate with Gephi using the Gephi Graph Streaming protocol and plugin.
"""
from __future__ import print_function, absolute_import
__author__ = 'panisson@gmail.com'
try:
from urllib2 import urlopen
except ImportError:
from urllib.request import urlopen
import json
import time
import sys
class JSONClient(object):
def __init__(self, autoflush=False, enable_timestamps=False, process_event_hook=None):
self.data = ""
self.autoflush = autoflush
self.enable_timestamps = enable_timestamps
if enable_timestamps:
def default_peh(event):
event['t'] = int(time.time())
return event
else:
default_peh = lambda e: e
if process_event_hook is None:
self.peh = default_peh
else:
self.peh = lambda e: default_peh(process_event_hook(e))
def flush(self):
if len(self.data) > 0:
self._send(self.data)
self.data = ""
def _send(self, data):
""" Overwrite in subclass. """
pass
def add_node(self, id, flush=True, **attributes):
self.data += json.dumps(self.peh({"an":{id:attributes}})) + '\r\n'
if(self.autoflush): self.flush()
def change_node(self, id, flush=True, **attributes):
self.data += json.dumps(self.peh({"cn":{id:attributes}})) + '\r\n'
if(self.autoflush): self.flush()
def delete_node(self, id):
self._send(json.dumps(self.peh({"dn":{id:{}}})) + '\r\n')
def add_edge(self, id, source, target, directed=True, **attributes):
attributes['source'] = source
attributes['target'] = target
attributes['directed'] = directed
self.data += json.dumps(self.peh({"ae":{id:attributes}})) + '\r\n'
if(self.autoflush): self.flush()
def delete_edge(self, id):
self._send(json.dumps(self.peh({"de":{id:{}}})) + '\r\n')
def clean(self):
self._send(json.dumps(self.peh({"dn":{"filter":"ALL"}})) + '\r\n')
class GephiClient(JSONClient):
def __init__(self, url='http://127.0.0.1:8080/workspace0', autoflush=False):
JSONClient.__init__(self, autoflush)
self.url = url
def _send(self, data):
# For python3 we need to convert data string to bytes
if sys.version_info[0] > 2:
data = bytes(data, encoding='utf-8')
conn = urlopen(self.url+ '?operation=updateGraph', data)
return conn.read()
class GephiFileHandler(JSONClient):
def __init__(self, out, **params):
params['autoflush'] = True
JSONClient.__init__(self, **params)
self.out = out
def _send(self, data):
self.out.write(data)