log_dialog.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/python3
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2012 Agnieszka Kostrzewa <agnieszka.kostrzewa@gmail.com>
  6. # Copyright (C) 2012 Marek Marczykowski <marmarek@mimuw.edu.pl>
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. #
  21. #
  22. import sys
  23. from PyQt5 import QtWidgets # pylint: disable=import-error
  24. from . import ui_logdlg # pylint: disable=no-name-in-module
  25. from . import clipboard
  26. import os
  27. from qubesadmin import Qubes
  28. # Display only this size of log
  29. LOG_DISPLAY_SIZE = 1024*1024
  30. class LogDialog(ui_logdlg.Ui_LogDialog, QtWidgets.QDialog):
  31. # pylint: disable=too-few-public-methods
  32. def __init__(self, app, log_path, parent=None):
  33. super(LogDialog, self).__init__(parent)
  34. self.app = app
  35. self.log_path = log_path
  36. self.setupUi(self)
  37. self.setWindowTitle(log_path)
  38. self.copy_to_qubes_clipboard.clicked.connect(
  39. self.copy_to_clipboard_triggered)
  40. self.copy_to_qubes_clipboard.clicked.connect(
  41. self.copy_to_clipboard_triggered)
  42. self.__init_log_text__()
  43. def __init_log_text__(self):
  44. self.displayed_text = ""
  45. log = open(self.log_path)
  46. log.seek(0, os.SEEK_END)
  47. if log.tell() > LOG_DISPLAY_SIZE:
  48. self.displayed_text = self.tr(
  49. "(Showing only last %d bytes of file)\n") % LOG_DISPLAY_SIZE
  50. log.seek(log.tell()-LOG_DISPLAY_SIZE, os.SEEK_SET)
  51. else:
  52. log.seek(0, os.SEEK_SET)
  53. self.displayed_text += log.read()
  54. log.close()
  55. self.log_text.setPlainText(self.displayed_text)
  56. self.log_text.show()
  57. def copy_to_clipboard_triggered(self):
  58. clipboard.copy_text_to_qubes_clipboard(self.displayed_text)
  59. def main():
  60. qubes_app = Qubes()
  61. qt_app = QtWidgets.QApplication(sys.argv)
  62. log_window = LogDialog(qubes_app, sys.argv[1])
  63. log_window.show()
  64. qt_app.exec_()
  65. qt_app.exit()
  66. if __name__ == "__main__":
  67. main()