add alfred socket support (--alfred-sock)

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

View file

@ -1,20 +1,35 @@
import subprocess
import json
import os
def _fetch(data_type):
output = subprocess.check_output(
["alfred-json", "-z", "-f", "json", "-r", str(data_type)])
return json.loads(output.decode("utf-8")).values()
class Alfred(object):
"""
Bindings for the alfred-json utility
"""
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():
return _fetch(158)
output = subprocess.check_output(cmd)
return json.loads(output.decode("utf-8")).values()
def nodeinfo(self):
return self._fetch(158)
def statistics():
return _fetch(159)
def statistics(self):
return self._fetch(159)
def vis():
return _fetch(160)
def vis(self):
return self._fetch(160)