view.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 urwid
  18. import logging
  19. import asyncio
  20. import datetime as dt
  21. from scroll import ScrollBar, Scrollable
  22. from model import Model
  23. event_loop = asyncio.get_event_loop()
  24. class LeftList(urwid.ListBox):
  25. def focus_next(self):
  26. try:
  27. self.body.set_focus(self.body.get_next(
  28. self.body.get_focus()[1])[1])
  29. except:
  30. pass
  31. def focus_previous(self):
  32. try:
  33. self.body.set_focus(self.body.get_prev(
  34. self.body.get_focus()[1])[1])
  35. except:
  36. pass
  37. class NodeView(urwid.WidgetWrap):
  38. def __init__(self, info):
  39. self.type = "node"
  40. self.name = info
  41. self.text = urwid.Text(f"{self.name}")
  42. super().__init__(self.text)
  43. self._w = urwid.AttrWrap(self._w, None)
  44. self.update_w()
  45. def selectable(self):
  46. return True
  47. def keypress(self, size, key):
  48. #if key in ('q'):
  49. # raise urwid.ExitMainLoop()
  50. return key
  51. def update_w(self):
  52. self._w.focus_attr = 'line'
  53. def get_widget(self):
  54. return "NodeView"
  55. def get_name(self):
  56. return self.name
  57. def get_type(self):
  58. return self.type
  59. class ConnectView(urwid.WidgetWrap):
  60. def __init__(self, node, kind):
  61. self.type = f"{kind}-connect"
  62. self.name = (f"{node}", f"{kind}")
  63. self.text = urwid.Text(f" {kind}")
  64. super().__init__(self.text)
  65. self._w = urwid.AttrWrap(self._w, None)
  66. self.update_w()
  67. def selectable(self):
  68. return True
  69. def keypress(self, size, key):
  70. return key
  71. def update_w(self):
  72. self._w.focus_attr = 'line'
  73. def get_widget(self):
  74. return "ConnectView"
  75. def get_name(self):
  76. return self.name
  77. def get_type(self):
  78. return self.type
  79. class SlotView(urwid.WidgetWrap):
  80. def __init__(self, node, type, id, info):
  81. self.id = id
  82. self.type = type
  83. self.name = (f"{node}", f"{id}")
  84. self.addr = info
  85. if len(id) == 1:
  86. self.text = urwid.Text(f" {id}: {self.addr}")
  87. else:
  88. self.text = urwid.Text(f" {self.addr}")
  89. super().__init__(self.text)
  90. self._w = urwid.AttrWrap(self._w, None)
  91. self.update_w()
  92. def selectable(self):
  93. return True
  94. def keypress(self, size, key):
  95. return key
  96. def update_w(self):
  97. self._w.focus_attr = 'line'
  98. def get_widget(self):
  99. return "SlotView"
  100. def get_name(self):
  101. return self.name
  102. def get_addr(self):
  103. return self.addr
  104. def get_type(self):
  105. return self.type
  106. class View():
  107. palette = [
  108. ('body','light gray','default', 'standout'),
  109. ('line','dark cyan','default','standout'),
  110. ]
  111. def __init__(self, model):
  112. self.model = model
  113. info_text = urwid.Text("")
  114. self.pile = urwid.Pile([info_text])
  115. scroll = ScrollBar(Scrollable(self.pile))
  116. rightbox = urwid.LineBox(scroll)
  117. self.listbox_content = []
  118. self.listwalker = urwid.SimpleListWalker(self.listbox_content)
  119. self.list = LeftList(self.listwalker)
  120. leftbox = urwid.LineBox(self.list)
  121. columns = urwid.Columns([leftbox, rightbox], focus_column=0)
  122. self.ui = urwid.Frame(urwid.AttrWrap( columns, 'body' ))
  123. async def update_view(self):
  124. known_nodes = []
  125. known_inbound = []
  126. while True:
  127. await asyncio.sleep(0.1)
  128. for index, item in enumerate(self.listwalker.contents):
  129. known_nodes.append(item.get_name())
  130. # Render get_info()
  131. for node, values in self.model.nodes.items():
  132. if node in known_nodes:
  133. continue
  134. else:
  135. widget = NodeView(node)
  136. self.listwalker.contents.append(widget)
  137. if values['outbound']:
  138. widget = ConnectView(node, "outbound")
  139. self.listwalker.contents.append(widget)
  140. for i, info in values['outbound'].items():
  141. widget = SlotView(node, "outbound", i, info)
  142. self.listwalker.contents.append(widget)
  143. if values['inbound']:
  144. widget = ConnectView(node, "inbound")
  145. self.listwalker.contents.append(widget)
  146. for i, info in values['inbound'].items():
  147. widget = SlotView(node, "inbound", i, info)
  148. self.listwalker.contents.append(widget)
  149. if values['manual']:
  150. widget = ConnectView(node, "manual")
  151. self.listwalker.contents.append(widget)
  152. for i, info in values['manual'].items():
  153. widget = SlotView(node, "manual", i, info)
  154. self.listwalker.contents.append(widget)
  155. if values['seed']:
  156. widget = ConnectView(node, "seed")
  157. self.listwalker.contents.append(widget)
  158. for i, info in values['seed'].items():
  159. widget = SlotView(node, "seed", i, info)
  160. self.listwalker.contents.append(widget)
  161. # Update outbound slot info
  162. for index, item in enumerate(self.listwalker.contents):
  163. if item.get_type() == "outbound":
  164. name = item.get_name()
  165. node = name[0]
  166. if name in self.model.nodes[node]['event']:
  167. value = self.model.nodes[node]['event'].get(name)
  168. widget = SlotView(node, "outbound", name[1], value)
  169. self.listwalker.contents[index] = widget
  170. # Update new inbound connections
  171. for index, item in enumerate(self.listwalker.contents):
  172. if item.get_type() == "inbound":
  173. name = item.get_name()
  174. if name[1] not in known_inbound:
  175. known_inbound.append(name[1])
  176. for node, value in self.model.nodes.items():
  177. for id, addr in value['inbound'].items():
  178. if id in known_inbound:
  179. continue
  180. else:
  181. widget = SlotView(node, "inbound", id, addr)
  182. self.listwalker.contents.append(widget)
  183. # Remove disconnected inbounds
  184. for inbound in known_inbound:
  185. for value in self.model.nodes.values():
  186. if inbound in value['inbound']:
  187. continue
  188. for index, item in enumerate(self.listwalker.contents):
  189. name = item.get_name()
  190. if name[1] == id:
  191. del self.listwalker.contents[index]
  192. # Render subscribe_events() (right menu)
  193. async def render_info(self):
  194. while True:
  195. await asyncio.sleep(0.01)
  196. self.pile.contents.clear()
  197. logging.debug(self.pile.contents)
  198. focus_w = self.list.get_focus()
  199. if focus_w[0] is None:
  200. continue
  201. else:
  202. match focus_w[0].get_widget():
  203. case "NodeView":
  204. logging.debug("node selected")
  205. # TODO: We will display additional node info here.
  206. self.pile.contents.append((
  207. urwid.Text(f""),
  208. self.pile.options()))
  209. case "ConnectView":
  210. logging.debug("connection selected")
  211. name = focus_w[0].get_name()
  212. info = self.model.nodes.get(name[0])
  213. if name in info['event']:
  214. ev = info['event'].get(name)
  215. logging.debug(f"{ev}")
  216. self.pile.contents.append((
  217. urwid.Text(f" {ev}"),
  218. self.pile.options()))
  219. case "SlotView":
  220. logging.debug("slot selected")
  221. addr = focus_w[0].get_addr()
  222. name = focus_w[0].get_name()
  223. info = self.model.nodes.get(name[0])
  224. if addr in info['msgs']:
  225. msg = info['msgs'].get(addr)
  226. for m in msg:
  227. time = m[0]
  228. event = m[1]
  229. msg = m[2]
  230. self.pile.contents.append((urwid.Text(
  231. f"{time}: {event}: {msg}"),
  232. self.pile.options()))