respondd.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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._aliases = {}
  9. try:
  10. with open("alias.json", 'r') as cfg_handle:
  11. self._aliases = json.load(cfg_handle)
  12. except IOError:
  13. raise
  14. pass
  15. def getNode_ID(self):
  16. if 'node_id' in self._aliases["nodeinfo"]:
  17. return self._aliases["nodeinfo"]["node_id"]
  18. else:
  19. return lib.helper.getDevice_MAC(self._config["batman"]).replace(':', '')
  20. def getStruct(self, root=None):
  21. j = self._get()
  22. j['node_id'] = self.getNode_ID()
  23. if not root is None:
  24. j_tmp = j
  25. j = {}
  26. j[root] = j_tmp
  27. return j
  28. def getJSON(self, root=None):
  29. return bytes(json.dumps(self.getStruct(), separators=(',', ':')), 'UTF-8')
  30. def getJSONCompressed(self, root=None):
  31. return self.compress(self.getJSON(root))
  32. def compress(self, 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. gzip_data = encoder.compress(data)
  35. gzip_data = gzip_data + encoder.flush()
  36. return gzip_data
  37. def _get(self):
  38. return {}
  39. pass