2
0

__init__.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. from copy import deepcopy
  2. from .alfred import AlfredParser
  3. from .batman import BatmanParser
  4. from .dashing import DashingClient
  5. from .graphite import GraphitePush
  6. from .server import ApiServer
  7. from .storage import Storage
  8. __all__ = [
  9. 'AlfredParser', 'BatmanParser',
  10. 'DashingClient', 'GraphitePush',
  11. 'Storage', 'ApiServer',
  12. 'dict_merge', 'merge_alfred_batman', 'mac2id' ]
  13. def mac2id(mac):
  14. return mac.lower().replace(':', '')
  15. def dict_merge(a, b):
  16. '''recursively merges dict's. not just simple a['key'] = b['key'], if
  17. both a and bhave a key who's value is a dict then dict_merge is called
  18. on both values and the result stored in the returned dictionary.'''
  19. if not isinstance(b, dict):
  20. return b
  21. result = deepcopy(a)
  22. for k, v in b.iteritems():
  23. if k in result and isinstance(result[k], dict):
  24. result[k] = dict_merge(result[k], v)
  25. elif k in result and isinstance(result[k], list):
  26. result[k] = result[k] + [ deepcopy(x) for x in v if x not in result[k] ]
  27. else:
  28. result[k] = deepcopy(v)
  29. return result
  30. def merge_alfred_batman(alfreddata, batmandata):
  31. merged = {}
  32. batlookup = {}
  33. for nodeid in batmandata:
  34. batlookup[nodeid] = nodeid
  35. for bda in batmandata[nodeid]['aliases']:
  36. batlookup[bda] = nodeid
  37. for nodeid in alfreddata:
  38. nodeinfo = alfreddata[nodeid]
  39. candidates = set()
  40. candidates.add(nodeid)
  41. if 'mac' in nodeinfo: candidates.add(mac2id(nodeinfo['mac']))
  42. if 'macs' in nodeinfo:
  43. for mac in nodeinfo['macs']:
  44. candidates.add(mac2id(mac))
  45. if 'network' in nodeinfo:
  46. n = nodeinfo['network']
  47. if 'mac' in n: candidates.add(mac2id(n['mac']))
  48. if 'mesh_interfaces' in n:
  49. for mac in n['mesh_interfaces']:
  50. candidates.add(mac2id(mac))
  51. if not 'neighbours' in nodeinfo: nodeinfo['neighbours'] = []
  52. for candidate_raw in candidates:
  53. candidate = batlookup[candidate_raw] if candidate_raw in batlookup else candidate_raw
  54. if candidate in batmandata:
  55. nodeinfo = dict_merge(nodeinfo, batmandata[candidate])
  56. merged[nodeid] = nodeinfo
  57. return merged