console.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/python
  2. # Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. # Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. #
  5. # Permission is hereby granted, free of charge, to any person
  6. # obtaining a copy of this software and associated documentation
  7. # files (the "Software"), to deal in the Software without
  8. # restriction, including without limitation the rights to use,
  9. # copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the
  11. # Software is furnished to do so, subject to the following
  12. # conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be
  15. # included in all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  19. # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. # OTHER DEALINGS IN THE SOFTWARE.
  25. import sys
  26. import os
  27. import socket
  28. from gi.repository import Gtk
  29. # Client Console commands
  30. CMD_CLEAR = "clear" # Clear console output
  31. CMD_EXIT = "exit" # Close console
  32. CMD_HELP = "help" # Console help
  33. CMD_VOID = ""
  34. class Console:
  35. #------------------------------------------------------------------------------
  36. def __init__(self, address):
  37. builder = Gtk.Builder()
  38. builder.add_from_file("ui/console.glade")
  39. self.m_view = builder.get_object("textview1")
  40. self.m_buffer = builder.get_object("textbuffer1")
  41. self.m_entry = builder.get_object("entry1")
  42. self.m_window = builder.get_object('window1')
  43. self.m_window.set_title("Crown Console")
  44. self.m_window.show_all()
  45. builder.connect_signals(self)
  46. self.m_address = address
  47. self.m_sock = socket.create_connection((self.m_address, 10000))
  48. Gtk.main()
  49. #------------------------------------------------------------------------------
  50. def on_destroy(self, *args):
  51. self.m_sock.close()
  52. Gtk.main_quit(*args)
  53. #------------------------------------------------------------------------------
  54. def on_key_pressed(self, entry, event):
  55. # If return is pressed, run command
  56. if event.keyval == 0xff0d :
  57. cmd = entry.get_text()
  58. self.parse_command(cmd)
  59. #------------------------------------------------------------------------------
  60. def parse_command(self, cmd):
  61. if cmd == CMD_CLEAR:
  62. self.m_buffer.set_text("")
  63. self.m_entry.set_text("")
  64. elif cmd == CMD_EXIT:
  65. self.on_destroy()
  66. elif cmd == CMD_HELP:
  67. self.print_help()
  68. elif cmd == CMD_VOID:
  69. self.print_command('');
  70. else:
  71. self.run_command(cmd)
  72. #------------------------------------------------------------------------------
  73. def run_command(self, cmd):
  74. msg = ''
  75. # Send command to Crown
  76. self.m_sock.send(cmd.encode())
  77. self.print_command(cmd)
  78. # Receive response
  79. # msg = self.m_sock.recv(1024);
  80. # print(msg.decode('utf-8'))
  81. # self.print_command(msg.decode('utf-8'))
  82. #------------------------------------------------------------------------------
  83. def print_command(self, cmd):
  84. # Print command to console
  85. end_iter = self.m_buffer.get_end_iter()
  86. a_string = "> " + cmd + "\n"
  87. # Append command to the end of buffer
  88. self.m_buffer.insert(end_iter, a_string, len(a_string))
  89. # Reset entry
  90. self.m_entry.set_text("")
  91. #------------------------------------------------------------------------------
  92. # def popup_dialog(self, message, expl):
  93. # dialog = Gtk.MessageDialog(self.m_window, 0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, message)
  94. # dialog.format_secondary_text(expl)
  95. # dialog.run()
  96. # dialog.destroy()
  97. #------------------------------------------------------------------------------
  98. #------------------------------------------------------------------------------
  99. def main():
  100. if len(sys.argv) != 2:
  101. print("Usage: console.py <ip-address>")
  102. exit(-1)
  103. address = sys.argv[1]
  104. console = Console(address)
  105. main()