respondd.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python3
  2. import json
  3. import zlib
  4. import lib.helper
  5. class Respondd:
  6. def __init__(self, config):
  7. self._config = config
  8. self._aliasOverlay = {}
  9. try:
  10. with open('alias.json', 'r') as fh:
  11. self._aliasOverlay = json.load(fh)
  12. except IOError:
  13. raise
  14. def getNodeID(self):
  15. if 'node_id' in self._aliasOverlay['nodeinfo']:
  16. return self._aliasOverlay['nodeinfo']['node_id']
  17. else:
  18. return lib.helper.getInterfaceMAC(self._config['batman']).replace(':', '')
  19. def getStruct(self, rootName=None):
  20. ret = self._get()
  21. ret['node_id'] = self.getNodeID()
  22. if rootName is not None:
  23. ret_tmp = ret
  24. ret = {}
  25. ret[rootName] = ret_tmp
  26. return ret
  27. def getJSON(self, rootName=None):
  28. return bytes(json.dumps(self.getStruct(rootName), separators=(',', ':')), 'UTF-8')
  29. def getJSONCompressed(self, rootName=None):
  30. return self.compress(self.getJSON(rootName))
  31. @staticmethod
  32. def compress(data):
  33. encoder = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, -15) # The data may be decompressed using zlib and many zlib bindings using -15 as the window size parameter.
  34. dataGzip = encoder.compress(data)
  35. dataGzip += encoder.flush()
  36. return dataGzip
  37. @staticmethod
  38. def _get():
  39. return {}