model.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # This file is part of DarkFi (https://dark.fi)
  2. #
  3. # Copyright (C) 2020-2023 Dyne.org foundation
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import logging
  18. class Model:
  19. def __init__(self):
  20. self.nodes = {}
  21. def update(self, new_node):
  22. self.nodes.update(new_node)
  23. def __repr__(self):
  24. return f"{self.nodes}"
  25. class NodeInfo():
  26. def __init__(self, channels, slots):
  27. self.node = {}
  28. inbound = {}
  29. outbounds = {"slots": []}
  30. manual = {}
  31. seed = {}
  32. for name, channels in info.items():
  33. channel_lookup = {}
  34. for channel in channels:
  35. id = channel["id"]
  36. channel_lookup[id] = channel
  37. for channel in channels:
  38. if channel["session"] != "inbound":
  39. continue
  40. url = channel["url"]
  41. inbound["inbound"] = url
  42. for i, id in enumerate(slots):
  43. if id == 0:
  44. outbounds["slots"].append(f"{i}: none")
  45. continue
  46. assert id in channel_lookup
  47. url = channel_lookup[id]["url"]
  48. outbounds["slots"].append(f"{i}: {url}")
  49. for channel in channels:
  50. if channel["session"] != "seed":
  51. continue
  52. url = channel["url"]
  53. seed["seed"] = url
  54. for channel in channels:
  55. if channel["session"] != "manual":
  56. continue
  57. url = channel["url"]
  58. manual["manual"] = url
  59. self.node[name] = [inbound, outbounds, manual,
  60. seed]
  61. def __repr__(self):
  62. return f"{self.node}"