Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

52 wiersze
1.4 KiB

  1. from typing import Tuple, Dict, Any, Set, Iterable, List
  2. from .normalize import normalize_mac, is_mac
  3. DEFAULT_MAC_FIELDS: tuple[str, ...] = (
  4. "mac",
  5. "beacon_mac",
  6. "beaconMac",
  7. "device_mac",
  8. "deviceMac",
  9. "tag_mac",
  10. "tagMac",
  11. )
  12. def _first_present(item: dict, keys: Iterable[str]) -> str:
  13. for k in keys:
  14. if k in item and item.get(k):
  15. return str(item.get(k))
  16. return ""
  17. def extract_beacon_macs(data: object, mac_fields: tuple[str, ...] = DEFAULT_MAC_FIELDS
  18. ) -> Tuple[Set[str], Dict[str, Any], List[str]]:
  19. allowed: Set[str] = set()
  20. by_mac: Dict[str, Any] = {}
  21. invalid: List[str] = []
  22. items = None
  23. if isinstance(data, list):
  24. items = data
  25. elif isinstance(data, dict):
  26. for key in ("items", "beacons", "trackers", "data", "result"):
  27. v = data.get(key)
  28. if isinstance(v, list):
  29. items = v
  30. break
  31. if not items:
  32. return allowed, by_mac, invalid
  33. for item in items:
  34. if not isinstance(item, dict):
  35. continue
  36. raw = _first_present(item, mac_fields)
  37. mac = normalize_mac(raw)
  38. if mac and is_mac(mac):
  39. allowed.add(mac)
  40. by_mac[mac] = item
  41. else:
  42. if raw:
  43. invalid.append(str(raw))
  44. return allowed, by_mac, invalid