view.py 9.3 KB

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