add alfred socket support (--alfred-sock)

This commit is contained in:
Martin Weinelt 2015-03-24 22:48:00 +01:00
parent c74b7b95fb
commit 6fba8ad21b
2 changed files with 33 additions and 12 deletions

View file

@ -11,7 +11,8 @@ from datetime import datetime
import networkx as nx import networkx as nx
from networkx.readwrite import json_graph from networkx.readwrite import json_graph
from lib import alfred, graph, nodes from lib import graph, nodes
from lib.alfred import Alfred
from lib.batman import Batman from lib.batman import Batman
from lib.rrddb import RRD from lib.rrddb import RRD
@ -34,6 +35,8 @@ def main(params):
for node_id, node in nodedb['nodes'].items(): for node_id, node in nodedb['nodes'].items():
node['flags']['online'] = False node['flags']['online'] = False
alfred = Alfred(unix_sockpath=params['alfred_sock'])
nodes.import_nodeinfo(nodedb['nodes'], alfred.nodeinfo(), nodes.import_nodeinfo(nodedb['nodes'], alfred.nodeinfo(),
now, assume_online=True) now, assume_online=True)
@ -91,6 +94,9 @@ if __name__ == '__main__':
parser.add_argument('-m', '--mesh', action='append', parser.add_argument('-m', '--mesh', action='append',
default=['bat0'], default=['bat0'],
help='batman mesh interface (defaults to bat0)') help='batman mesh interface (defaults to bat0)')
parser.add_argument('-s', '--alfred-sock',
default=None,
help='alfred unix socket path')
parser.add_argument('-d', '--dest-dir', action='store', parser.add_argument('-d', '--dest-dir', action='store',
help='destination directory for generated files', help='destination directory for generated files',
required=True) required=True)

View file

@ -1,20 +1,35 @@
import subprocess import subprocess
import json import json
import os
def _fetch(data_type): class Alfred(object):
output = subprocess.check_output( """
["alfred-json", "-z", "-f", "json", "-r", str(data_type)]) Bindings for the alfred-json utility
return json.loads(output.decode("utf-8")).values() """
def __init__(self, unix_sockpath=None):
if unix_sockpath:
if os.path.exists(unix_sockpath):
self.unix_sock = unix_sockpath
else:
raise RuntimeError('alfred: invalid unix socket path given')
def _fetch(self, data_type):
cmd = ['alfred-json',
'-z',
'-f', 'json',
'-r', data_type]
if self.unix_sock:
cmd.extend(['-s', self.unix_sock])
def nodeinfo(): output = subprocess.check_output(cmd)
return _fetch(158) return json.loads(output.decode("utf-8")).values()
def nodeinfo(self):
return self._fetch(158)
def statistics(): def statistics(self):
return _fetch(159) return self._fetch(159)
def vis(self):
def vis(): return self._fetch(160)
return _fetch(160)