__init__.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. else:
  26. result[k] = deepcopy(v)
  27. return result
  28. def merge_alfred_batman(alfreddata, batmandata):
  29. merged = {}
  30. for nodeid in alfreddata:
  31. nodeinfo = alfreddata[nodeid]
  32. candidates = set()
  33. candidates.add(nodeid)
  34. if 'mac' in nodeinfo: candidates.add(mac2id(nodeinfo['mac']))
  35. if 'macs' in nodeinfo:
  36. for mac in nodeinfo['macs']:
  37. candidates.add(mac2id(mac))
  38. for candidate in candidates:
  39. if candidate in batmandata:
  40. nodeinfo = dict_merge(nodeinfo, batmandata[candidate])
  41. merged[nodeid] = nodeinfo
  42. return merged