view.py 9.2 KB

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