GEX config edit utility (PyQt)

gexync.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/bin/env python3
  2. import gex
  3. import sys
  4. from PyQt4 import QtGui, QtCore
  5. import ini_syntax
  6. class GexIniEditor(QtGui.QMainWindow):
  7. """
  8. Gexync is a GEX ini file editor.
  9. The editor loads the INI file through the communication interface
  10. without having to mount the virtual filesystem, which is unreliable
  11. on some systems.
  12. This utility allows live editing of the UNITS.INI file.
  13. On save, a new version is loaded with formatting and error messages
  14. generated by GEX, as if the virtual config filesystem was re-mounted.
  15. The editor does not keep GEX claimed, instead does so only when needed.
  16. This allows testing of the current configuration without having to close
  17. and reopen the editor.
  18. """
  19. def __init__(self, xferLambda):
  20. self.xferLambda = xferLambda
  21. self.filenum = int(sys.argv[1]) if len(sys.argv)>1 else 0
  22. super().__init__()
  23. self.initUI()
  24. # TODO let user pick GEX device if multiple
  25. def initToolbar(self):
  26. icon = self.style().standardIcon(QtGui.QStyle.SP_BrowserReload)
  27. loadAction = QtGui.QAction(icon, 'Reload', self)
  28. loadAction.setShortcut('Ctrl+O')
  29. loadAction.triggered.connect(self.gexLoad)
  30. icon = self.style().standardIcon(QtGui.QStyle.SP_DialogSaveButton)
  31. syncAction = QtGui.QAction(icon, 'Write Changes', self)
  32. syncAction.setShortcut('Ctrl+S')
  33. syncAction.triggered.connect(self.gexSync)
  34. icon = self.style().standardIcon(QtGui.QStyle.SP_DialogOkButton)
  35. persAction = QtGui.QAction(icon, 'Persist', self)
  36. persAction.setShortcut('Ctrl+P')
  37. persAction.triggered.connect(self.gexPersist)
  38. self.toolbar = self.addToolBar('Toolbar')
  39. self.toolbar.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon)
  40. self.toolbar.addAction(loadAction)
  41. self.toolbar.addAction(syncAction)
  42. self.toolbar.addSeparator()
  43. self.toolbar.addAction(persAction)
  44. def initEditor(self):
  45. self.editor = QtGui.QPlainTextEdit()
  46. # Editor background and text color
  47. pal = QtGui.QPalette()
  48. bgc = QtGui.QColor(0xFFFFF6)
  49. pal.setColor(QtGui.QPalette.Base, bgc)
  50. textc = QtGui.QColor(0x000000)
  51. pal.setColor(QtGui.QPalette.Text, textc)
  52. self.editor.setPalette(pal)
  53. # Font
  54. font = QtGui.QFont('Liberation Mono', 12)
  55. font.setStyleHint(QtGui.QFont.TypeWriter)
  56. self.editor.setFont(font)
  57. # Initial size
  58. self.highlight = ini_syntax.IniHighlighter(self.editor.document())
  59. def initUI(self):
  60. self.setWindowTitle('GEX config file editor')
  61. self.initToolbar()
  62. self.initEditor()
  63. self.setCentralWidget(self.editor)
  64. self.show()
  65. self.gexLoad()
  66. def gexLoad(self):
  67. self.editor.setPlainText("")
  68. self.editor.repaint()
  69. client = gex.Client(self.xferLambda(), load_units=False)
  70. read_ini = client.ini_read(self.filenum)
  71. client.close()
  72. self.editor.setPlainText(read_ini)
  73. self.highlight.rehighlight()
  74. self.setWindowTitle('GEX config file editor')
  75. def gexSync(self):
  76. new_txt = self.editor.toPlainText()
  77. self.editor.setPlainText("")
  78. self.editor.repaint()
  79. client = gex.Client(self.xferLambda(), load_units=False)
  80. client.ini_write(new_txt)
  81. read_ini = client.ini_read(self.filenum)
  82. client.close()
  83. self.editor.setPlainText(read_ini)
  84. self.highlight.rehighlight()
  85. self.setWindowTitle('*GEX config file editor')
  86. def gexPersist(self):
  87. client = gex.Client(self.xferLambda(), load_units=False)
  88. client.ini_persist()
  89. client.close()
  90. self.setWindowTitle('GEX config file editor')
  91. if __name__ == '__main__':
  92. app = QtGui.QApplication(sys.argv)
  93. editor = GexIniEditor(lambda: gex.TrxRawUSB())
  94. # editor = GexIniEditor(lambda: gex.TrxSerialThread(port='/dev/ttyUSB1',
  95. # baud=57600))
  96. # centered resize
  97. w = 800
  98. h = 900
  99. ss = app.desktop().availableGeometry().size()
  100. editor.setGeometry(int(ss.width() / 2 - w / 2), int(ss.height() / 2 - h / 2), w, h)
  101. sys.exit(app.exec_())